[PowerPC][compiler-rt][builtins]Add __fixtfti builtin on PowerPC

Implements __fixtfti builtin for PowerPC. This builtin converts a
long double (IBM double-double) to a signed int128. The conversion relies on
the unsigned conversion of the absolute value of the long double.

Tests included for both positive and negative long doubles.

Patch By: Baptiste Saleil

Differential Revision: https://reviews.llvm.org/D69730
This commit is contained in:
Lei Huang 2019-11-25 14:06:21 -06:00
parent 19edfb3728
commit 9e676d9c7e
4 changed files with 1647 additions and 0 deletions

View File

@ -519,6 +519,7 @@ set(mips64el_SOURCES ${GENERIC_TF_SOURCES}
set(powerpc64_SOURCES
ppc/divtc3.c
ppc/fixtfti.c
ppc/fixtfdi.c
ppc/fixunstfti.c
ppc/fixunstfdi.c

View File

@ -0,0 +1,38 @@
//===--- lib/builtins/ppc/fixtfti.c - Convert long double->int128 *-C -*---===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements converting the 128bit IBM/PowerPC long double (double-
// double) data type to a signed 128 bit integer.
//
//===----------------------------------------------------------------------===//
#include "../int_math.h"
// Convert long double into a signed 128-bit integer.
__int128_t __fixtfti(long double input) {
// If we are trying to convert a NaN, return the NaN bit pattern.
if (crt_isnan(input)) {
return ((__uint128_t)0x7FF8000000000000ll) << 64 |
(__uint128_t)0x0000000000000000ll;
}
// Note: overflow is an undefined behavior for this conversion.
// For this reason, overflow is not checked here.
// If the long double is negative, use unsigned conversion from its absolute
// value.
if (input < 0.0) {
__uint128_t result = (__uint128_t)(-input);
return -((__int128_t)result);
}
// Otherwise, use unsigned conversion from the input value.
__uint128_t result = (__uint128_t)input;
return result;
}

View File

@ -0,0 +1,53 @@
// REQUIRES: target-is-powerpc64le
// RUN: %clang_builtins %s %librt -o %t && %run %t
#include <stdint.h>
#include <stdio.h>
#include "fixtfti_test.h"
// The long double representation, with the high and low portions of
// the long double, and the corresponding bit patterns of each double.
typedef union {
long double ld;
double d[2]; // [0] is the high double, [1] is the low double.
unsigned long long ull[2]; // High and low doubles as 64-bit integers.
} ldUnion;
__int128_t __fixtfti(long double);
int main(int argc, char *argv[]) {
// Necessary long double and (unsigned) 128 bit integer
// declarations used to compare the computed and expected results
// from converting the IBM double-double to int128.
ldUnion ldInput;
__uint128_t expectedResult, computedResult;
for (int i = 0; i < numTests; ++i) {
// Set the expected 128 bit integer and the high and low
// values of the long double input.
ldInput.d[0] = testList[i].hiInput;
ldInput.d[1] = testList[i].loInput;
expectedResult = testList[i].result128;
// Get the computed 128 bit integer from the long double to
// int128 conversion. Here we cast to unsigned int128 to
// get the bit pattern of the result.
computedResult = (__uint128_t) __fixtfti(ldInput.ld);
if (computedResult != expectedResult) {
printf("Error for __fixtfti at input %La = ( %a , %a ):\n", ldInput.ld,
ldInput.d[0], ldInput.d[1]);
printf("\tExpected __int128_t: 0x%016llx 0x%016llx\n",
(unsigned long long)(expectedResult >> 64),
(unsigned long long)expectedResult);
printf("\tComputed __int128_t: 0x%016llx 0x%016llx\n\n",
(unsigned long long)(computedResult >> 64),
(unsigned long long)computedResult);
return 1;
}
}
return 0;
}

File diff suppressed because it is too large Load Diff