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
CUDApp / tests / runtime / cm_math_unit.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #include "cudapp_native.h" |
| 3 | #include <inttypes.h> |
| 4 | #include <math.h> |
| 5 | #include <stdint.h> |
| 6 | #include <stdio.h> |
| 7 | #include <string.h> |
| 8 | static uint64_t bits(double x) { uint64_t b; memcpy(&b, &x, 8); return b; } |
| 9 | static double value(uint64_t b) { double x; memcpy(&x, &b, 8); return x; } |
| 10 | static uint64_t ordered(uint64_t b) { return b >> 63 ? ~b : b | UINT64_C(0x8000000000000000); } |
| 11 | static uint64_t maxima[8], arguments[8], got_bits[8], ref_bits[8]; |
| 12 | static int check(unsigned mode, uint64_t arg) { |
| 13 | double x = value(arg), ref; |
| 14 | switch (mode) { |
| 15 | case 0: ref = log(x); break; |
| 16 | case 1: ref = log2(x); break; |
| 17 | case 2: ref = ceil(x); break; |
| 18 | case 3: ref = exp(x); break; |
| 19 | case 4: ref = exp2(x); break; |
| 20 | case 5: ref = sqrt(x); break; |
| 21 | case 6: ref = erfc(x); break; |
| 22 | default: ref = fabs(x); break; |
| 23 | } |
| 24 | uint64_t out; |
| 25 | if (omega_cm_math(&out, mode, arg)) return 1; |
| 26 | if (mode == 7 && out != (arg & UINT64_C(0x7fffffffffffffff))) return 1; |
| 27 | double got = value(out); |
| 28 | if (isnan(ref)) return !isnan(got); |
| 29 | if (isnan(got)) return 1; |
| 30 | uint64_t a = ordered(out), b = ordered(bits(ref)); |
| 31 | uint64_t delta = a >= b ? a - b : b - a; |
| 32 | if (delta > maxima[mode]) { |
| 33 | maxima[mode] = delta; arguments[mode] = arg; |
| 34 | got_bits[mode] = out; ref_bits[mode] = bits(ref); |
| 35 | } |
| 36 | uint64_t limit = mode == 3 ? 1 : mode == 6 ? 2 : 0; |
| 37 | if (delta > limit) return 1; |
| 38 | return 0; |
| 39 | } |
| 40 | int main(void) { |
| 41 | int bad = 0; |
| 42 | /* Wolfram 150-digit rounding references, not the glibc oracle. */ |
| 43 | uint64_t out; |
| 44 | if (omega_cm_math(&out, 3, UINT64_C(0x3e50000000000000)) || out != UINT64_C(0x3ff0000004000001)) ++bad; |
| 45 | if (omega_cm_math(&out, 6, UINT64_C(0x402570dc08794a6a)) || out != UINT64_C(0x354eb3d792b02a8a)) ++bad; |
| 46 | uint64_t special[] = {0, UINT64_C(0x8000000000000000), 1, 2, UINT64_C(0x000fffffffffffff), UINT64_C(0x0010000000000000), UINT64_C(0x3fefffffffffffff), UINT64_C(0x3ff0000000000000), UINT64_C(0x3ff0000000000001), UINT64_C(0x7fefffffffffffff), UINT64_C(0x7ff0000000000000), UINT64_C(0xfff0000000000000), UINT64_C(0x7ff8000000000000), UINT64_C(0x7ff0000000000001)}; |
| 47 | for (unsigned m = 0; m < 8; ++m) { |
| 48 | for (unsigned i = 0; i < sizeof special / sizeof *special; ++i) { |
| 49 | bad += check(m, special[i]); bad += check(m, special[i] | UINT64_C(0x8000000000000000)); |
| 50 | } |
| 51 | /* Every exponent boundary and its adjacent representable values. */ |
| 52 | for (uint64_t e = 1; e < 2047; ++e) { |
| 53 | uint64_t b = e << 52; |
| 54 | for (unsigned d = 0; d < 3; ++d) { |
| 55 | bad += check(m, b + d - 1); bad += check(m, (b + d - 1) | UINT64_C(0x8000000000000000)); |
| 56 | } |
| 57 | } |
| 58 | uint64_t rng = UINT64_C(0xda079346a277); |
| 59 | for (unsigned i = 0; i < 65536; ++i) { |
| 60 | rng = rng * UINT64_C(6364136223846793005) + 1; |
| 61 | bad += check(m, rng); |
| 62 | } |
| 63 | if (m == 3 || m == 4 || m == 6) { |
| 64 | int lo = m == 3 ? -6000 : m == 4 ? -8608 : -224; |
| 65 | int hi = m == 3 ? 5680 : m == 4 ? 8192 : 224; |
| 66 | for (int i = lo; i <= hi; ++i) { |
| 67 | double x = (double)i * 0.125; |
| 68 | bad += check(m, bits(x)); |
| 69 | bad += check(m, bits(nextafter(x, INFINITY))); |
| 70 | bad += check(m, bits(nextafter(x, -INFINITY))); |
| 71 | } |
| 72 | } |
| 73 | printf("CM MATH mode=%u max_ulp=%" PRIu64 " input_bits=%016" PRIx64 " got=%016" PRIx64 " oracle=%016" PRIx64 "\n", m, maxima[m], arguments[m], got_bits[m], ref_bits[m]); |
| 74 | } |
| 75 | if (bad) { fprintf(stderr, "CM MATH differential FAIL errors=%d\n", bad); return 1; } |
| 76 | puts("CM MATH differential sweep complete"); return 0; |
| 77 | } |