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 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
|
/* -----------------------------------------------------------------------------
*
* (c) Lennart Augustsson
* (c) The GHC Team, 1998-2000
*
* Miscellaneous support for floating-point primitives
*
* ---------------------------------------------------------------------------*/
#include "HsFFI.h"
#include "Rts.h" // XXX wrong (for IEEE_FLOATING_POINT and WORDS_BIGENDIAN)
#define IEEE_FLOATING_POINT 1
union stg_ieee754_flt
{
float f;
struct {
#if WORDS_BIGENDIAN
unsigned int negative:1;
unsigned int exponent:8;
unsigned int mantissa:23;
#else
unsigned int mantissa:23;
unsigned int exponent:8;
unsigned int negative:1;
#endif
} ieee;
struct {
#if WORDS_BIGENDIAN
unsigned int negative:1;
unsigned int exponent:8;
unsigned int quiet_nan:1;
unsigned int mantissa:22;
#else
unsigned int mantissa:22;
unsigned int quiet_nan:1;
unsigned int exponent:8;
unsigned int negative:1;
#endif
} ieee_nan;
};
/*
To recap, here's the representation of a double precision
IEEE floating point number:
sign 63 sign bit (0==positive, 1==negative)
exponent 62-52 exponent (biased by 1023)
fraction 51-0 fraction (bits to right of binary point)
*/
union stg_ieee754_dbl
{
double d;
struct {
#if WORDS_BIGENDIAN
unsigned int negative:1;
unsigned int exponent:11;
unsigned int mantissa0:20;
unsigned int mantissa1:32;
#else
#if FLOAT_WORDS_BIGENDIAN
unsigned int mantissa0:20;
unsigned int exponent:11;
unsigned int negative:1;
unsigned int mantissa1:32;
#else
unsigned int mantissa1:32;
unsigned int mantissa0:20;
unsigned int exponent:11;
unsigned int negative:1;
#endif
#endif
} ieee;
/* This format makes it easier to see if a NaN is a signalling NaN. */
struct {
#if WORDS_BIGENDIAN
unsigned int negative:1;
unsigned int exponent:11;
unsigned int quiet_nan:1;
unsigned int mantissa0:19;
unsigned int mantissa1:32;
#else
#if FLOAT_WORDS_BIGENDIAN
unsigned int mantissa0:19;
unsigned int quiet_nan:1;
unsigned int exponent:11;
unsigned int negative:1;
unsigned int mantissa1:32;
#else
unsigned int mantissa1:32;
unsigned int mantissa0:19;
unsigned int quiet_nan:1;
unsigned int exponent:11;
unsigned int negative:1;
#endif
#endif
} ieee_nan;
};
/*
* Predicates for testing for extreme IEEE fp values.
*/
/* In case you don't support IEEE, you'll just get dummy defs.. */
#if defined(IEEE_FLOATING_POINT)
HsInt
isDoubleFinite(HsDouble d)
{
union stg_ieee754_dbl u;
u.d = d;
return u.ieee.exponent != 2047;
}
HsInt
isDoubleNaN(HsDouble d)
{
union stg_ieee754_dbl u;
u.d = d;
return (
u.ieee.exponent == 2047 /* 2^11 - 1 */ && /* Is the exponent all ones? */
(u.ieee.mantissa0 != 0 || u.ieee.mantissa1 != 0)
/* and the mantissa non-zero? */
);
}
HsInt
isDoubleInfinite(HsDouble d)
{
union stg_ieee754_dbl u;
u.d = d;
/* Inf iff exponent is all ones, mantissa all zeros */
return (
u.ieee.exponent == 2047 /* 2^11 - 1 */ &&
u.ieee.mantissa0 == 0 &&
u.ieee.mantissa1 == 0
);
}
HsInt
isDoubleDenormalized(HsDouble d)
{
union stg_ieee754_dbl u;
u.d = d;
/* A (single/double/quad) precision floating point number
is denormalised iff:
- exponent is zero
- mantissa is non-zero.
- (don't care about setting of sign bit.)
*/
return (
u.ieee.exponent == 0 &&
(u.ieee.mantissa0 != 0 ||
u.ieee.mantissa1 != 0)
);
}
HsInt
isDoubleNegativeZero(HsDouble d)
{
union stg_ieee754_dbl u;
u.d = d;
/* sign (bit 63) set (only) => negative zero */
return (
u.ieee.negative == 1 &&
u.ieee.exponent == 0 &&
u.ieee.mantissa0 == 0 &&
u.ieee.mantissa1 == 0);
}
/* Same tests, this time for HsFloats. */
/*
To recap, here's the representation of a single precision
IEEE floating point number:
sign 31 sign bit (0 == positive, 1 == negative)
exponent 30-23 exponent (biased by 127)
fraction 22-0 fraction (bits to right of binary point)
*/
HsInt
isFloatFinite(HsFloat f)
{
union stg_ieee754_flt u;
u.f = f;
return u.ieee.exponent != 255;
}
HsInt
isFloatNaN(HsFloat f)
{
union stg_ieee754_flt u;
u.f = f;
/* Floating point NaN iff exponent is all ones, mantissa is
non-zero (but see below.) */
return (
u.ieee.exponent == 255 /* 2^8 - 1 */ &&
u.ieee.mantissa != 0);
}
HsInt
isFloatInfinite(HsFloat f)
{
union stg_ieee754_flt u;
u.f = f;
/* A float is Inf iff exponent is max (all ones),
and mantissa is min(all zeros.) */
return (
u.ieee.exponent == 255 /* 2^8 - 1 */ &&
u.ieee.mantissa == 0);
}
HsInt
isFloatDenormalized(HsFloat f)
{
union stg_ieee754_flt u;
u.f = f;
/* A (single/double/quad) precision floating point number
is denormalised iff:
- exponent is zero
- mantissa is non-zero.
- (don't care about setting of sign bit.)
*/
return (
u.ieee.exponent == 0 &&
u.ieee.mantissa != 0);
}
HsInt
isFloatNegativeZero(HsFloat f)
{
union stg_ieee754_flt u;
u.f = f;
/* sign (bit 31) set (only) => negative zero */
return (
u.ieee.negative &&
u.ieee.exponent == 0 &&
u.ieee.mantissa == 0);
}
/*
There are glibc versions around with buggy rintf or rint, hence we
provide our own. We always round ties to even, so we can be simpler.
*/
#define FLT_HIDDEN 0x800000
#define FLT_POWER2 0x1000000
HsFloat
rintFloat(HsFloat f)
{
union stg_ieee754_flt u;
u.f = f;
/* if real exponent > 22, it's already integral, infinite or nan */
if (u.ieee.exponent > 149) /* 22 + 127 */
{
return u.f;
}
if (u.ieee.exponent < 126) /* (-1) + 127, abs(f) < 0.5 */
{
/* only used for rounding to Integral a, so don't care about -0.0 */
return 0.0;
}
/* 0.5 <= abs(f) < 2^23 */
unsigned int half, mask, mant, frac;
half = 1 << (149 - u.ieee.exponent); /* bit for 0.5 */
mask = 2*half - 1; /* fraction bits */
mant = u.ieee.mantissa | FLT_HIDDEN; /* add hidden bit */
frac = mant & mask; /* get fraction */
mant ^= frac; /* truncate mantissa */
if ((frac < half) || ((frac == half) && ((mant & (2*half)) == 0)))
{
/* this means we have to truncate */
if (mant == 0)
{
/* f == ±0.5, return 0.0 */
return 0.0;
}
else
{
/* remove hidden bit and set mantissa */
u.ieee.mantissa = mant ^ FLT_HIDDEN;
return u.f;
}
}
else
{
/* round away from zero, increment mantissa */
mant += 2*half;
if (mant == FLT_POWER2)
{
/* next power of 2, increase exponent and set mantissa to 0 */
u.ieee.mantissa = 0;
u.ieee.exponent += 1;
return u.f;
}
else
{
/* remove hidden bit and set mantissa */
u.ieee.mantissa = mant ^ FLT_HIDDEN;
return u.f;
}
}
}
#define DBL_HIDDEN 0x100000
#define DBL_POWER2 0x200000
#define LTOP_BIT 0x80000000
HsDouble
rintDouble(HsDouble d)
{
union stg_ieee754_dbl u;
u.d = d;
/* if real exponent > 51, it's already integral, infinite or nan */
if (u.ieee.exponent > 1074) /* 51 + 1023 */
{
return u.d;
}
if (u.ieee.exponent < 1022) /* (-1) + 1023, abs(d) < 0.5 */
{
/* only used for rounding to Integral a, so don't care about -0.0 */
return 0.0;
}
unsigned int half, mask, mant, frac;
if (u.ieee.exponent < 1043) /* 20 + 1023, real exponent < 20 */
{
/* the fractional part meets the higher part of the mantissa */
half = 1 << (1042 - u.ieee.exponent); /* bit for 0.5 */
mask = 2*half - 1; /* fraction bits */
mant = u.ieee.mantissa0 | DBL_HIDDEN; /* add hidden bit */
frac = mant & mask; /* get fraction */
mant ^= frac; /* truncate mantissa */
if ((frac < half) ||
((frac == half) && (u.ieee.mantissa1 == 0) /* a tie */
&& ((mant & (2*half)) == 0)))
{
/* truncate */
if (mant == 0)
{
/* d = ±0.5, return 0.0 */
return 0.0;
}
/* remove hidden bit and set mantissa */
u.ieee.mantissa0 = mant ^ DBL_HIDDEN;
u.ieee.mantissa1 = 0;
return u.d;
}
else /* round away from zero */
{
/* zero low mantissa bits */
u.ieee.mantissa1 = 0;
/* increment integer part of mantissa */
mant += 2*half;
if (mant == DBL_POWER2)
{
/* power of 2, increment exponent and zero mantissa */
u.ieee.mantissa0 = 0;
u.ieee.exponent += 1;
return u.d;
}
/* remove hidden bit */
u.ieee.mantissa0 = mant ^ DBL_HIDDEN;
return u.d;
}
}
else
{
/* 20 <= real exponent < 52, fractional part entirely in mantissa1 */
half = 1 << (1074 - u.ieee.exponent); /* bit for 0.5 */
mask = 2*half - 1; /* fraction bits */
mant = u.ieee.mantissa1; /* no hidden bit here */
frac = mant & mask; /* get fraction */
mant ^= frac; /* truncate mantissa */
if ((frac < half) ||
((frac == half) && /* tie */
(((half == LTOP_BIT) ? (u.ieee.mantissa0 & 1) /* yuck */
: (mant & (2*half)))
== 0)))
{
/* truncate */
u.ieee.mantissa1 = mant;
return u.d;
}
else
{
/* round away from zero */
/* increment mantissa */
mant += 2*half;
u.ieee.mantissa1 = mant;
if (mant == 0)
{
/* low part of mantissa overflowed */
/* increment high part of mantissa */
mant = u.ieee.mantissa0 + 1;
if (mant == DBL_HIDDEN)
{
/* hit power of 2 */
/* zero mantissa */
u.ieee.mantissa0 = 0;
/* and increment exponent */
u.ieee.exponent += 1;
return u.d;
}
else
{
u.ieee.mantissa0 = mant;
return u.d;
}
}
else
{
return u.d;
}
}
}
}
#else /* ! IEEE_FLOATING_POINT */
/* Dummy definitions of predicates - they all return "normal" values */
HsInt isDoubleFinite(HsDouble d) { return 1;}
HsInt isDoubleNaN(HsDouble d) { return 0; }
HsInt isDoubleInfinite(HsDouble d) { return 0; }
HsInt isDoubleDenormalized(HsDouble d) { return 0; }
HsInt isDoubleNegativeZero(HsDouble d) { return 0; }
HsInt isFloatFinite(HsFloat f) { return 1; }
HsInt isFloatNaN(HsFloat f) { return 0; }
HsInt isFloatInfinite(HsFloat f) { return 0; }
HsInt isFloatDenormalized(HsFloat f) { return 0; }
HsInt isFloatNegativeZero(HsFloat f) { return 0; }
/* For exotic floating point formats, we can't do much */
/* We suppose the format has not too many bits */
/* I hope nobody tries to build GHC where this is wrong */
#define FLT_UPP 536870912.0
HsFloat
rintFloat(HsFloat f)
{
if ((f > FLT_UPP) || (f < (-FLT_UPP)))
{
return f;
}
else
{
int i = (int)f;
float g = i;
float d = f - g;
if (d > 0.5)
{
return g + 1.0;
}
if (d == 0.5)
{
return (i & 1) ? (g + 1.0) : g;
}
if (d == -0.5)
{
return (i & 1) ? (g - 1.0) : g;
}
if (d < -0.5)
{
return g - 1.0;
}
return g;
}
}
#define DBL_UPP 2305843009213693952.0
HsDouble
rintDouble(HsDouble d)
{
if ((d > DBL_UPP) || (d < (-DBL_UPP)))
{
return d;
}
else
{
HsInt64 i = (HsInt64)d;
double e = i;
double r = d - e;
if (r > 0.5)
{
return e + 1.0;
}
if (r == 0.5)
{
return (i & 1) ? (e + 1.0) : e;
}
if (r == -0.5)
{
return (i & 1) ? (e - 1.0) : e;
}
if (r < -0.5)
{
return e - 1.0;
}
return e;
}
}
#endif /* ! IEEE_FLOATING_POINT */
|