[clang] Handle __declspec() attributes in using
[llvm-project.git] / compiler-rt / lib / builtins / floatsisf.c
blobc01f81e41e8eaa166b9a06bedd02534d62bf46a0
1 //===-- lib/floatsisf.c - integer -> single-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 single-precision conversion for the
10 // compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even
11 // mode.
13 //===----------------------------------------------------------------------===//
15 #define SINGLE_PRECISION
16 #include "fp_lib.h"
18 #include "int_lib.h"
20 COMPILER_RT_ABI fp_t __floatsisf(si_int a) {
22 const int aWidth = sizeof a * CHAR_BIT;
24 // Handle zero as a special case to protect clz
25 if (a == 0)
26 return fromRep(0);
28 // All other cases begin by extracting the sign and absolute value of a
29 rep_t sign = 0;
30 if (a < 0) {
31 sign = signBit;
32 a = -a;
35 // Exponent of (fp_t)a is the width of abs(a).
36 const int exponent = (aWidth - 1) - clzsi(a);
37 rep_t result;
39 // Shift a into the significand field, rounding if it is a right-shift
40 if (exponent <= significandBits) {
41 const int shift = significandBits - exponent;
42 result = (rep_t)a << shift ^ implicitBit;
43 } else {
44 const int shift = exponent - significandBits;
45 result = (rep_t)a >> shift ^ implicitBit;
46 rep_t round = (rep_t)a << (typeWidth - shift);
47 if (round > signBit)
48 result++;
49 if (round == signBit)
50 result += result & 1;
53 // Insert the exponent
54 result += (rep_t)(exponent + exponentBias) << significandBits;
55 // Insert the sign bit and return
56 return fromRep(result | sign);
59 #if defined(__ARM_EABI__)
60 #if defined(COMPILER_RT_ARMHF_TARGET)
61 AEABI_RTABI fp_t __aeabi_i2f(int a) { return __floatsisf(a); }
62 #else
63 COMPILER_RT_ALIAS(__floatsisf, __aeabi_i2f)
64 #endif
65 #endif