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
|
#ifndef FENV_PRIVATE_H
#define FENV_PRIVATE_H 1
#include <fenv.h>
#include <fpu_control.h>
#ifdef __SSE2_MATH__
# define math_opt_barrier(x) \
({ __typeof(x) __x; \
if (sizeof (x) <= sizeof (double)) \
__asm ("" : "=x" (__x) : "0" (x)); \
else \
__asm ("" : "=t" (__x) : "0" (x)); \
__x; })
# define math_force_eval(x) \
do { \
if (sizeof (x) <= sizeof (double)) \
__asm __volatile ("" : : "x" (x)); \
else \
__asm __volatile ("" : : "f" (x)); \
} while (0)
#else
# define math_opt_barrier(x) \
({ __typeof (x) __x; \
__asm ("" : "=t" (__x) : "0" (x)); \
__x; })
# define math_force_eval(x) \
do { \
__typeof (x) __x = (x); \
if (sizeof (x) <= sizeof (double)) \
__asm __volatile ("" : : "m" (__x)); \
else \
__asm __volatile ("" : : "f" (__x)); \
} while (0)
#endif
/* This file is used by both the 32- and 64-bit ports. The 64-bit port
has a field in the fenv_t for the mxcsr; the 32-bit port does not.
Instead, we (ab)use the only 32-bit field extant in the struct. */
#ifndef __x86_64__
# define __mxcsr __eip
#endif
/* All of these functions are private to libm, and are all used in pairs
to save+change the fp state and restore the original state. Thus we
need not care for both the 387 and the sse unit, only the one we're
actually using. */
#if defined __AVX__ || defined SSE2AVX
# define STMXCSR "vstmxcsr"
# define LDMXCSR "vldmxcsr"
#else
# define STMXCSR "stmxcsr"
# define LDMXCSR "ldmxcsr"
#endif
static __always_inline void
libc_feholdexcept_sse (fenv_t *e)
{
unsigned int mxcsr;
asm (STMXCSR " %0" : "=m" (*&mxcsr));
e->__mxcsr = mxcsr;
mxcsr = (mxcsr | 0x1f80) & ~0x3f;
asm volatile (LDMXCSR " %0" : : "m" (*&mxcsr));
}
static __always_inline void
libc_feholdexcept_387 (fenv_t *e)
{
/* Recall that fnstenv has a side-effect of masking exceptions.
Clobber all of the fp registers so that the TOS field is 0. */
asm volatile ("fnstenv %0; fnclex"
: "=m"(*e)
: : "st", "st(1)", "st(2)", "st(3)",
"st(4)", "st(5)", "st(6)", "st(7)");
}
static __always_inline void
libc_fesetround_sse (int r)
{
unsigned int mxcsr;
asm (STMXCSR " %0" : "=m" (*&mxcsr));
mxcsr = (mxcsr & ~0x6000) | (r << 3);
asm volatile (LDMXCSR " %0" : : "m" (*&mxcsr));
}
static __always_inline void
libc_fesetround_387 (int r)
{
fpu_control_t cw;
_FPU_GETCW (cw);
cw = (cw & ~0xc00) | r;
_FPU_SETCW (cw);
}
static __always_inline void
libc_feholdexcept_setround_sse (fenv_t *e, int r)
{
unsigned int mxcsr;
asm (STMXCSR " %0" : "=m" (*&mxcsr));
e->__mxcsr = mxcsr;
mxcsr = ((mxcsr | 0x1f80) & ~0x603f) | (r << 3);
asm volatile (LDMXCSR " %0" : : "m" (*&mxcsr));
}
/* Set both rounding mode and precision. A convenience function for use
by libc_feholdexcept_setround and libc_feholdexcept_setround_53bit. */
static __always_inline void
libc_feholdexcept_setround_387_prec (fenv_t *e, int r)
{
libc_feholdexcept_387 (e);
fpu_control_t cw = e->__control_word;
cw &= ~(_FPU_RC_ZERO | _FPU_EXTENDED);
cw |= r | 0x3f;
_FPU_SETCW (cw);
}
static __always_inline void
libc_feholdexcept_setround_387 (fenv_t *e, int r)
{
libc_feholdexcept_setround_387_prec (e, r | _FPU_EXTENDED);
}
static __always_inline void
libc_feholdexcept_setround_387_53bit (fenv_t *e, int r)
{
libc_feholdexcept_setround_387_prec (e, r | _FPU_DOUBLE);
}
static __always_inline int
libc_fetestexcept_sse (int e)
{
unsigned int mxcsr;
asm volatile (STMXCSR " %0" : "=m" (*&mxcsr));
return mxcsr & e & FE_ALL_EXCEPT;
}
static __always_inline int
libc_fetestexcept_387 (int ex)
{
fexcept_t temp;
asm volatile ("fnstsw %0" : "=a" (temp));
return temp & ex & FE_ALL_EXCEPT;
}
static __always_inline void
libc_fesetenv_sse (fenv_t *e)
{
asm volatile (LDMXCSR " %0" : : "m" (e->__mxcsr));
}
static __always_inline void
libc_fesetenv_387 (fenv_t *e)
{
/* Clobber all fp registers so that the TOS value we saved earlier is
compatible with the current state of the compiler. */
asm volatile ("fldenv %0"
: : "m" (*e)
: "st", "st(1)", "st(2)", "st(3)",
"st(4)", "st(5)", "st(6)", "st(7)");
}
static __always_inline int
libc_feupdateenv_test_sse (fenv_t *e, int ex)
{
unsigned int mxcsr, old_mxcsr, cur_ex;
asm volatile (STMXCSR " %0" : "=m" (*&mxcsr));
cur_ex = mxcsr & FE_ALL_EXCEPT;
/* Merge current exceptions with the old environment. */
old_mxcsr = e->__mxcsr;
mxcsr = old_mxcsr | cur_ex;
asm volatile (LDMXCSR " %0" : : "m" (*&mxcsr));
/* Raise SIGFPE for any new exceptions since the hold. Expect that
the normal environment has all exceptions masked. */
if (__glibc_unlikely (~(old_mxcsr >> 7) & cur_ex))
__feraiseexcept (cur_ex);
/* Test for exceptions raised since the hold. */
return cur_ex & ex;
}
static __always_inline int
libc_feupdateenv_test_387 (fenv_t *e, int ex)
{
fexcept_t cur_ex;
/* Save current exceptions. */
asm volatile ("fnstsw %0" : "=a" (cur_ex));
cur_ex &= FE_ALL_EXCEPT;
/* Reload original environment. */
libc_fesetenv_387 (e);
/* Merge current exceptions. */
__feraiseexcept (cur_ex);
/* Test for exceptions raised since the hold. */
return cur_ex & ex;
}
static __always_inline void
libc_feupdateenv_sse (fenv_t *e)
{
libc_feupdateenv_test_sse (e, 0);
}
static __always_inline void
libc_feupdateenv_387 (fenv_t *e)
{
libc_feupdateenv_test_387 (e, 0);
}
static __always_inline void
libc_feholdsetround_sse (fenv_t *e, int r)
{
unsigned int mxcsr;
asm (STMXCSR " %0" : "=m" (*&mxcsr));
e->__mxcsr = mxcsr;
mxcsr = (mxcsr & ~0x6000) | (r << 3);
asm volatile (LDMXCSR " %0" : : "m" (*&mxcsr));
}
static __always_inline void
libc_feholdsetround_387_prec (fenv_t *e, int r)
{
fpu_control_t cw;
_FPU_GETCW (cw);
e->__control_word = cw;
cw &= ~(_FPU_RC_ZERO | _FPU_EXTENDED);
cw |= r;
_FPU_SETCW (cw);
}
static __always_inline void
libc_feholdsetround_387 (fenv_t *e, int r)
{
libc_feholdsetround_387_prec (e, r | _FPU_EXTENDED);
}
static __always_inline void
libc_feholdsetround_387_53bit (fenv_t *e, int r)
{
libc_feholdsetround_387_prec (e, r | _FPU_DOUBLE);
}
static __always_inline void
libc_feresetround_sse (fenv_t *e)
{
unsigned int mxcsr;
asm (STMXCSR " %0" : "=m" (*&mxcsr));
mxcsr = (mxcsr & ~0x6000) | (e->__mxcsr & 0x6000);
asm volatile (LDMXCSR " %0" : : "m" (*&mxcsr));
}
static __always_inline void
libc_feresetround_387 (fenv_t *e)
{
_FPU_SETCW (e->__control_word);
}
#ifdef __SSE_MATH__
# define libc_feholdexceptf libc_feholdexcept_sse
# define libc_fesetroundf libc_fesetround_sse
# define libc_feholdexcept_setroundf libc_feholdexcept_setround_sse
# define libc_fetestexceptf libc_fetestexcept_sse
# define libc_fesetenvf libc_fesetenv_sse
# define libc_feupdateenv_testf libc_feupdateenv_test_sse
# define libc_feupdateenvf libc_feupdateenv_sse
# define libc_feholdsetroundf libc_feholdsetround_sse
# define libc_feresetroundf libc_feresetround_sse
#else
# define libc_feholdexceptf libc_feholdexcept_387
# define libc_fesetroundf libc_fesetround_387
# define libc_feholdexcept_setroundf libc_feholdexcept_setround_387
# define libc_fetestexceptf libc_fetestexcept_387
# define libc_fesetenvf libc_fesetenv_387
# define libc_feupdateenv_testf libc_feupdateenv_test_387
# define libc_feupdateenvf libc_feupdateenv_387
# define libc_feholdsetroundf libc_feholdsetround_387
# define libc_feresetroundf libc_feresetround_387
#endif /* __SSE_MATH__ */
#ifdef __SSE2_MATH__
# define libc_feholdexcept libc_feholdexcept_sse
# define libc_fesetround libc_fesetround_sse
# define libc_feholdexcept_setround libc_feholdexcept_setround_sse
# define libc_fetestexcept libc_fetestexcept_sse
# define libc_fesetenv libc_fesetenv_sse
# define libc_feupdateenv_test libc_feupdateenv_test_sse
# define libc_feupdateenv libc_feupdateenv_sse
# define libc_feholdsetround libc_feholdsetround_sse
# define libc_feresetround libc_feresetround_sse
#else
# define libc_feholdexcept libc_feholdexcept_387
# define libc_fesetround libc_fesetround_387
# define libc_feholdexcept_setround libc_feholdexcept_setround_387
# define libc_fetestexcept libc_fetestexcept_387
# define libc_fesetenv libc_fesetenv_387
# define libc_feupdateenv_test libc_feupdateenv_test_387
# define libc_feupdateenv libc_feupdateenv_387
# define libc_feholdsetround libc_feholdsetround_387
# define libc_feresetround libc_feresetround_387
#endif /* __SSE2_MATH__ */
#define libc_feholdexceptl libc_feholdexcept_387
#define libc_fesetroundl libc_fesetround_387
#define libc_feholdexcept_setroundl libc_feholdexcept_setround_387
#define libc_fetestexceptl libc_fetestexcept_387
#define libc_fesetenvl libc_fesetenv_387
#define libc_feupdateenv_testl libc_feupdateenv_test_387
#define libc_feupdateenvl libc_feupdateenv_387
#define libc_feholdsetroundl libc_feholdsetround_387
#define libc_feresetroundl libc_feresetround_387
#ifndef __SSE2_MATH__
# define libc_feholdexcept_setround_53bit libc_feholdexcept_setround_387_53bit
# define libc_feholdsetround_53bit libc_feholdsetround_387_53bit
#endif
/* We have support for rounding mode context. */
#define HAVE_RM_CTX 1
static __always_inline void
libc_feholdexcept_setround_sse_ctx (struct rm_ctx *ctx, int r)
{
unsigned int mxcsr, new_mxcsr;
asm (STMXCSR " %0" : "=m" (*&mxcsr));
new_mxcsr = ((mxcsr | 0x1f80) & ~0x603f) | (r << 3);
ctx->env.__mxcsr = mxcsr;
if (__glibc_unlikely (mxcsr != new_mxcsr))
{
asm volatile (LDMXCSR " %0" : : "m" (*&new_mxcsr));
ctx->updated_status = true;
}
else
ctx->updated_status = false;
}
/* Unconditional since we want to overwrite any exceptions that occurred in the
context. This is also why all fehold* functions unconditionally write into
ctx->env. */
static __always_inline void
libc_fesetenv_sse_ctx (struct rm_ctx *ctx)
{
libc_fesetenv_sse (&ctx->env);
}
static __always_inline void
libc_feupdateenv_sse_ctx (struct rm_ctx *ctx)
{
if (__glibc_unlikely (ctx->updated_status))
libc_feupdateenv_test_sse (&ctx->env, 0);
}
static __always_inline void
libc_feholdexcept_setround_387_prec_ctx (struct rm_ctx *ctx, int r)
{
libc_feholdexcept_387 (&ctx->env);
fpu_control_t cw = ctx->env.__control_word;
fpu_control_t old_cw = cw;
cw &= ~(_FPU_RC_ZERO | _FPU_EXTENDED);
cw |= r | 0x3f;
if (__glibc_unlikely (old_cw != cw))
{
_FPU_SETCW (cw);
ctx->updated_status = true;
}
else
ctx->updated_status = false;
}
static __always_inline void
libc_feholdexcept_setround_387_ctx (struct rm_ctx *ctx, int r)
{
libc_feholdexcept_setround_387_prec_ctx (ctx, r | _FPU_EXTENDED);
}
static __always_inline void
libc_feholdexcept_setround_387_53bit_ctx (struct rm_ctx *ctx, int r)
{
libc_feholdexcept_setround_387_prec_ctx (ctx, r | _FPU_DOUBLE);
}
static __always_inline void
libc_feholdsetround_387_prec_ctx (struct rm_ctx *ctx, int r)
{
fpu_control_t cw, new_cw;
_FPU_GETCW (cw);
new_cw = cw;
new_cw &= ~(_FPU_RC_ZERO | _FPU_EXTENDED);
new_cw |= r;
ctx->env.__control_word = cw;
if (__glibc_unlikely (new_cw != cw))
{
_FPU_SETCW (new_cw);
ctx->updated_status = true;
}
else
ctx->updated_status = false;
}
static __always_inline void
libc_feholdsetround_387_ctx (struct rm_ctx *ctx, int r)
{
libc_feholdsetround_387_prec_ctx (ctx, r | _FPU_EXTENDED);
}
static __always_inline void
libc_feholdsetround_387_53bit_ctx (struct rm_ctx *ctx, int r)
{
libc_feholdsetround_387_prec_ctx (ctx, r | _FPU_DOUBLE);
}
static __always_inline void
libc_feholdsetround_sse_ctx (struct rm_ctx *ctx, int r)
{
unsigned int mxcsr, new_mxcsr;
asm (STMXCSR " %0" : "=m" (*&mxcsr));
new_mxcsr = (mxcsr & ~0x6000) | (r << 3);
ctx->env.__mxcsr = mxcsr;
if (__glibc_unlikely (new_mxcsr != mxcsr))
{
asm volatile (LDMXCSR " %0" : : "m" (*&new_mxcsr));
ctx->updated_status = true;
}
else
ctx->updated_status = false;
}
static __always_inline void
libc_feresetround_sse_ctx (struct rm_ctx *ctx)
{
if (__glibc_unlikely (ctx->updated_status))
libc_feresetround_sse (&ctx->env);
}
static __always_inline void
libc_feresetround_387_ctx (struct rm_ctx *ctx)
{
if (__glibc_unlikely (ctx->updated_status))
_FPU_SETCW (ctx->env.__control_word);
}
static __always_inline void
libc_feupdateenv_387_ctx (struct rm_ctx *ctx)
{
if (__glibc_unlikely (ctx->updated_status))
libc_feupdateenv_test_387 (&ctx->env, 0);
}
#ifdef __SSE_MATH__
# define libc_feholdexcept_setroundf_ctx libc_feholdexcept_setround_sse_ctx
# define libc_fesetenvf_ctx libc_fesetenv_sse_ctx
# define libc_feupdateenvf_ctx libc_feupdateenv_sse_ctx
# define libc_feholdsetroundf_ctx libc_feholdsetround_sse_ctx
# define libc_feresetroundf_ctx libc_feresetround_sse_ctx
#else
# define libc_feholdexcept_setroundf_ctx libc_feholdexcept_setround_387_ctx
# define libc_feupdateenvf_ctx libc_feupdateenv_387_ctx
# define libc_feholdsetroundf_ctx libc_feholdsetround_387_ctx
# define libc_feresetroundf_ctx libc_feresetround_387_ctx
#endif /* __SSE_MATH__ */
#ifdef __SSE2_MATH__
# define libc_feholdexcept_setround_ctx libc_feholdexcept_setround_sse_ctx
# define libc_fesetenv_ctx libc_fesetenv_sse_ctx
# define libc_feupdateenv_ctx libc_feupdateenv_sse_ctx
# define libc_feholdsetround_ctx libc_feholdsetround_sse_ctx
# define libc_feresetround_ctx libc_feresetround_sse_ctx
#else
# define libc_feholdexcept_setround_ctx libc_feholdexcept_setround_387_ctx
# define libc_feupdateenv_ctx libc_feupdateenv_387_ctx
# define libc_feholdsetround_ctx libc_feholdsetround_387_ctx
# define libc_feresetround_ctx libc_feresetround_387_ctx
#endif /* __SSE2_MATH__ */
#define libc_feholdexcept_setroundl_ctx libc_feholdexcept_setround_387_ctx
#define libc_feupdateenvl_ctx libc_feupdateenv_387_ctx
#define libc_feholdsetroundl_ctx libc_feholdsetround_387_ctx
#define libc_feresetroundl_ctx libc_feresetround_387_ctx
#ifndef __SSE2_MATH__
# define libc_feholdsetround_53bit_ctx libc_feholdsetround_387_53bit_ctx
# define libc_feresetround_53bit_ctx libc_feresetround_387_ctx
#endif
#undef __mxcsr
#endif /* FENV_PRIVATE_H */
|