modernc.org/ccgo/v3@v3.16.14/lib/testdata/gcc-9.1.0/gcc/testsuite/gcc.c-torture/execute/ieee/copysign1.c (about)

     1  #include <string.h>
     2  #include <stdlib.h>
     3  #include <float.h>
     4  
     5  #define fpsizeoff	sizeof(float)
     6  #define fpsizeof	sizeof(double)
     7  #define fpsizeofl	sizeof(long double)
     8  
     9  #ifndef __ccgo_test__
    10  /* Work around the fact that with the Intel double-extended precision,
    11     we've got a 10 byte type stuffed into some amount of padding.  And
    12     the fact that -ffloat-store is going to stuff this value temporarily
    13     into some bit of stack frame that we've no control over and can't zero.  */
    14  #if LDBL_MANT_DIG == 64
    15  # if defined(__i386__) || defined(__x86_64__) || defined (__ia64__)
    16  #  undef fpsizeofl
    17  #  define fpsizeofl	10
    18  # endif
    19  #endif
    20  
    21  /* Work around the fact that the sign of the second double in the IBM
    22     double-double format is not strictly specified when it contains a zero.
    23     For instance, -0.0L can be represented with either (-0.0, +0.0) or
    24     (-0.0, -0.0).  The former is what we'll get from the compiler when it
    25     builds constants; the later is what we'll get from the negation operator
    26     at runtime.  */
    27  /* ??? This hack only works for big-endian, which is fortunately true for
    28     AIX and, Darwin.  */
    29  #if LDBL_MANT_DIG == 106
    30  # undef fpsizeofl
    31  # define fpsizeofl	sizeof(double)
    32  #endif
    33  #endif
    34  
    35  
    36  #define TEST(TYPE, EXT)						\
    37  TYPE c##EXT (TYPE x, TYPE y)					\
    38  {								\
    39    return __builtin_copysign##EXT (x, y);			\
    40  }								\
    41  								\
    42  struct D##EXT { TYPE x, y, z; };				\
    43  								\
    44  static const struct D##EXT T##EXT[] = {				\
    45    { 1.0, 2.0, 1.0 },						\
    46    { 1.0, -2.0, -1.0 },						\
    47    { -1.0, -2.0, -1.0 },						\
    48    { 0.0, -2.0, -0.0 },						\
    49    { -0.0, -2.0, -0.0 },						\
    50    { -0.0, 2.0, 0.0 },						\
    51    { __builtin_inf##EXT (), -0.0, -__builtin_inf##EXT () },	\
    52    { -__builtin_nan##EXT (""), __builtin_inf##EXT (),		\
    53      __builtin_nan##EXT ("") }					\
    54  };								\
    55  								\
    56  void test##EXT (void)						\
    57  {								\
    58    int i, n = sizeof (T##EXT) / sizeof (T##EXT[0]);		\
    59    TYPE r;							\
    60    for (i = 0; i < n; ++i)					\
    61      {								\
    62        r = c##EXT (T##EXT[i].x, T##EXT[i].y);			\
    63        if (memcmp (&r, &T##EXT[i].z, fpsizeof##EXT) != 0)	\
    64  	abort ();						\
    65      }								\
    66  }
    67  
    68  TEST(float, f)
    69  TEST(double, )
    70  TEST(long double, l)
    71  
    72  int main()
    73  {
    74    testf();
    75    test();
    76    testl();
    77    return 0;
    78  }