omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / tests / native / v2 / fsom2.c
| 1 | /* A hosted verification caller of the freestanding ABI v2 binding for sha256-om2.om, fsout.om |
| 2 | * and freestanding/f64.om. The f64 unit keeps integer parameters/results and uses hardware FP |
| 3 | * only internally. `fsom2 trap` calls a trapping C API wrapper and must not return. */ |
| 4 | #include "omega_api_decl.h" |
| 5 | #include <pthread.h> |
| 6 | #include <stdint.h> |
| 7 | #include <stdio.h> |
| 8 | #include <string.h> |
| 9 | |
| 10 | /* Four threads hash "abc" concurrently, each on its own stack (no shared runtime state). */ |
| 11 | static void *spin(void *arg) |
| 12 | { |
| 13 | static const uint8_t want[4] = {0xba, 0x78, 0x16, 0xbf}; |
| 14 | uint8_t o[32]; |
| 15 | (void)arg; |
| 16 | for (int i = 0; i < 2000; i++) { |
| 17 | memset(o, 0, sizeof o); |
| 18 | sha256_hash((const uint8_t *)"abc", 3, o); |
| 19 | if (memcmp(o, want, 4) != 0) |
| 20 | return (void *)1; |
| 21 | } |
| 22 | return 0; |
| 23 | } |
| 24 | |
| 25 | static void hex(const char *name, const uint8_t *d) |
| 26 | { |
| 27 | printf("%s ", name); |
| 28 | for (int i = 0; i < 32; i++) |
| 29 | printf("%02x", d[i]); |
| 30 | printf("\n"); |
| 31 | } |
| 32 | |
| 33 | static void once(const char *name, const char *m) |
| 34 | { |
| 35 | uint8_t o[32]; |
| 36 | memset(o, 0xa5, sizeof o); |
| 37 | sha256_hash((const uint8_t *)m, strlen(m), o); |
| 38 | hex(name, o); |
| 39 | } |
| 40 | |
| 41 | /* WolframLanguageEvaluator: |
| 42 | * Map[Function[b,{b,Quotient[b,2^63],Mod[Quotient[b,2^52],2^11],Mod[b,2^52]}], |
| 43 | * {16^^8000000000000000,1,16^^7ff8000000000001}] |
| 44 | * -> {{9223372036854775808,1,0,0},{1,0,0,1},{9221120237041090561,0,2047,2251799813685249}} |
| 45 | * These are negative zero, the least positive subnormal and a quiet NaN with payload. |
| 46 | * Volatile reference inputs force real C hardware operations, not folded NaN constants. */ |
| 47 | /* The x86 backend deliberately implements Arm's positive default NaN, not SSE's |
| 48 | * negative indefinite. Normalize ONLY the C reference for signed-zero / signed-zero, |
| 49 | * and only its exact negative-indefinite encoding. Never normalize the Omega result |
| 50 | * or an operand NaN. Source: omega-mv c6c30fe3 seed/omc/omc.om xfnans/xfdefnan. */ |
| 51 | static int floatmatch(uint32_t status, uint64_t got, uint64_t want, |
| 52 | unsigned mode, uint64_t a, uint64_t b) |
| 53 | { |
| 54 | const uint64_t nz = UINT64_C(9223372036854775808); |
| 55 | if (mode == 1 && (a == 0 || a == nz) && (b == 0 || b == nz) && |
| 56 | want == UINT64_C(0xfff8000000000000)) |
| 57 | want = UINT64_C(0x7ff8000000000000); |
| 58 | return status == 0 && got == want; |
| 59 | } |
| 60 | |
| 61 | static int floatbits(void) |
| 62 | { |
| 63 | union { uint64_t u; double d; } ordinary[] = {{.d = 1.0}, {.d = -2.0}}; |
| 64 | const uint64_t input[] = { |
| 65 | UINT64_C(9223372036854775808), UINT64_C(1), UINT64_C(9221120237041090561), |
| 66 | ordinary[0].u, ordinary[1].u |
| 67 | }; |
| 68 | _Static_assert(sizeof(double) == sizeof(uint64_t), "binary64 reference required"); |
| 69 | const uint64_t indefinite = UINT64_C(0xfff8000000000000); |
| 70 | const uint64_t canonical = UINT64_C(0x7ff8000000000000); |
| 71 | const uint64_t nz = input[0]; |
| 72 | if (!floatmatch(0, canonical, indefinite, 1, nz, nz) || |
| 73 | floatmatch(0, indefinite, indefinite, 1, nz, nz) || |
| 74 | floatmatch(0, input[2], indefinite, 1, nz, nz) || |
| 75 | floatmatch(1, canonical, indefinite, 1, nz, nz)) |
| 76 | return 0; |
| 77 | /* The SAME negative bit pattern is preserved when it is an operand NaN, |
| 78 | * in either position. A global NaN-sign normalization would fail here. */ |
| 79 | for (unsigned side = 0; side < 2; side++) { |
| 80 | uint64_t r = 99; |
| 81 | uint32_t status = omega2_fbits(&r, 0, side ? input[1] : indefinite, |
| 82 | side ? indefinite : input[1]); |
| 83 | if (!floatmatch(status, r, indefinite, 0, 0, 0)) |
| 84 | return 0; |
| 85 | } |
| 86 | for (unsigned i = 0; i < sizeof input / sizeof input[0]; i++) |
| 87 | for (unsigned j = 0; j < sizeof input / sizeof input[0]; j++) |
| 88 | for (unsigned mode = 0; mode < 4; mode++) { |
| 89 | union { uint64_t u; double d; } av = {.u = input[i]}, bv = {.u = input[j]}, want; |
| 90 | volatile double a = av.d, b = bv.d; |
| 91 | if (mode == 0) |
| 92 | want.d = a + b; |
| 93 | else if (mode == 1) |
| 94 | want.d = a / b; |
| 95 | else if (mode == 2) |
| 96 | want.d = -a; |
| 97 | else |
| 98 | want.d = (a + b) / 2.0; |
| 99 | uint64_t r = 99; |
| 100 | uint32_t status = omega2_fbits(&r, mode, input[i], input[j]); |
| 101 | if (!floatmatch(status, r, want.u, mode, input[i], input[j])) { |
| 102 | fprintf(stderr, "floatbits mismatch mode=%u i=%u j=%u status=%u got=%016llx want=%016llx\n", |
| 103 | mode, i, j, status, (unsigned long long)r, (unsigned long long)want.u); |
| 104 | return 0; |
| 105 | } |
| 106 | } |
| 107 | return 1; |
| 108 | } |
| 109 | |
| 110 | static uint8_t million[1000000]; |
| 111 | |
| 112 | int main(int argc, char **argv) |
| 113 | { |
| 114 | if (argc > 1 && strcmp(argv[1], "trap") == 0) { |
| 115 | uint8_t o[16]; |
| 116 | fsout(0, o); |
| 117 | printf("RETURNED\n"); |
| 118 | return 0; |
| 119 | } |
| 120 | once("empty", ""); |
| 121 | once("abc", "abc"); |
| 122 | once("448", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"); |
| 123 | sha256_ctx c; |
| 124 | uint8_t o[32]; |
| 125 | memset(million, 'a', sizeof million); |
| 126 | memset(o, 0xa5, sizeof o); |
| 127 | sha256_init(&c); |
| 128 | sha256_update(&c, million, 123); |
| 129 | sha256_update(&c, million + 123, sizeof million - 123); |
| 130 | sha256_final(&c, o); |
| 131 | hex("million", o); |
| 132 | /* A refused argument (a null view of 5 bytes): status 32, the out buffer untouched. */ |
| 133 | uint64_t r = 99; |
| 134 | memset(o, 0xa5, sizeof o); |
| 135 | uint32_t s = omega2_sha256_hash(&r, NULL, 5, o); |
| 136 | int same = 1; |
| 137 | for (int i = 0; i < 32; i++) |
| 138 | same &= o[i] == 0xa5; |
| 139 | printf("badarg %u %d\n", s, same); |
| 140 | /* Success: the whole out buffer comes back (written byte 7, the rest zero-filled). */ |
| 141 | uint8_t b[16]; |
| 142 | memset(b, 0xaa, sizeof b); |
| 143 | s = omega2_fsout(&r, 1, b); |
| 144 | int zero = 1; |
| 145 | for (int i = 1; i < 16; i++) |
| 146 | zero &= b[i] == 0; |
| 147 | printf("outok %u %llu %u %d\n", s, (unsigned long long)r, b[0], zero); |
| 148 | /* A trap after the out write: nonzero status, the caller's buffer as it was. */ |
| 149 | memset(b, 0xaa, sizeof b); |
| 150 | s = omega2_fsout(&r, 0, b); |
| 151 | same = 1; |
| 152 | for (int i = 0; i < 16; i++) |
| 153 | same &= b[i] == 0xaa; |
| 154 | printf("outtrap %u %d\n", s, same); |
| 155 | pthread_t t[4]; |
| 156 | int bad = 0; |
| 157 | for (int i = 0; i < 4; i++) |
| 158 | bad |= pthread_create(&t[i], 0, spin, 0) != 0; |
| 159 | for (int i = 0; i < 4; i++) { |
| 160 | void *v = (void *)1; |
| 161 | bad |= pthread_join(t[i], &v) != 0 || v != 0; |
| 162 | } |
| 163 | printf("threads %d\n", !bad); |
| 164 | int fp = floatbits(); |
| 165 | printf("floatbits %d\n", fp); |
| 166 | if (!fp) |
| 167 | return 1; |
| 168 | return 0; |
| 169 | } |