Path: blob/main/Modules/_hacl/include/krml/internal/target.h
12 views
/* Copyright (c) INRIA and Microsoft Corporation. All rights reserved.1Licensed under the Apache 2.0 License. */23#ifndef __KRML_TARGET_H4#define __KRML_TARGET_H56#include <stdlib.h>7#include <stddef.h>8#include <stdio.h>9#include <stdbool.h>10#include <inttypes.h>11#include <limits.h>12#include <assert.h>1314/* Since KaRaMeL emits the inline keyword unconditionally, we follow the15* guidelines at https://gcc.gnu.org/onlinedocs/gcc/Inline.html and make this16* __inline__ to ensure the code compiles with -std=c90 and earlier. */17#ifdef __GNUC__18# define inline __inline__19#endif2021/******************************************************************************/22/* Macros that KaRaMeL will generate. */23/******************************************************************************/2425/* For "bare" targets that do not have a C stdlib, the user might want to use26* [-add-early-include '"mydefinitions.h"'] and override these. */27#ifndef KRML_HOST_PRINTF28# define KRML_HOST_PRINTF printf29#endif3031#if ( \32(defined __STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \33(!(defined KRML_HOST_EPRINTF)))34# define KRML_HOST_EPRINTF(...) fprintf(stderr, __VA_ARGS__)35#elif !(defined KRML_HOST_EPRINTF) && defined(_MSC_VER)36# define KRML_HOST_EPRINTF(...) fprintf(stderr, __VA_ARGS__)37#endif3839#ifndef KRML_HOST_EXIT40# define KRML_HOST_EXIT exit41#endif4243#ifndef KRML_HOST_MALLOC44# define KRML_HOST_MALLOC malloc45#endif4647#ifndef KRML_HOST_CALLOC48# define KRML_HOST_CALLOC calloc49#endif5051#ifndef KRML_HOST_FREE52# define KRML_HOST_FREE free53#endif5455#ifndef KRML_HOST_IGNORE56# define KRML_HOST_IGNORE(x) (void)(x)57#endif5859/* In FStar.Buffer.fst, the size of arrays is uint32_t, but it's a number of60* *elements*. Do an ugly, run-time check (some of which KaRaMeL can eliminate).61*/62#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 4))63# define _KRML_CHECK_SIZE_PRAGMA \64_Pragma("GCC diagnostic ignored \"-Wtype-limits\"")65#else66# define _KRML_CHECK_SIZE_PRAGMA67#endif6869#define KRML_CHECK_SIZE(size_elt, sz) \70do { \71_KRML_CHECK_SIZE_PRAGMA \72if (((size_t)(sz)) > ((size_t)(SIZE_MAX / (size_elt)))) { \73KRML_HOST_PRINTF( \74"Maximum allocatable size exceeded, aborting before overflow at " \75"%s:%d\n", \76__FILE__, __LINE__); \77KRML_HOST_EXIT(253); \78} \79} while (0)8081/* Macros for prettier unrolling of loops */82#define KRML_LOOP1(i, n, x) { \83x \84i += n; \85}8687#define KRML_LOOP2(i, n, x) \88KRML_LOOP1(i, n, x) \89KRML_LOOP1(i, n, x)9091#define KRML_LOOP3(i, n, x) \92KRML_LOOP2(i, n, x) \93KRML_LOOP1(i, n, x)9495#define KRML_LOOP4(i, n, x) \96KRML_LOOP2(i, n, x) \97KRML_LOOP2(i, n, x)9899#define KRML_LOOP5(i, n, x) \100KRML_LOOP4(i, n, x) \101KRML_LOOP1(i, n, x)102103#define KRML_LOOP6(i, n, x) \104KRML_LOOP4(i, n, x) \105KRML_LOOP2(i, n, x)106107#define KRML_LOOP7(i, n, x) \108KRML_LOOP4(i, n, x) \109KRML_LOOP3(i, n, x)110111#define KRML_LOOP8(i, n, x) \112KRML_LOOP4(i, n, x) \113KRML_LOOP4(i, n, x)114115#define KRML_LOOP9(i, n, x) \116KRML_LOOP8(i, n, x) \117KRML_LOOP1(i, n, x)118119#define KRML_LOOP10(i, n, x) \120KRML_LOOP8(i, n, x) \121KRML_LOOP2(i, n, x)122123#define KRML_LOOP11(i, n, x) \124KRML_LOOP8(i, n, x) \125KRML_LOOP3(i, n, x)126127#define KRML_LOOP12(i, n, x) \128KRML_LOOP8(i, n, x) \129KRML_LOOP4(i, n, x)130131#define KRML_LOOP13(i, n, x) \132KRML_LOOP8(i, n, x) \133KRML_LOOP5(i, n, x)134135#define KRML_LOOP14(i, n, x) \136KRML_LOOP8(i, n, x) \137KRML_LOOP6(i, n, x)138139#define KRML_LOOP15(i, n, x) \140KRML_LOOP8(i, n, x) \141KRML_LOOP7(i, n, x)142143#define KRML_LOOP16(i, n, x) \144KRML_LOOP8(i, n, x) \145KRML_LOOP8(i, n, x)146147#define KRML_UNROLL_FOR(i, z, n, k, x) do { \148uint32_t i = z; \149KRML_LOOP##n(i, k, x) \150} while (0)151152#define KRML_ACTUAL_FOR(i, z, n, k, x) \153do { \154for (uint32_t i = z; i < n; i += k) { \155x \156} \157} while (0)158159#ifndef KRML_UNROLL_MAX160#define KRML_UNROLL_MAX 16161#endif162163/* 1 is the number of loop iterations, i.e. (n - z)/k as evaluated by krml */164#if 0 <= KRML_UNROLL_MAX165#define KRML_MAYBE_FOR0(i, z, n, k, x)166#else167#define KRML_MAYBE_FOR0(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)168#endif169170#if 1 <= KRML_UNROLL_MAX171#define KRML_MAYBE_FOR1(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 1, k, x)172#else173#define KRML_MAYBE_FOR1(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)174#endif175176#if 2 <= KRML_UNROLL_MAX177#define KRML_MAYBE_FOR2(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 2, k, x)178#else179#define KRML_MAYBE_FOR2(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)180#endif181182#if 3 <= KRML_UNROLL_MAX183#define KRML_MAYBE_FOR3(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 3, k, x)184#else185#define KRML_MAYBE_FOR3(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)186#endif187188#if 4 <= KRML_UNROLL_MAX189#define KRML_MAYBE_FOR4(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 4, k, x)190#else191#define KRML_MAYBE_FOR4(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)192#endif193194#if 5 <= KRML_UNROLL_MAX195#define KRML_MAYBE_FOR5(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 5, k, x)196#else197#define KRML_MAYBE_FOR5(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)198#endif199200#if 6 <= KRML_UNROLL_MAX201#define KRML_MAYBE_FOR6(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 6, k, x)202#else203#define KRML_MAYBE_FOR6(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)204#endif205206#if 7 <= KRML_UNROLL_MAX207#define KRML_MAYBE_FOR7(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 7, k, x)208#else209#define KRML_MAYBE_FOR7(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)210#endif211212#if 8 <= KRML_UNROLL_MAX213#define KRML_MAYBE_FOR8(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 8, k, x)214#else215#define KRML_MAYBE_FOR8(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)216#endif217218#if 9 <= KRML_UNROLL_MAX219#define KRML_MAYBE_FOR9(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 9, k, x)220#else221#define KRML_MAYBE_FOR9(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)222#endif223224#if 10 <= KRML_UNROLL_MAX225#define KRML_MAYBE_FOR10(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 10, k, x)226#else227#define KRML_MAYBE_FOR10(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)228#endif229230#if 11 <= KRML_UNROLL_MAX231#define KRML_MAYBE_FOR11(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 11, k, x)232#else233#define KRML_MAYBE_FOR11(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)234#endif235236#if 12 <= KRML_UNROLL_MAX237#define KRML_MAYBE_FOR12(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 12, k, x)238#else239#define KRML_MAYBE_FOR12(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)240#endif241242#if 13 <= KRML_UNROLL_MAX243#define KRML_MAYBE_FOR13(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 13, k, x)244#else245#define KRML_MAYBE_FOR13(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)246#endif247248#if 14 <= KRML_UNROLL_MAX249#define KRML_MAYBE_FOR14(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 14, k, x)250#else251#define KRML_MAYBE_FOR14(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)252#endif253254#if 15 <= KRML_UNROLL_MAX255#define KRML_MAYBE_FOR15(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 15, k, x)256#else257#define KRML_MAYBE_FOR15(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)258#endif259260#if 16 <= KRML_UNROLL_MAX261#define KRML_MAYBE_FOR16(i, z, n, k, x) KRML_UNROLL_FOR(i, z, 16, k, x)262#else263#define KRML_MAYBE_FOR16(i, z, n, k, x) KRML_ACTUAL_FOR(i, z, n, k, x)264#endif265#endif266267268