Debian Codegraph @

Modules/_hacl/include/krml/internal/target.h

raw
1 /* Copyright (c) INRIA and Microsoft Corporation. All rights reserved.
2 Licensed under the Apache 2.0 and MIT Licenses. */
3
4 #ifndef KRML_HEADER_TARGET_H
5 #define KRML_HEADER_TARGET_H
6
7 #include <assert.h>
8 #include <inttypes.h>
9 #include <limits.h>
10 #include <stdbool.h>
11 #include <stddef.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14
15 typedef float float32_t;
16 typedef double float64_t;
17
18 /* Since KaRaMeL emits the inline keyword unconditionally, we follow the
19 * guidelines at https://gcc.gnu.org/onlinedocs/gcc/Inline.html and make this
20 * __inline__ to ensure the code compiles with -std=c90 and earlier. */
21 #ifdef __GNUC__
22 # define inline __inline__
23 #endif
24
25 /* There is no support for aligned_alloc() in macOS before Catalina, so
26 * let's make a macro to use _mm_malloc() and _mm_free() functions
27 * from mm_malloc.h. */
28 #if defined(__APPLE__) && defined(__MACH__)
29 # include <AvailabilityMacros.h>
30 # if defined(MAC_OS_X_VERSION_MIN_REQUIRED) && \
31 (MAC_OS_X_VERSION_MIN_REQUIRED < 101500)
32 # include <mm_malloc.h>
33 # define LEGACY_MACOS
34 # else
35 # undef LEGACY_MACOS
36 #endif
37 #endif
38
39 /******************************************************************************/
40 /* Macros that KaRaMeL will generate. */
41 /******************************************************************************/
42
43 /* For "bare" targets that do not have a C stdlib, the user might want to use
44 * [-add-early-include '"mydefinitions.h"'] and override these. */
45 #ifndef KRML_HOST_PRINTF
46 # define KRML_HOST_PRINTF printf
47 #endif
48
49 #if ( \
50 (defined __STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
51 (!(defined KRML_HOST_EPRINTF)))
52 # define KRML_HOST_EPRINTF(...) fprintf(stderr, __VA_ARGS__)
53 #elif !(defined KRML_HOST_EPRINTF) && defined(_MSC_VER)
54 # define KRML_HOST_EPRINTF(...) fprintf(stderr, __VA_ARGS__)
55 #endif
56
57 #ifndef KRML_HOST_EXIT
58 # define KRML_HOST_EXIT exit
59 #endif
60
61 #ifndef KRML_HOST_MALLOC
62 # define KRML_HOST_MALLOC malloc
63 #endif
64
65 #ifndef KRML_HOST_CALLOC
66 # define KRML_HOST_CALLOC calloc
67 #endif
68
69 #ifndef KRML_HOST_FREE
70 # define KRML_HOST_FREE free
71 #endif
72
73 #ifndef KRML_HOST_IGNORE
74 # define KRML_HOST_IGNORE(x) (void)(x)
75 #endif
76
77 #ifndef KRML_MAYBE_UNUSED_VAR
78 # define KRML_MAYBE_UNUSED_VAR(x) KRML_HOST_IGNORE(x)
79 #endif
80
81 #ifndef KRML_MAYBE_UNUSED
82 # if defined(__GNUC__) || defined(__clang__)
83 # define KRML_MAYBE_UNUSED __attribute__((unused))
84 # else
85 # define KRML_MAYBE_UNUSED
86 # endif
87 #endif
88
89 #ifndef KRML_ATTRIBUTE_TARGET
90 # if defined(__GNUC__) || defined(__clang__)
91 # define KRML_ATTRIBUTE_TARGET(x) __attribute__((target(x)))
92 # else
93 # define KRML_ATTRIBUTE_TARGET(x)
94 # endif
95 #endif
96
97 #ifndef KRML_NOINLINE
98 # if defined (__GNUC__) || defined (__clang__)
99 # define KRML_NOINLINE __attribute__((noinline,unused))
100 # elif defined(_MSC_VER)
101 # define KRML_NOINLINE __declspec(noinline)
102 # elif defined (__SUNPRO_C)
103 # define KRML_NOINLINE __attribute__((noinline))
104 # else
105 # define KRML_NOINLINE
106 # warning "The KRML_NOINLINE macro is not defined for this toolchain!"
107 # warning "The compiler may defeat side-channel resistance with optimizations."
108 # warning "Please locate target.h and try to fill it out with a suitable definition for this compiler."
109 # endif
110 #endif
111
112 #ifndef KRML_MUSTINLINE
113 # if defined(_MSC_VER)
114 # define KRML_MUSTINLINE inline __forceinline
115 # elif defined (__GNUC__)
116 # define KRML_MUSTINLINE inline __attribute__((always_inline))
117 # elif defined (__SUNPRO_C)
118 # define KRML_MUSTINLINE inline __attribute__((always_inline))
119 # else
120 # define KRML_MUSTINLINE inline
121 # warning "The KRML_MUSTINLINE macro defaults to plain inline for this toolchain!"
122 # warning "Please locate target.h and try to fill it out with a suitable definition for this compiler."
123 # endif
124 #endif
125
126 #ifndef KRML_PRE_ALIGN
127 # ifdef _MSC_VER
128 # define KRML_PRE_ALIGN(X) __declspec(align(X))
129 # else
130 # define KRML_PRE_ALIGN(X)
131 # endif
132 #endif
133
134 #ifndef KRML_POST_ALIGN
135 # ifdef _MSC_VER
136 # define KRML_POST_ALIGN(X)
137 # else
138 # define KRML_POST_ALIGN(X) __attribute__((aligned(X)))
139 # endif
140 #endif
141
142 /* MinGW-W64 does not support C11 aligned_alloc, but it supports
143 * MSVC's _aligned_malloc.
144 */
145 #ifndef KRML_ALIGNED_MALLOC
146 # ifdef __MINGW32__
147 # include <_mingw.h>
148 # endif
149 # if ( \
150 defined(_MSC_VER) || \
151 (defined(__MINGW32__) && defined(__MINGW64_VERSION_MAJOR)))
152 # define KRML_ALIGNED_MALLOC(X, Y) _aligned_malloc(Y, X)
153 # elif defined(LEGACY_MACOS)
154 # define KRML_ALIGNED_MALLOC(X, Y) _mm_malloc(Y, X)
155 # else
156 # define KRML_ALIGNED_MALLOC(X, Y) aligned_alloc(X, Y)
157 # endif
158 #endif
159
160 /* Since aligned allocations with MinGW-W64 are done with
161 * _aligned_malloc (see above), such pointers must be freed with
162 * _aligned_free.
163 */
164 #ifndef KRML_ALIGNED_FREE
165 # ifdef __MINGW32__
166 # include <_mingw.h>
167 # endif
168 # if ( \
169 defined(_MSC_VER) || \
170 (defined(__MINGW32__) && defined(__MINGW64_VERSION_MAJOR)))
171 # define KRML_ALIGNED_FREE(X) _aligned_free(X)
172 # elif defined(LEGACY_MACOS)
173 # define KRML_ALIGNED_FREE(X) _mm_free(X)
174 # else
175 # define KRML_ALIGNED_FREE(X) free(X)
176 # endif
177 #endif
178
179 #ifndef KRML_HOST_TIME
180
181 # include <time.h>
182
183 /* Prims_nat not yet in scope */
184 inline static int32_t krml_time(void) {
185 return (int32_t)time(NULL);
186 }
187
188 # define KRML_HOST_TIME krml_time
189 #endif
190
191 /* In statement position, exiting is easy. */
192 #define KRML_EXIT \
193 do { \
194 KRML_HOST_PRINTF("Unimplemented function at %s:%d\n", __FILE__, __LINE__); \
195 KRML_HOST_EXIT(254); \
196 } while (0)
197
198 /* In expression position, use the comma-operator and a malloc to return an
199 * expression of the right size. KaRaMeL passes t as the parameter to the macro.
200 */
201 #define KRML_EABORT(t, msg) \
202 (KRML_HOST_PRINTF("KaRaMeL abort at %s:%d\n%s\n", __FILE__, __LINE__, msg), \
203 KRML_HOST_EXIT(255), *((t *)KRML_HOST_MALLOC(sizeof(t))))
204
205 /* In FStar.Buffer.fst, the size of arrays is uint32_t, but it's a number of
206 * *elements*. Do an ugly, run-time check (some of which KaRaMeL can eliminate).
207 */
208 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 4))
209 # define _KRML_CHECK_SIZE_PRAGMA \
210 _Pragma("GCC diagnostic ignored \"-Wtype-limits\"")
211 #else
212 # define _KRML_CHECK_SIZE_PRAGMA
213 #endif
214
215 #define KRML_CHECK_SIZE(size_elt, sz) \
216 do { \
217 _KRML_CHECK_SIZE_PRAGMA \
218 if (((size_t)(sz)) > ((size_t)(SIZE_MAX / (size_elt)))) { \
219 KRML_HOST_PRINTF( \
220 "Maximum allocatable size exceeded, aborting before overflow at " \
221 "%s:%d\n", \
222 __FILE__, __LINE__); \
223 KRML_HOST_EXIT(253); \
224 } \
225 } while (0)
226
227 #if defined(_MSC_VER) && _MSC_VER < 1900
228 # define KRML_HOST_SNPRINTF(buf, sz, fmt, arg) \
229 _snprintf_s(buf, sz, _TRUNCATE, fmt, arg)
230 #else
231 # define KRML_HOST_SNPRINTF(buf, sz, fmt, arg) snprintf(buf, sz, fmt, arg)
232 #endif
233
234 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 4))
235 # define KRML_DEPRECATED(x) __attribute__((deprecated(x)))
236 #elif defined(__GNUC__)
237 /* deprecated attribute is not defined in GCC < 4.5. */
238 # define KRML_DEPRECATED(x)
239 #elif defined(__SUNPRO_C)
240 # define KRML_DEPRECATED(x) __attribute__((deprecated(x)))
241 #elif defined(_MSC_VER)
242 # define KRML_DEPRECATED(x) __declspec(deprecated(x))
243 #endif
244
245 /* Macros for prettier unrolling of loops */
246 #define KRML_LOOP1(i, n, x) { \
247 x \
248 i += n; \
249 (void) i; \
250 }
251
252 #define KRML_LOOP2(i, n, x) \
253 KRML_LOOP1(i, n, x) \
254 KRML_LOOP1(i, n, x)
255
256 #define KRML_LOOP3(i, n, x) \
257 KRML_LOOP2(i, n, x) \
258 KRML_LOOP1(i, n, x)
259
260 #define KRML_LOOP4(i, n, x) \
261 KRML_LOOP2(i, n, x) \
262 KRML_LOOP2(i, n, x)
263
264 #define KRML_LOOP5(i, n, x) \
265 KRML_LOOP4(i, n, x) \
266 KRML_LOOP1(i, n, x)
267
268 #define KRML_LOOP6(i, n, x) \
269 KRML_LOOP4(i, n, x) \
270 KRML_LOOP2(i, n, x)
271
272 #define KRML_LOOP7(i, n, x) \
273 KRML_LOOP4(i, n, x) \
274 KRML_LOOP3(i, n, x)
275
276 #define KRML_LOOP8(i, n, x) \
277 KRML_LOOP4(i, n, x) \
278 KRML_LOOP4(i, n, x)
279
280 #define KRML_LOOP9(i, n, x) \
281 KRML_LOOP8(i, n, x) \
282 KRML_LOOP1(i, n, x)
283
284 #define KRML_LOOP10(i, n, x) \
285 KRML_LOOP8(i, n, x) \
286 KRML_LOOP2(i, n, x)
287
288 #define KRML_LOOP11(i, n, x) \
289 KRML_LOOP8(i, n, x) \
290 KRML_LOOP3(i, n, x)
291
292 #define KRML_LOOP12(i, n, x) \
293 KRML_LOOP8(i, n, x) \
294 KRML_LOOP4(i, n, x)
295
296 #define KRML_LOOP13(i, n, x) \
297 KRML_LOOP8(i, n, x) \
298 KRML_LOOP5(i, n, x)
299
300 #define KRML_LOOP14(i, n, x) \
301 KRML_LOOP8(i, n, x) \
302 KRML_LOOP6(i, n, x)
303
304 #define KRML_LOOP15(i, n, x) \
305 KRML_LOOP8(i, n, x) \
306 KRML_LOOP7(i, n, x)
307
308 #define KRML_LOOP16(i, n, x) \
309 KRML_LOOP8(i, n, x) \
310 KRML_LOOP8(i, n, x)
311
312 #define KRML_UNROLL_FOR(i, z, n, k, x) \
313 do { \
314 uint32_t i = z; \
315 KRML_LOOP##n(i, k, x) \
316 } while (0)
317
318 #define KRML_ACTUAL_FOR(i, z, n, k, x) \
319 do { \
320 for (uint32_t i = z; i < n; i += k) { \
321 x \
322 } \
323 } while (0)
324
325 #ifndef KRML_UNROLL_MAX
326 # define KRML_UNROLL_MAX 16
327 #endif
328
329 /* 1 is the number of loop iterations, i.e. (n - z)/k as evaluated by krml */
330 #if 0 <= KRML_UNROLL_MAX
331 # define KRML_MAYBE_FOR0(i, z, n, k, x)
332 #else
333 # define KRML_MAYBE_FOR0(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)
334 #endif
335
336 #if 1 <= KRML_UNROLL_MAX
337 # define KRML_MAYBE_FOR1(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 1, k, x)
338 #else
339 # define KRML_MAYBE_FOR1(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)
340 #endif
341
342 #if 2 <= KRML_UNROLL_MAX
343 # define KRML_MAYBE_FOR2(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 2, k, x)
344 #else
345 # define KRML_MAYBE_FOR2(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)
346 #endif
347
348 #if 3 <= KRML_UNROLL_MAX
349 # define KRML_MAYBE_FOR3(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 3, k, x)
350 #else
351 # define KRML_MAYBE_FOR3(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)
352 #endif
353
354 #if 4 <= KRML_UNROLL_MAX
355 # define KRML_MAYBE_FOR4(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 4, k, x)
356 #else
357 # define KRML_MAYBE_FOR4(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)
358 #endif
359
360 #if 5 <= KRML_UNROLL_MAX
361 # define KRML_MAYBE_FOR5(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 5, k, x)
362 #else
363 # define KRML_MAYBE_FOR5(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)
364 #endif
365
366 #if 6 <= KRML_UNROLL_MAX
367 # define KRML_MAYBE_FOR6(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 6, k, x)
368 #else
369 # define KRML_MAYBE_FOR6(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)
370 #endif
371
372 #if 7 <= KRML_UNROLL_MAX
373 # define KRML_MAYBE_FOR7(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 7, k, x)
374 #else
375 # define KRML_MAYBE_FOR7(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)
376 #endif
377
378 #if 8 <= KRML_UNROLL_MAX
379 # define KRML_MAYBE_FOR8(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 8, k, x)
380 #else
381 # define KRML_MAYBE_FOR8(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)
382 #endif
383
384 #if 9 <= KRML_UNROLL_MAX
385 # define KRML_MAYBE_FOR9(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 9, k, x)
386 #else
387 # define KRML_MAYBE_FOR9(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)
388 #endif
389
390 #if 10 <= KRML_UNROLL_MAX
391 # define KRML_MAYBE_FOR10(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 10, k, x)
392 #else
393 # define KRML_MAYBE_FOR10(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)
394 #endif
395
396 #if 11 <= KRML_UNROLL_MAX
397 # define KRML_MAYBE_FOR11(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 11, k, x)
398 #else
399 # define KRML_MAYBE_FOR11(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)
400 #endif
401
402 #if 12 <= KRML_UNROLL_MAX
403 # define KRML_MAYBE_FOR12(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 12, k, x)
404 #else
405 # define KRML_MAYBE_FOR12(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)
406 #endif
407
408 #if 13 <= KRML_UNROLL_MAX
409 # define KRML_MAYBE_FOR13(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 13, k, x)
410 #else
411 # define KRML_MAYBE_FOR13(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)
412 #endif
413
414 #if 14 <= KRML_UNROLL_MAX
415 # define KRML_MAYBE_FOR14(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 14, k, x)
416 #else
417 # define KRML_MAYBE_FOR14(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)
418 #endif
419
420 #if 15 <= KRML_UNROLL_MAX
421 # define KRML_MAYBE_FOR15(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 15, k, x)
422 #else
423 # define KRML_MAYBE_FOR15(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)
424 #endif
425
426 #if 16 <= KRML_UNROLL_MAX
427 # define KRML_MAYBE_FOR16(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 16, k, x)
428 #else
429 # define KRML_MAYBE_FOR16(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)
430 #endif
431 #endif /* KRML_HEADER_TARGET_H */