Debian Codegraph @

kernel/byterun/rocq_uint63_emul.h

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 # pragma once
12
13 # include <caml/callback.h>
14 # include <caml/stack.h>
15
16
17 #define Is_uint63(v) (Tag_val(v) == Custom_tag)
18
19 #define uint_of_value(val) (((uint32_t)(val)) >> 1)
20
21 # define DECLARE_NULLOP(name) \
22 value uint63_##name() { \
23 static value const *cb = 0; \
24 CAMLparam0(); \
25 if (!cb) cb = caml_named_value("uint63 " #name); \
26 CAMLreturn(*cb); \
27 }
28
29 # define DECLARE_UNOP(name) \
30 value uint63_##name##_ml(value x) { \
31 static value const *cb = 0; \
32 CAMLparam1(x); \
33 if (!cb) cb = caml_named_value("uint63 " #name); \
34 CAMLreturn(caml_callback(*cb, x)); \
35 }
36
37 # define CALL_UNOP_NOASSIGN(name, x) \
38 value uint63_return_value__; \
39 value uint63_arg_x__ = (x); \
40 Setup_for_gc; \
41 uint63_return_value__ = uint63_##name##_ml(uint63_arg_x__); \
42 Restore_after_gc
43
44 # define CALL_UNOP(name, x) do{ \
45 CALL_UNOP_NOASSIGN(name, x); \
46 accu = uint63_return_value__; \
47 }while(0)
48
49 # define CALL_PREDICATE(r, name, x) do{ \
50 CALL_UNOP_NOASSIGN(name, x); \
51 (r) = Int_val(uint63_return_value__); \
52 }while(0)
53
54 # define DECLARE_BINOP(name) \
55 value uint63_##name##_ml(value x, value y) { \
56 static value const *cb = 0; \
57 CAMLparam2(x, y); \
58 if (!cb) cb = caml_named_value("uint63 " #name); \
59 CAMLreturn(caml_callback2(*cb, x, y)); \
60 }
61
62 # define CALL_BINOP_NOASSIGN(name, x, y) \
63 value uint63_return_value__; \
64 value uint63_arg_x__ = (x); \
65 value uint63_arg_y__ = (y); \
66 Setup_for_gc; \
67 uint63_return_value__ = uint63_##name##_ml(uint63_arg_x__, uint63_arg_y__); \
68 Restore_after_gc
69
70 # define CALL_BINOP(name, x, y) do{ \
71 CALL_BINOP_NOASSIGN(name, x, y); \
72 accu = uint63_return_value__; \
73 }while(0)
74
75 # define CALL_RELATION(r, name, x, y) do{ \
76 CALL_BINOP_NOASSIGN(name, x, y); \
77 (r) = Int_val(uint63_return_value__); \
78 }while(0)
79
80 # define DECLARE_TEROP(name) \
81 value uint63_##name##_ml(value x, value y, value z) { \
82 static value const *cb = 0; \
83 CAMLparam3(x, y, z); \
84 if (!cb) cb = caml_named_value("uint63 " #name); \
85 CAMLreturn(caml_callback3(*cb, x, y, z)); \
86 }
87
88 # define CALL_TEROP(name, x, y, z) do{ \
89 value uint63_return_value__; \
90 value uint63_arg_x__ = (x); \
91 value uint63_arg_y__ = (y); \
92 value uint63_arg_z__ = (z); \
93 Setup_for_gc; \
94 uint63_return_value__ = uint63_##name##_ml(uint63_arg_x__, uint63_arg_y__, uint63_arg_z__); \
95 Restore_after_gc; \
96 accu = uint63_return_value__; \
97 }while(0)
98
99 DECLARE_NULLOP(zero)
100 DECLARE_NULLOP(one)
101 DECLARE_UNOP(neg)
102 #define Uint63_neg(x) CALL_UNOP(neg, x)
103 DECLARE_BINOP(add)
104 #define Uint63_add(x, y) CALL_BINOP(add, x, y)
105 DECLARE_BINOP(addcarry)
106 #define Uint63_addcarry(x, y) CALL_BINOP(addcarry, x, y)
107 DECLARE_TEROP(addmuldiv)
108 #define Uint63_addmuldiv(x, y, z) CALL_TEROP(addmuldiv, x, y, z)
109 DECLARE_BINOP(div)
110 #define Uint63_div(x, y) CALL_BINOP(div, x, y)
111 DECLARE_BINOP(divs)
112 #define Uint63_divs(x, y) CALL_BINOP(divs, x, y)
113 DECLARE_BINOP(eq)
114 #define Uint63_eq(r, x, y) CALL_RELATION(r, eq, x, y)
115 DECLARE_UNOP(eq0)
116 #define Uint63_eq0(r, x) CALL_PREDICATE(r, eq0, x)
117 DECLARE_UNOP(eqm1)
118 #define Uint63_eqm1(r, x) CALL_PREDICATE(r, eqm1, x)
119 DECLARE_UNOP(head0)
120 #define Uint63_head0(x) CALL_UNOP(head0, x)
121 DECLARE_BINOP(land)
122 #define Uint63_land(x, y) CALL_BINOP(land, x, y)
123 DECLARE_BINOP(leq)
124 #define Uint63_leq(r, x, y) CALL_RELATION(r, leq, x, y)
125 DECLARE_BINOP(les)
126 #define Uint63_les(r, x, y) CALL_RELATION(r, les, x, y)
127 DECLARE_BINOP(lor)
128 #define Uint63_lor(x, y) CALL_BINOP(lor, x, y)
129 DECLARE_BINOP(lsl)
130 #define Uint63_lsl(x, y) CALL_BINOP(lsl, x, y)
131 DECLARE_BINOP(lsr)
132 #define Uint63_lsr(x, y) CALL_BINOP(lsr, x, y)
133 DECLARE_BINOP(asr)
134 #define Uint63_asr(x, y) CALL_BINOP(asr, x, y)
135 DECLARE_BINOP(lt)
136 #define Uint63_lt(r, x, y) CALL_RELATION(r, lt, x, y)
137 DECLARE_BINOP(lts)
138 #define Uint63_lts(r, x, y) CALL_RELATION(r, lts, x, y)
139 DECLARE_BINOP(lxor)
140 #define Uint63_lxor(x, y) CALL_BINOP(lxor, x, y)
141 DECLARE_BINOP(mod)
142 #define Uint63_mod(x, y) CALL_BINOP(mod, x, y)
143 DECLARE_BINOP(mods)
144 #define Uint63_mods(x, y) CALL_BINOP(mods, x, y)
145 DECLARE_BINOP(mul)
146 #define Uint63_mul(x, y) CALL_BINOP(mul, x, y)
147 DECLARE_BINOP(sub)
148 #define Uint63_sub(x, y) CALL_BINOP(sub, x, y)
149 DECLARE_BINOP(subcarry)
150 #define Uint63_subcarry(x, y) CALL_BINOP(subcarry, x, y)
151 DECLARE_UNOP(tail0)
152 #define Uint63_tail0(x) CALL_UNOP(tail0, x)
153
154 DECLARE_TEROP(div21_ml)
155 # define Uint63_div21(x, y, z, q) do{ \
156 value uint63_return_value__; \
157 value uint63_arg_x__ = (x); \
158 value uint63_arg_y__ = (y); \
159 value uint63_arg_z__ = (z); \
160 Setup_for_gc; \
161 uint63_return_value__ = \
162 uint63_div21_ml_ml(uint63_arg_x__, uint63_arg_y__, uint63_arg_z__); \
163 Restore_after_gc; \
164 *q = Field(uint63_return_value__, 0); \
165 accu = Field(uint63_return_value__, 1); \
166 }while(0)
167
168 DECLARE_BINOP(mulc_ml)
169 # define Uint63_mulc(x, y, h) do{ \
170 value uint63_return_value__; \
171 value uint63_arg_x__ = (x); \
172 value uint63_arg_y__ = (y); \
173 Setup_for_gc; \
174 uint63_return_value__ = \
175 uint63_mulc_ml_ml(uint63_arg_x__, uint63_arg_y__); \
176 Restore_after_gc; \
177 *(h) = Field(uint63_return_value__, 0); \
178 accu = Field(uint63_return_value__, 1); \
179 }while(0)
180
181 DECLARE_UNOP(to_float)
182 #define Uint63_to_double(x) CALL_UNOP(to_float, x)
183
184 DECLARE_UNOP(of_float)
185 #define Uint63_of_double(f) do{ \
186 Rocq_copy_double(f); \
187 CALL_UNOP(of_float, accu); \
188 }while(0)
189
190 DECLARE_UNOP(of_int)
191 #define Uint63_of_int(x) CALL_UNOP(of_int, x)
192
193 DECLARE_BINOP(to_int_min)
194 #define Uint63_to_int_min(n, m) CALL_BINOP(to_int_min, n, m)