modernc.org/cc@v1.0.1/testdata/gcc-6.3.0/gcc/testsuite/gcc.c-torture/execute/bswap-2.c (about)

     1  #ifdef __UINT32_TYPE__
     2  typedef __UINT32_TYPE__ uint32_t;
     3  #else
     4  typedef __UINT32_TYPE__ unsigned;
     5  #endif
     6  
     7  struct bitfield {
     8    unsigned char f0:7;
     9    unsigned char   :1;
    10    unsigned char f1:7;
    11    unsigned char   :1;
    12    unsigned char f2:7;
    13    unsigned char   :1;
    14    unsigned char f3:7;
    15  };
    16  
    17  struct ok {
    18    unsigned char f0;
    19    unsigned char f1;
    20    unsigned char f2;
    21    unsigned char f3;
    22  };
    23  
    24  union bf_or_uint32 {
    25    struct ok inval;
    26    struct bitfield bfval;
    27  };
    28  
    29  __attribute__ ((noinline, noclone)) uint32_t
    30  partial_read_le32 (union bf_or_uint32 in)
    31  {
    32    return in.bfval.f0 | (in.bfval.f1 << 8)
    33  	 | (in.bfval.f2 << 16) | (in.bfval.f3 << 24);
    34  }
    35  
    36  __attribute__ ((noinline, noclone)) uint32_t
    37  partial_read_be32 (union bf_or_uint32 in)
    38  {
    39    return in.bfval.f3 | (in.bfval.f2 << 8)
    40  	 | (in.bfval.f1 << 16) | (in.bfval.f0 << 24);
    41  }
    42  
    43  __attribute__ ((noinline, noclone)) uint32_t
    44  fake_read_le32 (char *x, char *y)
    45  {
    46    unsigned char c0, c1, c2, c3;
    47  
    48    c0 = x[0];
    49    c1 = x[1];
    50    *y = 1;
    51    c2 = x[2];
    52    c3 = x[3];
    53    return c0 | c1 << 8 | c2 << 16 | c3 << 24;
    54  }
    55  
    56  __attribute__ ((noinline, noclone)) uint32_t
    57  fake_read_be32 (char *x, char *y)
    58  {
    59    unsigned char c0, c1, c2, c3;
    60  
    61    c0 = x[0];
    62    c1 = x[1];
    63    *y = 1;
    64    c2 = x[2];
    65    c3 = x[3];
    66    return c3 | c2 << 8 | c1 << 16 | c0 << 24;
    67  }
    68  
    69  __attribute__ ((noinline, noclone)) uint32_t
    70  incorrect_read_le32 (char *x, char *y)
    71  {
    72    unsigned char c0, c1, c2, c3;
    73  
    74    c0 = x[0];
    75    c1 = x[1];
    76    c2 = x[2];
    77    c3 = x[3];
    78    *y = 1;
    79    return c0 | c1 << 8 | c2 << 16 | c3 << 24;
    80  }
    81  
    82  __attribute__ ((noinline, noclone)) uint32_t
    83  incorrect_read_be32 (char *x, char *y)
    84  {
    85    unsigned char c0, c1, c2, c3;
    86  
    87    c0 = x[0];
    88    c1 = x[1];
    89    c2 = x[2];
    90    c3 = x[3];
    91    *y = 1;
    92    return c3 | c2 << 8 | c1 << 16 | c0 << 24;
    93  }
    94  
    95  int
    96  main ()
    97  {
    98    union bf_or_uint32 bfin;
    99    uint32_t out;
   100    char cin[] = { 0x83, 0x85, 0x87, 0x89 };
   101  
   102    if (sizeof (uint32_t) * __CHAR_BIT__ != 32)
   103      return 0;
   104    bfin.inval = (struct ok) { 0x83, 0x85, 0x87, 0x89 };
   105    out = partial_read_le32 (bfin);
   106    /* Test what bswap would do if its check are not strict enough instead of
   107       what is the expected result as there is too many possible results with
   108       bitfields.  */
   109    if (out == 0x89878583)
   110      __builtin_abort ();
   111    bfin.inval = (struct ok) { 0x83, 0x85, 0x87, 0x89 };
   112    out = partial_read_be32 (bfin);
   113    /* Test what bswap would do if its check are not strict enough instead of
   114       what is the expected result as there is too many possible results with
   115       bitfields.  */
   116    if (out == 0x83858789)
   117      __builtin_abort ();
   118    out = fake_read_le32 (cin, &cin[2]);
   119    if (out != 0x89018583)
   120      __builtin_abort ();
   121    cin[2] = 0x87;
   122    out = fake_read_be32 (cin, &cin[2]);
   123    if (out != 0x83850189)
   124      __builtin_abort ();
   125    cin[2] = 0x87;
   126    out = incorrect_read_le32 (cin, &cin[2]);
   127    if (out != 0x89878583)
   128      __builtin_abort ();
   129    cin[2] = 0x87;
   130    out = incorrect_read_be32 (cin, &cin[2]);
   131    if (out != 0x83858789)
   132      __builtin_abort ();
   133    return 0;
   134  }