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 533
|
/*
streflop: STandalone REproducible FLOating-Point
Nicolas Brodu, 2006
Code released according to the GNU Lesser General Public License
Heavily relies on GNU Libm, itself depending on netlib fplibm, GNU MP, and IBM MP lib.
Uses SoftFloat too.
Please read the history and copyright information in the documentation provided with the source code
*/
/*
For reference, the layout of the MXCSR register:
FZ:RC:RC:PM:UM:OM:ZM:DM:IM:Rsvd:PE:UE:OE:ZE:DE:IE
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
And the layout of the 387 FPU control word register:
Rsvd:Rsvd:Rsvd:X:RC:RC:PC:PC:Rsvd:Rsvd:PM:UM:OM:ZM:DM:IM
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Where:
Rsvd - Reserved
FZ - Flush to Zero
RC - Rounding Control
PM - Precision Mask
UM - Underflow Mask
OM - Overflow Mask
ZM - Zerodivide Mask
DM - Denormal Mask
IM - Invalid Mask
PE - Precision Exception
UE - Underflow Exception
OE - Overflow Exception
ZE - Zerodivide Exception
DE - Denormal Exception
IE - Invalid Exception
X - Infinity control (unused on 387 and higher)
PC - Precision Control
Source: Intel Architecture Software Development Manual, Volume 1, Basic Architecture
*/
// Included by the main streflop include file
// module broken apart for logical code separation
#ifndef STREFLOP_FPU_H
#define STREFLOP_FPU_H
// Can safely make the symbols from softfloat visible to user program, protected in namespace
#if defined(STREFLOP_SOFT)
#include "softfloat/softfloat.h"
#endif
namespace streflop {
// We do not use libm, so let's copy a few flags and C99 functions from fenv.h
// Give warning in case these flags would be defined already, this is indication
// of potential confusion!
#if defined(FE_INVALID) || defined(FE_DENORMAL) || defined(FE_DIVBYZERO) || defined(FE_OVERFLOW) || defined(FE_UNDERFLOW) || defined(FE_INEXACT) || defined(FE_DOWNWARD) || defined(FE_TONEAREST) || defined(FE_TOWARDZERO) || defined(FE_UPWARD)
#warning STREFLOP: FE_XXX flags were already defined and will be redefined! Check you do not use the system libm.
#undef FE_INVALID
#undef FE_DENORMAL
#undef FE_DIVBYZERO
#undef FE_OVERFLOW
#undef FE_UNDERFLOW
#undef FE_INEXACT
#undef FE_INEXACT
#undef FE_ALL_EXCEPT
#undef FE_DOWNWARD
#undef FE_TONEAREST
#undef FE_TOWARDZERO
#undef FE_UPWARD
#endif // defined(FE_INVALID) || ...
// Flags for FPU exceptions
enum FPU_Exceptions {
// Invalid operation. If not signaling, gives NaN instead
FE_INVALID = 0x0001,
#define FE_INVALID FE_INVALID
// Extension: for x86 and SSE
// Denormal operand. If not signaling, use denormal arithmetic as usual
FE_DENORMAL = 0x0002,
#define FE_DENORMAL FE_DENORMAL
// Division by zero. If not signaling, uses +/- infinity
FE_DIVBYZERO = 0x0004,
#define FE_DIVBYZERO FE_DIVBYZERO
// Overflow. If not signaling, round to nearest (including infinity) according to rounding mode
FE_OVERFLOW = 0x0008,
#define FE_OVERFLOW FE_OVERFLOW
// Underflow. If not signaling, use 0 instead
FE_UNDERFLOW = 0x0010,
#define FE_UNDERFLOW FE_UNDERFLOW
// Rounding was not exact (ex: sqrt(2) is never exact) or when overflow causes rounding
FE_INEXACT = 0x0020,
#define FE_INEXACT FE_INEXACT
// Combination of all the above
FE_ALL_EXCEPT = 0x003F
#define FE_ALL_EXCEPT FE_ALL_EXCEPT
};
// Flags for FPU rounding modes
enum FPU_RoundMode {
FE_TONEAREST = 0x0000,
#define FE_TONEAREST FE_TONEAREST
FE_DOWNWARD = 0x0400,
#define FE_DOWNWARD FE_DOWNWARD
FE_UPWARD = 0x0800,
#define FE_UPWARD FE_UPWARD
FE_TOWARDZERO = 0x0C00
#define FE_TOWARDZERO FE_TOWARDZERO
};
/* Note: SSE control word, bits 0..15
0->5: Run-time status flags
6: DAZ (denormals are zero, i.e. don't use denormals if bit is 1)
7->12: Exception flags, same meaning as for the x87 ones
13,14: Rounding flags, same meaning as for the x87 ones
15: Flush to zero (FTZ) for automatic handling of underflow (default is NO)
*/
// plan for portability
#if defined(_MSC_VER)
#define STREFLOP_FSTCW(cw) do { short tmp; __asm { fstcw tmp }; (cw) = tmp; } while (0)
#define STREFLOP_FLDCW(cw) do { short tmp = (cw); __asm { fclex }; __asm { fldcw tmp }; } while (0)
#define STREFLOP_STMXCSR(cw) do { int tmp; __asm { stmxcsr tmp }; (cw) = tmp; } while (0)
#define STREFLOP_LDMXCSR(cw) do { int tmp = (cw); __asm { ldmxcsr tmp }; } while (0)
#else
#define STREFLOP_FSTCW(cw) do { asm volatile ("fstcw %0" : "=m" (cw) : ); } while (0)
#define STREFLOP_FLDCW(cw) do { asm volatile ("fclex \n fldcw %0" : : "m" (cw)); } while (0)
#define STREFLOP_STMXCSR(cw) do { asm volatile ("stmxcsr %0" : "=m" (cw) : ); } while (0)
#define STREFLOP_LDMXCSR(cw) do { asm volatile ("ldmxcsr %0" : : "m" (cw) ); } while (0)
#endif // defined(_MSC_VER)
// Subset of all C99 functions
#if defined(STREFLOP_X87)
/// Raise exception for these flags
inline int feraiseexcept(FPU_Exceptions excepts) {
unsigned short fpu_mode;
STREFLOP_FSTCW(fpu_mode);
fpu_mode &= ~( excepts ); // generate error for selection
STREFLOP_FLDCW(fpu_mode);
return 0;
}
/// Clear exceptions for these flags
inline int feclearexcept(int excepts) {
unsigned short fpu_mode;
STREFLOP_FSTCW(fpu_mode);
fpu_mode |= excepts;
STREFLOP_FLDCW(fpu_mode);
return 0;
}
/// Get current rounding mode
inline int fegetround() {
unsigned short fpu_mode;
STREFLOP_FSTCW(fpu_mode);
return fpu_mode & 0x0C00;
}
/// Set a new rounding mode
inline int fesetround(FPU_RoundMode roundMode) {
unsigned short fpu_mode;
STREFLOP_FSTCW(fpu_mode);
fpu_mode &= 0xF3FF; // clear current mode
fpu_mode |= roundMode; // sets new mode
STREFLOP_FLDCW(fpu_mode);
return 0;
}
typedef short int fpenv_t;
/// Default env. Defined in SMath.cpp to be 0, and initialized on first use to the permanent holder
extern fpenv_t FE_DFL_ENV;
/// Get FP env into the given structure
inline int fegetenv(fpenv_t *envp) {
// check that default env exists, otherwise save it now
if (!FE_DFL_ENV) STREFLOP_FSTCW(FE_DFL_ENV);
// Now store env into argument
STREFLOP_FSTCW(*envp);
return 0;
}
/// Sets FP env from the given structure
inline int fesetenv(const fpenv_t *envp) {
// check that default env exists, otherwise save it now
if (!FE_DFL_ENV) STREFLOP_FSTCW(FE_DFL_ENV);
// Now overwrite current env by argument
STREFLOP_FLDCW(*envp);
return 0;
}
/// get env and clear exceptions
inline int feholdexcept(fpenv_t *envp) {
fegetenv(envp);
feclearexcept(FE_ALL_EXCEPT);
return 0;
}
template<typename T> inline void streflop_init() {
struct X {};
X Unknown_numeric_type;
// unknown types do not compile
T error = Unknown_numeric_type;
}
/// Initialize the FPU for the different types
/// this may also be called to switch between code sections using
/// different precisions
template<> inline void streflop_init<Simple>() {
unsigned short fpu_mode;
STREFLOP_FSTCW(fpu_mode);
fpu_mode &= 0xFCFF; // 32 bits internal operations
STREFLOP_FLDCW(fpu_mode);
// Enable signaling nans if compiled with this option.
#if defined(__SUPPORT_SNAN__)
feraiseexcept(streflop::FPU_Exceptions(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW));
#endif
}
template<> inline void streflop_init<Double>() {
unsigned short fpu_mode;
STREFLOP_FSTCW(fpu_mode);
fpu_mode &= 0xFCFF;
fpu_mode |= 0x0200; // 64 bits internal operations
STREFLOP_FLDCW(fpu_mode);
#if defined(__SUPPORT_SNAN__)
feraiseexcept(streflop::FPU_Exceptions(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW));
#endif
}
#if defined(Extended)
template<> inline void streflop_init<Extended>() {
unsigned short fpu_mode;
STREFLOP_FSTCW(fpu_mode);
fpu_mode &= 0xFCFF;
fpu_mode |= 0x0300; // 80 bits internal operations
STREFLOP_FLDCW(fpu_mode);
#if defined(__SUPPORT_SNAN__)
feraiseexcept(streflop::FPU_Exceptions(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW));
#endif
}
#endif // defined(Extended)
#elif defined(STREFLOP_SSE)
/// Raise exception for these flags
inline int feraiseexcept(FPU_Exceptions excepts) {
// Just in case the compiler would store a value on the st(x) registers
unsigned short x87_mode;
STREFLOP_FSTCW(x87_mode);
x87_mode &= ~( excepts ); // generate error for selection
STREFLOP_FLDCW(x87_mode);
int sse_mode;
STREFLOP_STMXCSR(sse_mode);
sse_mode &= ~( excepts << 7 ); // generate error for selection
STREFLOP_LDMXCSR(sse_mode);
return 0;
}
/// Clear exceptions for these flags
inline int feclearexcept(int excepts) {
// Just in case the compiler would store a value on the st(x) registers
unsigned short x87_mode;
STREFLOP_FSTCW(x87_mode);
x87_mode |= excepts;
STREFLOP_FLDCW(x87_mode);
int sse_mode;
STREFLOP_STMXCSR(sse_mode);
sse_mode |= excepts << 7;
STREFLOP_LDMXCSR(sse_mode);
return 0;
}
/// Get current rounding mode
inline int fegetround() {
int sse_mode;
STREFLOP_STMXCSR(sse_mode);
return (sse_mode>>3) & 0x00000C00;
}
/// Set a new rounding mode
inline int fesetround(FPU_RoundMode roundMode) {
int sse_mode;
STREFLOP_STMXCSR(sse_mode);
sse_mode &= 0xFFFF9FFF; // clear current mode
sse_mode |= roundMode<<3; // sets new mode
STREFLOP_LDMXCSR(sse_mode);
return 0;
}
/// stores both x87 and SSE words
struct fpenv_t {
int sse_mode;
short int x87_mode;
};
/// Default env. Defined in SMath.cpp, structs are initialized to 0
extern fpenv_t FE_DFL_ENV;
/// Get FP env into the given structure
inline int fegetenv(fpenv_t *envp) {
// check that default env exists, otherwise save it now
if (!FE_DFL_ENV.x87_mode) STREFLOP_FSTCW(FE_DFL_ENV.x87_mode);
// Now store env into argument
STREFLOP_FSTCW(envp->x87_mode);
// For SSE
if (!FE_DFL_ENV.sse_mode) STREFLOP_STMXCSR(FE_DFL_ENV.sse_mode);
// Now store env into argument
STREFLOP_STMXCSR(envp->sse_mode);
return 0;
}
/// Sets FP env from the given structure
inline int fesetenv(const fpenv_t *envp) {
// check that default env exists, otherwise save it now
if (!FE_DFL_ENV.x87_mode) STREFLOP_FSTCW(FE_DFL_ENV.x87_mode);
// Now overwrite current env by argument
STREFLOP_FLDCW(envp->x87_mode);
// For SSE
if (!FE_DFL_ENV.sse_mode) STREFLOP_STMXCSR(FE_DFL_ENV.sse_mode);
// Now overwrite current env by argument
STREFLOP_LDMXCSR(envp->sse_mode);
return 0;
}
/// get env and clear exceptions
inline int feholdexcept(fpenv_t *envp) {
fegetenv(envp);
feclearexcept(FE_ALL_EXCEPT);
return 0;
}
template<typename T> inline void streflop_init() {
// Do nothing by default, or for unknown types
}
/// Initialize the FPU for the different types
/// this may also be called to switch between code sections using
/// different precisions
template<> inline void streflop_init<Simple>() {
// Just in case the compiler would store a value on the st(x) registers
unsigned short x87_mode;
STREFLOP_FSTCW(x87_mode);
x87_mode &= 0xFCFF; // 32 bits internal operations
STREFLOP_FLDCW(x87_mode);
int sse_mode;
STREFLOP_STMXCSR(sse_mode);
#if defined(STREFLOP_NO_DENORMALS)
sse_mode |= 0x8040; // set DAZ and FTZ
#else
sse_mode &= 0xFFFF7FBF; // clear DAZ and FTZ
#endif
STREFLOP_LDMXCSR(sse_mode);
}
template<> inline void streflop_init<Double>() {
// Just in case the compiler would store a value on the st(x) registers
unsigned short x87_mode;
STREFLOP_FSTCW(x87_mode);
x87_mode &= 0xFCFF;
x87_mode |= 0x0200; // 64 bits internal operations
STREFLOP_FLDCW(x87_mode);
int sse_mode;
STREFLOP_STMXCSR(sse_mode);
#if defined(STREFLOP_NO_DENORMALS)
sse_mode |= 0x8040; // set DAZ and FTZ
#else
sse_mode &= 0xFFFF7FBF; // clear DAZ and FTZ
#endif
STREFLOP_LDMXCSR(sse_mode);
}
#if defined(Extended)
template<> inline void streflop_init<Extended>() {
// Just in case the compiler would store a value on the st(x) registers
unsigned short x87_mode;
STREFLOP_FSTCW(x87_mode);
x87_mode &= 0xFCFF;
x87_mode |= 0x0300; // 80 bits internal operations
STREFLOP_FLDCW(x87_mode);
int sse_mode;
STREFLOP_STMXCSR(sse_mode);
#if defined(STREFLOP_NO_DENORMALS)
sse_mode |= 0x8040; // set DAZ and FTZ
#else
sse_mode &= 0xFFFF7FBF; // clear DAZ and FTZ
#endif
STREFLOP_LDMXCSR(sse_mode);
}
#endif // defined(Extended)
#elif defined(STREFLOP_SOFT)
/// Raise exception for these flags
inline int feraiseexcept(FPU_Exceptions excepts) {
// Use positive logic
SoftFloat::float_exception_realtraps |= excepts;
return 0;
}
/// Clear exceptions for these flags
inline int feclearexcept(int excepts) {
// Use positive logic
SoftFloat::float_exception_realtraps &= ~( excepts );
return 0;
}
/// Get current rounding mode
inline int fegetround() {
// see softfloat.h for the definition
switch (SoftFloat::float_rounding_mode) {
case SoftFloat::float_round_down: return FE_DOWNWARD;
case SoftFloat::float_round_up: return FE_UPWARD;
case SoftFloat::float_round_to_zero: return FE_TOWARDZERO;
default:; // is also initial mode
}
// case SoftFloat::float_round_nearest_even:
return FE_TONEAREST;
}
/// Set a new rounding mode
inline int fesetround(FPU_RoundMode roundMode) {
// see softfloat.h for the definition
switch (roundMode) {
case FE_DOWNWARD: SoftFloat::float_rounding_mode = SoftFloat::float_round_down; return 0;
case FE_UPWARD: SoftFloat::float_rounding_mode = SoftFloat::float_round_up; return 0;
case FE_TOWARDZERO: SoftFloat::float_rounding_mode = SoftFloat::float_round_to_zero; return 0;
case FE_TONEAREST: SoftFloat::float_rounding_mode = SoftFloat::float_round_nearest_even; return 0;
}
// Error, invalid mode
return 1;
}
/// SoftFloat environment comprises non-volatile state variables
struct fpenv_t {
char tininess;
char rounding_mode;
int exception_realtraps;
};
/// Default env. Defined in SMath.cpp, initialized to some invalid value for detection
extern fpenv_t FE_DFL_ENV;
/// Get FP env into the given structure
inline int fegetenv(fpenv_t *envp) {
// check that default env exists, otherwise save it now
if (FE_DFL_ENV.tininess==42) {
// First use: save default environment now
FE_DFL_ENV.tininess = SoftFloat::float_detect_tininess;
FE_DFL_ENV.rounding_mode = SoftFloat::float_rounding_mode;
FE_DFL_ENV.exception_realtraps = SoftFloat::float_exception_realtraps;
}
// Now get the current env in the given argument
envp->tininess = SoftFloat::float_detect_tininess;
envp->rounding_mode = SoftFloat::float_rounding_mode;
envp->exception_realtraps = SoftFloat::float_exception_realtraps;
return 0;
}
/// Sets FP env from the given structure
inline int fesetenv(const fpenv_t *envp) {
// check that default env exists, otherwise save it now
if (FE_DFL_ENV.tininess==42) {
// First use: save default environment now
FE_DFL_ENV.tininess = SoftFloat::float_detect_tininess;
FE_DFL_ENV.rounding_mode = SoftFloat::float_rounding_mode;
FE_DFL_ENV.exception_realtraps = SoftFloat::float_exception_realtraps;
}
// Now get the current env in the given argument
SoftFloat::float_detect_tininess = envp->tininess;
SoftFloat::float_rounding_mode = envp->rounding_mode;
SoftFloat::float_exception_realtraps = envp->exception_realtraps;
return 0;
}
/// get env and clear exceptions
inline int feholdexcept(fpenv_t *envp) {
fegetenv(envp);
feclearexcept(FE_ALL_EXCEPT);
return 0;
}
template<typename T> inline void streflop_init() {
// Do nothing by default, or for unknown types
}
/// Initialize the FPU for the different types
/// this may also be called to switch between code sections using
/// different precisions
template<> inline void streflop_init<Simple>() {
}
template<> inline void streflop_init<Double>() {
}
template<> inline void streflop_init<Extended>() {
}
#else // defined(STREFLOP_X87)
#error STREFLOP: Invalid combination or unknown FPU type.
#endif // defined(STREFLOP_X87)
}
#endif // STREFLOP_FPU_H
|