xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/floatsidf.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 //===-- lib/floatsidf.c - integer -> double-precision conversion --*- C -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements integer to double-precision conversion for the
10 // compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even
11 // mode.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #define DOUBLE_PRECISION
16 #include "fp_lib.h"
17 
18 #include "int_lib.h"
19 
20 COMPILER_RT_ABI fp_t __floatsidf(si_int a) {
21 
22   const int aWidth = sizeof a * CHAR_BIT;
23 
24   // Handle zero as a special case to protect clz
25   if (a == 0)
26     return fromRep(0);
27 
28   // All other cases begin by extracting the sign and absolute value of a
29   rep_t sign = 0;
30   su_int aAbs = (su_int)a;
31   if (a < 0) {
32     sign = signBit;
33     aAbs = -aAbs;
34   }
35 
36   // Exponent of (fp_t)a is the width of abs(a).
37   const int exponent = (aWidth - 1) - clzsi(aAbs);
38   rep_t result;
39 
40   // Shift a into the significand field and clear the implicit bit.  Extra
41   // cast to unsigned int is necessary to get the correct behavior for
42   // the input INT_MIN.
43   const int shift = significandBits - exponent;
44   result = (rep_t)aAbs << shift ^ implicitBit;
45 
46   // Insert the exponent
47   result += (rep_t)(exponent + exponentBias) << significandBits;
48   // Insert the sign bit and return
49   return fromRep(result | sign);
50 }
51 
52 #if defined(__ARM_EABI__)
53 #if defined(COMPILER_RT_ARMHF_TARGET)
54 AEABI_RTABI fp_t __aeabi_i2d(si_int a) { return __floatsidf(a); }
55 #else
56 COMPILER_RT_ALIAS(__floatsidf, __aeabi_i2d)
57 #endif
58 #endif
59