Branch
Hash :
f3b9112a
Author :
Date :
2025-06-11T14:43:04
fenv*: Fix compilation error with mingw 13. It's caused by an ABI change in mingw: <https://sourceforge.net/p/mingw-w64/mingw-w64/ci/5c5973cf5f021db8fd75e9667e63881ccd169320/>. Reported by Collin Funk in <https://lists.gnu.org/archive/html/bug-gnulib/2025-04/msg00215.html>. * m4/fenv-environment.m4 (gl_FENV_ENVIRONMENT): Update comments for mingw 13. * lib/fenv-private.h (exceptions_to_x86hardware, x86hardware_to_exceptions): On mingw >= 13, define these like on MSVC. * lib/fenv-round.c (fegetround, fesetround): Do the safe mapping also on mingw >= 13. * lib/fenv-except-state-set.c (fesetexceptflag): Do the exceptions_to_x86hardware conversion also on other platforms than MSVC. * lib/fenv-except-tracking-clear.c (feclearexcept): Likewise. * lib/fenv-except-tracking-set.c (fesetexcept): Likewise. * lib/fenv-except-trapping.c (feenableexcept, fedisableexcept, fegetexcept): Do the exceptions_to_x86hardware and x86hardware_to_exceptions conversions also on other platforms than MSVC. * lib/fenv-env.c (fegetenv, fesetenv): Add new implementation for mingw >= 13. * doc/posix-functions/fesetenv.texi: Mention the new mingw bug. * doc/posix-functions/feupdateenv.texi: Mention the new mingw bug.
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 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
/* Functions for controlling the floating-point environment as a whole.
Copyright (C) 1997-2025 Free Software Foundation, Inc.
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Based on glibc/sysdeps/<cpu>/{fegetenv.c,fesetenv.c}
together with glibc/sysdeps/<cpu>/{fpu_control.h,fenv_private.h,fenv_libc.h}. */
#include <config.h>
/* Specification. */
#include <fenv.h>
#include "fenv-private.h"
#include "verify.h"
/* The big problem of the fegetenv/fesetenv API is that it is not well-defined
which parts of the floating-point environment need to be restored by fesetenv
and which don't. For example:
- On x86_64 and i386, some platforms restore all the x86_387_fenv_t,
whereas others restore only the control word and the status words.
- On m68k, musl libc restores the fpiar register value, whereas glibc
doesn't.
Some platforms don't even do this consistently. For example, musl libc on
sh4 restores the precision mode bit (fpscr bit 19) during an fesetenv of a
previously saved environment, but not during an fesetenv (FE_DFL_ENV)
invocation. */
#if defined _AIX && defined __powerpc__ /* AIX */
/* On AIX, fenv_t is a struct { unsigned short rmode; unsigned int fpstat, trapstate; }. */
/* On AIX, the register fpscr is augmented with a 32-bit word named fpscrx
in thread-local storage. Therefore AIX does complex things behind the scene,
and its best to use the system-provided fegetenv() and fesetenv() functions
and just add the missing behaviour, namely saving and restoring the exception
trap bits. */
# include <float.h>
# include <fpxcp.h>
# include <fptrap.h>
int
fegetenv (fenv_t *envp)
#undef fegetenv
{
/* Invoke the original fegetenv(), and additionally save the exception trap
bits (fpscr bits 7..3). */
union { unsigned long long u; double f; } memenv;
_FPU_GETCW_AS_DOUBLE (memenv.f);
fegetenv (envp);
/* AIX's original fegetenv() sets envp->trapstate = 0. */
envp->trapstate = FE_ALL_EXCEPT & ((unsigned int) memenv.u << 22);
return 0;
}
int
fesetenv (fenv_t const *envp)
#undef fesetenv
{
/* Invoke the original fesetenv(), and additionally restore the exception trap
bits (fpscr bits 7..3). */
if (fesetenv (envp) != 0)
return -1;
union { unsigned long long u; double f; } orig_memenv, memenv;
_FPU_GETCW_AS_DOUBLE (orig_memenv.f);
unsigned int trapbits;
if (envp == FE_DFL_ENV)
trapbits = 0;
else
trapbits = (envp->trapstate & FE_ALL_EXCEPT) >> 22;
memenv.u = (orig_memenv.u & ~0x000000f8) | trapbits;
if (!(memenv.u == orig_memenv.u))
{
if ((orig_memenv.u & 0x000000f8) == 0 && (memenv.u & 0x000000f8) != 0)
{
/* Put the thread into precise trapping mode. */
/* Documentation: <https://www.ibm.com/docs/en/aix/7.3?topic=f-fp-trap-subroutine> */
fp_trap (FP_TRAP_SYNC);
}
_FPU_SETCW_AS_DOUBLE (memenv.f);
if ((orig_memenv.u & 0x000000f8) != 0 && (memenv.u & 0x000000f8) == 0)
{
/* Put the thread into no-trapping mode. */
/* Documentation: <https://www.ibm.com/docs/en/aix/7.3?topic=f-fp-trap-subroutine> */
fp_trap (FP_TRAP_OFF);
}
}
return 0;
}
#elif defined __GNUC__ || defined __clang__ || defined _MSC_VER
# if (defined __x86_64__ || defined _M_X64) || (defined __i386 || defined _M_IX86)
/* On all OSes except MSVC, mingw ≥ 13, macOS, Solaris, fenv_t is
binary-equivalent to
- either a x86_387_fenv_t (7 'unsigned int' words)
where mxcsr is stored:
- for glibc/i386: in __eip = more[1].
- for musl libc/i386: the lower 6 bits ORed into __status_word.
- for FreeBSD/i386, Haiku/i386, mingw/i386:
the upper 16 bits in __reserved1, the lower 16 bits in __reserved2.
- or a struct { x86_387_fenv_t 387; unsigned int mxcsr; }
On MSVC, it's a
struct { unsigned int _Fe_ctl, _Fe_stat; }, where
_Fe_ctl bits 9..8 are the rounding direction,
_Fe_ctl bits 4..0 are the exception trap bits (inverted),
_Fe_stat bits 4..0 are the exception flags.
On mingw ≥ 13, it's a
struct { unsigned int _Fe_ctl, _Fe_stat; }, where
_Fe_ctl bits 9..8 are the rounding direction,
_Fe_ctl bits 23..22 are the rounding direction
from the 387 compatible floating-point unit,
_Fe_ctl bits 31..30 are the rounding direction
from the SSE compatible floating-point unit,
_Fe_ctl bits 4..0 are the exception trap bits (inverted),
_Fe_ctl bits 20..16 are the exception trap bits (inverted)
from the 387 compatible floating-point unit,
_Fe_ctl bits 28..24 are the exception trap bits (inverted)
from the SSE compatible floating-point unit,
_Fe_stat bits 4..0 are the exception flags,
_Fe_stat bits 20..16 are the exception flags
from the 387 compatible floating-point unit,
_Fe_stat bits 28..24 are the exception flags
from the SSE compatible floating-point unit.
On macOS, it's a
struct { unsigned short __control, __status; unsigned int __mxcsr; ... }.
On Solaris, it's a
struct { __fex_handler_t __handlers; unsigned long __fsr; }. */
/* Like FE_ALL_EXCEPT, except that it also includes FE_DENORMAL. */
# define _FE_ALL_EXCEPT (0x3F * FE_INVALID)
int
fegetenv (fenv_t *envp)
{
# if defined _MSC_VER
unsigned int mxcsr;
_FPU_GETSSECW (mxcsr);
envp->_Fe_ctl = ((mxcsr & 0x6000) >> 5) /* rounding direction */
/* exception trap bits (inverted): */
| (mxcsr & (1 << 7) ? FE_INVALID : 0)
| (mxcsr & (1 << 9) ? FE_DIVBYZERO : 0)
| (mxcsr & (1 << 10) ? FE_OVERFLOW : 0)
| (mxcsr & (1 << 11) ? FE_UNDERFLOW : 0)
| (mxcsr & (1 << 12) ? FE_INEXACT : 0);
envp->_Fe_stat = /* exception flags: */
(mxcsr & (1 << 0) ? FE_INVALID : 0)
| (mxcsr & (1 << 2) ? FE_DIVBYZERO : 0)
| (mxcsr & (1 << 3) ? FE_OVERFLOW : 0)
| (mxcsr & (1 << 4) ? FE_UNDERFLOW : 0)
| (mxcsr & (1 << 5) ? FE_INEXACT : 0);
# elif (defined __MINGW32__ && FE_INVALID != 0x01) /* mingw ≥ 13 */
x86_387_fenv_t env387;
__asm__ __volatile__ ("fnstenv %0" : "=m" (*&env387));
/* Note: fnstenv masks all floating-point exceptions until the fldcw
below. */
__asm__ __volatile__ ("fldcw %0" : : "m" (env387.__control_word));
/* rounding direction */
unsigned int round_387 = (env387.__control_word & 0x0C00) >> 2;
/* exception trap bits (inverted) */
unsigned int trapbits_387 = x86hardware_to_exceptions (env387.__control_word);
/* exception flags */
unsigned int excflags_387 = x86hardware_to_exceptions (env387.__status_word);
unsigned int mxcsr;
_FPU_GETSSECW (mxcsr);
/* rounding direction */
unsigned int round_sse = (mxcsr & 0x6000) >> 5;
/* exception trap bits (inverted) */
unsigned int trapbits_sse = x86hardware_to_exceptions (mxcsr >> 7);
/* exception flags */
unsigned int excflags_sse = x86hardware_to_exceptions (mxcsr);
envp->_Fe_ctl =
(round_sse << 22) | (round_387 << 14) | round_sse | round_387
| (trapbits_sse << 24) | (trapbits_387 << 16) | trapbits_sse | trapbits_387;
envp->_Fe_stat =
(excflags_sse << 24) | (excflags_387 << 16) | excflags_sse | excflags_387;
# elif defined __APPLE__ && defined __MACH__ /* macOS */
_FPU_GETCW (envp->__control);
_FPU_GETSTAT (envp->__status);
_FPU_GETSSECW (envp->__mxcsr);
# elif defined __sun /* Solaris */
fex_getexcepthandler (&envp->__handlers, FEX_ALL);
unsigned short fctrl;
unsigned short fstat;
unsigned int mxcsr;
_FPU_GETCW (fctrl);
_FPU_GETSTAT (fstat);
_FPU_GETSSECW (mxcsr);
envp->__fsr =
(((unsigned int) (fctrl ^ 0x3F) & ~0xE0C0U) << 16)
| ((unsigned int) fstat | (mxcsr & 0x3F));
# else
verify (sizeof (fenv_t) >= sizeof (x86_387_fenv_t));
__asm__ __volatile__ ("fnstenv %0" : "=m" (* (x86_387_fenv_t *) envp));
/* Note: fnstenv masks all floating-point exceptions until the fldcw
below. */
__asm__ __volatile__ ("fldcw %0" : : "m" (((x86_387_fenv_t *) envp)->__control_word));
unsigned int mxcsr;
_FPU_GETSSECW (mxcsr);
if (sizeof (fenv_t) > sizeof (x86_387_fenv_t))
{
/* Store mxcsr in the struct field after the x86_387_fenv_t. */
* (unsigned int *) ((x86_387_fenv_t *) envp + 1) = mxcsr;
}
else
{
# if defined __GLIBC__
((x86_387_fenv_t *) envp)->more[1] = mxcsr;
# elif MUSL_LIBC
((x86_387_fenv_t *) envp)->__status_word |= mxcsr & 0x3F;
# else
((x86_387_fenv_t *) envp)->__reserved1 = mxcsr >> 16;
((x86_387_fenv_t *) envp)->__reserved2 = mxcsr & 0xFFFF;
# endif
}
# endif
return 0;
}
int
fesetenv (fenv_t const *envp)
{
# if defined _MSC_VER
unsigned int mxcsr;
_FPU_GETSSECW (mxcsr);
mxcsr &= ~(0x6000 | (0x3F << 7) | 0x3F);
/* On MSVC, FE_DFL_ENV is the address of a global variable.
Let's ignore this variable. */
if (envp == FE_DFL_ENV)
/* Default: rounding direction = FE_TONEAREST, exception trap bits = 0,
exception flags = 0. */
mxcsr = mxcsr | (0x3F << 7);
else
mxcsr = mxcsr
| ((envp->_Fe_ctl & 0x300) << 5) /* rounding direction */
/* exception trap bits (inverted): */
| (envp->_Fe_ctl & FE_INVALID ? 1 << 7 : 0)
| (envp->_Fe_ctl & FE_DIVBYZERO ? 1 << 9 : 0)
| (envp->_Fe_ctl & FE_OVERFLOW ? 1 << 10 : 0)
| (envp->_Fe_ctl & FE_UNDERFLOW ? 1 << 11 : 0)
| (envp->_Fe_ctl & FE_INEXACT ? 1 << 12 : 0)
/* exception flags: */
| (envp->_Fe_stat & FE_INVALID ? 1 << 0 : 0)
| (envp->_Fe_stat & FE_DIVBYZERO ? 1 << 2 : 0)
| (envp->_Fe_stat & FE_OVERFLOW ? 1 << 3 : 0)
| (envp->_Fe_stat & FE_UNDERFLOW ? 1 << 4 : 0)
| (envp->_Fe_stat & FE_INEXACT ? 1 << 5 : 0);
_FPU_SETSSECW (mxcsr);
# elif (defined __MINGW32__ && FE_INVALID != 0x01) /* mingw ≥ 13 */
unsigned short env_fctrl;
unsigned short env_fstat;
unsigned int env_mxcsr;
/* On mingw, FE_DFL_ENV is NULL. */
if (envp == FE_DFL_ENV)
{
env_fctrl = 0x3F;
env_fstat = 0;
env_mxcsr = 0x3F << 7;
}
else
{
/* rounding direction */
unsigned int round_387 = (envp->_Fe_ctl >> 14) & 0x0300;
unsigned int round_sse = (envp->_Fe_ctl >> 22) & 0x0300;
/* exception trap bits (inverted) */
unsigned int trapbits_387 = (envp->_Fe_ctl >> 16) & 0x1F;
unsigned int trapbits_sse = (envp->_Fe_ctl >> 24) & 0x1F;
/* exception flags */
unsigned int excflags_387 = (envp->_Fe_stat >> 16) & 0x1F;
unsigned int excflags_sse = (envp->_Fe_stat >> 24) & 0x1F;
env_fctrl = (round_387 << 2) | exceptions_to_x86hardware (trapbits_387);
env_fstat = exceptions_to_x86hardware (excflags_387);
env_mxcsr = (round_sse << 5)
| (exceptions_to_x86hardware (trapbits_sse) << 7)
| exceptions_to_x86hardware (excflags_sse);
}
/* In the SSE unit. */
unsigned int mxcsr = env_mxcsr;
_FPU_SETSSECW (mxcsr);
/* In the 387 unit. */
x86_387_fenv_t env387;
__asm__ __volatile__ ("fnstenv %0" : "=m" (*&env387));
/* Note: fnstenv masks all floating-point exceptions until the fldenv
below. */
env387.__control_word = env_fctrl;
env387.__status_word = env_fstat;
__asm__ __volatile__ ("fldenv %0" : : "m" (*&env387));
# elif defined __APPLE__ && defined __MACH__ /* macOS */
unsigned short fctrl;
unsigned short fstat;
unsigned int mxcsr;
/* On macOS, FE_DFL_ENV is the address of a global variable;
no special code is needed in this case. */
if (FE_DFL_ENV == (const fenv_t *) (-1) && envp == FE_DFL_ENV)
{
fctrl = 0x3F;
fstat = 0;
mxcsr = 0x3F << 7;
}
else
{
fctrl = envp->__control;
fstat = envp->__status;
mxcsr = envp->__mxcsr;
}
/* In the SSE unit. */
_FPU_SETSSECW (mxcsr);
/* In the 387 unit. */
x86_387_fenv_t env387;
__asm__ __volatile__ ("fnstenv %0" : "=m" (*&env387));
/* Note: fnstenv masks all floating-point exceptions until the fldenv
below. */
env387.__control_word = fctrl;
env387.__status_word = fstat;
__asm__ __volatile__ ("fldenv %0" : : "m" (*&env387));
# elif defined __sun /* Solaris */
unsigned short env_fctrl;
unsigned int env_mxcsr;
/* On Solaris, FE_DFL_ENV is the address of a global variable;
no special code is needed in this case. */
if (FE_DFL_ENV == (const fenv_t *) (-1) && envp == FE_DFL_ENV)
{
env_fctrl = 0x3F;
env_mxcsr = 0x3F << 7;
}
else
{
fex_setexcepthandler (&envp->__handlers, FEX_ALL);
env_fctrl = (envp->__fsr >> 16) ^ 0x3F;
env_mxcsr = envp->__fsr & 0x3F;
}
/* Store the exception flags in mxcsr, not in env387.__status_word. */
/* In the SSE unit. */
unsigned int mxcsr;
_FPU_GETSSECW (mxcsr);
mxcsr = (mxcsr & ~0x3F) | env_mxcsr;
_FPU_SETSSECW (mxcsr);
/* In the 387 unit. */
x86_387_fenv_t env387;
__asm__ __volatile__ ("fnstenv %0" : "=m" (*&env387));
/* Note: fnstenv masks all floating-point exceptions until the fldenv
below. */
env387.__control_word = env_fctrl;
env387.__status_word &= ~0x3F;
__asm__ __volatile__ ("fldenv %0" : : "m" (*&env387));
# else
verify (sizeof (fenv_t) >= sizeof (x86_387_fenv_t));
unsigned short env_fctrl;
unsigned short env_fstat;
unsigned int env_mxcsr;
/* On *BSD, Solaris, Cygwin, Android, Haiku, Minix, FE_DFL_ENV is the address
of a global variable; no special code is needed in this case.
But on mingw, FE_DFL_ENV is NULL. */
if (envp == FE_DFL_ENV)
{
env_fctrl = 0x3F;
env_fstat = 0;
env_mxcsr = 0x3F << 7;
}
else
{
env_fctrl = ((x86_387_fenv_t const *) envp)->__control_word;
env_fstat = ((x86_387_fenv_t const *) envp)->__status_word;
if (sizeof (fenv_t) > sizeof (x86_387_fenv_t))
env_mxcsr = * (unsigned int const *) ((x86_387_fenv_t const *) envp + 1);
else
{
# if defined __GLIBC__
env_mxcsr = ((x86_387_fenv_t const *) envp)->more[1];
# elif MUSL_LIBC
env_mxcsr = env_fstat & 0x3F;
# else
env_mxcsr = (((x86_387_fenv_t const *) envp)->__reserved1 << 16)
| ((x86_387_fenv_t const *) envp)->__reserved2;
# endif
}
}
/* In the SSE unit. */
unsigned int mxcsr = env_mxcsr;
_FPU_SETSSECW (mxcsr);
/* In the 387 unit. */
x86_387_fenv_t env387;
__asm__ __volatile__ ("fnstenv %0" : "=m" (*&env387));
/* Note: fnstenv masks all floating-point exceptions until the fldenv
below. */
env387.__control_word = env_fctrl;
env387.__status_word = env_fstat;
__asm__ __volatile__ ("fldenv %0" : : "m" (*&env387));
# endif
return 0;
}
# elif defined __aarch64__ /* arm64 */
/* On all OSes except FreeBSD and macOS, fenv_t is binary-equivalent to a
struct { unsigned int fpcr, fpsr; }.
On FreeBSD, it's binary-equivalent to a
struct { unsigned int fpsr, fpcr; }, i.e. the fields are swapped.
On macOS, it's binary-equivalent to a
struct { unsigned long fpsr, fpcr; }. */
int
fegetenv (fenv_t *envp)
{
unsigned long fpcr;
unsigned long fpsr;
_FPU_GETCW (fpcr);
_FPU_GETFPSR (fpsr);
# if defined __APPLE__ && defined __MACH__
((unsigned long *) envp)[0] = fpsr;
((unsigned long *) envp)[1] = fpcr;
# elif defined __FreeBSD__
((unsigned int *) envp)[0] = fpsr;
((unsigned int *) envp)[1] = fpcr;
# else
((unsigned int *) envp)[0] = fpcr;
((unsigned int *) envp)[1] = fpsr;
# endif
return 0;
}
int
fesetenv (fenv_t const *envp)
{
unsigned long orig_fpcr, fpcr;
unsigned long orig_fpsr, fpsr;
_FPU_GETCW (orig_fpcr);
_FPU_GETFPSR (orig_fpsr);
/* On *BSD and Android, FE_DFL_ENV is the address of a global variable;
no special code is needed in this case. */
if (FE_DFL_ENV == (const fenv_t *) (-1) && envp == FE_DFL_ENV)
{
/* Default: rounding direction = FE_TONEAREST, exceptions trap bits = 0. */
fpcr = (orig_fpcr & 0xFE0FE0F8U) | 0x00000000U;
/* Default: exception flags = 0. */
fpsr = (orig_fpsr & 0x0FFFFFE0U) | 0x00000000U;
}
else
{
# if defined __APPLE__ && defined __MACH__
fpsr = ((unsigned long const *) envp)[0];
fpcr = ((unsigned long const *) envp)[1];
# elif defined __FreeBSD__
fpsr = ((unsigned int const *) envp)[0];
fpcr = ((unsigned int const *) envp)[1];
# else
fpcr = ((unsigned int const *) envp)[0];
fpsr = ((unsigned int const *) envp)[1];
# endif
}
if (fpcr != orig_fpcr)
_FPU_SETCW (fpcr);
if (fpsr != orig_fpsr)
_FPU_SETFPSR (fpsr);
return 0;
}
# elif defined __arm__
/* On all OSes except OpenBSD, fenv_t is binary-equivalent to an 'unsigned int'.
On OpenBSD, it's a struct { unsigned int __sticky, __mask, __round; }. */
int
fegetenv (fenv_t *envp)
{
# ifdef __SOFTFP__
return -1;
# else
unsigned int fpscr;
_FPU_GETCW (fpscr);
# if defined __OpenBSD__
envp->__sticky = fpscr & FE_ALL_EXCEPT;
envp->__mask = (fpscr >> 8) & FE_ALL_EXCEPT;
envp->__round = (fpscr >> 22) & 3;
# else
* (unsigned int *) envp = fpscr;
# endif
return 0;
# endif
}
int
fesetenv (fenv_t const *envp)
{
# ifdef __SOFTFP__
return -1;
# else
unsigned int fpscr, orig_fpscr;
_FPU_GETCW (orig_fpscr);
/* On *BSD and Android, FE_DFL_ENV is the address of a global variable;
no special code is needed in this case. */
if (FE_DFL_ENV == (const fenv_t *) (-1) && envp == FE_DFL_ENV)
/* Default: rounding direction = FE_TONEAREST, exception trap bits = 0,
exception flags = 0. */
fpscr = (orig_fpscr & 0x00086060) | 0x00000000;
else
/* Here, glibc ignores orig_fpscr entirely... */
fpscr = (orig_fpscr & ~((3U << 22) | (FE_ALL_EXCEPT << 8) | FE_ALL_EXCEPT))
# if defined __OpenBSD__
| ((envp->__round & 3U) << 22)
| ((envp->__mask & FE_ALL_EXCEPT) << 8)
| (envp->__sticky & FE_ALL_EXCEPT)
# else
| (* (unsigned int const *) envp & ((3U << 22) | (FE_ALL_EXCEPT << 8) | FE_ALL_EXCEPT))
# endif
;
if (((fpscr ^ orig_fpscr) & ~0xF0000000) != 0)
_FPU_SETCW (fpscr);
return 0;
# endif
}
# elif defined __alpha
/* On all OSes except OpenBSD, fenv_t is binary-equivalent to an 'unsigned long'.
On OpenBSD, it's a struct { unsigned int __sticky, __mask, __round; }. */
/* Like FE_ALL_EXCEPT, except that it also includes FE_DENORMAL. */
# define _FE_ALL_EXCEPT (0x3FUL * FE_INVALID)
int
fegetenv (fenv_t *envp)
{
unsigned long swcr = __ieee_get_fp_control ();
unsigned long fpcr;
_FPU_GETCW (fpcr);
# if defined __OpenBSD__
envp->__sticky = (swcr >> 17) & _FE_ALL_EXCEPT;
envp->__mask = (swcr >> 1) & _FE_ALL_EXCEPT;
envp->__round = (fpcr >> 58) & 0x3UL;
# else
* (unsigned long *) envp =
(fpcr & 0x0C00000000000000UL) /* rounding direction */
| (swcr & 0x007E307EUL); /* exception flags,
mapping of denormal inputs and outputs,
exception trap bits */
# endif
return 0;
}
int
fesetenv (fenv_t const *envp)
{
unsigned long env;
/* On *BSD, FE_DFL_ENV is the address of a global variable;
on glibc, it is a magic address. */
if (envp == FE_DFL_ENV)
/* Default: rounding direction = FE_TONEAREST, exception flags = 0,
mapping = 0, exception trap bits = 0. */
env = 0x0800000000000000UL;
else
{
# if defined __OpenBSD__
env = ((unsigned long) (envp->__round & 0x3UL) << 58)
| ((envp->__sticky & _FE_ALL_EXCEPT) << 17)
| ((envp->__mask & _FE_ALL_EXCEPT) << 1);
# else
env = * (unsigned long const *) envp;
# endif
}
/* Set the rounding direction. */
unsigned long fpcr, orig_fpcr;
_FPU_GETCW (orig_fpcr);
fpcr = (orig_fpcr & ~0x0C00000000000000UL) | (env & 0x0C00000000000000UL);
if (fpcr != orig_fpcr)
_FPU_SETCW (fpcr);
/* Set the exception flags, the mapping of denormal inputs and outputs, and
the exception trap bits. */
__ieee_set_fp_control (env & 0x007E307EUL);
return 0;
}
# elif defined __hppa
/* On all OSes except glibc, fenv_t is binary-equivalent to an 'unsigned int'.
On glibc, it's a struct { unsigned int __status_word, __exception[7]; },
of which only the __status_word field is used. */
int
fegetenv (fenv_t *envp)
{
unsigned int fpstatus;
_FPU_GETCW (fpstatus);
* (unsigned int *) envp = fpstatus;
return 0;
}
int
fesetenv (fenv_t const *envp)
{
unsigned int fpstatus, orig_fpstatus;
_FPU_GETCW (orig_fpstatus);
/* On *BSD, FE_DFL_ENV is the address of a global variable;
no special code is needed in this case. */
if (FE_DFL_ENV == (const fenv_t *) (-1) && envp == FE_DFL_ENV)
/* Default: exception flags = 0, rounding direction = FE_TONEAREST,
exception trap bits = 0. */
fpstatus = 0x00000000U;
else
{
unsigned int env = * (unsigned int const *) envp;
fpstatus = (orig_fpstatus & ~0xF800061FU) | (env & 0xF800061FU);
}
if (fpstatus != orig_fpstatus)
_FPU_SETCW (fpstatus);
return 0;
}
# elif defined __ia64__
/* On all OSes, fenv_t is binary-equivalent to an 'unsigned long'. */
/* Like FE_ALL_EXCEPT, except that it also includes FE_DENORMAL. */
# define _FE_ALL_EXCEPT (0x3FUL * FE_INVALID)
int
fegetenv (fenv_t *envp)
{
unsigned long fpsr;
_FPU_GETCW (fpsr);
* (unsigned long *) envp = fpsr;
return 0;
}
int
fesetenv (fenv_t const *envp)
{
unsigned long env;
/* On NetBSD, FE_DFL_ENV is the address of a global variable;
on glibc, it is a magic address. */
if (envp == FE_DFL_ENV)
/* Default:
sf0 = 0b0000000001100, sf1 = 0b0000001001110, sf2 = sf3 = 0000001001100
i.e. precision control = 80-bits "extended",
rounding direction = FE_TONEAREST,
exception flags = 0,
exceptions trap bits = all 1, i.e. all masked. */
env = 0x0009804C0270033FUL;
else
env = * (unsigned long const *) envp;
unsigned long fpsr = env;
_FPU_SETCW (fpsr);
return 0;
}
# elif defined __m68k__
/* On all OSes, fenv_t is binary-equivalent to a struct
{ unsigned int control_register, status_register, instruction_address; }. */
# define _FPU_GETFPIAR(cw) __asm__ __volatile__ ("fmove%.l %/fpiar, %0" : "=dm" (cw))
# define _FPU_SETFPIAR(cw) __asm__ __volatile__ ("fmove%.l %0, %/fpiar" : : "dm" (cw))
int
fegetenv (fenv_t *envp)
{
unsigned int fpcr;
unsigned int fpsr;
unsigned int fpiar;
_FPU_GETCW (fpcr);
_FPU_GETFPSR (fpsr);
_FPU_GETFPIAR (fpiar);
((unsigned int *) envp)[0] = fpcr;
((unsigned int *) envp)[1] = fpsr;
((unsigned int *) envp)[2] = fpiar;
return 0;
}
int
fesetenv (fenv_t const *envp)
{
unsigned int env_fpcr;
unsigned int env_fpsr;
if (envp == FE_DFL_ENV)
{
/* Default: exceptions trap bits = 0, rounding direction = FE_TONEAREST. */
env_fpcr = 0;
/* Default: exception flags = 0. */
env_fpsr = 0;
}
else
{
/* No need to restore the instruction address here. */
env_fpcr = ((unsigned int const *) envp)[0];
env_fpsr = ((unsigned int const *) envp)[1];
}
unsigned int fpcr, orig_fpcr;
unsigned int fpsr, orig_fpsr;
unsigned int fpiar, orig_fpiar;
_FPU_GETCW (orig_fpcr);
_FPU_GETFPSR (orig_fpsr);
_FPU_GETFPIAR (orig_fpiar);
/* glibc uses 0x3E30U here. I think 0xFF30U is a better choice. */
fpcr = (orig_fpcr & ~0xFF30U) | (env_fpcr & 0xFF30U);
fpsr = (orig_fpsr & ~0x00F8U) | (env_fpsr & 0x00F8U);
fpiar = orig_fpiar;
_FPU_SETCW (fpcr);
_FPU_SETFPSR (fpsr);
_FPU_SETFPIAR (fpiar);
return 0;
}
# elif defined __mips__
/* On all OSes, fenv_t is binary-equivalent to an 'unsigned int'. */
int
fegetenv (fenv_t *envp)
{
unsigned int fcsr;
_FPU_GETCW (fcsr);
* (unsigned int *) envp = fcsr;
return 0;
}
int
fesetenv (fenv_t const *envp)
{
unsigned int env;
/* On *BSD, FE_DFL_ENV is the address of a global variable;
no special code is needed in this case. */
if (FE_DFL_ENV == (const fenv_t *) (-1) && envp == FE_DFL_ENV)
/* Default: exceptions trap bits = 0, exception flags = 0,
rounding direction = FE_TONEAREST. */
env = 0x00000000U;
else
env = * (unsigned int const *) envp;
unsigned int fcsr = env;
_FPU_SETCW (fcsr);
return 0;
}
# elif defined __loongarch__
/* On all OSes, fenv_t is binary-equivalent to an 'unsigned int'. */
int
fegetenv (fenv_t *envp)
{
unsigned int fcsr;
_FPU_GETCW (fcsr);
* (unsigned int *) envp = fcsr;
return 0;
}
int
fesetenv (fenv_t const *envp)
{
unsigned int env;
if (envp == FE_DFL_ENV)
/* Default: exception flags = 0, rounding direction = FE_TONEAREST,
exceptions trap bits = 0. */
env = 0x00000000U;
else
env = * (unsigned int const *) envp;
unsigned int fcsr = env;
_FPU_SETCW (fcsr);
return 0;
}
# elif defined __powerpc__
/* On all OSes except *BSD and AIX, fenv_t is a 'double'.
On *BSD, it's an 'unsigned int'.
On AIX, it's a struct { unsigned short rmode; unsigned int fpstat, trapstate; }.
But AIX has already been dealt with above. */
# if defined __linux__
# include <sys/prctl.h>
# endif
int
fegetenv (fenv_t *envp)
{
# if defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
union { unsigned long long u; double f; } memenv;
_FPU_GETCW_AS_DOUBLE (memenv.f);
* (unsigned int *) envp = (unsigned int) memenv.u;
# else
_FPU_GETCW_AS_DOUBLE (*envp);
# endif
return 0;
}
int
fesetenv (fenv_t const *envp)
{
union { unsigned long long u; double f; } orig_memenv, memenv;
_FPU_GETCW_AS_DOUBLE (orig_memenv.f);
/* On glibc and *BSD, FE_DFL_ENV is the address of a global variable;
no special code is needed in this case. */
if (FE_DFL_ENV == (const fenv_t *) (-1) && envp == FE_DFL_ENV)
memenv.u = 0xFFF8000000000000ULL;
else
# if defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
memenv.u = * (unsigned int const *) envp;
# else
memenv.f = *envp;
# endif
if (!(memenv.u == orig_memenv.u))
{
if ((orig_memenv.u & 0x000000f8) == 0 && (memenv.u & 0x000000f8) != 0)
{
/* Put the thread into precise trapping mode. */
# if defined __linux__ || defined __NetBSD__
prctl (PR_SET_FPEXC, PR_FP_EXC_PRECISE);
# endif
}
_FPU_SETCW_AS_DOUBLE (memenv.f);
if ((orig_memenv.u & 0x000000f8) != 0 && (memenv.u & 0x000000f8) == 0)
{
/* Put the thread into no-trapping mode. */
# if defined __linux__ || defined __NetBSD__
prctl (PR_SET_FPEXC, PR_FP_EXC_DISABLED);
# endif
}
}
return 0;
}
# elif defined __riscv
/* On all OSes except FreeBSD, fenv_t is binary-equivalent to an 'unsigned int'.
On FreeBSD, it's binary-equivalent to an 'unsigned long'. */
# define _FPU_GETCW(cw) __asm__ __volatile__ ("frcsr %0" : "=r" (cw))
# define _FPU_SETCW(cw) __asm__ __volatile__ ("fscsr %z0" : : "rJ" (cw))
int
fegetenv (fenv_t *envp)
{
unsigned int fcsr;
_FPU_GETCW (fcsr);
# if defined __FreeBSD__
* (unsigned long *) envp = fcsr;
# else
* (unsigned int *) envp = fcsr;
# endif
return 0;
}
int
fesetenv (fenv_t const *envp)
{
unsigned int env;
/* On *BSD and Android, FE_DFL_ENV is the address of a global variable;
no special code is needed in this case. */
if (FE_DFL_ENV == (const fenv_t *) (-1) && envp == FE_DFL_ENV)
/* Default: rounding direction = FE_TONEAREST, exception flags = 0. */
env = 0x00000000U;
else
# if defined __FreeBSD__
env = * (unsigned long const *) envp;
# else
env = * (unsigned int const *) envp;
# endif
unsigned int fcsr = env;
_FPU_SETCW (fcsr);
return 0;
}
# elif defined __s390__ || defined __s390x__
/* On all OSes, fenv_t is binary-equivalent to a struct whose first (and only
relevant) field is an 'unsigned int'. */
int
fegetenv (fenv_t *envp)
{
unsigned int fpc;
_FPU_GETCW (fpc);
* (unsigned int *) envp = fpc;
return 0;
}
int
fesetenv (fenv_t const *envp)
{
unsigned int env;
if (envp == FE_DFL_ENV)
/* Default: exceptions trap bits = 0, exception flags = 0,
rounding direction = FE_TONEAREST. */
env = 0x00000000U;
else
env = * (unsigned int const *) envp;
unsigned int fpc = env;
_FPU_SETCW (fpc);
return 0;
}
# elif defined __sh__
/* On all OSes, fenv_t is binary-equivalent to an 'unsigned int'. */
int
fegetenv (fenv_t *envp)
{
unsigned int fpscr;
_FPU_GETCW (fpscr);
* (unsigned int *) envp = fpscr;
return 0;
}
int
fesetenv (fenv_t const *envp)
{
unsigned int env;
/* On OpenBSD, FE_DFL_ENV is the address of a global variable;
no special code is needed in this case. */
if (FE_DFL_ENV == (const fenv_t *) (-1) && envp == FE_DFL_ENV)
/* Default: PR = 1, exceptions trap bits = 0, exception flags = 0,
rounding direction = FE_TONEAREST. */
env = 0x00080000U;
else
env = * (unsigned int const *) envp;
unsigned int fpscr = env;
_FPU_SETCW (fpscr);
return 0;
}
# elif defined __sparc
/* On all OSes except Solaris, fenv_t is binary-equivalent to an 'unsigned long'.
On Solaris, it's a struct { __fex_handler_t __handlers; unsigned long __fsr; }. */
int
fegetenv (fenv_t *envp)
{
unsigned long fsr;
_FPU_GETCW (fsr);
# if defined __sun
fex_getexcepthandler (&envp->__handlers, FEX_ALL);
envp->__fsr = fsr;
# else
* (unsigned long *) envp = fsr;
# endif
return 0;
}
int
fesetenv (fenv_t const *envp)
{
unsigned long fsr;
/* On *BSD and Solaris, FE_DFL_ENV is the address of a global variable;
no special code is needed in this case. */
if (FE_DFL_ENV == (const fenv_t *) (-1) && envp == FE_DFL_ENV)
{
/* Default: rounding direction = FE_TONEAREST, exceptions trap bits = 0,
exception flags = 0. */
fsr = 0x00000000U;
}
else
{
# if defined __sun
fex_setexcepthandler (&envp->__handlers, FEX_ALL);
fsr = envp->__fsr;
# else
fsr = * (unsigned long const *) envp;
# endif
}
_FPU_SETCW (fsr);
return 0;
}
# else
# if defined __GNUC__ || defined __clang__
# warning "Unknown CPU / architecture. Please report your platform and compiler to <bug-gnulib@gnu.org>."
# endif
# define NEED_FALLBACK 1
# endif
#else
/* The compiler does not support __asm__ statements or equivalent
intrinsics. */
# if defined __sun && ((defined __x86_64__ || defined _M_X64) || (defined __i386 || defined _M_IX86)) && defined __SUNPRO_C
/* Solaris/i386, Solaris/x86_64. */
/* Like FE_ALL_EXCEPT, except that it also includes FE_DENORMAL. */
# define _FE_ALL_EXCEPT (0x3F * FE_INVALID)
/* Accessors for the 387 unit's special registers. */
static void
getfctrl (unsigned short *fctrl_p)
{
# if defined __x86_64__ || defined _M_X64
asm ("fnstcw (%rdi)");
# else
/* The compiler generates a stack frame. Therefore the first argument is in
8(%ebp), not in 4(%esp). */
asm ("movl 8(%ebp),%eax");
asm ("fnstcw (%eax)");
# endif
}
static void
getfstat (unsigned short *fstat_p)
{
# if defined __x86_64__ || defined _M_X64
asm ("fnstsw (%rdi)");
# else
/* The compiler generates a stack frame. Therefore the first argument is in
8(%ebp), not in 4(%esp). */
asm ("movl 8(%ebp),%eax");
asm ("fnstsw (%eax)");
# endif
}
static void
getenv387 (x86_387_fenv_t *env387_p)
{
# if defined __x86_64__ || defined _M_X64
asm ("fnstenv (%rdi)");
# else
/* The compiler generates a stack frame. Therefore the first argument is in
8(%ebp), not in 4(%esp). */
asm ("movl 8(%ebp),%eax");
asm ("fnstenv (%eax)");
# endif
}
static void
setenv387 (x86_387_fenv_t const *env387_p)
{
# if defined __x86_64__ || defined _M_X64
asm ("fldenv (%rdi)");
# else
/* The compiler generates a stack frame. Therefore the first argument is in
8(%ebp), not in 4(%esp). */
asm ("movl 8(%ebp),%eax");
asm ("fldenv (%eax)");
# endif
}
/* Accessors for the mxcsr register. */
static void
getssecw (unsigned int *mxcsr_p)
{
# if defined __x86_64__ || defined _M_X64
asm ("stmxcsr (%rdi)");
# else
/* The compiler generates a stack frame. Therefore the first argument is in
8(%ebp), not in 4(%esp). */
asm ("movl 8(%ebp),%eax");
asm ("stmxcsr (%eax)");
# endif
}
static void
setssecw (unsigned int const *mxcsr_p)
{
# if defined __x86_64__ || defined _M_X64
asm ("ldmxcsr (%rdi)");
# else
/* The compiler generates a stack frame. Therefore the first argument is in
8(%ebp), not in 4(%esp). */
asm ("movl 8(%ebp),%eax");
asm ("ldmxcsr (%eax)");
# endif
}
int
fegetenv (fenv_t *envp)
{
fex_getexcepthandler (&envp->__handlers, FEX_ALL);
unsigned short fctrl;
unsigned short fstat;
unsigned int mxcsr;
getfctrl (&fctrl);
getfstat (&fstat);
getssecw (&mxcsr);
envp->__fsr =
(((unsigned int) (fctrl ^ 0x3F) & ~0xE0C0U) << 16)
| ((unsigned int) fstat | (mxcsr & 0x3F));
return 0;
}
int
fesetenv (fenv_t const *envp)
{
unsigned short env_fctrl;
unsigned int env_mxcsr;
/* On Solaris, FE_DFL_ENV is the address of a global variable;
no special code is needed in this case. */
if (FE_DFL_ENV == (const fenv_t *) (-1) && envp == FE_DFL_ENV)
{
env_fctrl = 0x3F;
env_mxcsr = 0x3F << 7;
}
else
{
fex_setexcepthandler (&envp->__handlers, FEX_ALL);
env_fctrl = (envp->__fsr >> 16) ^ 0x3F;
env_mxcsr = envp->__fsr & 0x3F;
}
/* Store the exception flags in mxcsr, not in env387.__status_word. */
/* In the SSE unit. */
unsigned int mxcsr;
getssecw (&mxcsr);
mxcsr = (mxcsr & ~0x3F) | env_mxcsr;
setssecw (&mxcsr);
/* In the 387 unit. */
x86_387_fenv_t env387;
getenv387 (&env387);
/* Note: fnstenv masks all floating-point exceptions until the setenv387
below. */
env387.__control_word = env_fctrl;
env387.__status_word &= ~0x3F;
setenv387 (&env387);
return 0;
}
#elif defined __sun && defined __sparc && defined __SUNPRO_C
/* Solaris/sparc. */
/* Accessors for the fsr register. */
static void
getfsr (unsigned long *fsr_p)
{
# if defined __sparcv9 || defined __arch64__ /* sparc64 */
asm ("stx %fsr,[%i0]");
# else
asm ("st %fsr,[%i0]");
# endif
}
static void
setfsr (unsigned long const *fsr_p)
{
# if defined __sparcv9 || defined __arch64__ /* sparc64 */
asm ("ldx [%i0],%fsr");
# else
asm ("ld [%i0],%fsr");
# endif
}
int
fegetenv (fenv_t *envp)
{
unsigned long fsr;
getfsr (&fsr);
fex_getexcepthandler (&envp->__handlers, FEX_ALL);
envp->__fsr = fsr;
return 0;
}
int
fesetenv (fenv_t const *envp)
{
unsigned long fsr;
/* On *BSD and Solaris, FE_DFL_ENV is the address of a global variable;
no special code is needed in this case. */
if (FE_DFL_ENV == (const fenv_t *) (-1) && envp == FE_DFL_ENV)
{
/* Default: rounding direction = FE_TONEAREST, exceptions trap bits = 0,
exception flags = 0. */
fsr = 0x00000000U;
}
else
{
fex_setexcepthandler (&envp->__handlers, FEX_ALL);
fsr = envp->__fsr;
}
setfsr (&fsr);
return 0;
}
# else
# define NEED_FALLBACK 1
# endif
#endif
#if NEED_FALLBACK
/* A dummy fallback. */
int
fegetenv (fenv_t *envp)
{
return -1;
}
int
fesetenv (fenv_t const *envp)
{
return -1;
}
#endif