xref: /openbsd-src/gnu/llvm/compiler-rt/lib/builtins/floatdidf.c (revision 810390e339a5425391477d5d41c78d7cab2424ac)
13cab2bb3Spatrick //===-- floatdidf.c - Implement __floatdidf -------------------------------===//
23cab2bb3Spatrick //
33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick //
73cab2bb3Spatrick //===----------------------------------------------------------------------===//
83cab2bb3Spatrick //
93cab2bb3Spatrick // This file implements __floatdidf for the compiler_rt library.
103cab2bb3Spatrick //
113cab2bb3Spatrick //===----------------------------------------------------------------------===//
123cab2bb3Spatrick 
133cab2bb3Spatrick #include "int_lib.h"
143cab2bb3Spatrick 
153cab2bb3Spatrick // Returns: convert a to a double, rounding toward even.
163cab2bb3Spatrick 
173cab2bb3Spatrick // Assumption: double is a IEEE 64 bit floating point type
183cab2bb3Spatrick //             di_int is a 64 bit integral type
193cab2bb3Spatrick 
203cab2bb3Spatrick // seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm
213cab2bb3Spatrick // mmmm
223cab2bb3Spatrick 
23d89ec533Spatrick #ifndef __SOFTFP__
243cab2bb3Spatrick // Support for systems that have hardware floating-point; we'll set the inexact
253cab2bb3Spatrick // flag as a side-effect of this computation.
263cab2bb3Spatrick 
__floatdidf(di_int a)273cab2bb3Spatrick COMPILER_RT_ABI double __floatdidf(di_int a) {
283cab2bb3Spatrick   static const double twop52 = 4503599627370496.0; // 0x1.0p52
293cab2bb3Spatrick   static const double twop32 = 4294967296.0;       // 0x1.0p32
303cab2bb3Spatrick 
313cab2bb3Spatrick   union {
323cab2bb3Spatrick     int64_t x;
333cab2bb3Spatrick     double d;
343cab2bb3Spatrick   } low = {.d = twop52};
353cab2bb3Spatrick 
363cab2bb3Spatrick   const double high = (int32_t)(a >> 32) * twop32;
373cab2bb3Spatrick   low.x |= a & INT64_C(0x00000000ffffffff);
383cab2bb3Spatrick 
393cab2bb3Spatrick   const double result = (high - twop52) + low.d;
403cab2bb3Spatrick   return result;
413cab2bb3Spatrick }
423cab2bb3Spatrick 
433cab2bb3Spatrick #else
443cab2bb3Spatrick // Support for systems that don't have hardware floating-point; there are no
453cab2bb3Spatrick // flags to set, and we don't want to code-gen to an unknown soft-float
463cab2bb3Spatrick // implementation.
473cab2bb3Spatrick 
__floatdidf(di_int a)483cab2bb3Spatrick COMPILER_RT_ABI double __floatdidf(di_int a) {
493cab2bb3Spatrick   if (a == 0)
503cab2bb3Spatrick     return 0.0;
513cab2bb3Spatrick   const unsigned N = sizeof(di_int) * CHAR_BIT;
523cab2bb3Spatrick   const di_int s = a >> (N - 1);
533cab2bb3Spatrick   a = (a ^ s) - s;
543cab2bb3Spatrick   int sd = N - __builtin_clzll(a); // number of significant digits
553cab2bb3Spatrick   int e = sd - 1;                  // exponent
563cab2bb3Spatrick   if (sd > DBL_MANT_DIG) {
573cab2bb3Spatrick     //  start:  0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
583cab2bb3Spatrick     //  finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
593cab2bb3Spatrick     //                                                12345678901234567890123456
603cab2bb3Spatrick     //  1 = msb 1 bit
613cab2bb3Spatrick     //  P = bit DBL_MANT_DIG-1 bits to the right of 1
623cab2bb3Spatrick     // Q = bit DBL_MANT_DIG bits to the right of 1
633cab2bb3Spatrick     //  R = "or" of all bits to the right of Q
643cab2bb3Spatrick     switch (sd) {
653cab2bb3Spatrick     case DBL_MANT_DIG + 1:
663cab2bb3Spatrick       a <<= 1;
673cab2bb3Spatrick       break;
683cab2bb3Spatrick     case DBL_MANT_DIG + 2:
693cab2bb3Spatrick       break;
703cab2bb3Spatrick     default:
713cab2bb3Spatrick       a = ((du_int)a >> (sd - (DBL_MANT_DIG + 2))) |
723cab2bb3Spatrick           ((a & ((du_int)(-1) >> ((N + DBL_MANT_DIG + 2) - sd))) != 0);
733cab2bb3Spatrick     };
743cab2bb3Spatrick     // finish:
753cab2bb3Spatrick     a |= (a & 4) != 0; // Or P into R
763cab2bb3Spatrick     ++a;               // round - this step may add a significant bit
773cab2bb3Spatrick     a >>= 2;           // dump Q and R
783cab2bb3Spatrick     // a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits
793cab2bb3Spatrick     if (a & ((du_int)1 << DBL_MANT_DIG)) {
803cab2bb3Spatrick       a >>= 1;
813cab2bb3Spatrick       ++e;
823cab2bb3Spatrick     }
833cab2bb3Spatrick     // a is now rounded to DBL_MANT_DIG bits
843cab2bb3Spatrick   } else {
853cab2bb3Spatrick     a <<= (DBL_MANT_DIG - sd);
863cab2bb3Spatrick     // a is now rounded to DBL_MANT_DIG bits
873cab2bb3Spatrick   }
883cab2bb3Spatrick   double_bits fb;
893cab2bb3Spatrick   fb.u.s.high = ((su_int)s & 0x80000000) |        // sign
901f9cb04fSpatrick                 ((su_int)(e + 1023) << 20) |      // exponent
913cab2bb3Spatrick                 ((su_int)(a >> 32) & 0x000FFFFF); // mantissa-high
923cab2bb3Spatrick   fb.u.s.low = (su_int)a;                         // mantissa-low
933cab2bb3Spatrick   return fb.f;
943cab2bb3Spatrick }
953cab2bb3Spatrick #endif
963cab2bb3Spatrick 
973cab2bb3Spatrick #if defined(__ARM_EABI__)
983cab2bb3Spatrick #if defined(COMPILER_RT_ARMHF_TARGET)
__aeabi_l2d(di_int a)993cab2bb3Spatrick AEABI_RTABI double __aeabi_l2d(di_int a) { return __floatdidf(a); }
1003cab2bb3Spatrick #else
1013cab2bb3Spatrick COMPILER_RT_ALIAS(__floatdidf, __aeabi_l2d)
1023cab2bb3Spatrick #endif
1033cab2bb3Spatrick #endif
104*810390e3Srobert 
105*810390e3Srobert #if defined(__MINGW32__) && defined(__arm__)
106*810390e3Srobert COMPILER_RT_ALIAS(__floatdidf, __i64tod)
107*810390e3Srobert #endif
108