mirror of
https://github.com/llvm-mirror/libcxx.git
synced 2025-10-21 23:30:38 +08:00
Fix FP comparisons when SSE isn't available
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@349387 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
#include <cassert>
|
||||
|
||||
#include "hexfloat.h"
|
||||
#include "truncate_fp.h"
|
||||
|
||||
// convertible to int/float/double/etc
|
||||
template <class T, int N=0>
|
||||
@@ -807,23 +808,31 @@ void test_atanh()
|
||||
assert(atanh(0) == 0);
|
||||
}
|
||||
|
||||
void test_cbrt()
|
||||
{
|
||||
static_assert((std::is_same<decltype(cbrt((float)0)), float>::value), "");
|
||||
static_assert((std::is_same<decltype(cbrt((bool)0)), double>::value), "");
|
||||
static_assert((std::is_same<decltype(cbrt((unsigned short)0)), double>::value), "");
|
||||
static_assert((std::is_same<decltype(cbrt((int)0)), double>::value), "");
|
||||
static_assert((std::is_same<decltype(cbrt((unsigned int)0)), double>::value), "");
|
||||
static_assert((std::is_same<decltype(cbrt((long)0)), double>::value), "");
|
||||
static_assert((std::is_same<decltype(cbrt((unsigned long)0)), double>::value), "");
|
||||
static_assert((std::is_same<decltype(cbrt((long long)0)), double>::value), "");
|
||||
static_assert((std::is_same<decltype(cbrt((unsigned long long)0)), double>::value), "");
|
||||
static_assert((std::is_same<decltype(cbrt((double)0)), double>::value), "");
|
||||
static_assert((std::is_same<decltype(cbrt((long double)0)), long double>::value), "");
|
||||
void test_cbrt() {
|
||||
static_assert((std::is_same<decltype(cbrt((float) 0)), float>::value), "");
|
||||
static_assert((std::is_same<decltype(cbrt((bool) 0)), double>::value), "");
|
||||
static_assert((std::is_same<decltype(cbrt((unsigned short) 0)),
|
||||
double>::value), "");
|
||||
static_assert((std::is_same<decltype(cbrt((int) 0)), double>::value), "");
|
||||
static_assert((std::is_same<decltype(cbrt((unsigned int) 0)),
|
||||
double>::value), "");
|
||||
static_assert((std::is_same<decltype(cbrt((long) 0)), double>::value), "");
|
||||
static_assert((std::is_same<decltype(cbrt((unsigned long) 0)),
|
||||
double>::value), "");
|
||||
static_assert((std::is_same<decltype(cbrt((long long) 0)), double>::value),
|
||||
"");
|
||||
static_assert((std::is_same<decltype(cbrt((unsigned long long) 0)),
|
||||
double>::value), "");
|
||||
static_assert((std::is_same<decltype(cbrt((double) 0)), double>::value),
|
||||
"");
|
||||
static_assert((std::is_same<decltype(cbrt((long double) 0)),
|
||||
long double>::value), "");
|
||||
static_assert((std::is_same<decltype(cbrtf(0)), float>::value), "");
|
||||
static_assert((std::is_same<decltype(cbrtl(0)), long double>::value), "");
|
||||
static_assert((std::is_same<decltype(cbrt(Ambiguous())), Ambiguous>::value), "");
|
||||
assert(cbrt(1) == 1);
|
||||
static_assert((std::is_same<decltype(cbrt(Ambiguous())), Ambiguous>::value),
|
||||
"");
|
||||
assert(truncate_fp(cbrt(1)) == 1);
|
||||
|
||||
}
|
||||
|
||||
void test_copysign()
|
||||
|
@@ -16,6 +16,7 @@
|
||||
|
||||
#include "test_macros.h"
|
||||
#include "hexfloat.h"
|
||||
#include "truncate_fp.h"
|
||||
|
||||
// convertible to int/float/double/etc
|
||||
template <class T, int N=0>
|
||||
@@ -860,7 +861,7 @@ void test_cbrt()
|
||||
static_assert((std::is_same<decltype(std::cbrtf(0)), float>::value), "");
|
||||
static_assert((std::is_same<decltype(std::cbrtl(0)), long double>::value), "");
|
||||
static_assert((std::is_same<decltype(cbrt(Ambiguous())), Ambiguous>::value), "");
|
||||
assert(std::cbrt(1) == 1);
|
||||
assert(truncate_fp(std::cbrt(1)) == 1);
|
||||
}
|
||||
|
||||
void test_copysign()
|
||||
|
@@ -15,6 +15,8 @@
|
||||
#include <random>
|
||||
#include <cassert>
|
||||
|
||||
#include "truncate_fp.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
@@ -22,35 +24,35 @@ int main()
|
||||
typedef float F;
|
||||
E r;
|
||||
F f = std::generate_canonical<F, 0>(r);
|
||||
assert(f == (16807 - E::min()) / (E::max() - E::min() + F(1)));
|
||||
assert(f == truncate_fp((16807 - E::min()) / (E::max() - E::min() + F(1))));
|
||||
}
|
||||
{
|
||||
typedef std::minstd_rand0 E;
|
||||
typedef float F;
|
||||
E r;
|
||||
F f = std::generate_canonical<F, 1>(r);
|
||||
assert(f == (16807 - E::min()) / (E::max() - E::min() + F(1)));
|
||||
assert(f == truncate_fp((16807 - E::min()) / (E::max() - E::min() + F(1))));
|
||||
}
|
||||
{
|
||||
typedef std::minstd_rand0 E;
|
||||
typedef float F;
|
||||
E r;
|
||||
F f = std::generate_canonical<F, std::numeric_limits<F>::digits - 1>(r);
|
||||
assert(f == (16807 - E::min()) / (E::max() - E::min() + F(1)));
|
||||
assert(f == truncate_fp((16807 - E::min()) / (E::max() - E::min() + F(1))));
|
||||
}
|
||||
{
|
||||
typedef std::minstd_rand0 E;
|
||||
typedef float F;
|
||||
E r;
|
||||
F f = std::generate_canonical<F, std::numeric_limits<F>::digits>(r);
|
||||
assert(f == (16807 - E::min()) / (E::max() - E::min() + F(1)));
|
||||
assert(f == truncate_fp((16807 - E::min()) / (E::max() - E::min() + F(1))));
|
||||
}
|
||||
{
|
||||
typedef std::minstd_rand0 E;
|
||||
typedef float F;
|
||||
E r;
|
||||
F f = std::generate_canonical<F, std::numeric_limits<F>::digits + 1>(r);
|
||||
assert(f == (16807 - E::min()) / (E::max() - E::min() + F(1)));
|
||||
assert(f == truncate_fp((16807 - E::min()) / (E::max() - E::min() + F(1))));
|
||||
}
|
||||
|
||||
{
|
||||
@@ -58,43 +60,43 @@ int main()
|
||||
typedef double F;
|
||||
E r;
|
||||
F f = std::generate_canonical<F, 0>(r);
|
||||
assert(f == (16807 - E::min()) / (E::max() - E::min() + F(1)));
|
||||
assert(f == truncate_fp((16807 - E::min()) / (E::max() - E::min() + F(1))));
|
||||
}
|
||||
{
|
||||
typedef std::minstd_rand0 E;
|
||||
typedef double F;
|
||||
E r;
|
||||
F f = std::generate_canonical<F, 1>(r);
|
||||
assert(f == (16807 - E::min()) / (E::max() - E::min() + F(1)));
|
||||
assert(f == truncate_fp((16807 - E::min()) / (E::max() - E::min() + F(1))));
|
||||
}
|
||||
{
|
||||
typedef std::minstd_rand0 E;
|
||||
typedef double F;
|
||||
E r;
|
||||
F f = std::generate_canonical<F, std::numeric_limits<F>::digits - 1>(r);
|
||||
assert(f ==
|
||||
assert(f == truncate_fp(
|
||||
(16807 - E::min() +
|
||||
(282475249 - E::min()) * (E::max() - E::min() + F(1))) /
|
||||
((E::max() - E::min() + F(1)) * (E::max() - E::min() + F(1))));
|
||||
((E::max() - E::min() + F(1)) * (E::max() - E::min() + F(1)))));
|
||||
}
|
||||
{
|
||||
typedef std::minstd_rand0 E;
|
||||
typedef double F;
|
||||
E r;
|
||||
F f = std::generate_canonical<F, std::numeric_limits<F>::digits>(r);
|
||||
assert(f ==
|
||||
assert(f == truncate_fp(
|
||||
(16807 - E::min() +
|
||||
(282475249 - E::min()) * (E::max() - E::min() + F(1))) /
|
||||
((E::max() - E::min() + F(1)) * (E::max() - E::min() + F(1))));
|
||||
((E::max() - E::min() + F(1)) * (E::max() - E::min() + F(1)))));
|
||||
}
|
||||
{
|
||||
typedef std::minstd_rand0 E;
|
||||
typedef double F;
|
||||
E r;
|
||||
F f = std::generate_canonical<F, std::numeric_limits<F>::digits + 1>(r);
|
||||
assert(f ==
|
||||
assert(f == truncate_fp(
|
||||
(16807 - E::min() +
|
||||
(282475249 - E::min()) * (E::max() - E::min() + F(1))) /
|
||||
((E::max() - E::min() + F(1)) * (E::max() - E::min() + F(1))));
|
||||
((E::max() - E::min() + F(1)) * (E::max() - E::min() + F(1)))));
|
||||
}
|
||||
}
|
||||
|
@@ -20,6 +20,7 @@
|
||||
#include <cassert>
|
||||
|
||||
#include "test_macros.h"
|
||||
#include "truncate_fp.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
@@ -41,7 +42,7 @@ int main()
|
||||
{
|
||||
std::chrono::duration<int, std::ratio<2, 3> > s1(30);
|
||||
std::chrono::duration<double, std::ratio<3, 5> > s2(5);
|
||||
assert(s1 / s2 == 20./3);
|
||||
assert(s1 / s2 == truncate_fp(20./3));
|
||||
}
|
||||
#if TEST_STD_VER >= 11
|
||||
{
|
||||
|
23
test/support/truncate_fp.h
Normal file
23
test/support/truncate_fp.h
Normal file
@@ -0,0 +1,23 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
inline long double truncate_fp(long double val) {
|
||||
volatile long double sink = val;
|
||||
return sink;
|
||||
}
|
||||
|
||||
inline double truncate_fp(double val) {
|
||||
volatile double sink = val;
|
||||
return sink;
|
||||
}
|
||||
|
||||
inline float truncate_fp(float val) {
|
||||
volatile float sink = val;
|
||||
return sink;
|
||||
}
|
Reference in New Issue
Block a user