| 1 | /* |
| 2 | * Test DIEBR and DIDBR instructions. |
| 3 | * |
| 4 | * Most inputs were discovered by fuzzing and exercise various corner cases in |
| 5 | * the helpers. |
| 6 | * |
| 7 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 8 | */ |
| 9 | #include <signal.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <asm/ucontext.h> |
| 13 | |
| 14 | static void sigfpe_handler(int sig, siginfo_t *info, void *puc) |
| 15 | { |
| 16 | struct ucontext *uc = puc; |
| 17 | unsigned short *xr_insn; |
| 18 | int r; |
| 19 | |
| 20 | xr_insn = (unsigned short *)(uc->uc_mcontext.regs.psw.addr - 6); |
| 21 | r = *xr_insn & 0xf; |
| 22 | uc->uc_mcontext.regs.gprs[r] = sig; |
| 23 | } |
| 24 | |
| 25 | #define DIVIDE_TO_INTEGER(name, floatN) \ |
| 26 | static inline __attribute__((__always_inline__)) int \ |
| 27 | name(floatN *r1, floatN r2, floatN *r3, int m4, int *sig) \ |
| 28 | { \ |
| 29 | int cc; \ |
| 30 | \ |
| 31 | asm(/* Make the initial CC predictable for suppression tests */ \ |
| 32 | "xr %[sig],%[sig]\n" \ |
| 33 | #name " %[r1],%[r3],%[r2],%[m4]\n" \ |
| 34 | "ipm %[cc]\n" \ |
| 35 | "srl %[cc],28" \ |
| 36 | /* \ |
| 37 | * Use earlyclobbers to prevent the compiler from reusing floating \ |
| 38 | * point registers. This instruction doesn't like it. \ |
| 39 | */ \ |
| 40 | : [r1] "+&f" (*r1), [r3] "+&f" (*r3), [sig] "=r" (*sig), [cc] "=d" (cc)\ |
| 41 | : [r2] "f" (r2), [m4] "i" (m4) \ |
| 42 | : "cc"); \ |
| 43 | \ |
| 44 | return cc; \ |
| 45 | } |
| 46 | |
| 47 | DIVIDE_TO_INTEGER(diebr, float) |
| 48 | DIVIDE_TO_INTEGER(didbr, double) |
| 49 | |
| 50 | #define TEST_DIVIDE_TO_INTEGER(name, intN, int_fmt, floatN, float_fmt) \ |
| 51 | static inline __attribute__((__always_inline__)) int \ |
| 52 | test_ ## name(unsigned intN r1i, unsigned intN r2i, int m4, int fpc, \ |
| 53 | unsigned intN r1o, unsigned intN r3o, int cco, unsigned int fpco,\ |
| 54 | int sigo) \ |
| 55 | { \ |
| 56 | union { \ |
| 57 | floatN f; \ |
| 58 | unsigned intN i; \ |
| 59 | } r1, r2, r3; \ |
| 60 | int cc, err = 0, sig; \ |
| 61 | \ |
| 62 | r1.i = r1i; \ |
| 63 | r2.i = r2i; \ |
| 64 | r3.i = 0x12345678; \ |
| 65 | printf("[ RUN ] %" float_fmt "(0x%" int_fmt \ |
| 66 | ") / %" float_fmt "(0x%" int_fmt ")\n", r1.f, r1.i, r2.f, r2.i); \ |
| 67 | asm volatile("sfpc %[fpc]" : : [fpc] "r" (fpc)); \ |
| 68 | cc = name(&r1.f, r2.f, &r3.f, m4, &sig); \ |
| 69 | asm volatile("stfpc %[fpc]" : [fpc] "=Q" (fpc)); \ |
| 70 | if (r1.i != r1o) { \ |
| 71 | printf("[ FAILED ] remainder 0x%" int_fmt \ |
| 72 | " != expected 0x%" int_fmt "\n", r1.i, r1o); \ |
| 73 | err += 1; \ |
| 74 | } \ |
| 75 | if (r3.i != r3o) { \ |
| 76 | printf("[ FAILED ] quotient 0x%" int_fmt \ |
| 77 | " != expected 0x%" int_fmt "\n", r3.i, r3o); \ |
| 78 | err += 1; \ |
| 79 | } \ |
| 80 | if (cc != cco) { \ |
| 81 | printf("[ FAILED ] cc %d != expected %d\n", cc, cco); \ |
| 82 | err += 1; \ |
| 83 | } \ |
| 84 | if (fpc != fpco) { \ |
| 85 | printf("[ FAILED ] fpc 0x%x != expected 0x%x\n", fpc, fpco); \ |
| 86 | err += 1; \ |
| 87 | } \ |
| 88 | if (sig != sigo) { \ |
| 89 | printf("[ FAILED ] signal 0x%x != expected 0x%x\n", sig, sigo); \ |
| 90 | err += 1; \ |
| 91 | } \ |
| 92 | \ |
| 93 | return err; \ |
| 94 | } |
| 95 | |
| 96 | TEST_DIVIDE_TO_INTEGER(diebr, int, "x", float, "f") |
| 97 | TEST_DIVIDE_TO_INTEGER(didbr, long, "lx", double, "lf") |
| 98 | |
| 99 | int main(void) |
| 100 | { |
| 101 | struct sigaction act = { |
| 102 | .sa_sigaction = sigfpe_handler, |
| 103 | .sa_flags = SA_SIGINFO, |
| 104 | }; |
| 105 | int err = 0; |
| 106 | |
| 107 | /* Set up SIG handler */ |
| 108 | if (sigaction(SIGFPE, &act, NULL)) { |
| 109 | printf("[ FAILED ] sigaction(SIGFPE) failed\n"); |
| 110 | return EXIT_FAILURE; |
| 111 | } |
| 112 | |
| 113 | /* 451 / 460 */ |
| 114 | err += test_diebr(0x43e1f1f1, 0x43e61616, 7, 0, |
| 115 | 0x43e1f1f1, 0, 0, 0, 0); |
| 116 | |
| 117 | /* 480 / 0 */ |
| 118 | err += test_diebr(0x43f00000, 0, 0, 0, |
| 119 | 0x7fc00000, 0x7fc00000, 1, 0x800000, 0); |
| 120 | |
| 121 | /* QNaN / QNaN */ |
| 122 | err += test_diebr(0xffffffff, 0xffffffff, 0, 0, |
| 123 | 0xffffffff, 0xffffffff, 1, 0, 0); |
| 124 | |
| 125 | /* -2.08E-8 / -2.08E-8 */ |
| 126 | err += test_diebr(0xb2b2b2b2, 0xb2b2b2b2, 0, 0, |
| 127 | 0x80000000, 0x3f800000, 0, 0, 0); |
| 128 | |
| 129 | /* |
| 130 | * Test partial remainder without quotient scaling (cc2). |
| 131 | * |
| 132 | * a = 12401981 / 268435456 |
| 133 | * b = -5723991 / 72057594037927936 |
| 134 | * q = a / b = -3329131425038336 / 5723991 =~ -581610178.1 |
| 135 | * n = round(q, float32, nearest_even) = -581610176 |
| 136 | * r_precise = a - b * n = 189155 / 1125899906842624 |
| 137 | * r = round(r_precise, float32, nearest_even) = r_precise |
| 138 | */ |
| 139 | err += test_diebr(0x3d3d3d3d, 0xaeaeaeae, 0, 0, |
| 140 | 0x2f38b8c0, 0xce0aaaab, 2, 0, 0); |
| 141 | |
| 142 | /* 1.07E-31 / 2.19 */ |
| 143 | err += test_diebr(0x0c0c0c0c, 0x400c0c0c, 6, 0, |
| 144 | 0xc00c0c0c, 0x3f800000, 0, 0x80000, 0); |
| 145 | |
| 146 | /* |
| 147 | * Test partial remainder with quotient scaling (cc3). |
| 148 | * |
| 149 | * a = 298343530578310714772108083200 |
| 150 | * b = -592137/10384593717069655257060992658440192 |
| 151 | * q = a / b |
| 152 | * = -1032725451057301340137043014721780674141077289604872315653324800 / |
| 153 | * 197379 |
| 154 | * =~ -5232195173029052432817285601415452880707052369324357280426.6 |
| 155 | * n = round(q, float32, nearest_even) |
| 156 | * = -5232194943010009439437691768433469154159343131709361094656 |
| 157 | * n / 2^192 = -6992213 / 8388608 |
| 158 | * r_precise = a - b * n = 13115851209189604982784 |
| 159 | * r = round(r_precise, float32, nearest_even) = r_precise |
| 160 | */ |
| 161 | err += test_diebr(0x7070ffff, 0x90909090, 0, 0, |
| 162 | 0x6431c0c0, 0xbf5562aa, 3, 0, 0); |
| 163 | |
| 164 | /* |
| 165 | * Test large, but representable quotient. |
| 166 | * |
| 167 | * a = -12040119 / 549755813888 |
| 168 | * b = 1 / 38685626227668133590597632 |
| 169 | * q = a / b = -847248053779631702016 |
| 170 | * n = round(q, float32, to_odd) = q |
| 171 | * r_precise = a - b * n = -0 |
| 172 | * r = round(r_precise, float32, nearest_even) = -0 |
| 173 | */ |
| 174 | err += test_diebr(0xb7b7b7b7, 0x15000000, 7, 0, |
| 175 | 0x80000000, 0xe237b7b7, 0, 0, 0); |
| 176 | |
| 177 | /* 0 / 0 */ |
| 178 | err += test_diebr(0, 0, 1, 0, |
| 179 | 0x7fc00000, 0x7fc00000, 1, 0x800000, 0); |
| 180 | |
| 181 | /* 4.3E-33 / -2.08E-8 with SIGFPE */ |
| 182 | err += test_diebr(0x09b2b2b2, 0xb2b2b2b2, 0, 0xfc000007, |
| 183 | 0xb2b2b2b1, 0xbf800000, 0, 0xfc000807, SIGFPE); |
| 184 | |
| 185 | /* |
| 186 | * Test tiny remainder scaling when FPC Underflow Mask is set. |
| 187 | * |
| 188 | * 1.19E-39 / -1.28E-9 = { r = 1.19E-39 * 2^192, n = -0 } |
| 189 | */ |
| 190 | err += test_diebr(0x000d0100, 0xb0b0b0b0, 6, 0xfc000000, |
| 191 | 0x5ed01000, 0x80000000, 0, 0xfc001000, SIGFPE); |
| 192 | |
| 193 | /* |
| 194 | * Test "inexact and incremented" DXC. |
| 195 | * |
| 196 | * a = 53555504 |
| 197 | * b = -520849213389117849600 |
| 198 | * q = a / b = -3347219 / 32553075836819865600 |
| 199 | * n = round(q, float32, to_odd) = -1 |
| 200 | * r_precise = a - b * n = -520849213389064294096 |
| 201 | * r = round(r_precise, float32, to_odd) = -520849213389117849600 |
| 202 | * abs(r) - abs(r_precise) = 53555504 |
| 203 | */ |
| 204 | err += test_diebr(0x4c4c4c4c, 0xe1e1e1e1, 0, 0xfc000007, |
| 205 | 0xe1e1e1e1, 0xbf800000, 0, 0xfc000c07, SIGFPE); |
| 206 | |
| 207 | /* 0 / 0 with SIGFPE */ |
| 208 | err += test_diebr(0, 0, 0, 0xfc000007, |
| 209 | 0, 0x12345678, 0, 0xfc008007, SIGFPE); |
| 210 | |
| 211 | /* 5.76E-16 / 5.39E+34 */ |
| 212 | err += test_diebr(0x26262626, 0x79262626, 6, 0, |
| 213 | 0xf9262626, 0x3f800000, 0, 0x80000, 0); |
| 214 | |
| 215 | /* -4.97E+17 / 2.03E-38 */ |
| 216 | err += test_diebr(0xdcdcdcdc, 0x00dcdcdc, 7, 0xfc000000, |
| 217 | 0x80000000, 0xbb800000, 1, 0xfc000000, 0); |
| 218 | |
| 219 | /* -1.23E+17 / SNaN */ |
| 220 | err += test_diebr(0xdbdb240b, 0xffac73ff, 4, 0, |
| 221 | 0xffec73ff, 0xffec73ff, 1, 0x800000, 0); |
| 222 | |
| 223 | /* 2.34E-38 / 3.27E-33 with SIGFPE */ |
| 224 | err += test_diebr(0x00ff0987, 0x0987c6f6, 6, 0x08000000, |
| 225 | 0x8987c6b6, 0x3f800000, 0, 0x8000800, SIGFPE); |
| 226 | |
| 227 | /* -5.93E+11 / -2.7E+4 */ |
| 228 | err += test_diebr(0xd30a0040, 0xc6d30a00, 0, 0xc4000000, |
| 229 | 0xc74a4400, 0x4ba766c6, 2, 0xc4000000, 0); |
| 230 | |
| 231 | /* 9.86E-32 / -inf */ |
| 232 | err += test_diebr(0x0c000029, 0xff800000, 0, 0, |
| 233 | 0xc000029, 0x80000000, 0, 0, 0); |
| 234 | |
| 235 | /* QNaN / SNaN */ |
| 236 | err += test_diebr(0xffff94ff, 0xff94ff24, 4, 7, |
| 237 | 0xffd4ff24, 0xffd4ff24, 1, 0x800007, 0); |
| 238 | |
| 239 | /* 2.8E-43 / -inf */ |
| 240 | err += test_diebr(0x000000c8, 0xff800000, 0, 0x7c000007, |
| 241 | 0x000000c8, 0x80000000, 0, 0x7c000007, 0); |
| 242 | |
| 243 | /* -1.7E+38 / -inf */ |
| 244 | err += test_diebr(0xff00003d, 0xff800000, 0, 0, |
| 245 | 0xff00003d, 0, 0, 0, 0); |
| 246 | |
| 247 | /* 1.94E-304 / 1.94E-304 */ |
| 248 | err += test_didbr(0x00e100e100e100e1, 0x00e100e100e100e1, 0, 1, |
| 249 | 0, 0x3ff0000000000000, 0, 1, 0); |
| 250 | |
| 251 | /* 4.82E-299 / 5.29E-308 */ |
| 252 | err += test_didbr(0x0200230200230200, 0x0023020023020023, 0, 0, |
| 253 | 0x8001a017d247b3f4, 0x41cb2aa05f000000, 0, 0, 0); |
| 254 | |
| 255 | /* -1.38E-75 / -3.77E+208 */ |
| 256 | err += test_didbr(0xb063eb3d63b063eb, 0xeb3d63b063eb3d63, 3, 0xe8000000, |
| 257 | 0x6b3d63b063eb3d63, 0x3ff0000000000000, 0, 0xe8000c00, |
| 258 | SIGFPE); |
| 259 | |
| 260 | /* 4.78E-299 / 6.88E-315 */ |
| 261 | err += test_didbr(0x0200000000000000, 0x0000000053020000, 0, 0, |
| 262 | 0x8000000020820000, 0x4338ac20dd47c6c1, 0, 0, 0); |
| 263 | |
| 264 | return err ? EXIT_FAILURE : EXIT_SUCCESS; |
| 265 | } |