1*3cab2bb3Spatrick //===-- lib/builtins/ppc/floattitf.c - Convert int128->long double -*-C -*-===// 2*3cab2bb3Spatrick // 3*3cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*3cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information. 5*3cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*3cab2bb3Spatrick // 7*3cab2bb3Spatrick //===----------------------------------------------------------------------===// 8*3cab2bb3Spatrick // 9*3cab2bb3Spatrick // This file implements converting a signed 128 bit integer to a 128bit IBM / 10*3cab2bb3Spatrick // PowerPC long double (double-double) value. 11*3cab2bb3Spatrick // 12*3cab2bb3Spatrick //===----------------------------------------------------------------------===// 13*3cab2bb3Spatrick 14*3cab2bb3Spatrick #include <stdint.h> 15*3cab2bb3Spatrick 16*3cab2bb3Spatrick // Conversions from signed and unsigned 64-bit int to long double. 17*3cab2bb3Spatrick long double __floatditf(int64_t); 18*3cab2bb3Spatrick long double __floatunditf(uint64_t); 19*3cab2bb3Spatrick 20*3cab2bb3Spatrick // Convert a signed 128-bit integer to long double. 21*3cab2bb3Spatrick // This uses the following property: Let hi and lo be 64-bits each, 22*3cab2bb3Spatrick // and let signed_val_k() and unsigned_val_k() be the value of the 23*3cab2bb3Spatrick // argument interpreted as a signed or unsigned k-bit integer. Then, 24*3cab2bb3Spatrick // 25*3cab2bb3Spatrick // signed_val_128(hi,lo) = signed_val_64(hi) * 2^64 + unsigned_val_64(lo) 26*3cab2bb3Spatrick // = (long double)hi * 2^64 + (long double)lo, 27*3cab2bb3Spatrick // 28*3cab2bb3Spatrick // where (long double)hi and (long double)lo are signed and 29*3cab2bb3Spatrick // unsigned 64-bit integer to long double conversions, respectively. __floattitf(__int128_t arg)30*3cab2bb3Spatricklong double __floattitf(__int128_t arg) { 31*3cab2bb3Spatrick // Split the int128 argument into 64-bit high and low int64 parts. 32*3cab2bb3Spatrick int64_t ArgHiPart = (int64_t)(arg >> 64); 33*3cab2bb3Spatrick uint64_t ArgLoPart = (uint64_t)arg; 34*3cab2bb3Spatrick 35*3cab2bb3Spatrick // Convert each 64-bit part into long double. The high part 36*3cab2bb3Spatrick // must be a signed conversion and the low part an unsigned conversion 37*3cab2bb3Spatrick // to ensure the correct result. 38*3cab2bb3Spatrick long double ConvertedHiPart = __floatditf(ArgHiPart); 39*3cab2bb3Spatrick long double ConvertedLoPart = __floatunditf(ArgLoPart); 40*3cab2bb3Spatrick 41*3cab2bb3Spatrick // The low bit of ArgHiPart corresponds to the 2^64 bit in arg. 42*3cab2bb3Spatrick // Multiply the high part by 2^64 to undo the right shift by 64-bits 43*3cab2bb3Spatrick // done in the splitting. Then, add to the low part to obtain the 44*3cab2bb3Spatrick // final result. 45*3cab2bb3Spatrick return ((ConvertedHiPart * 0x1.0p64) + ConvertedLoPart); 46*3cab2bb3Spatrick } 47