interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / tests / bw_mufu_ftz_test.c

96 lines · 5714 bytes Raw Blame History
1/* bw_mufu_ftz_test.c: the MUFU flush rule (tests/bw_mufu_ftz.h, omega #462) and every warp simulator's MUFU cases.
2 *
3 * Built once per model (Makefile test-mufu-ftz):
4 * no WS_SIM_HDR the helper itself: subnormal -> sign-preserving zero; zeros, normals, inf, NaN pass
5 * -DWS_SIM_HDR='"bw_warp_sim.h"' (and _k, _pa, _tc) one MUFU instruction through that simulator's ws_lane_exec
6 * The simulator cases expect the chip rule: a subnormal input or result is a zero of the same sign. Before #462 the
7 * first three simulators returned the IEEE subnormal (RCP of 1.5 * 2^127, EX2 of -130) and the IEEE finite value for a
8 * subnormal input (RCP / RSQ of 2^-127, 2^-140), and fail here. Unreachable: an RSQ result can never be subnormal
9 * (1 / sqrt(x) >= 2^-64 for every finite f32 x), so RSQ only exercises the input flush. */
10#ifdef WS_SIM_HDR
11#include WS_SIM_HDR /* the simulator under test; it must get its MUFU rule from bw_mufu_ftz.h */
12#else
13#include "bw_mufu_ftz.h"
14#endif
15#include <math.h>
16#include <stdint.h>
17#include <stdio.h>
18#include <string.h>
19
20static int g_checks, g_failed;
21__attribute__((unused)) static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; }
22static float u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; }
23/* expect bits exactly, or any NaN when want is a NaN */
24static void expect(const char *what, uint32_t in, uint32_t got, uint32_t want) {
25 int nan = (want & 0x7fffffffu) > 0x7f800000u, ok = nan ? (got & 0x7fffffffu) > 0x7f800000u : got == want;
26 g_checks++;
27 if (!ok) { g_failed++; printf("FAIL: %s(0x%08x) = 0x%08x (%g), want 0x%08x (%g)\n", what, in, got, u2f(got), want, u2f(want)); }
28}
29
30#ifndef WS_SIM_HDR
31#define MODEL "helper"
32static void run_model(void) {
33 /* every subnormal of both signs, sampled across the whole mantissa range, plus its extremes */
34 for (uint32_t m = 1; m < 0x800000u; m += (m < 64 || m > 0x7fffc0u) ? 1u : 4093u) {
35 expect("ftz", m, f2u(ws_mufu_ftz(u2f(m))), 0x00000000u);
36 expect("ftz", 0x80000000u | m, f2u(ws_mufu_ftz(u2f(0x80000000u | m))), 0x80000000u);
37 }
38 static const uint32_t pass[] = { 0x00000000u, 0x80000000u, 0x00800000u, 0x80800000u, 0x3f800000u, 0xbf800000u,
39 0x7f7fffffu, 0xff7fffffu, 0x7f800000u, 0xff800000u, 0x7fc00000u, 0xffc00000u, 0x7f800001u, 0x00ffffffu };
40 for (size_t i = 0; i < sizeof pass / sizeof pass[0]; i++) expect("ftz", pass[i], f2u(ws_mufu_ftz(u2f(pass[i]))), pass[i]);
41 /* the three functions, same table the simulators use below */
42 expect("ex2", f2u(-130.0f), f2u(ws_mufu_ex2(-130.0f)), 0x00000000u);
43 expect("rcp", 0xff400000u, f2u(ws_mufu_rcp(u2f(0xff400000u))), 0x80000000u);
44 expect("rsq", 0x00000200u, f2u(ws_mufu_rsq(u2f(0x00000200u))), 0x7f800000u);
45}
46#else
47#define MODEL WS_SIM_HDR
48static BlackwellIRProgram g_prog;
49static uint32_t sim_mufu(BlackwellIROpcode op, uint32_t x) {
50 static WsSim s; static WsLane l;
51 memset(&s, 0, sizeof s); memset(&l, 0, sizeof l);
52 BlackwellIRInsn in; memset(&in, 0, sizeof in);
53 in.op = op; in.dst_vreg = 0; in.src1_vreg = 1; in.src2_vreg = -1; in.src3_vreg = -1; in.ureg = -1;
54 s.p = &g_prog; l.r[1] = x; l.r[0] = 0xdeadbeefu;
55 ws_lane_exec(&s, &l, &in, 0, 0, 0, 0);
56 return l.r[0];
57}
58static void run_model(void) {
59 (void)ws_run; /* the warp scheduler is not under test here */
60 g_prog.regalloc.vreg_to_phys[0] = 0; g_prog.regalloc.vreg_to_phys[1] = 1;
61 static const struct { BlackwellIROpcode op; const char *name; uint32_t in, want; } c[] = {
62 /* subnormal result -> sign-preserving zero (IEEE: 0x00080000, 0x00155555 / 0x80155555) */
63 { BW_IR_MUFU_EX2, "EX2", 0xc3020000u /* -130 */, 0x00000000u },
64 { BW_IR_MUFU_EX2, "EX2", 0xc2fe0000u /* -127 */, 0x00000000u },
65 { BW_IR_MUFU_RCP, "RCP", 0x7f400000u /* 1.5 * 2^127 */, 0x00000000u },
66 { BW_IR_MUFU_RCP, "RCP", 0xff400000u /* -1.5 * 2^127 */, 0x80000000u },
67 /* subnormal input -> treated as a zero of the same sign (IEEE: 2^127, -2^127, 2^70) */
68 { BW_IR_MUFU_RCP, "RCP", 0x00400000u /* 2^-127 */, 0x7f800000u },
69 { BW_IR_MUFU_RCP, "RCP", 0x80400000u /* -2^-127 */, 0xff800000u },
70 { BW_IR_MUFU_RSQ, "RSQ", 0x00000200u /* 2^-140 */, 0x7f800000u },
71 { BW_IR_MUFU_EX2, "EX2", 0x80000200u /* -2^-140 */, 0x3f800000u },
72 /* controls: unchanged by the flush */
73 { BW_IR_MUFU_EX2, "EX2", 0xc2fc0000u /* -126 -> smallest normal */, 0x00800000u },
74 { BW_IR_MUFU_EX2, "EX2", 0x40400000u /* 3 */, 0x41000000u },
75 { BW_IR_MUFU_EX2, "EX2", 0xff800000u /* -inf */, 0x00000000u },
76 { BW_IR_MUFU_EX2, "EX2", 0x7f800000u /* +inf */, 0x7f800000u },
77 { BW_IR_MUFU_EX2, "EX2", 0x7fc00000u /* NaN */, 0x7fc00000u },
78 { BW_IR_MUFU_RCP, "RCP", 0x7e800000u /* 2^126 -> smallest normal */, 0x00800000u },
79 { BW_IR_MUFU_RCP, "RCP", 0x40000000u /* 2 */, 0x3f000000u },
80 { BW_IR_MUFU_RCP, "RCP", 0x80000000u /* -0 */, 0xff800000u },
81 { BW_IR_MUFU_RCP, "RCP", 0xff800000u /* -inf */, 0x80000000u },
82 { BW_IR_MUFU_RCP, "RCP", 0x7fc00000u /* NaN */, 0x7fc00000u },
83 { BW_IR_MUFU_RSQ, "RSQ", 0x40800000u /* 4 */, 0x3f000000u },
84 { BW_IR_MUFU_RSQ, "RSQ", 0x00800000u /* 2^-126 */, 0x5f000000u },
85 { BW_IR_MUFU_RSQ, "RSQ", 0x7f800000u /* +inf */, 0x00000000u },
86 { BW_IR_MUFU_RSQ, "RSQ", 0x80000000u /* -0 */, 0xff800000u },
87 };
88 for (size_t i = 0; i < sizeof c / sizeof c[0]; i++) expect(c[i].name, c[i].in, sim_mufu(c[i].op, c[i].in), c[i].want);
89}
90#endif
91
92int main(void) {
93 run_model();
94 printf("%s: mufu-ftz %s: %d checks, %d failed\n", g_failed ? "FAIL" : "PASS", MODEL, g_checks, g_failed);
95 return g_failed ? 1 : 0;
96}