Debian Codegraph @

kernel/byterun/rocq_float64.c

raw symbols SCIP
1 /************************************************************************/
2 /* * The Rocq Prover / The Rocq Development Team */
3 /* v * Copyright INRIA, CNRS and contributors */
4 /* <O___,, * (see version control and CREDITS file for authors & dates) */
5 /* \VV/ **************************************************************/
6 /* // * This file is distributed under the terms of the */
7 /* * GNU Lesser General Public License Version 2.1 */
8 /* * (see LICENSE file for the text of the license) */
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; // x is +oo or NaN
27 bits.d = x;
28 int64_t i = bits.u;
29 if (i >= 0) ++bits.u; // x >= +0.0, go away from zero
30 else if (bits.u + bits.u == 0) bits.u = 1; // x is -0.0, should almost never happen
31 else --bits.u; // x < 0.0, go toward zero
32 return bits.d;
33 }
34
35 static double next_down(double x) {
36 union double_bits bits;
37 if (!(x > -INFINITY)) return x; // x is -oo or NaN
38 bits.d = x;
39 int64_t i = bits.u;
40 if (i == 0) bits.u = INT64_MIN + 1; // x is +0.0
41 else if (i < 0) ++bits.u; // x <= -0.0, go away from zero
42 else --bits.u; // x > 0.0, go toward zero
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 }