xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/floatsisf.c (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric //===-- lib/floatsisf.c - integer -> single-precision conversion --*- C -*-===//
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 integer to single-precision conversion for the
100b57cec5SDimitry Andric // compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even
110b57cec5SDimitry Andric // mode.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #define SINGLE_PRECISION
160b57cec5SDimitry Andric #include "fp_lib.h"
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric #include "int_lib.h"
190b57cec5SDimitry Andric 
201fd87a68SDimitry Andric COMPILER_RT_ABI fp_t __floatsisf(si_int a) {
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric   const int aWidth = sizeof a * CHAR_BIT;
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric   // Handle zero as a special case to protect clz
250b57cec5SDimitry Andric   if (a == 0)
260b57cec5SDimitry Andric     return fromRep(0);
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric   // All other cases begin by extracting the sign and absolute value of a
290b57cec5SDimitry Andric   rep_t sign = 0;
30*5f757f3fSDimitry Andric   su_int aAbs = (su_int)a;
310b57cec5SDimitry Andric   if (a < 0) {
320b57cec5SDimitry Andric     sign = signBit;
33*5f757f3fSDimitry Andric     aAbs = -aAbs;
340b57cec5SDimitry Andric   }
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric   // Exponent of (fp_t)a is the width of abs(a).
37*5f757f3fSDimitry Andric   const int exponent = (aWidth - 1) - clzsi(aAbs);
380b57cec5SDimitry Andric   rep_t result;
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric   // Shift a into the significand field, rounding if it is a right-shift
410b57cec5SDimitry Andric   if (exponent <= significandBits) {
420b57cec5SDimitry Andric     const int shift = significandBits - exponent;
43*5f757f3fSDimitry Andric     result = (rep_t)aAbs << shift ^ implicitBit;
440b57cec5SDimitry Andric   } else {
450b57cec5SDimitry Andric     const int shift = exponent - significandBits;
46*5f757f3fSDimitry Andric     result = (rep_t)aAbs >> shift ^ implicitBit;
47*5f757f3fSDimitry Andric     rep_t round = (rep_t)aAbs << (typeWidth - shift);
480b57cec5SDimitry Andric     if (round > signBit)
490b57cec5SDimitry Andric       result++;
500b57cec5SDimitry Andric     if (round == signBit)
510b57cec5SDimitry Andric       result += result & 1;
520b57cec5SDimitry Andric   }
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric   // Insert the exponent
550b57cec5SDimitry Andric   result += (rep_t)(exponent + exponentBias) << significandBits;
560b57cec5SDimitry Andric   // Insert the sign bit and return
570b57cec5SDimitry Andric   return fromRep(result | sign);
580b57cec5SDimitry Andric }
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric #if defined(__ARM_EABI__)
610b57cec5SDimitry Andric #if defined(COMPILER_RT_ARMHF_TARGET)
620b57cec5SDimitry Andric AEABI_RTABI fp_t __aeabi_i2f(int a) { return __floatsisf(a); }
630b57cec5SDimitry Andric #else
640b57cec5SDimitry Andric COMPILER_RT_ALIAS(__floatsisf, __aeabi_i2f)
650b57cec5SDimitry Andric #endif
660b57cec5SDimitry Andric #endif
67