xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/floatdidf.c (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
10b57cec5SDimitry Andric //===-- floatdidf.c - Implement __floatdidf -------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements __floatdidf for the compiler_rt library.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "int_lib.h"
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric // Returns: convert a to a double, rounding toward even.
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric // Assumption: double is a IEEE 64 bit floating point type
180b57cec5SDimitry Andric //             di_int is a 64 bit integral type
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric // seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm
210b57cec5SDimitry Andric // mmmm
220b57cec5SDimitry Andric 
23fe6060f1SDimitry Andric #ifndef __SOFTFP__
240b57cec5SDimitry Andric // Support for systems that have hardware floating-point; we'll set the inexact
250b57cec5SDimitry Andric // flag as a side-effect of this computation.
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric COMPILER_RT_ABI double __floatdidf(di_int a) {
280b57cec5SDimitry Andric   static const double twop52 = 4503599627370496.0; // 0x1.0p52
290b57cec5SDimitry Andric   static const double twop32 = 4294967296.0;       // 0x1.0p32
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric   union {
320b57cec5SDimitry Andric     int64_t x;
330b57cec5SDimitry Andric     double d;
340b57cec5SDimitry Andric   } low = {.d = twop52};
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric   const double high = (int32_t)(a >> 32) * twop32;
370b57cec5SDimitry Andric   low.x |= a & INT64_C(0x00000000ffffffff);
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric   const double result = (high - twop52) + low.d;
400b57cec5SDimitry Andric   return result;
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric #else
440b57cec5SDimitry Andric // Support for systems that don't have hardware floating-point; there are no
450b57cec5SDimitry Andric // flags to set, and we don't want to code-gen to an unknown soft-float
460b57cec5SDimitry Andric // implementation.
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric COMPILER_RT_ABI double __floatdidf(di_int a) {
490b57cec5SDimitry Andric   if (a == 0)
500b57cec5SDimitry Andric     return 0.0;
510b57cec5SDimitry Andric   const unsigned N = sizeof(di_int) * CHAR_BIT;
520b57cec5SDimitry Andric   const di_int s = a >> (N - 1);
530b57cec5SDimitry Andric   a = (a ^ s) - s;
540b57cec5SDimitry Andric   int sd = N - __builtin_clzll(a); // number of significant digits
550b57cec5SDimitry Andric   int e = sd - 1;                  // exponent
560b57cec5SDimitry Andric   if (sd > DBL_MANT_DIG) {
570b57cec5SDimitry Andric     //  start:  0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
580b57cec5SDimitry Andric     //  finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
590b57cec5SDimitry Andric     //                                                12345678901234567890123456
600b57cec5SDimitry Andric     //  1 = msb 1 bit
610b57cec5SDimitry Andric     //  P = bit DBL_MANT_DIG-1 bits to the right of 1
620b57cec5SDimitry Andric     // Q = bit DBL_MANT_DIG bits to the right of 1
630b57cec5SDimitry Andric     //  R = "or" of all bits to the right of Q
640b57cec5SDimitry Andric     switch (sd) {
650b57cec5SDimitry Andric     case DBL_MANT_DIG + 1:
660b57cec5SDimitry Andric       a <<= 1;
670b57cec5SDimitry Andric       break;
680b57cec5SDimitry Andric     case DBL_MANT_DIG + 2:
690b57cec5SDimitry Andric       break;
700b57cec5SDimitry Andric     default:
710b57cec5SDimitry Andric       a = ((du_int)a >> (sd - (DBL_MANT_DIG + 2))) |
720b57cec5SDimitry Andric           ((a & ((du_int)(-1) >> ((N + DBL_MANT_DIG + 2) - sd))) != 0);
730b57cec5SDimitry Andric     };
740b57cec5SDimitry Andric     // finish:
750b57cec5SDimitry Andric     a |= (a & 4) != 0; // Or P into R
760b57cec5SDimitry Andric     ++a;               // round - this step may add a significant bit
770b57cec5SDimitry Andric     a >>= 2;           // dump Q and R
780b57cec5SDimitry Andric     // a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits
790b57cec5SDimitry Andric     if (a & ((du_int)1 << DBL_MANT_DIG)) {
800b57cec5SDimitry Andric       a >>= 1;
810b57cec5SDimitry Andric       ++e;
820b57cec5SDimitry Andric     }
830b57cec5SDimitry Andric     // a is now rounded to DBL_MANT_DIG bits
840b57cec5SDimitry Andric   } else {
850b57cec5SDimitry Andric     a <<= (DBL_MANT_DIG - sd);
860b57cec5SDimitry Andric     // a is now rounded to DBL_MANT_DIG bits
870b57cec5SDimitry Andric   }
880b57cec5SDimitry Andric   double_bits fb;
890b57cec5SDimitry Andric   fb.u.s.high = ((su_int)s & 0x80000000) |        // sign
905ffd83dbSDimitry Andric                 ((su_int)(e + 1023) << 20) |      // exponent
910b57cec5SDimitry Andric                 ((su_int)(a >> 32) & 0x000FFFFF); // mantissa-high
920b57cec5SDimitry Andric   fb.u.s.low = (su_int)a;                         // mantissa-low
930b57cec5SDimitry Andric   return fb.f;
940b57cec5SDimitry Andric }
950b57cec5SDimitry Andric #endif
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric #if defined(__ARM_EABI__)
980b57cec5SDimitry Andric #if defined(COMPILER_RT_ARMHF_TARGET)
990b57cec5SDimitry Andric AEABI_RTABI double __aeabi_l2d(di_int a) { return __floatdidf(a); }
1000b57cec5SDimitry Andric #else
1010b57cec5SDimitry Andric COMPILER_RT_ALIAS(__floatdidf, __aeabi_l2d)
1020b57cec5SDimitry Andric #endif
1030b57cec5SDimitry Andric #endif
104*349cc55cSDimitry Andric 
105*349cc55cSDimitry Andric #if defined(__MINGW32__) && defined(__arm__)
106*349cc55cSDimitry Andric COMPILER_RT_ALIAS(__floatdidf, __i64tod)
107*349cc55cSDimitry Andric #endif
108