xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/ppc/fixtfti.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 //===--- lib/builtins/ppc/fixtfti.c - Convert long double->int128 *-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 converting the 128bit IBM/PowerPC long double (double-
10 // double) data type to a signed 128 bit integer.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "../int_math.h"
15 
16 // Convert long double into a signed 128-bit integer.
17 __int128_t __fixtfti(long double input) {
18 
19   // If we are trying to convert a NaN, return the NaN bit pattern.
20   if (crt_isnan(input)) {
21     return ((__uint128_t)0x7FF8000000000000ll) << 64 |
22            (__uint128_t)0x0000000000000000ll;
23   }
24 
25   // Note: overflow is an undefined behavior for this conversion.
26   // For this reason, overflow is not checked here.
27 
28   // If the long double is negative, use unsigned conversion from its absolute
29   // value.
30   if (input < 0.0) {
31     __uint128_t result = (__uint128_t)(-input);
32     return -((__int128_t)result);
33   }
34 
35   // Otherwise, use unsigned conversion from the input value.
36   __uint128_t result = (__uint128_t)input;
37   return result;
38 }
39