| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include <math.h> |
| 12 |
#include <stdint.h> |
| 13 |
|
| 14 |
#define CAML_INTERNALS |
| 15 |
#include <caml/alloc.h> |
| 16 |
|
| 17 |
#include "rocq_values.h" |
| 18 |
|
| 19 |
union double_bits { |
| 20 |
double d; |
| 21 |
uint64_t u; |
| 22 |
}; |
| 23 |
|
| 24 |
static double next_up(double x) { |
| 25 |
union double_bits bits; |
| 26 |
if (!(x < INFINITY)) return x; |
| 27 |
bits.d = x; |
| 28 |
int64_t i = bits.u; |
| 29 |
if (i >= 0) ++bits.u; |
| 30 |
else if (bits.u + bits.u == 0) bits.u = 1; |
| 31 |
else --bits.u; |
| 32 |
return bits.d; |
| 33 |
} |
| 34 |
|
| 35 |
static double next_down(double x) { |
| 36 |
union double_bits bits; |
| 37 |
if (!(x > -INFINITY)) return x; |
| 38 |
bits.d = x; |
| 39 |
int64_t i = bits.u; |
| 40 |
if (i == 0) bits.u = INT64_MIN + 1; |
| 41 |
else if (i < 0) ++bits.u; |
| 42 |
else --bits.u; |
| 43 |
return bits.d; |
| 44 |
} |
| 45 |
|
| 46 |
#define DECLARE_FBINOP(name, e) \ |
| 47 |
double rocq_##name(double x, double y) { \ |
| 48 |
return e; \ |
| 49 |
} \ |
| 50 |
\ |
| 51 |
value rocq_##name##_byte(value x, value y) { \ |
| 52 |
return caml_copy_double(rocq_##name(Double_val(x), Double_val(y))); \ |
| 53 |
} |
| 54 |
|
| 55 |
#define DECLARE_FUNOP(name, e) \ |
| 56 |
double rocq_##name(double x) { \ |
| 57 |
return e; \ |
| 58 |
} \ |
| 59 |
\ |
| 60 |
value rocq_##name##_byte(value x) { \ |
| 61 |
return caml_copy_double(rocq_##name(Double_val(x))); \ |
| 62 |
} |
| 63 |
|
| 64 |
DECLARE_FBINOP(fmul, x * y) |
| 65 |
DECLARE_FBINOP(fadd, x + y) |
| 66 |
DECLARE_FBINOP(fsub, x - y) |
| 67 |
DECLARE_FBINOP(fdiv, x / y) |
| 68 |
DECLARE_FUNOP(fsqrt, sqrt(x)) |
| 69 |
DECLARE_FUNOP(next_up, next_up(x)) |
| 70 |
DECLARE_FUNOP(next_down, next_down(x)) |
| 71 |
|
| 72 |
value rocq_is_double(value x) { |
| 73 |
return Val_long(Is_double(x)); |
| 74 |
} |