pastebin - collaborative debugging tool
rovema.kpaste.net RSS


math_signaling_nan/signaling_nan_test1.c
Posted by Anonymous on Wed 7th Jun 2023 18:21
raw | new post

  1.  
  2. /*
  3.  * math_signaling_nan/signaling_nan_test1.c - SNAN/signaling nan playground
  4.  *
  5.  *
  6.  * Compile with:
  7.  * $ gcc -fsignaling-nans -g -Wall signaling_nan_test1.c -lm #
  8.  *
  9.  */
  10.  
  11. #define _GNU_SOURCE 1
  12. #define __STDC_WANT_IEC_60559_BFP_EXT__ 1
  13.  
  14. #include <stdlib.h>
  15. #include <stdint.h>
  16. #include <stdio.h>
  17. #include <math.h>
  18. #include <fenv.h>
  19. #include <signal.h>
  20.  
  21. /*
  22.  * See $ gcc -fsignaling-nans -D__STDC_WANT_IEC_60559_BFP_EXT__ \
  23.  * -E -dM -include math.h - < /dev/null 2>&1 | grep -F 'builtin_nan'
  24.  * to see defines
  25.  */
  26.  
  27. #ifndef __SUPPORT_SNAN__
  28. #error __SUPPORT_SNAN__ not set
  29. #endif
  30.  
  31. const uint64_t double_quiet_NaN =               0x7FF8000000000000;
  32. const uint64_t double_signaling_NaN =           0x7FF0000001000000;
  33. const uint64_t double_quiet_location_bit =      0x0008000000000000;
  34.  
  35. void catch_mathsig(int sig, siginfo_t *info, void *ucontext)
  36. {
  37.         feclearexcept(FE_INVALID);
  38.         feclearexcept(FE_ALL_EXCEPT);
  39.         (void)printf("math error = %d, si_code=%d\n", sig, info->si_code);
  40. }
  41.  
  42.  
  43. void printbytes(const void *mem, size_t si)
  44. {
  45.         const unsigned char *m = mem;
  46.         size_t i;
  47.        
  48.         for(i=0 ; i < si ; i++) {
  49.                 (void)printf("%2.2x", m[i]);
  50.         }
  51.         (void)puts("");
  52. }
  53.  
  54. void printdoublelayout(double *d)
  55. {
  56.         printbytes(d, sizeof(*d));
  57.        
  58.         /* this is not portable! */
  59.         if (sizeof(double) == sizeof(long)) {
  60.                 uint64_t float_bits = *((unsigned long *)((void *)d));
  61.                 (void)printf("%lx (snan=%s)\n",
  62.                         float_bits,
  63.                         ((float_bits & double_quiet_location_bit)?
  64.                                 "false":"true"));
  65.         }
  66. }
  67.  
  68. int main(int ac, char *av[])
  69. {
  70.         (void)puts("# start.");
  71.  
  72.         (void)puts("# mark sigaction.");
  73.         struct sigaction sact;
  74.         sact.sa_sigaction = catch_mathsig;
  75.         sigemptyset (&sact.sa_mask);
  76.         sact.sa_flags = SA_SIGINFO;
  77. #if 0
  78.         sigaction(SIGFPE, &sact, (struct sigaction *)0);
  79.         feenableexcept(FE_ALL_EXCEPT);
  80. #endif
  81.         (void)puts("# mark print quiet nan.");
  82.         double fq = NAN;
  83.  
  84.         (void)printf("fq=%f\n", fq);
  85.         printdoublelayout(&fq);
  86.  
  87.         double fs = SNAN;
  88.        
  89.         (void)puts("# mark sin().");
  90.         sin(fs);
  91. #if 0
  92.         (void)puts("# mark sqrt().");
  93.         sqrt(-2);
  94. #endif
  95.         (void)puts("# mark print signaling nan.");
  96.         (void)printf("fs=%f\n", fs);
  97.         printdoublelayout(&fs);
  98.        
  99.         if (fs != fq)
  100.                 (void)puts("fs != fq");
  101.         if (fs == fq)
  102.                 (void)puts("fs == fq");
  103.  
  104.         (void)puts("# done.");
  105.         return EXIT_SUCCESS;
  106. }

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.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at