1  /* Test front-end conversions, optimizer conversions, and run-time
       2     conversions between different arithmetic types.
       3  
       4     Constants are specified in a non-obvious way to make them work for
       5     any word size.  Their value on a 32-bit machine is indicated in the
       6     comments.
       7  
       8     Note that this code is NOT intended for testing of accuracy of fp
       9     conversions.  */
      10  
      11  float
      12  u2f(u)
      13       unsigned int u;
      14  {
      15    return u;
      16  }
      17  
      18  double
      19  u2d(u)
      20       unsigned int u;
      21  {
      22    return u;
      23  }
      24  
      25  long double
      26  u2ld(u)
      27       unsigned int u;
      28  {
      29    return u;
      30  }
      31  
      32  float
      33  s2f(s)
      34       int s;
      35  {
      36    return s;
      37  }
      38  
      39  double
      40  s2d(s)
      41       int s;
      42  {
      43    return s;
      44  }
      45  
      46  long double
      47  s2ld(s)
      48       int s;
      49  {
      50    return s;
      51  }
      52  
      53  int
      54  fnear (float x, float y)
      55  {
      56    float t = x - y;
      57    return t == 0 || x / t > 1000000.0;
      58  }
      59  
      60  int
      61  dnear (double x, double y)
      62  {
      63    double t = x - y;
      64    return t == 0 || x / t > 100000000000000.0;
      65  }
      66  
      67  int
      68  ldnear (long double x, long double y)
      69  {
      70    long double t = x - y;
      71    return t == 0 || x / t > 100000000000000000000000000000000.0;
      72  }
      73  
      74  test_integer_to_float()
      75  {
      76    if (u2f(0U) != (float) 0U)				/* 0 */
      77      abort();
      78    if (!fnear (u2f(~0U), (float) ~0U))			/* 0xffffffff */
      79      abort();
      80    if (!fnear (u2f((~0U) >> 1), (float) ((~0U) >> 1)))	/* 0x7fffffff */
      81      abort();
      82    if (u2f(~((~0U) >> 1)) != (float) ~((~0U) >> 1))	/* 0x80000000 */
      83      abort();
      84  
      85    if (u2d(0U) != (double) 0U)				/* 0 */
      86      abort();
      87    if (!dnear (u2d(~0U), (double) ~0U))			/* 0xffffffff */
      88      abort();
      89    if (!dnear (u2d((~0U) >> 1),(double) ((~0U) >> 1)))	/* 0x7fffffff */
      90      abort();
      91    if (u2d(~((~0U) >> 1)) != (double) ~((~0U) >> 1))	/* 0x80000000 */
      92      abort();
      93  
      94    if (u2ld(0U) != (long double) 0U)			/* 0 */
      95      abort();
      96    if (!ldnear (u2ld(~0U), (long double) ~0U))		/* 0xffffffff */
      97      abort();
      98    if (!ldnear (u2ld((~0U) >> 1),(long double) ((~0U) >> 1)))	/* 0x7fffffff */
      99      abort();
     100    if (u2ld(~((~0U) >> 1)) != (long double) ~((~0U) >> 1))	/* 0x80000000 */
     101      abort();
     102  
     103    if (s2f(0) != (float) 0)				/* 0 */
     104      abort();
     105    if (!fnear (s2f(~0), (float) ~0))			/* 0xffffffff */
     106      abort();
     107    if (!fnear (s2f((int)((~0U) >> 1)), (float)(int)((~0U) >> 1))) /* 0x7fffffff */
     108      abort();
     109    if (s2f((int)(~((~0U) >> 1))) != (float)(int)~((~0U) >> 1)) /* 0x80000000 */
     110      abort();
     111  
     112    if (s2d(0) != (double) 0)				/* 0 */
     113      abort();
     114    if (!dnear (s2d(~0), (double) ~0))			/* 0xffffffff */
     115      abort();
     116    if (!dnear (s2d((int)((~0U) >> 1)), (double)(int)((~0U) >> 1))) /* 0x7fffffff */
     117      abort();
     118    if (s2d((int)~((~0U) >> 1)) != (double)(int)~((~0U) >> 1)) /* 0x80000000 */
     119      abort();
     120  
     121    if (s2ld(0) != (long double) 0)			/* 0 */
     122      abort();
     123    if (!ldnear (s2ld(~0), (long double) ~0))		/* 0xffffffff */
     124      abort();
     125    if (!ldnear (s2ld((int)((~0U) >> 1)), (long double)(int)((~0U) >> 1))) /* 0x7fffffff */
     126      abort();
     127    if (s2ld((int)~((~0U) >> 1)) != (long double)(int)~((~0U) >> 1)) /* 0x80000000 */
     128      abort();
     129  }
     130  
     131  #if __GNUC__
     132  float
     133  ull2f(u)
     134       unsigned long long int u;
     135  {
     136    return u;
     137  }
     138  
     139  double
     140  ull2d(u)
     141       unsigned long long int u;
     142  {
     143    return u;
     144  }
     145  
     146  long double
     147  ull2ld(u)
     148       unsigned long long int u;
     149  {
     150    return u;
     151  }
     152  
     153  float
     154  sll2f(s)
     155       long long int s;
     156  {
     157    return s;
     158  }
     159  
     160  double
     161  sll2d(s)
     162       long long int s;
     163  {
     164    return s;
     165  }
     166  
     167  long double
     168  sll2ld(s)
     169       long long int s;
     170  {
     171    return s;
     172  }
     173  
     174  test_longlong_integer_to_float()
     175  {
     176    if (ull2f(0ULL) != (float) 0ULL)			/* 0 */
     177      abort();
     178    if (ull2f(~0ULL) != (float) ~0ULL)			/* 0xffffffff */
     179      abort();
     180    if (ull2f((~0ULL) >> 1) != (float) ((~0ULL) >> 1))	/* 0x7fffffff */
     181      abort();
     182    if (ull2f(~((~0ULL) >> 1)) != (float) ~((~0ULL) >> 1)) /* 0x80000000 */
     183      abort();
     184  
     185    if (ull2d(0ULL) != (double) 0ULL)			/* 0 */
     186      abort();
     187  #if __HAVE_68881__
     188    /* Some 68881 targets return values in fp0, with excess precision.
     189       But the compile-time conversion to double works correctly.  */
     190    if (! dnear (ull2d(~0ULL), (double) ~0ULL))		/* 0xffffffff */
     191      abort();
     192    if (! dnear (ull2d((~0ULL) >> 1), (double) ((~0ULL) >> 1))) /* 0x7fffffff */
     193      abort();
     194  #else
     195    if (ull2d(~0ULL) != (double) ~0ULL)			/* 0xffffffff */
     196      abort();
     197    if (ull2d((~0ULL) >> 1) != (double) ((~0ULL) >> 1))	/* 0x7fffffff */
     198      abort();
     199  #endif
     200    if (ull2d(~((~0ULL) >> 1)) != (double) ~((~0ULL) >> 1)) /* 0x80000000 */
     201      abort();
     202  
     203    if (ull2ld(0ULL) != (long double) 0ULL)		/* 0 */
     204      abort();
     205    if (ull2ld(~0ULL) != (long double) ~0ULL)		/* 0xffffffff */
     206      abort();
     207    if (ull2ld((~0ULL) >> 1) != (long double) ((~0ULL) >> 1))	/* 0x7fffffff */
     208      abort();
     209    if (ull2ld(~((~0ULL) >> 1)) != (long double) ~((~0ULL) >> 1)) /* 0x80000000 */
     210      abort();
     211  
     212    if (sll2f(0LL) != (float) 0LL)			/* 0 */
     213      abort();
     214    if (sll2f(~0LL) != (float) ~0LL)			/* 0xffffffff */
     215      abort();
     216    if (! fnear (sll2f((long long int)((~0ULL) >> 1)), (float)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */
     217      abort();
     218    if (sll2f((long long int)(~((~0ULL) >> 1))) != (float)(long long int)~((~0ULL) >> 1)) /* 0x80000000 */
     219      abort();
     220  
     221    if (sll2d(0LL) != (double) 0LL)			/* 0 */
     222      abort();
     223    if (sll2d(~0LL) != (double) ~0LL)			/* 0xffffffff */
     224      abort();
     225    if (!dnear (sll2d((long long int)((~0ULL) >> 1)), (double)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */
     226      abort();
     227    if (! dnear (sll2d((long long int)~((~0ULL) >> 1)), (double)(long long int)~((~0ULL) >> 1))) /* 0x80000000 */
     228      abort();
     229  
     230    if (sll2ld(0LL) != (long double) 0LL)			/* 0 */
     231      abort();
     232    if (sll2ld(~0LL) != (long double) ~0LL)		/* 0xffffffff */
     233      abort();
     234    if (!ldnear (sll2ld((long long int)((~0ULL) >> 1)), (long double)(long long int)((~0ULL) >> 1))) /* 0x7fffffff */
     235      abort();
     236    if (! ldnear (sll2ld((long long int)~((~0ULL) >> 1)), (long double)(long long int)~((~0ULL) >> 1))) /* 0x80000000 */
     237      abort();
     238  }
     239  #endif
     240  
     241  unsigned int
     242  f2u(float f)
     243  {
     244    return (unsigned) f;
     245  }
     246  
     247  unsigned int
     248  d2u(double d)
     249  {
     250    return (unsigned) d;
     251  }
     252  
     253  unsigned int
     254  ld2u(long double d)
     255  {
     256    return (unsigned) d;
     257  }
     258  
     259  int
     260  f2s(float f)
     261  {
     262    return (int) f;
     263  }
     264  
     265  int
     266  d2s(double d)
     267  {
     268    return (int) d;
     269  }
     270  
     271  int
     272  ld2s(long double d)
     273  {
     274    return (int) d;
     275  }
     276  
     277  test_float_to_integer()
     278  {
     279    if (f2u(0.0) != 0)
     280      abort();
     281    if (f2u(0.999) != 0)
     282      abort();
     283    if (f2u(1.0) != 1)
     284      abort();
     285    if (f2u(1.99) != 1)
     286      abort();
     287    if (f2u((float) ((~0U) >> 1)) != (~0U) >> 1 &&	/* 0x7fffffff */
     288        f2u((float) ((~0U) >> 1)) != ((~0U) >> 1) + 1)
     289      abort();
     290    if (f2u((float) ~((~0U) >> 1)) != ~((~0U) >> 1))	/* 0x80000000 */
     291      abort();
     292  
     293   /* These tests require double precision, so for hosts that don't offer
     294      that much precision, just ignore these test.  */
     295   if (sizeof (double) >= 8) {
     296    if (d2u(0.0) != 0)
     297      abort();
     298    if (d2u(0.999) != 0)
     299      abort();
     300    if (d2u(1.0) != 1)
     301      abort();
     302    if (d2u(1.99) != 1)
     303      abort();
     304    if (d2u((double) (~0U)) != ~0U)			/* 0xffffffff */
     305      abort();
     306    if (d2u((double) ((~0U) >> 1)) != (~0U) >> 1)		/* 0x7fffffff */
     307      abort();
     308    if (d2u((double) ~((~0U) >> 1)) != ~((~0U) >> 1))	/* 0x80000000 */
     309      abort();
     310   }
     311  
     312   /* These tests require long double precision, so for hosts that don't offer
     313      that much precision, just ignore these test.  */
     314   if (sizeof (long double) >= 8) {
     315    if (ld2u(0.0) != 0)
     316      abort();
     317    if (ld2u(0.999) != 0)
     318      abort();
     319    if (ld2u(1.0) != 1)
     320      abort();
     321    if (ld2u(1.99) != 1)
     322      abort();
     323    if (ld2u((long double) (~0U)) != ~0U)			/* 0xffffffff */
     324      abort();
     325    if (ld2u((long double) ((~0U) >> 1)) != (~0U) >> 1)	/* 0x7fffffff */
     326      abort();
     327    if (ld2u((long double) ~((~0U) >> 1)) != ~((~0U) >> 1))	/* 0x80000000 */
     328      abort();
     329   }
     330  
     331    if (f2s(0.0) != 0)
     332      abort();
     333    if (f2s(0.999) != 0)
     334      abort();
     335    if (f2s(1.0) != 1)
     336      abort();
     337    if (f2s(1.99) != 1)
     338      abort();
     339    if (f2s(-0.999) != 0)
     340      abort();
     341    if (f2s(-1.0) != -1)
     342      abort();
     343    if (f2s(-1.99) != -1)
     344      abort();
     345    if (f2s((float)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */
     346      abort();
     347  
     348   /* These tests require double precision, so for hosts that don't offer
     349      that much precision, just ignore these test.  */
     350   if (sizeof (double) >= 8) {
     351    if (d2s(0.0) != 0)
     352      abort();
     353    if (d2s(0.999) != 0)
     354      abort();
     355    if (d2s(1.0) != 1)
     356      abort();
     357    if (d2s(1.99) != 1)
     358      abort();
     359    if (d2s(-0.999) != 0)
     360      abort();
     361    if (d2s(-1.0) != -1)
     362      abort();
     363    if (d2s(-1.99) != -1)
     364      abort();
     365    if (d2s((double) ((~0U) >> 1)) != (~0U) >> 1)		/* 0x7fffffff */
     366      abort();
     367    if (d2s((double)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */
     368      abort();
     369   }
     370  
     371   /* These tests require long double precision, so for hosts that don't offer
     372      that much precision, just ignore these test.  */
     373   if (sizeof (long double) >= 8) {
     374    if (ld2s(0.0) != 0)
     375      abort();
     376    if (ld2s(0.999) != 0)
     377      abort();
     378    if (ld2s(1.0) != 1)
     379      abort();
     380    if (ld2s(1.99) != 1)
     381      abort();
     382    if (ld2s(-0.999) != 0)
     383      abort();
     384    if (ld2s(-1.0) != -1)
     385      abort();
     386    if (ld2s(-1.99) != -1)
     387      abort();
     388    if (ld2s((long double) ((~0U) >> 1)) != (~0U) >> 1)		/* 0x7fffffff */
     389      abort();
     390    if (ld2s((long double)(int)~((~0U) >> 1)) != (int)~((~0U) >> 1)) /* 0x80000000 */
     391      abort();
     392   }
     393  }
     394  
     395  #if __GNUC__
     396  unsigned long long int
     397  f2ull(float f)
     398  {
     399    return (unsigned long long int) f;
     400  }
     401  
     402  unsigned long long int
     403  d2ull(double d)
     404  {
     405    return (unsigned long long int) d;
     406  }
     407  
     408  unsigned long long int
     409  ld2ull(long double d)
     410  {
     411    return (unsigned long long int) d;
     412  }
     413  
     414  long long int
     415  f2sll(float f)
     416  {
     417    return (long long int) f;
     418  }
     419  
     420  long long int
     421  d2sll(double d)
     422  {
     423    return (long long int) d;
     424  }
     425  
     426  long long int
     427  ld2sll(long double d)
     428  {
     429    return (long long int) d;
     430  }
     431  
     432  test_float_to_longlong_integer()
     433  {
     434    if (f2ull(0.0) != 0LL)
     435      abort();
     436    if (f2ull(0.999) != 0LL)
     437      abort();
     438    if (f2ull(1.0) != 1LL)
     439      abort();
     440    if (f2ull(1.99) != 1LL)
     441      abort();
     442    if (f2ull((float) ((~0ULL) >> 1)) != (~0ULL) >> 1 &&	/* 0x7fffffff */
     443        f2ull((float) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1)
     444      abort();
     445    if (f2ull((float) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */
     446      abort();
     447  
     448    if (d2ull(0.0) != 0LL)
     449      abort();
     450    if (d2ull(0.999) != 0LL)
     451      abort();
     452    if (d2ull(1.0) != 1LL)
     453      abort();
     454    if (d2ull(1.99) != 1LL)
     455      abort();
     456    if (d2ull((double) ((~0ULL) >> 1)) != (~0ULL) >> 1 &&	/* 0x7fffffff */
     457        d2ull((double) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1)
     458      abort();
     459    if (d2ull((double) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */
     460      abort();
     461  
     462    if (ld2ull(0.0) != 0LL)
     463      abort();
     464    if (ld2ull(0.999) != 0LL)
     465      abort();
     466    if (ld2ull(1.0) != 1LL)
     467      abort();
     468    if (ld2ull(1.99) != 1LL)
     469      abort();
     470    if (ld2ull((long double) ((~0ULL) >> 1)) != (~0ULL) >> 1 &&	/* 0x7fffffff */
     471        ld2ull((long double) ((~0ULL) >> 1)) != ((~0ULL) >> 1) + 1)
     472      abort();
     473    if (ld2ull((long double) ~((~0ULL) >> 1)) != ~((~0ULL) >> 1)) /* 0x80000000 */
     474      abort();
     475  
     476  
     477    if (f2sll(0.0) != 0LL)
     478      abort();
     479    if (f2sll(0.999) != 0LL)
     480      abort();
     481    if (f2sll(1.0) != 1LL)
     482      abort();
     483    if (f2sll(1.99) != 1LL)
     484      abort();
     485    if (f2sll(-0.999) != 0LL)
     486      abort();
     487    if (f2sll(-1.0) != -1LL)
     488      abort();
     489    if (f2sll(-1.99) != -1LL)
     490      abort();
     491    if (f2sll((float)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */
     492      abort();
     493  
     494    if (d2sll(0.0) != 0LL)
     495      abort();
     496    if (d2sll(0.999) != 0LL)
     497      abort();
     498    if (d2sll(1.0) != 1LL)
     499      abort();
     500    if (d2sll(1.99) != 1LL)
     501      abort();
     502    if (d2sll(-0.999) != 0LL)
     503      abort();
     504    if (d2sll(-1.0) != -1LL)
     505      abort();
     506    if (d2sll(-1.99) != -1LL)
     507      abort();
     508    if (d2sll((double)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */
     509      abort();
     510  
     511    if (ld2sll(0.0) != 0LL)
     512      abort();
     513    if (ld2sll(0.999) != 0LL)
     514      abort();
     515    if (ld2sll(1.0) != 1LL)
     516      abort();
     517    if (ld2sll(1.99) != 1LL)
     518      abort();
     519    if (ld2sll(-0.999) != 0LL)
     520      abort();
     521    if (ld2sll(-1.0) != -1LL)
     522      abort();
     523    if (ld2sll(-1.99) != -1LL)
     524      abort();
     525    if (ld2sll((long double)(long long int)~((~0ULL) >> 1)) != (long long int)~((~0ULL) >> 1)) /* 0x80000000 */
     526      abort();
     527  }
     528  #endif
     529  
     530  main()
     531  {
     532    test_integer_to_float();
     533    test_float_to_integer();
     534  #if __GNUC__
     535    test_longlong_integer_to_float();
     536    test_float_to_longlong_integer();
     537  #endif
     538    exit(0);
     539  }