1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
2011-07-27 07:17:02 -07:00
|
|
|
/* OCaml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../LICENSE. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
2014-12-27 06:41:49 -08:00
|
|
|
/* The interface of this file is in "caml/mlvalues.h" and "caml/alloc.h" */
|
2003-12-15 10:10:51 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2002-09-23 11:03:03 -07:00
|
|
|
#include <string.h>
|
|
|
|
|
2014-12-27 06:41:49 -08:00
|
|
|
#include "caml/alloc.h"
|
|
|
|
#include "caml/fail.h"
|
|
|
|
#include "caml/memory.h"
|
|
|
|
#include "caml/mlvalues.h"
|
|
|
|
#include "caml/misc.h"
|
|
|
|
#include "caml/reverse.h"
|
|
|
|
#include "caml/stacks.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2012-01-16 02:23:51 -08:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <float.h>
|
2015-07-26 12:28:48 -07:00
|
|
|
#ifndef isnan
|
2012-01-16 02:23:51 -08:00
|
|
|
#define isnan _isnan
|
2015-07-26 12:28:48 -07:00
|
|
|
#endif
|
|
|
|
#ifndef isfinite
|
2012-01-16 02:23:51 -08:00
|
|
|
#define isfinite _finite
|
|
|
|
#endif
|
2015-07-26 12:28:48 -07:00
|
|
|
#endif
|
2012-01-16 02:23:51 -08:00
|
|
|
|
1996-07-01 05:43:28 -07:00
|
|
|
#ifdef ARCH_ALIGN_DOUBLE
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLexport double caml_Double_val(value val)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
union { value v[2]; double d; } buffer;
|
|
|
|
|
|
|
|
Assert(sizeof(double) == 2 * sizeof(value));
|
|
|
|
buffer.v[0] = Field(val, 0);
|
|
|
|
buffer.v[1] = Field(val, 1);
|
|
|
|
return buffer.d;
|
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLexport void caml_Store_double_val(value val, double dbl)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
union { value v[2]; double d; } buffer;
|
|
|
|
|
|
|
|
Assert(sizeof(double) == 2 * sizeof(value));
|
|
|
|
buffer.d = dbl;
|
|
|
|
Field(val, 0) = buffer.v[0];
|
|
|
|
Field(val, 1) = buffer.v[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLexport value caml_copy_double(double d)
|
1995-07-27 10:41:09 -07:00
|
|
|
{
|
|
|
|
value res;
|
|
|
|
|
|
|
|
#define Setup_for_gc
|
|
|
|
#define Restore_after_gc
|
|
|
|
Alloc_small(res, Double_wosize, Double_tag);
|
|
|
|
#undef Setup_for_gc
|
|
|
|
#undef Restore_after_gc
|
|
|
|
Store_double_val(res, d);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_format_float(value fmt, value arg)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
value res;
|
2012-01-16 02:23:51 -08:00
|
|
|
double d = Double_val(arg);
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2012-01-16 02:23:51 -08:00
|
|
|
#ifdef HAS_BROKEN_PRINTF
|
|
|
|
if (isfinite(d)) {
|
|
|
|
#endif
|
2014-04-15 10:09:13 -07:00
|
|
|
res = caml_alloc_sprintf(String_val(fmt), d);
|
2012-01-16 02:23:51 -08:00
|
|
|
#ifdef HAS_BROKEN_PRINTF
|
|
|
|
} else {
|
2014-04-15 10:09:13 -07:00
|
|
|
if (isnan(d)) {
|
2012-01-16 02:23:51 -08:00
|
|
|
res = caml_copy_string("nan");
|
2014-04-15 10:09:13 -07:00
|
|
|
} else {
|
2012-01-16 02:23:51 -08:00
|
|
|
if (d > 0)
|
|
|
|
res = caml_copy_string("inf");
|
|
|
|
else
|
|
|
|
res = caml_copy_string("-inf");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
1995-05-04 03:15:53 -07:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-04-15 10:09:13 -07:00
|
|
|
#if 0
|
2005-03-04 06:51:31 -08:00
|
|
|
/*CAMLprim*/ value caml_float_of_substring(value vs, value idx, value l)
|
|
|
|
{
|
|
|
|
char parse_buffer[64];
|
|
|
|
char * buf, * src, * dst, * end;
|
|
|
|
mlsize_t len, lenvs;
|
|
|
|
double d;
|
2005-09-22 07:21:50 -07:00
|
|
|
intnat flen = Long_val(l);
|
|
|
|
intnat fidx = Long_val(idx);
|
2005-03-04 06:51:31 -08:00
|
|
|
|
|
|
|
lenvs = caml_string_length(vs);
|
|
|
|
len =
|
|
|
|
fidx >= 0 && fidx < lenvs && flen > 0 && flen <= lenvs - fidx
|
|
|
|
? flen : 0;
|
|
|
|
buf = len < sizeof(parse_buffer) ? parse_buffer : caml_stat_alloc(len + 1);
|
|
|
|
src = String_val(vs) + fidx;
|
|
|
|
dst = buf;
|
|
|
|
while (len--) {
|
|
|
|
char c = *src++;
|
|
|
|
if (c != '_') *dst++ = c;
|
|
|
|
}
|
|
|
|
*dst = 0;
|
2005-10-12 07:50:03 -07:00
|
|
|
if (dst == buf) goto error;
|
2005-03-04 06:51:31 -08:00
|
|
|
d = strtod((const char *) buf, &end);
|
2005-10-12 07:50:03 -07:00
|
|
|
if (end != dst) goto error;
|
2005-03-04 06:51:31 -08:00
|
|
|
if (buf != parse_buffer) caml_stat_free(buf);
|
|
|
|
return caml_copy_double(d);
|
2005-10-12 07:50:03 -07:00
|
|
|
error:
|
|
|
|
if (buf != parse_buffer) caml_stat_free(buf);
|
|
|
|
caml_failwith("float_of_string");
|
2005-03-04 06:51:31 -08:00
|
|
|
}
|
2014-04-15 10:09:13 -07:00
|
|
|
#endif
|
2005-03-04 06:51:31 -08:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_float_of_string(value vs)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2002-02-04 08:44:55 -08:00
|
|
|
char parse_buffer[64];
|
|
|
|
char * buf, * src, * dst, * end;
|
|
|
|
mlsize_t len;
|
|
|
|
double d;
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
len = caml_string_length(vs);
|
2003-12-31 06:20:40 -08:00
|
|
|
buf = len < sizeof(parse_buffer) ? parse_buffer : caml_stat_alloc(len + 1);
|
2002-02-04 08:44:55 -08:00
|
|
|
src = String_val(vs);
|
|
|
|
dst = buf;
|
|
|
|
while (len--) {
|
|
|
|
char c = *src++;
|
|
|
|
if (c != '_') *dst++ = c;
|
|
|
|
}
|
|
|
|
*dst = 0;
|
2005-10-12 07:50:03 -07:00
|
|
|
if (dst == buf) goto error;
|
2002-02-04 08:44:55 -08:00
|
|
|
d = strtod((const char *) buf, &end);
|
2005-10-12 07:50:03 -07:00
|
|
|
if (end != dst) goto error;
|
2003-12-31 06:20:40 -08:00
|
|
|
if (buf != parse_buffer) caml_stat_free(buf);
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(d);
|
2005-10-12 07:50:03 -07:00
|
|
|
error:
|
|
|
|
if (buf != parse_buffer) caml_stat_free(buf);
|
|
|
|
caml_failwith("float_of_string");
|
2015-07-17 07:31:05 -07:00
|
|
|
return Val_unit; /* not reached */
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_int_of_float(value f)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2005-09-22 07:21:50 -07:00
|
|
|
return Val_long((intnat) Double_val(f));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_float_of_int(value n)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double((double) Long_val(n));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_neg_float(value f)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(- Double_val(f));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_abs_float(value f)
|
1996-03-07 05:46:28 -08:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(fabs(Double_val(f)));
|
1996-03-07 05:46:28 -08:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_add_float(value f, value g)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(Double_val(f) + Double_val(g));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_sub_float(value f, value g)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(Double_val(f) - Double_val(g));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_mul_float(value f, value g)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(Double_val(f) * Double_val(g));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_div_float(value f, value g)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(Double_val(f) / Double_val(g));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_exp_float(value f)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(exp(Double_val(f)));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_floor_float(value f)
|
1997-03-04 06:55:17 -08:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(floor(Double_val(f)));
|
1997-03-04 06:55:17 -08:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_fmod_float(value f1, value f2)
|
1997-03-04 06:55:17 -08:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(fmod(Double_val(f1), Double_val(f2)));
|
1997-03-04 06:55:17 -08:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_frexp_float(value f)
|
1997-03-04 06:55:17 -08:00
|
|
|
{
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLparam1 (f);
|
|
|
|
CAMLlocal2 (res, mantissa);
|
1997-05-26 10:16:31 -07:00
|
|
|
int exponent;
|
1999-11-29 11:03:05 -08:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
mantissa = caml_copy_double(frexp (Double_val(f), &exponent));
|
2003-12-29 14:15:02 -08:00
|
|
|
res = caml_alloc_tuple(2);
|
1999-11-29 11:03:05 -08:00
|
|
|
Field(res, 0) = mantissa;
|
|
|
|
Field(res, 1) = Val_int(exponent);
|
|
|
|
CAMLreturn (res);
|
1997-03-04 06:55:17 -08:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_ldexp_float(value f, value i)
|
1997-03-04 06:55:17 -08:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(ldexp(Double_val(f), Int_val(i)));
|
1997-03-04 06:55:17 -08:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_log_float(value f)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(log(Double_val(f)));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_log10_float(value f)
|
1997-03-04 06:55:17 -08:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(log10(Double_val(f)));
|
1997-03-04 06:55:17 -08:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_modf_float(value f)
|
1997-03-04 06:55:17 -08:00
|
|
|
{
|
1997-05-26 10:16:31 -07:00
|
|
|
double frem;
|
2009-04-01 09:08:37 -07:00
|
|
|
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLparam1 (f);
|
|
|
|
CAMLlocal3 (res, quo, rem);
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
quo = caml_copy_double(modf (Double_val(f), &frem));
|
|
|
|
rem = caml_copy_double(frem);
|
2003-12-29 14:15:02 -08:00
|
|
|
res = caml_alloc_tuple(2);
|
1999-11-29 11:03:05 -08:00
|
|
|
Field(res, 0) = quo;
|
|
|
|
Field(res, 1) = rem;
|
|
|
|
CAMLreturn (res);
|
1997-03-04 06:55:17 -08:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_sqrt_float(value f)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(sqrt(Double_val(f)));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_power_float(value f, value g)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(pow(Double_val(f), Double_val(g)));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_sin_float(value f)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(sin(Double_val(f)));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_sinh_float(value f)
|
1997-03-04 06:55:17 -08:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(sinh(Double_val(f)));
|
1997-03-04 06:55:17 -08:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_cos_float(value f)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(cos(Double_val(f)));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_cosh_float(value f)
|
1997-03-04 06:55:17 -08:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(cosh(Double_val(f)));
|
1997-03-04 06:55:17 -08:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_tan_float(value f)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(tan(Double_val(f)));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_tanh_float(value f)
|
1997-03-04 06:55:17 -08:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(tanh(Double_val(f)));
|
1997-03-04 06:55:17 -08:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_asin_float(value f)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(asin(Double_val(f)));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_acos_float(value f)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(acos(Double_val(f)));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_atan_float(value f)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(atan(Double_val(f)));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_atan2_float(value f, value g)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(atan2(Double_val(f), Double_val(g)));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_ceil_float(value f)
|
1997-03-04 06:55:17 -08:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
return caml_copy_double(ceil(Double_val(f)));
|
1997-03-04 06:55:17 -08:00
|
|
|
}
|
|
|
|
|
2011-06-04 01:55:55 -07:00
|
|
|
CAMLexport double caml_hypot(double x, double y)
|
|
|
|
{
|
|
|
|
#ifdef HAS_C99_FLOAT_OPS
|
|
|
|
return hypot(x, y);
|
|
|
|
#else
|
|
|
|
double tmp, ratio;
|
|
|
|
if (x != x) return x; /* NaN */
|
|
|
|
if (y != y) return y; /* NaN */
|
|
|
|
x = fabs(x); y = fabs(y);
|
|
|
|
if (x < y) { tmp = x; x = y; y = tmp; }
|
|
|
|
if (x == 0.0) return 0.0;
|
|
|
|
ratio = y / x;
|
|
|
|
return x * sqrt(1.0 + ratio * ratio);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
CAMLprim value caml_hypot_float(value f, value g)
|
|
|
|
{
|
|
|
|
return caml_copy_double(caml_hypot(Double_val(f), Double_val(g)));
|
|
|
|
}
|
|
|
|
|
2009-04-01 09:08:37 -07:00
|
|
|
/* These emulations of expm1() and log1p() are due to William Kahan.
|
|
|
|
See http://www.plunk.org/~hatch/rightway.php */
|
|
|
|
CAMLexport double caml_expm1(double x)
|
|
|
|
{
|
2011-06-04 01:55:55 -07:00
|
|
|
#ifdef HAS_C99_FLOAT_OPS
|
2009-04-01 09:08:37 -07:00
|
|
|
return expm1(x);
|
|
|
|
#else
|
|
|
|
double u = exp(x);
|
|
|
|
if (u == 1.)
|
|
|
|
return x;
|
|
|
|
if (u - 1. == -1.)
|
|
|
|
return -1.;
|
|
|
|
return (u - 1.) * x / log(u);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
CAMLexport double caml_log1p(double x)
|
|
|
|
{
|
2011-06-04 01:55:55 -07:00
|
|
|
#ifdef HAS_C99_FLOAT_OPS
|
2009-04-01 09:08:37 -07:00
|
|
|
return log1p(x);
|
|
|
|
#else
|
|
|
|
double u = 1. + x;
|
|
|
|
if (u == 1.)
|
|
|
|
return x;
|
|
|
|
else
|
|
|
|
return log(u) * x / (u - 1.);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
CAMLprim value caml_expm1_float(value f)
|
|
|
|
{
|
|
|
|
return caml_copy_double(caml_expm1(Double_val(f)));
|
|
|
|
}
|
|
|
|
|
|
|
|
CAMLprim value caml_log1p_float(value f)
|
|
|
|
{
|
|
|
|
return caml_copy_double(caml_log1p(Double_val(f)));
|
|
|
|
}
|
|
|
|
|
2011-06-04 01:55:55 -07:00
|
|
|
union double_as_two_int32 {
|
|
|
|
double d;
|
|
|
|
#if defined(ARCH_BIG_ENDIAN) || (defined(__arm__) && !defined(__ARM_EABI__))
|
2014-08-27 02:58:33 -07:00
|
|
|
struct { uint32_t h; uint32_t l; } i;
|
2011-06-04 01:55:55 -07:00
|
|
|
#else
|
2014-08-27 02:58:33 -07:00
|
|
|
struct { uint32_t l; uint32_t h; } i;
|
2011-06-04 01:55:55 -07:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
CAMLexport double caml_copysign(double x, double y)
|
|
|
|
{
|
|
|
|
#ifdef HAS_C99_FLOAT_OPS
|
|
|
|
return copysign(x, y);
|
|
|
|
#else
|
|
|
|
union double_as_two_int32 ux, uy;
|
|
|
|
ux.d = x;
|
|
|
|
uy.d = y;
|
|
|
|
ux.i.h &= 0x7FFFFFFFU;
|
|
|
|
ux.i.h |= (uy.i.h & 0x80000000U);
|
|
|
|
return ux.d;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
CAMLprim value caml_copysign_float(value f, value g)
|
|
|
|
{
|
|
|
|
return caml_copy_double(caml_copysign(Double_val(f), Double_val(g)));
|
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_eq_float(value f, value g)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
return Val_bool(Double_val(f) == Double_val(g));
|
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_neq_float(value f, value g)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
return Val_bool(Double_val(f) != Double_val(g));
|
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_le_float(value f, value g)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
return Val_bool(Double_val(f) <= Double_val(g));
|
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_lt_float(value f, value g)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
return Val_bool(Double_val(f) < Double_val(g));
|
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_ge_float(value f, value g)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
return Val_bool(Double_val(f) >= Double_val(g));
|
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_gt_float(value f, value g)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
return Val_bool(Double_val(f) > Double_val(g));
|
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_float_compare(value vf, value vg)
|
2003-04-01 00:46:39 -08:00
|
|
|
{
|
|
|
|
double f = Double_val(vf);
|
|
|
|
double g = Double_val(vg);
|
2003-11-21 07:56:56 -08:00
|
|
|
if (f == g) return Val_int(0);
|
|
|
|
if (f < g) return Val_int(-1);
|
|
|
|
if (f > g) return Val_int(1);
|
|
|
|
/* One or both of f and g is NaN. Order according to the
|
|
|
|
convention NaN = NaN and NaN < x for all other floats x. */
|
|
|
|
if (f == f) return Val_int(1); /* f is not NaN, g is NaN */
|
|
|
|
if (g == g) return Val_int(-1); /* g is not NaN, f is NaN */
|
|
|
|
return Val_int(0); /* both f and g are NaN */
|
2003-04-01 00:46:39 -08:00
|
|
|
}
|
|
|
|
|
2001-02-05 00:51:16 -08:00
|
|
|
enum { FP_normal, FP_subnormal, FP_zero, FP_infinite, FP_nan };
|
1995-10-16 09:59:18 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_classify_float(value vd)
|
2001-02-05 00:51:16 -08:00
|
|
|
{
|
2002-08-05 05:19:50 -07:00
|
|
|
/* Cygwin 1.3 has problems with fpclassify (PR#1293), so don't use it */
|
2015-07-17 07:31:05 -07:00
|
|
|
/* FIXME Cygwin 1.3 is ancient! Revisit this decision. */
|
|
|
|
#if defined(fpclassify) && !defined(__CYGWIN__) && !defined(__MINGW32__)
|
2001-02-05 00:51:16 -08:00
|
|
|
switch (fpclassify(Double_val(vd))) {
|
|
|
|
case FP_NAN:
|
|
|
|
return Val_int(FP_nan);
|
|
|
|
case FP_INFINITE:
|
|
|
|
return Val_int(FP_infinite);
|
|
|
|
case FP_ZERO:
|
|
|
|
return Val_int(FP_zero);
|
|
|
|
case FP_SUBNORMAL:
|
|
|
|
return Val_int(FP_subnormal);
|
|
|
|
default: /* case FP_NORMAL */
|
|
|
|
return Val_int(FP_normal);
|
|
|
|
}
|
|
|
|
#else
|
2011-06-04 01:55:55 -07:00
|
|
|
union double_as_two_int32 u;
|
2014-08-27 02:58:33 -07:00
|
|
|
uint32_t h, l;
|
2004-01-09 07:33:31 -08:00
|
|
|
|
|
|
|
u.d = Double_val(vd);
|
|
|
|
h = u.i.h; l = u.i.l;
|
2001-02-05 00:51:16 -08:00
|
|
|
l = l | (h & 0xFFFFF);
|
|
|
|
h = h & 0x7FF00000;
|
|
|
|
if ((h | l) == 0)
|
|
|
|
return Val_int(FP_zero);
|
|
|
|
if (h == 0)
|
|
|
|
return Val_int(FP_subnormal);
|
|
|
|
if (h == 0x7FF00000) {
|
|
|
|
if (l == 0)
|
|
|
|
return Val_int(FP_infinite);
|
|
|
|
else
|
|
|
|
return Val_int(FP_nan);
|
|
|
|
}
|
|
|
|
return Val_int(FP_normal);
|
1995-10-16 09:59:18 -07:00
|
|
|
#endif
|
2001-02-05 00:51:16 -08:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
/* The [caml_init_ieee_float] function should initialize floating-point hardware
|
2001-02-05 00:51:16 -08:00
|
|
|
so that it behaves as much as possible like the IEEE standard.
|
|
|
|
In particular, return special numbers like Infinity and NaN instead
|
|
|
|
of signalling exceptions. Currently, everyone is in IEEE mode
|
2001-03-26 19:08:20 -08:00
|
|
|
at program startup, except FreeBSD prior to 4.0R. */
|
1995-10-16 09:59:18 -07:00
|
|
|
|
2001-03-13 21:36:59 -08:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
#include <osreldate.h>
|
2001-03-26 19:08:20 -08:00
|
|
|
#if (__FreeBSD_version < 400017)
|
2001-03-13 21:36:59 -08:00
|
|
|
#include <floatingpoint.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
void caml_init_ieee_floats(void)
|
1995-10-16 09:59:18 -07:00
|
|
|
{
|
2001-03-26 19:08:20 -08:00
|
|
|
#if defined(__FreeBSD__) && (__FreeBSD_version < 400017)
|
|
|
|
fpsetmask(0);
|
2001-03-13 21:36:59 -08:00
|
|
|
#endif
|
1995-10-16 09:59:18 -07:00
|
|
|
}
|