Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.35 KB | None | 0 0
  1. /*************************************************
  2.  * The Open Group Base Specifications Issue 6
  3.  * IEEE Std 1003.1, 2004 Edition
  4.  *************************************************/
  5. #define _XOPEN_SOURCE 600
  6.  
  7. #include <ctype.h>
  8. #include <errno.h>
  9. #include <inttypes.h>
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <locale.h>
  15. #include <sys/utsname.h>
  16.  
  17. int main ( int argc, char *argv[] ) {
  18.  
  19.     /* note hex representation of pi is
  20.      *   0x4000 0x921f 0xb544 0x42d1 0x8469 0x898c 0xc517 0x01b8
  21.      * IEEE754-2008 binary64 hex representation of pi is
  22.      *    40 09 21 fb 54 44 2d 18   */
  23.     int j;
  24.  
  25.     long double pi = 3.14159265358979323846264338327950288419716939937510L;
  26.  
  27.     struct utsname uname_data;
  28.  
  29.     setlocale( LC_MESSAGES, "C" );
  30.     if ( uname( &uname_data ) < 0 ) {
  31.         fprintf ( stderr,
  32.                  "WARNING : Could not attain system uname data.\n" );
  33.         perror ( "uname" );
  34.     } else {
  35.         printf ( "-------------------------------" );
  36.         printf ( "------------------------------\n" );
  37.         printf ( "        system name = %s\n", uname_data.sysname );
  38.         printf ( "          node name = %s\n", uname_data.nodename );
  39.         printf ( "            release = %s\n", uname_data.release );
  40.         printf ( "            version = %s\n", uname_data.version );
  41.         printf ( "            machine = %s\n", uname_data.machine );
  42.         printf ( "-------------------------------" );
  43.         printf ( "------------------------------" );
  44.     }
  45.     printf ("\n");
  46.  
  47.     uint8_t x86_fp80[16] = { 0x35, 0xc2, 0x68, 0x21, 0xa2, 0xda, 0x0f, 0xc9,
  48.                              0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  49.  
  50.     printf("x86_fp80[16]\n");
  51.     printf("    0x35, 0xc2, 0x68, 0x21, 0xa2, 0xda, 0x0f, 0xc9,\n");
  52.     printf("    0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00\n");
  53.     printf("out ");
  54.     for ( j=0; j<16; j++ ) {
  55.         printf("0x%02x ", ((uint8_t *)&x86_fp80)[j] );
  56.     }
  57.     printf("\n");
  58.     printf("    x86_fp80 may be %38.34Le\n", *(long double*)&x86_fp80);
  59.     printf("    x86_fp80  or be %18.14g\n", *(double*)&x86_fp80);
  60.     printf("-------------------------------------------------\n");
  61.  
  62.  
  63.     uint8_t pi_fp64le[16] = { 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40,
  64.                               0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  65.     printf("pi_fp64le[16]\n");
  66.     printf("    0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40,\n");
  67.     printf("    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00\n");
  68.     printf("out ");
  69.     for ( j=0; j<16; j++ ) {
  70.         printf("0x%02x ", ((uint8_t *)&pi_fp64le)[j] );
  71.     }
  72.     printf("\n");
  73.     printf("   pi_fp64le may be %38.34Le\n", *(long double*)&pi_fp64le);
  74.     printf("   pi_fp64le  or be %18.14g\n", *(double*)&pi_fp64le);
  75.     printf("-------------------------------------------------\n");
  76.  
  77.  
  78.     uint8_t pi_fp64be[16] = { 0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18,
  79.                               0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  80.     printf("pi_fp64be[16]\n");
  81.     printf("    0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18,\n");
  82.     printf("    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00\n");
  83.     printf("out ");
  84.     for ( j=0; j<16; j++ ) {
  85.         printf("0x%02x ", ((uint8_t *)&pi_fp64be)[j] );
  86.     }
  87.     printf("\n");
  88.     printf("   pi_fp64be may be %38.34Le\n", *(long double*)&pi_fp64be);
  89.     printf("   pi_fp64be  or be %18.14g\n", *(double*)&pi_fp64be);
  90.     printf("-------------------------------------------------\n");
  91.  
  92.  
  93.     uint8_t pi_fp128le[16] = { 0xb8, 0x01, 0x17, 0xc5, 0x8c, 0x89, 0x69, 0x84,
  94.                                0xd1, 0x42, 0x44, 0xb5, 0x1f, 0x92, 0x00, 0x40 };
  95.     printf("pi_fp128le[16]\n");
  96.     printf("    0xb8, 0x01, 0x17, 0xc5, 0x8c, 0x89, 0x69, 0x84,\n");
  97.     printf("    0xd1, 0x42, 0x44, 0xb5, 0x1f, 0x92, 0x00, 0x40\n");
  98.     printf("out ");
  99.     for ( j=0; j<16; j++ ) {
  100.         printf("0x%02x ", ((uint8_t *)&pi_fp128le)[j] );
  101.     }
  102.     printf("\n");
  103.     printf("  pi_fp128le may be %38.34Le\n", *(long double*)&pi_fp128le);
  104.     printf("  pi_fp128le  or be %18.14g\n", *(double*)&pi_fp128le);
  105.     printf("-------------------------------------------------\n");
  106.  
  107.     uint8_t pi_fp128be[16] = { 0x40, 0x00, 0x92, 0x1f, 0xb5, 0x44, 0x42, 0xd1,
  108.                                0x84, 0x69, 0x89, 0x8c, 0xc5, 0x17, 0x01, 0xb8 };
  109.     printf("pi_fp128be[16]\n");
  110.     printf("    0x40, 0x00, 0x92, 0x1f, 0xb5, 0x44, 0x42, 0xd1,\n");
  111.     printf("    0x84, 0x69, 0x89, 0x8c, 0xc5, 0x17, 0x01, 0xb8\n");
  112.     printf("out ");
  113.     for ( j=0; j<16; j++ ) {
  114.         printf("0x%02x ", ((uint8_t *)&pi_fp128be)[j] );
  115.     }
  116.     printf("\n");      
  117.     printf("  pi_fp128be may be %38.34Le\n", *(long double*)&pi_fp128be);
  118.     printf("  pi_fp128be  or be %18.14g\n", *(double*)&pi_fp128be);
  119.     printf("-------------------------------------------------\n");
  120.  
  121.     for ( j=0; j<sizeof(long double); j++ )
  122.         printf("%02x ", ((unsigned char *)&pi)[j] );
  123.  
  124.     printf("\n" );
  125.  
  126.     printf("pi may be %38.34Le\n", pi);
  127.  
  128.     return (EXIT_SUCCESS);
  129. }
  130.  
  131. /* OUTPUT:
  132. $ make
  133. gcc main.c -o main.exe
  134. main.c:15:25: fatal error: sys/utsname.h: No such file or directory
  135.  #include <sys/utsname.h>
  136.                          ^
  137. compilation terminated.
  138. Makefile:2: recipe for target 'all' failed
  139. mingw32-make.exe: *** [all] Error 1
  140. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement