1//===-- lib/builtins/ppc/floattitf.c - Convert int128->long double -*-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 a signed 128 bit integer to a 128bit IBM /
10// PowerPC long double (double-double) value.
11//
12//===----------------------------------------------------------------------===//
13
14#include <stdint.h>
15
16// Conversions from signed and unsigned 64-bit int to long double.
17long double __floatditf(int64_t);
18long double __floatunditf(uint64_t);
19
20// Convert a signed 128-bit integer to long double.
21// This uses the following property: Let hi and lo be 64-bits each,
22// and let signed_val_k() and unsigned_val_k() be the value of the
23// argument interpreted as a signed or unsigned k-bit integer. Then,
24//
25// signed_val_128(hi,lo) = signed_val_64(hi) * 2^64 + unsigned_val_64(lo)
26// = (long double)hi * 2^64 + (long double)lo,
27//
28// where (long double)hi and (long double)lo are signed and
29// unsigned 64-bit integer to long double conversions, respectively.
30long double __floattitf(__int128_t arg) {
31 // Split the int128 argument into 64-bit high and low int64 parts.
32 int64_t ArgHiPart = (int64_t)(arg >> 64);
33 uint64_t ArgLoPart = (uint64_t)arg;
34
35 // Convert each 64-bit part into long double. The high part
36 // must be a signed conversion and the low part an unsigned conversion
37 // to ensure the correct result.
38 long double ConvertedHiPart = __floatditf(ArgHiPart);
39 long double ConvertedLoPart = __floatunditf(ArgLoPart);
40
41 // The low bit of ArgHiPart corresponds to the 2^64 bit in arg.
42 // Multiply the high part by 2^64 to undo the right shift by 64-bits
43 // done in the splitting. Then, add to the low part to obtain the
44 // final result.
45 return ((ConvertedHiPart * 0x1.0p64) + ConvertedLoPart);
46}
47

source code of compiler-rt/lib/builtins/ppc/floattitf.c