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/CUDApp/blob/main/tests/ampere/test_f16_behavior.c # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. 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 `CUDApp / tests / ampere / test_f16_behavior.c` 186 lines · 7188 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* Dedicated boundary, native sweep, and negative mutant controls for scalar f16. */ 3 4 #include 5 #include 6 #include 7 #include 8 9 extern uint32_t omega_native_f16(uint64_t *result, uint64_t p0); 10 11 static uint16_t native_reference(uint32_t u) { 12 float f; 13 memcpy(&f, &u, 4); 14 _Float16 h = (_Float16)f; 15 uint16_t r; 16 memcpy(&r, &h, 2); 17 return r; 18 } 19 20 /* Mutant 1: Truncation towards zero (omits +4095/lsb rounding) */ 21 static uint16_t mutant_truncate(uint32_t u) { 22 uint32_t sign = (u >> 16) & 0x8000u; 23 uint32_t abs_u = u & 0x7fffffffu; 24 if (abs_u >= 0x7f800000u) { 25 if (abs_u > 0x7f800000u) return (uint16_t)(sign | 0x7e00u | ((abs_u >> 13) & 0x03ffu)); 26 return (uint16_t)(sign | 0x7c00u); 27 } 28 if (abs_u >= 0x47800000u) return (uint16_t)(sign | 0x7c00u); 29 if (abs_u >= 0x38800000u) { 30 uint32_t val = abs_u - 0x38000000u; 31 return (uint16_t)(sign | (val >> 13)); 32 } 33 return (uint16_t)sign; 34 } 35 36 /* Mutant 2: Round ties to away/odd instead of even (lsb bit ignored) */ 37 static uint16_t mutant_no_tie_even(uint32_t u) { 38 uint32_t sign = (u >> 16) & 0x8000u; 39 uint32_t abs_u = u & 0x7fffffffu; 40 if (abs_u >= 0x7f800000u) return (uint16_t)(sign | 0x7e00u); 41 if (abs_u >= 0x477ff000u) return (uint16_t)(sign | 0x7c00u); 42 if (abs_u >= 0x38800000u) { 43 uint32_t val = (abs_u + 0x1000u) - 0x38000000u; 44 return (uint16_t)(sign | (val >> 13)); 45 } 46 return (uint16_t)sign; 47 } 48 49 /* Mutant 3: Overflow threshold off by 1 (0x477ff001 instead of 0x477ff000) */ 50 static uint16_t mutant_bad_overflow_threshold(uint32_t u) { 51 uint32_t sign = (u >> 16) & 0x8000u; 52 uint32_t abs_u = u & 0x7fffffffu; 53 if (abs_u >= 0x7f800000u) return (uint16_t)(sign | 0x7e00u); 54 if (abs_u > 0x477ff000u) return (uint16_t)(sign | 0x7c00u); 55 if (abs_u >= 0x38800000u) { 56 uint32_t lsb = (abs_u >> 13) & 1u; 57 uint32_t rounded = abs_u + 0x0fffu + lsb; 58 uint32_t val = rounded - 0x38000000u; 59 return (uint16_t)(sign | (val >> 13)); 60 } 61 return (uint16_t)sign; 62 } 63 64 /* Mutant 4: Subnormal shift/bias off by 1 */ 65 static uint16_t mutant_bad_subnormal(uint32_t u) { 66 uint32_t sign = (u >> 16) & 0x8000u; 67 uint32_t abs_u = u & 0x7fffffffu; 68 if (abs_u >= 0x38800000u) { 69 uint64_t res = 0; 70 omega_native_f16(&res, u); 71 return (uint16_t)res; 72 } 73 return (uint16_t)(sign | 0x0002u); /* corrupted subnormal */ 74 } 75 76 /* Mutant 5: NaN quiet bit omitted */ 77 static uint16_t mutant_nan_no_quiet(uint32_t u) { 78 uint32_t sign = (u >> 16) & 0x8000u; 79 uint32_t abs_u = u & 0x7fffffffu; 80 if (abs_u > 0x7f800000u) { 81 return (uint16_t)(sign | 0x7c00u | ((abs_u >> 13) & 0x01ffu)); /* bit 9 (0x0200) missing */ 82 } 83 uint64_t res = 0; 84 omega_native_f16(&res, u); 85 return (uint16_t)res; 86 } 87 88 /* Mutant 6: Sign bit ignored / zeroed */ 89 static uint16_t mutant_ignore_sign(uint32_t u) { 90 uint64_t res = 0; 91 omega_native_f16(&res, u & 0x7fffffffu); 92 return (uint16_t)res; 93 } 94 95 int main(void) { 96 /* 1. Boundary table verification */ 97 struct { 98 uint32_t u; 99 uint16_t expected; 100 const char *desc; 101 } boundaries[] = { 102 {0x00000000u, 0x0000u, "+0.0"}, 103 {0x80000000u, 0x8000u, "-0.0"}, 104 {0x33000000u, 0x0000u, "underflow cutoff"}, 105 {0x33000001u, 0x0001u, "min subnormal"}, 106 {0x387fdfffu, 0x03ffu, "max subnormal (below round-to-normal)"}, 107 {0x387fe000u, 0x0400u, "subnormal round up to min normal"}, 108 {0x387fffffu, 0x0400u, "subnormal top round to min normal"}, 109 {0x38800000u, 0x0400u, "min normal"}, 110 {0x3f800000u, 0x3c00u, "1.0f"}, 111 {0x3f801000u, 0x3c00u, "RNE tie down to even"}, 112 {0x3f803000u, 0x3c02u, "RNE tie up to even"}, 113 {0x477fe000u, 0x7bffu, "max normal"}, 114 {0x477fefffu, 0x7bffu, "max normal below overflow"}, 115 {0x477ff000u, 0x7c00u, "+overflow to +inf"}, 116 {0xc77ff000u, 0xfc00u, "-overflow to -inf"}, 117 {0x7f800000u, 0x7c00u, "+inf"}, 118 {0xff800000u, 0xfc00u, "-inf"}, 119 {0x7fc00000u, 0x7e00u, "canonical qNaN"}, 120 {0x7f800001u, 0x7e00u, "sNaN made quiet"}, 121 {0xffc00000u, 0xfe00u, "-qNaN"}, 122 {0x7fffffffu, 0x7fffu, "NaN payload preservation"}, 123 }; 124 125 for (size_t i = 0; i < sizeof(boundaries)/sizeof(boundaries[0]); i++) { 126 uint64_t res = 0; 127 uint32_t rc = omega_native_f16(&res, boundaries[i].u); 128 if (rc != 0) { 129 fprintf(stderr, "FAIL: %s: rc %u != 0\n", boundaries[i].desc, rc); 130 return 1; 131 } 132 if ((uint16_t)res != boundaries[i].expected) { 133 fprintf(stderr, "FAIL: %s (0x%08x): got 0x%04x, expected 0x%04x\n", 134 boundaries[i].desc, boundaries[i].u, (uint16_t)res, boundaries[i].expected); 135 return 1; 136 } 137 uint16_t ref = native_reference(boundaries[i].u); 138 if ((uint16_t)res != ref) { 139 fprintf(stderr, "FAIL: %s (0x%08x): got 0x%04x, native ref 0x%04x\n", 140 boundaries[i].desc, boundaries[i].u, (uint16_t)res, ref); 141 return 1; 142 } 143 } 144 145 /* 2. Negative mutant controls */ 146 int mutant1_caught = 0; 147 int mutant2_caught = 0; 148 int mutant3_caught = 0; 149 int mutant4_caught = 0; 150 int mutant5_caught = 0; 151 int mutant6_caught = 0; 152 153 for (size_t i = 0; i < sizeof(boundaries)/sizeof(boundaries[0]); i++) { 154 uint32_t u = boundaries[i].u; 155 uint16_t exp = boundaries[i].expected; 156 if (mutant_truncate(u) != exp) mutant1_caught++; 157 if (mutant_no_tie_even(u) != exp) mutant2_caught++; 158 if (mutant_bad_overflow_threshold(u) != exp) mutant3_caught++; 159 if (mutant_bad_subnormal(u) != exp) mutant4_caught++; 160 if (mutant_nan_no_quiet(u) != exp) mutant5_caught++; 161 if (mutant_ignore_sign(u) != exp) mutant6_caught++; 162 } 163 164 if (!mutant1_caught) { fprintf(stderr, "FAIL: mutant1 (truncate) not caught\n"); return 1; } 165 if (!mutant2_caught) { fprintf(stderr, "FAIL: mutant2 (no tie even) not caught\n"); return 1; } 166 if (!mutant3_caught) { fprintf(stderr, "FAIL: mutant3 (bad overflow threshold) not caught\n"); return 1; } 167 if (!mutant4_caught) { fprintf(stderr, "FAIL: mutant4 (bad subnormal) not caught\n"); return 1; } 168 if (!mutant5_caught) { fprintf(stderr, "FAIL: mutant5 (nan no quiet) not caught\n"); return 1; } 169 if (!mutant6_caught) { fprintf(stderr, "FAIL: mutant6 (ignore sign) not caught\n"); return 1; } 170 171 /* 3. Comprehensive stepped sweep across 2^32 inputs against native _Float16 */ 172 uint64_t diffs = 0; 173 for (uint64_t i = 0; i < 0x100000000ull; i += 97) { 174 uint32_t u = (uint32_t)i; 175 uint64_t res = 0; 176 omega_native_f16(&res, u); 177 uint16_t ref = native_reference(u); 178 if ((uint16_t)res != ref) { 179 fprintf(stderr, "Diff at 0x%08x: res 0x%04x != ref 0x%04x\n", u, (uint16_t)res, ref); 180 if (++diffs > 10) return 1; 181 } 182 } 183 184 puts("F16 BEHAVIOR CONTROLS PASS (all boundaries, native sweep, and negative mutants verified)"); 185 return 0; 186 }