Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-bridge/blob/main/tests/native/freestanding/f32.c # omega-bridge Omega-first language interoperability: checked conversion backends and native language bindings Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-bridge / tests / native / freestanding / f32.c` 117 lines ยท 5754 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 2 * Verification oracle only: the implementation is freestanding/f32.om. 3 * The full exact-rational WolframLanguageEvaluator queries/results are filed in 4 * evidence/native-freestanding-f32/. They derive the binary32 inputs below, 5 * non-contracting witness {0,`2826960896`}, RNE tie `1065353216` and preserved 6 * half-minimum-normal `4194304`. Non-NaNs compare bit-exact; arithmetic NaNs 7 * compare class only using 0x7fffffff AS COMPARISON CANONICALIZATION ONLY. 8 * No integer-ABI result is rewritten, and no raw 0/0 NaN payload is asserted. */ 9 #include 10 #include 11 #include "omega_native.h" 12 #define COUNT(a) (sizeof(a) / sizeof((a)[0])) 13 static const uint32_t inputs[] = { 14 0, UINT32_C(`2147483648`), 1, UINT32_C(`8388607`), UINT32_C(`8388608`), 15 UINT32_C(`1065353216`), UINT32_C(`3221225472`), UINT32_C(`2139095039`), 16 UINT32_C(`2139095040`), UINT32_C(`4286578688`), UINT32_C(`2147483647`), 17 UINT32_C(`2143363909`), UINT32_C(`1065353217`), UINT32_C(`1065353214`), 18 UINT32_C(`3212836864`), UINT32_C(`4290847557`) 19 }; 20 static uint64_t results[COUNT(inputs) * COUNT(inputs) * 7 + 3]; 21 _Static_assert(sizeof(float) == sizeof(uint32_t) && FLT_RADIX == 2 && FLT_MANT_DIG == 24, 22 "binary32 reference required"); 23 static uint32_t comparison_bits(uint32_t bits) { 24 const uint32_t exponent = UINT32_C(`2139095040`), fraction = UINT32_C(`8388607`); 25 return (bits & exponent) == exponent && (bits & fraction) != 0 ? 26 UINT32_C(`2147483647`) : bits; 27 } 28 static int matches(uint32_t status, uint64_t got, uint32_t want) { 29 return status == OMEGA_OK && got <= UINT32_MAX && 30 comparison_bits((uint32_t)got) == comparison_bits(want); 31 } 32 static int exercise(void) { 33 const uint32_t nan = UINT32_C(`2147483647`), payload = UINT32_C(`2143363909`); 34 const uint32_t inf = UINT32_C(`2139095040`), one = UINT32_C(`1065353216`); 35 if (!matches(0, payload, nan) || !matches(0, UINT32_C(`4290847557`), nan) || 36 matches(0, inf, nan) || matches(0, one + 1, one) || 37 matches(1, nan, nan) || matches(0, (UINT64_C(1) << 32) | nan, nan)) return 1; 38 unsigned pos = 0; 39 for (unsigned i = 0; i < COUNT(inputs); ++i) 40 for (unsigned j = 0; j < COUNT(inputs); ++j) 41 for (unsigned mode = 0; mode < 7; ++mode) { 42 union { uint32_t u; float f; } av = {.u = inputs[i]}, bv = {.u = inputs[j]}, want; 43 volatile float a = av.f, b = bv.f, c = -1.0f; 44 uint64_t expected = 0; 45 if (mode == 0) expected = inputs[i]; 46 else if (mode == 1) want.f = a + b; 47 else if (mode == 2) want.f = a / b; 48 else if (mode == 3) want.f = -a; 49 else if (mode == 4) { volatile float sum = a + b; want.f = sum / 2.0f; } 50 else if (mode == 5) { volatile float product = a * b; want.f = product + c; } 51 else expected = (a == b ? 1u : 0u) | (a != b ? 2u : 0u) | 52 (a < b ? 4u : 0u) | (a <= b ? 8u : 0u) | 53 (a > b ? 16u : 0u) | (a >= b ? 32u : 0u); 54 uint64_t got = 99; 55 uint32_t status = omega2_f32bits(&got, mode, inputs[i], inputs[j], UINT32_C(`3212836864`)); 56 if (mode == 0 || mode == 6) { 57 if (status != 0 || got != expected) return mode == 0 ? 3 : 2; 58 } else if (!matches(status, got, want.u)) return 2; 59 results[pos++] = got; /* Preserve raw integer-ABI bits, including arithmetic NaNs. */ 60 } 61 uint64_t got = 99; 62 if (omega2_f32bits(&got, 5, UINT32_C(`1065353217`), UINT32_C(`1065353214`), 63 UINT32_C(`3212836864`)) || got != 0) return 4; 64 results[pos++] = got; 65 if (omega2_f32bits(&got, 1, one, UINT32_C(`864026624`), 0) || got != one) return 4; 66 results[pos++] = got; /* RNE tie: 1 + 2^-24 -> 1, not the next binary32. */ 67 if (omega2_f32bits(&got, 2, UINT32_C(`8388608`), UINT32_C(`1073741824`), 0) || 68 got != UINT32_C(`4194304`)) return 4; 69 results[pos++] = got; /* Subnormals retained: 2^-126 / 2 -> 2^-127. */ 70 if (pos != COUNT(results)) return 5; 71 #ifdef OMEGA_F32_WRONG 72 if (results[0] != 1) return 90; 73 #endif 74 return 0; 75 } 76 #ifdef OMEGA_F32_BARE 77 #include 78 #define STRING_INNER(x) #x 79 #define STRING(x) STRING_INNER(x) 80 static long linux_sys3(long number, long a, long b, long c) { 81 #if defined(__aarch64__) 82 register long x0 __asm__("x0") = a; 83 register long x1 __asm__("x1") = b; 84 register long x2 __asm__("x2") = c; 85 register long x8 __asm__("x8") = number; 86 __asm__ volatile("svc #0" : "+r"(x0) : "r"(x1), "r"(x2), "r"(x8) : "memory", "cc"); 87 return x0; 88 #elif defined(__x86_64__) 89 long result; 90 __asm__ volatile("syscall" : "=a"(result) : "a"(number), "D"(a), "S"(b), "d"(c) 91 : "rcx", "r11", "memory", "cc"); 92 return result; 93 #else 94 #error "FP32 test startup supports only AArch64 and x86-64 Linux" 95 #endif 96 } 97 int omega_f32_main(void); 98 int omega_f32_main(void) { 99 int status = exercise(); 100 if (status) return status; 101 return linux_sys3(__NR_write, 1, (long)(uintptr_t)results, sizeof(results)) == (long)sizeof(results) ? 0 : 91; 102 } 103 #if defined(__aarch64__) 104 __asm__(".text\n.global _start\n.type _start,%function\n_start:\n" 105 "bl omega_f32_main\nmov x8,#" STRING(__NR_exit) "\nsvc #0\nbrk #0\n"); 106 #else 107 __asm__(".text\n.global _start\n.type _start,@function\n_start:\n" 108 "call omega_f32_main\nmov %rax,%rdi\nmov $" STRING(__NR_exit) ",%eax\nsyscall\nud2\n"); 109 #endif 110 #else 111 #include 112 int main(void) { 113 int status = exercise(); 114 if (status) return status; 115 return fwrite(results, 1, sizeof(results), stdout) == sizeof(results) ? 0 : 91; 116 } 117 #endif