1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
|
/*
* Included file to common source float/double checking
* The following macros should be defined:
* TYPE -- floating point type
* NAME -- convert a name to include the type
* UNS_TYPE -- type to hold TYPE as an unsigned number
* EXP_SIZE -- size in bits of the exponent
* MAN_SIZE -- size in bits of the mantissa
* UNS_ABS -- absolute value for UNS_TYPE
* FABS -- absolute value function for TYPE
* FMAX -- maximum function for TYPE
* FMIN -- minimum function for TYPE
* SQRT -- square root function for TYPE
* RMIN -- minimum random number to generate
* RMAX -- maximum random number to generate
* ASMDIV -- assembler instruction to do divide
* ASMSQRT -- assembler instruction to do square root
* BDIV -- # of bits of inaccuracy to allow for division
* BRSQRT -- # of bits of inaccuracy to allow for 1/sqrt
* INIT_DIV -- Initial values to test 1/x against
* INIT_RSQRT -- Initial values to test 1/sqrt(x) against
*/
typedef union
{
UNS_TYPE i;
TYPE x;
} NAME (union);
/*
* Input/output arrays.
*/
static NAME (union) NAME (div_input) [] __attribute__((__aligned__(32))) = INIT_DIV;
static NAME (union) NAME (rsqrt_input)[] __attribute__((__aligned__(32))) = INIT_RSQRT;
#define DIV_SIZE (sizeof (NAME (div_input)) / sizeof (TYPE))
#define RSQRT_SIZE (sizeof (NAME (rsqrt_input)) / sizeof (TYPE))
static TYPE NAME (div_expected)[DIV_SIZE] __attribute__((__aligned__(32)));
static TYPE NAME (div_output) [DIV_SIZE] __attribute__((__aligned__(32)));
static TYPE NAME (rsqrt_expected)[RSQRT_SIZE] __attribute__((__aligned__(32)));
static TYPE NAME (rsqrt_output) [RSQRT_SIZE] __attribute__((__aligned__(32)));
/*
* Crack a floating point number into sign bit, exponent, and mantissa.
*/
static void
NAME (crack) (TYPE number, unsigned int *p_sign, unsigned *p_exponent, UNS_TYPE *p_mantissa)
{
NAME (union) u;
UNS_TYPE bits;
u.x = number;
bits = u.i;
*p_sign = (unsigned int)((bits >> (EXP_SIZE + MAN_SIZE)) & 0x1);
*p_exponent = (unsigned int)((bits >> MAN_SIZE) & ((((UNS_TYPE)1) << EXP_SIZE) - 1));
*p_mantissa = bits & ((((UNS_TYPE)1) << MAN_SIZE) - 1);
return;
}
/*
* Prevent optimizer from eliminating + 0.0 to remove -0.0.
*/
volatile TYPE NAME (math_diff_0) = ((TYPE) 0.0);
/*
* Return negative if two numbers are significanly different or return the
* number of bits that are different in the mantissa.
*/
static int
NAME (math_diff) (TYPE a, TYPE b, int bits)
{
TYPE zero = NAME (math_diff_0);
unsigned int sign_a, sign_b;
unsigned int exponent_a, exponent_b;
UNS_TYPE mantissa_a, mantissa_b, diff;
int i;
/* eliminate signed zero. */
a += zero;
b += zero;
/* special case Nan. */
if (__builtin_isnan (a))
return (__builtin_isnan (b) ? 0 : -1);
if (a == b)
return 0;
/* special case infinity. */
if (__builtin_isinf (a))
return (__builtin_isinf (b) ? 0 : -1);
/* punt on denormal numbers. */
if (!__builtin_isnormal (a) || !__builtin_isnormal (b))
return -1;
NAME (crack) (a, &sign_a, &exponent_a, &mantissa_a);
NAME (crack) (b, &sign_b, &exponent_b, &mantissa_b);
/* If the sign is different, there is no hope. */
if (sign_a != sign_b)
return -1;
/* If the exponent is off by 1, see if the values straddle the power of two,
and adjust things to do the mantassa check if we can. */
if ((exponent_a == (exponent_b+1)) || (exponent_a == (exponent_b-1)))
{
TYPE big = FMAX (a, b);
TYPE small = FMIN (a, b);
TYPE diff = FABS (a - b);
unsigned int sign_big, sign_small, sign_test;
unsigned int exponent_big, exponent_small, exponent_test;
UNS_TYPE mantissa_big, mantissa_small, mantissa_test;
NAME (crack) (big, &sign_big, &exponent_big, &mantissa_big);
NAME (crack) (small, &sign_small, &exponent_small, &mantissa_small);
NAME (crack) (small - diff, &sign_test, &exponent_test, &mantissa_test);
if ((sign_test == sign_small) && (exponent_test == exponent_small))
{
mantissa_a = mantissa_small;
mantissa_b = mantissa_test;
}
else
{
NAME (crack) (big + diff, &sign_test, &exponent_test, &mantissa_test);
if ((sign_test == sign_big) && (exponent_test == exponent_big))
{
mantissa_a = mantissa_big;
mantissa_b = mantissa_test;
}
else
return -1;
}
}
else if (exponent_a != exponent_b)
return -1;
diff = UNS_ABS (mantissa_a - mantissa_b);
for (i = MAN_SIZE; i > 0; i--)
{
if ((diff & ((UNS_TYPE)1) << (i-1)) != 0)
return i;
}
return -1;
}
/*
* Turn off inlining to make code inspection easier.
*/
static void NAME (asm_div) (void) __attribute__((__noinline__));
static void NAME (vector_div) (void) __attribute__((__noinline__));
static void NAME (scalar_div) (void) __attribute__((__noinline__));
static void NAME (asm_rsqrt) (void) __attribute__((__noinline__));
static void NAME (vector_rsqrt) (void) __attribute__((__noinline__));
static void NAME (scalar_rsqrt) (void) __attribute__((__noinline__));
static void NAME (check_div) (const char *) __attribute__((__noinline__));
static void NAME (check_rsqrt) (const char *) __attribute__((__noinline__));
static void NAME (run) (void) __attribute__((__noinline__));
/*
* Division function that might be vectorized.
*/
static void
NAME (vector_div) (void)
{
size_t i;
for (i = 0; i < DIV_SIZE; i++)
NAME (div_output)[i] = ((TYPE) 1.0) / NAME (div_input)[i].x;
}
/*
* Division function that is not vectorized.
*/
static void
NAME (scalar_div) (void)
{
size_t i;
for (i = 0; i < DIV_SIZE; i++)
{
TYPE x = ((TYPE) 1.0) / NAME (div_input)[i].x;
TYPE y;
__asm__ ("" : "=d" (y) : "0" (x));
NAME (div_output)[i] = y;
}
}
/*
* Generate the division instruction via asm.
*/
static void
NAME (asm_div) (void)
{
size_t i;
for (i = 0; i < DIV_SIZE; i++)
{
TYPE x;
__asm__ (ASMDIV " %0,%1,%2"
: "=d" (x)
: "d" ((TYPE) 1.0), "d" (NAME (div_input)[i].x));
NAME (div_expected)[i] = x;
}
}
/*
* Reciprocal square root function that might be vectorized.
*/
static void
NAME (vector_rsqrt) (void)
{
size_t i;
for (i = 0; i < RSQRT_SIZE; i++)
NAME (rsqrt_output)[i] = ((TYPE) 1.0) / SQRT (NAME (rsqrt_input)[i].x);
}
/*
* Reciprocal square root function that is not vectorized.
*/
static void
NAME (scalar_rsqrt) (void)
{
size_t i;
for (i = 0; i < RSQRT_SIZE; i++)
{
TYPE x = ((TYPE) 1.0) / SQRT (NAME (rsqrt_input)[i].x);
TYPE y;
__asm__ ("" : "=d" (y) : "0" (x));
NAME (rsqrt_output)[i] = y;
}
}
/*
* Generate the 1/sqrt instructions via asm.
*/
static void
NAME (asm_rsqrt) (void)
{
size_t i;
for (i = 0; i < RSQRT_SIZE; i++)
{
TYPE x;
TYPE y;
__asm__ (ASMSQRT " %0,%1" : "=d" (x) : "d" (NAME (rsqrt_input)[i].x));
__asm__ (ASMDIV " %0,%1,%2" : "=d" (y) : "d" ((TYPE) 1.0), "d" (x));
NAME (rsqrt_expected)[i] = y;
}
}
/*
* Functions to abort or report errors.
*/
static int NAME (error_count) = 0;
#ifdef VERBOSE
static int NAME (max_bits_div) = 0;
static int NAME (max_bits_rsqrt) = 0;
#endif
/*
* Compare the expected value with the value we got.
*/
static void
NAME (check_div) (const char *test)
{
size_t i;
int b;
for (i = 0; i < DIV_SIZE; i++)
{
TYPE exp = NAME (div_expected)[i];
TYPE out = NAME (div_output)[i];
b = NAME (math_diff) (exp, out, BDIV);
#ifdef VERBOSE
if (b != 0)
{
NAME (union) u_in = NAME (div_input)[i];
NAME (union) u_exp;
NAME (union) u_out;
char explanation[64];
const char *p_exp;
if (b < 0)
p_exp = "failed";
else
{
p_exp = explanation;
sprintf (explanation, "%d bit error%s", b, (b > BDIV) ? ", failed" : "");
}
u_exp.x = exp;
u_out.x = out;
printf ("%s %s %s for 1.0 / %g [0x%llx], expected %g [0x%llx], got %g [0x%llx]\n",
TNAME (TYPE), test, p_exp,
(double) u_in.x, (unsigned long long) u_in.i,
(double) exp, (unsigned long long) u_exp.i,
(double) out, (unsigned long long) u_out.i);
}
#endif
if (b < 0 || b > BDIV)
NAME (error_count)++;
#ifdef VERBOSE
if (b > NAME (max_bits_div))
NAME (max_bits_div) = b;
#endif
}
}
static void
NAME (check_rsqrt) (const char *test)
{
size_t i;
int b;
for (i = 0; i < RSQRT_SIZE; i++)
{
TYPE exp = NAME (rsqrt_expected)[i];
TYPE out = NAME (rsqrt_output)[i];
b = NAME (math_diff) (exp, out, BRSQRT);
#ifdef VERBOSE
if (b != 0)
{
NAME (union) u_in = NAME (rsqrt_input)[i];
NAME (union) u_exp;
NAME (union) u_out;
char explanation[64];
const char *p_exp;
if (b < 0)
p_exp = "failed";
else
{
p_exp = explanation;
sprintf (explanation, "%d bit error%s", b, (b > BDIV) ? ", failed" : "");
}
u_exp.x = exp;
u_out.x = out;
printf ("%s %s %s for 1 / sqrt (%g) [0x%llx], expected %g [0x%llx], got %g [0x%llx]\n",
TNAME (TYPE), test, p_exp,
(double) u_in.x, (unsigned long long) u_in.i,
(double) exp, (unsigned long long) u_exp.i,
(double) out, (unsigned long long) u_out.i);
}
#endif
if (b < 0 || b > BRSQRT)
NAME (error_count)++;
#ifdef VERBOSE
if (b > NAME (max_bits_rsqrt))
NAME (max_bits_rsqrt) = b;
#endif
}
}
/*
* Now do everything.
*/
static void
NAME (run) (void)
{
#ifdef VERBOSE
printf ("start run_%s, divide size = %ld, rsqrt size = %ld, %d bit%s for a/b, %d bit%s for 1/sqrt(a)\n",
TNAME (TYPE),
(long)DIV_SIZE,
(long)RSQRT_SIZE,
BDIV, (BDIV == 1) ? "" : "s",
BRSQRT, (BRSQRT == 1) ? "" : "s");
#endif
NAME (asm_div) ();
NAME (scalar_div) ();
NAME (check_div) ("scalar");
NAME (vector_div) ();
NAME (check_div) ("vector");
NAME (asm_rsqrt) ();
NAME (scalar_rsqrt) ();
NAME (check_rsqrt) ("scalar");
NAME (vector_rsqrt) ();
NAME (check_rsqrt) ("vector");
#ifdef VERBOSE
printf ("end run_%s, errors = %d, max div bits = %d, max rsqrt bits = %d\n",
TNAME (TYPE),
NAME (error_count),
NAME (max_bits_div),
NAME (max_bits_rsqrt));
#endif
}
|