xref: /llvm-project/compiler-rt/test/builtins/Unit/ppc/floattitf_test.c (revision 0b7c9e535bceb97b506410a6a8855efa82180e42)
1*0b7c9e53SAmy Kwan // REQUIRES: target-is-powerpc64le
2*0b7c9e53SAmy Kwan // RUN: %clang_builtins %s %librt -o %t && %run %t
3*0b7c9e53SAmy Kwan 
4*0b7c9e53SAmy Kwan /*
5*0b7c9e53SAmy Kwan  * Test case execution for: long double __floattitf (__int128_t)
6*0b7c9e53SAmy Kwan  * Conversion from 128 bit integer to long double (IBM double-double).
7*0b7c9e53SAmy Kwan  */
8*0b7c9e53SAmy Kwan 
9*0b7c9e53SAmy Kwan #include <stdint.h>
10*0b7c9e53SAmy Kwan #include <stdio.h>
11*0b7c9e53SAmy Kwan 
12*0b7c9e53SAmy Kwan #include "floattitf_test.h"
13*0b7c9e53SAmy Kwan 
14*0b7c9e53SAmy Kwan /* The long double representation, with the high and low portions of
15*0b7c9e53SAmy Kwan  * the long double, and the corresponding bit patterns of each double. */
16*0b7c9e53SAmy Kwan typedef union {
17*0b7c9e53SAmy Kwan   long double ld;
18*0b7c9e53SAmy Kwan   double d[2]; /* [0] is the high double, [1] is the low double. */
19*0b7c9e53SAmy Kwan   unsigned long long ull[2]; /* High and low doubles as 64-bit integers. */
20*0b7c9e53SAmy Kwan } ldUnion;
21*0b7c9e53SAmy Kwan 
22*0b7c9e53SAmy Kwan long double __floattitf(__int128_t);
23*0b7c9e53SAmy Kwan 
main(int argc,char * argv[])24*0b7c9e53SAmy Kwan int main(int argc, char *argv[]) {
25*0b7c9e53SAmy Kwan   /* Necessary long double and 128 bit integer declarations used to
26*0b7c9e53SAmy Kwan    * compare computed and expected high and low portions of the
27*0b7c9e53SAmy Kwan    * IBM double-double. */
28*0b7c9e53SAmy Kwan   ldUnion expectedLongDouble;
29*0b7c9e53SAmy Kwan   ldUnion computedLongDouble;
30*0b7c9e53SAmy Kwan   __int128_t result128;
31*0b7c9e53SAmy Kwan 
32*0b7c9e53SAmy Kwan   for (int i = 0; i < numTests; ++i) {
33*0b7c9e53SAmy Kwan     /* Set the expected high and low values of the long double,
34*0b7c9e53SAmy Kwan      * and the 128 bit integer input to be converted. */
35*0b7c9e53SAmy Kwan     expectedLongDouble.d[0] = tests[i].hi;
36*0b7c9e53SAmy Kwan     expectedLongDouble.d[1] = tests[i].lo;
37*0b7c9e53SAmy Kwan     result128 = tests[i].input128;
38*0b7c9e53SAmy Kwan 
39*0b7c9e53SAmy Kwan     /* Get the computed long double from the int128->long double conversion
40*0b7c9e53SAmy Kwan      * and check for errors between high and low portions. */
41*0b7c9e53SAmy Kwan     computedLongDouble.ld = __floattitf(result128);
42*0b7c9e53SAmy Kwan 
43*0b7c9e53SAmy Kwan     if ((computedLongDouble.d[0] != expectedLongDouble.d[0]) ||
44*0b7c9e53SAmy Kwan         (computedLongDouble.d[1] != expectedLongDouble.d[1])) {
45*0b7c9e53SAmy Kwan 
46*0b7c9e53SAmy Kwan       printf("Error on __floattitf( 0x%016llx 0x%016llx ):\n",
47*0b7c9e53SAmy Kwan              (long long)(tests[i].input128 >> 64),
48*0b7c9e53SAmy Kwan              (long long)tests[i].input128);
49*0b7c9e53SAmy Kwan       printf("\tExpected value - %La = ( %a, %a )\n", expectedLongDouble.ld,
50*0b7c9e53SAmy Kwan              expectedLongDouble.d[0], expectedLongDouble.d[1]);
51*0b7c9e53SAmy Kwan       printf("\tComputed value - %La = ( %a, %a )\n\n", computedLongDouble.ld,
52*0b7c9e53SAmy Kwan              computedLongDouble.d[0], computedLongDouble.d[1]);
53*0b7c9e53SAmy Kwan 
54*0b7c9e53SAmy Kwan       return 1;
55*0b7c9e53SAmy Kwan     }
56*0b7c9e53SAmy Kwan   }
57*0b7c9e53SAmy Kwan 
58*0b7c9e53SAmy Kwan   return 0;
59*0b7c9e53SAmy Kwan }
60