- /*
- * math_signaling_nan/signaling_nan_test1.c - SNAN/signaling nan playground
- *
- *
- * Compile with:
- * $ gcc -fsignaling-nans -g -Wall signaling_nan_test1.c -lm #
- *
- */
- #define _GNU_SOURCE 1
- #define __STDC_WANT_IEC_60559_BFP_EXT__ 1
- #include <stdlib.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <math.h>
- #include <fenv.h>
- #include <signal.h>
- /*
- * See $ gcc -fsignaling-nans -D__STDC_WANT_IEC_60559_BFP_EXT__ \
- * -E -dM -include math.h - < /dev/null 2>&1 | grep -F 'builtin_nan'
- * to see defines
- */
- #ifndef __SUPPORT_SNAN__
- #error __SUPPORT_SNAN__ not set
- #endif
- const uint64_t double_quiet_NaN = 0x7FF8000000000000;
- const uint64_t double_signaling_NaN = 0x7FF0000001000000;
- const uint64_t double_quiet_location_bit = 0x0008000000000000;
- void catch_mathsig(int sig, siginfo_t *info, void *ucontext)
- {
- feclearexcept(FE_INVALID);
- feclearexcept(FE_ALL_EXCEPT);
- }
- void printbytes(const void *mem, size_t si)
- {
- const unsigned char *m = mem;
- size_t i;
- for(i=0 ; i < si ; i++) {
- }
- }
- void printdoublelayout(double *d)
- {
- printbytes(d, sizeof(*d));
- /* this is not portable! */
- if (sizeof(double) == sizeof(long)) {
- uint64_t float_bits = *((unsigned long *)((void *)d));
- float_bits,
- ((float_bits & double_quiet_location_bit)?
- "false":"true"));
- }
- }
- int main(int ac, char *av[])
- {
- struct sigaction sact;
- sact.sa_sigaction = catch_mathsig;
- sigemptyset (&sact.sa_mask);
- sact.sa_flags = SA_SIGINFO;
- #if 0
- sigaction(SIGFPE, &sact, (struct sigaction *)0);
- feenableexcept(FE_ALL_EXCEPT);
- #endif
- double fq = NAN;
- printdoublelayout(&fq);
- double fs = SNAN;
- #if 0
- #endif
- printdoublelayout(&fs);
- if (fs != fq)
- if (fs == fq)
- return EXIT_SUCCESS;
- }
math_signaling_nan/signaling_nan_test1.c
Posted by Anonymous on Wed 7th Jun 2023 18:21
raw | new post
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.