Advertisement
GegoXaren

c-byteswap-test.c

Dec 26th, 2024 (edited)
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.60 KB | None | 0 0
  1. // $ gcc --pedantic c-byteswap-test.c -o c-byteswap-test && ./c-byteswap-test
  2. #include <byteswap.h>
  3. #include <stdio.h>
  4. #include <stdint.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <limits.h>
  8. #include <inttypes.h>
  9. #include <assert.h>
  10.  
  11.  
  12. int main (void) {
  13.   fprintf (stdout, "----  int16_t  ----\n");
  14.   {
  15.   int16_t a  = 0;
  16.   int16_t b  = 0;
  17.   int16_t a_r = 0;
  18.   int16_t b_r = 0;
  19.   a = 0xbeef;
  20.   b = 0xefbe;
  21.   a_r = bswap_16 (a);
  22.   b_r = bswap_16 (b);
  23.  
  24.   //fprintf (stdout, "print format: " PRIx16 "\n");
  25.  
  26.   fprintf (stdout,
  27.           "a: \t 0x%" PRIx16 "(%"PRIu16") \t b: \t 0x%" PRIx16 "(%"PRIu16")\n"
  28.           "b_r: \t 0x%" PRIx16"(%"PRIu16") \t a_r: \t 0x%" PRIx16 "(%"PRIu16")\n",
  29.           a, a, b, b,
  30.           b_r, b_r, a_r, a_r);
  31.   assert (b == a_r);
  32.   assert (a == b_r);
  33.   }
  34.   fprintf (stdout, "----  uint16_t  ----\n");
  35.   {
  36.   uint16_t a  = 0;
  37.   uint16_t b  = 0;
  38.   uint16_t a_r = 0;
  39.   uint16_t b_r = 0;
  40.   a =  0xbeef;
  41.   b =  0xefbe;
  42.   a_r = bswap_16 (a);
  43.   b_r = bswap_16 (b);
  44.  
  45.   //fprintf (stdout, "print format: " PRIx16 "\n");
  46.  
  47.   fprintf (stdout,
  48.           "a: \t 0x%" PRIx16 "(%"PRIi16") \t b: \t 0x%" PRIx16 "(%"PRIi16")\n"
  49.           "b_r: \t 0x%" PRIx16"(%"PRIi16") \t a_r: \t 0x%" PRIx16 "(%"PRIi16")\n",
  50.           a, a, b, b,
  51.           b_r, b_r, a_r, a_r);
  52.   assert (b == a_r);
  53.   assert (a == b_r);
  54.   }
  55.  
  56.   fprintf (stdout, "----  int32_t  ----\n");
  57.   {
  58.   int32_t a  = 0;
  59.   int32_t b  = 0;
  60.   int32_t a_r = 0;
  61.   int32_t b_r = 0;
  62.   a = 0xdeadbeef;
  63.   b = 0xefbeadde;
  64.   a_r = bswap_32 (a);
  65.   b_r = bswap_32 (b);
  66.  
  67.   // fprintf (stdout, "print format: " PRIx32 "\n");
  68.  
  69.   fprintf (stdout,
  70.           "a: \t 0x%" PRIx32 "(%"PRIi32") \t b: \t 0x%" PRIx32 "(%"PRIi32")\n"
  71.           "b_r: \t 0x%" PRIx32"(%"PRIi32") \t a_r: \t 0x%" PRIx32 "(%"PRIi32")\n",
  72.           a, a, b, b,
  73.           b_r, b_r, a_r, a_r);
  74.   assert (b == a_r);
  75.   assert (a == b_r);
  76.   }
  77.   fprintf (stdout, "----  uint32_t  ----\n");
  78.   {
  79.   uint32_t a  = 0;
  80.   uint32_t b  = 0;
  81.   uint32_t a_r = 0;
  82.   uint32_t b_r = 0;
  83.   a =  0xdeadbeef;
  84.   b =  0xefbeadde;
  85.   a_r = bswap_32 (a);
  86.   b_r = bswap_32 (b);
  87.  
  88.   // fprintf (stdout, "print format: " PRIx32 "\n");
  89.  
  90.   fprintf (stdout,
  91.           "a: \t 0x%" PRIx32 "(%"PRIi32") \t b: \t 0x%" PRIx32 "(%"PRIi32")\n"
  92.           "b_r: \t 0x%" PRIx32"(%"PRIi32") \t a_r: \t 0x%" PRIx32 "(%"PRIi32")\n",
  93.           a, a, b, b,
  94.           b_r, b_r, a_r, a_r);
  95.   assert (b == a_r);
  96.   assert (a == b_r);
  97.   }
  98.  
  99.   fprintf (stdout, "----  int64_t  ----\n");
  100.   {
  101.   int64_t a  = 0;
  102.   int64_t b  = 0;
  103.   int64_t a_r = 0;
  104.   int64_t b_r = 0;
  105.   a = 0xdeadbeefdeadbeef;
  106.   b = 0xefbeaddeefbeadde;
  107.   a_r = bswap_64 (a);
  108.   b_r = bswap_64 (b);
  109.  
  110.   // fprintf (stdout, "print format: " PRIx64 "\n");
  111.  
  112.   fprintf (stdout,
  113.           "a: \t 0x%" PRIx64 "(%"PRIi64") \t b: \t 0x%" PRIx64 "(%"PRIi64")\n"
  114.           "b_r: \t 0x%" PRIx64"(%"PRIi64") \t a_r: \t 0x%" PRIx64 "(%"PRIi64")\n",
  115.           a, a, b, b,
  116.           b_r, b_r, a_r, a_r);
  117.   assert (b == a_r);
  118.   assert (a == b_r);
  119.   }
  120.   fprintf (stdout, "----  uint64_t  ----\n");
  121.   {
  122.   uint64_t a  = 0;
  123.   uint64_t b  = 0;
  124.   uint64_t a_r = 0;
  125.   uint64_t b_r = 0;
  126.   a =  0xdeadbeefdeadbeef;
  127.   b =  0xefbeaddeefbeadde;
  128.   a_r = bswap_64 (a);
  129.   b_r = bswap_64 (b);
  130.  
  131.   // fprintf (stdout, "print format: " PRIx64 "\n");
  132.  
  133.   fprintf (stdout,
  134.           "a: \t 0x%" PRIx64 "(%"PRIu64") \t b: \t 0x%" PRIx64 "(%"PRIu64")\n"
  135.           "b_r: \t 0x%" PRIx64"(%"PRIu64") \t a_r: \t 0x%" PRIx64 "(%"PRIu64")\n",
  136.           a, a, b, b,
  137.           b_r, b_r, a_r, a_r);
  138.   assert (b == a_r);
  139.   assert (a == b_r);
  140.   }
  141.   return 0;
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement