summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/global/qfloat16
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2019-02-21 20:54:03 +0100
committerEdward Welbourne <edward.welbourne@qt.io>2019-04-24 13:08:43 +0000
commit2eb849ed516afc8e602dbec3da0ac12db2cdf8ec (patch)
tree7a0dbc067a675f69a3aa05a28f3045ac0e81639e /tests/auto/corelib/global/qfloat16
parentb21b07877a96c175ee51e83e1b41425c2e67beb3 (diff)
Implement std::numeric_limits<qfloat16>
This shall make it more nearly a first-class numeric type; in particular, I need some of these for testlib's comparing and formatting of float16 to handle NaNs and infinities sensibly. Change-Id: Ic894dd0eb3e05653cd7645ab496463e7a884dff8 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib/global/qfloat16')
-rw-r--r--tests/auto/corelib/global/qfloat16/tst_qfloat16.cpp142
1 files changed, 142 insertions, 0 deletions
diff --git a/tests/auto/corelib/global/qfloat16/tst_qfloat16.cpp b/tests/auto/corelib/global/qfloat16/tst_qfloat16.cpp
index 241dccb90e..b5a77a1de6 100644
--- a/tests/auto/corelib/global/qfloat16/tst_qfloat16.cpp
+++ b/tests/auto/corelib/global/qfloat16/tst_qfloat16.cpp
@@ -1,5 +1,6 @@
/****************************************************************************
**
+** Copyright (C) 2019 The Qt Company Ltd.
** Copyright (C) 2016 by Southwest Research Institute (R)
** Contact: https://www.qt.io/licensing/
**
@@ -48,6 +49,7 @@ private slots:
void arithOps();
void floatToFloat16();
void floatFromFloat16();
+ void limits();
};
void tst_qfloat16::fuzzyCompare_data()
@@ -345,5 +347,145 @@ void tst_qfloat16::floatFromFloat16()
QCOMPARE(out[i], expected[i]);
}
+static qfloat16 powf16(qfloat16 base, int raise)
+{
+ const qfloat16 one(1.f);
+ if (raise < 0) {
+ raise = -raise;
+ base = one / base;
+ }
+ qfloat16 answer = (raise & 1) ? base : one;
+ while (raise > 0) {
+ raise >>= 1;
+ base *= base;
+ if (raise & 1)
+ answer *= base;
+ }
+ return answer;
+}
+
+void tst_qfloat16::limits()
+{
+ // *NOT* using QCOMPARE() on finite qfloat16 values, since that uses fuzzy
+ // comparison, and we need exact here.
+ using Bounds = std::numeric_limits<qfloat16>;
+ QVERIFY(Bounds::is_specialized);
+ QVERIFY(Bounds::is_signed);
+ QVERIFY(!Bounds::is_integer);
+ QVERIFY(!Bounds::is_exact);
+ QVERIFY(Bounds::is_iec559);
+ QVERIFY(Bounds::is_bounded);
+ QVERIFY(!Bounds::is_modulo);
+ QVERIFY(!Bounds::traps);
+ QVERIFY(Bounds::has_infinity);
+ QVERIFY(Bounds::has_quiet_NaN);
+ QVERIFY(Bounds::has_signaling_NaN);
+ QCOMPARE(Bounds::has_denorm, std::denorm_present);
+ QCOMPARE(Bounds::round_style, std::round_to_nearest);
+ QCOMPARE(Bounds::radix, 2);
+ // Untested: has_denorm_loss
+
+ // A few common values:
+ const qfloat16 zero(0), one(1), ten(10);
+ QVERIFY(qIsFinite(zero));
+ QVERIFY(!qIsInf(zero));
+ QVERIFY(!qIsNaN(zero));
+ QVERIFY(qIsFinite(one));
+ QVERIFY(!qIsInf(one));
+ QVERIFY(!qIsNaN(one));
+ QVERIFY(qIsFinite(ten));
+ QVERIFY(!qIsInf(ten));
+ QVERIFY(!qIsNaN(ten));
+
+ // digits in the mantissa, including the implicit 1 before the binary dot at its left:
+ QVERIFY(qfloat16(1 << (Bounds::digits - 1)) + one > qfloat16(1 << (Bounds::digits - 1)));
+ QVERIFY(qfloat16(1 << Bounds::digits) + one == qfloat16(1 << Bounds::digits));
+
+ // There is a wilful of-by-one in how m(ax|in)_exponent are defined; they're
+ // the lowest and highest n for which radix^{n-1} are normal and finite.
+ const qfloat16 two(Bounds::radix);
+ qfloat16 bit = powf16(two, Bounds::max_exponent - 1);
+ QVERIFY(qIsFinite(bit));
+ QVERIFY(qIsInf(bit * two));
+ bit = powf16(two, Bounds::min_exponent - 1);
+ QVERIFY(bit.isNormal());
+ QVERIFY(!(bit / two).isNormal());
+ QVERIFY(bit / two > zero);
+
+ // Base ten (with no matching off-by-one idiocy):
+ // the lowest negative number n such that 10^n is a valid normalized value
+ qfloat16 low10(powf16(ten, Bounds::min_exponent10));
+ QVERIFY(low10 > zero);
+ QVERIFY(low10.isNormal());
+ low10 /= ten;
+ QVERIFY(low10 == zero || !low10.isNormal());
+ // the largest positive number n such that 10^n is a representable finite value
+ qfloat16 high10(powf16(ten, Bounds::max_exponent10));
+ QVERIFY(high10 > zero);
+ QVERIFY(qIsFinite(high10));
+ QVERIFY(!qIsFinite(high10 * ten));
+
+ // How many digits are significant ? (Casts avoid linker errors ...)
+ QCOMPARE(int(Bounds::digits10), 3); // 9.79e-4 has enough sigificant digits:
+ qfloat16 below(9.785e-4f), above(9.794e-4f);
+#if 0 // Sadly, the QEMU x-compile for arm64 "optimises" comparisons:
+ const bool overOptimised = false;
+#else
+ const bool overOptimised = (below != above);
+ if (overOptimised)
+ QEXPECT_FAIL("", "Over-optimised on QEMU", Continue);
+#endif // (but it did, so should, pass everywhere else, confirming digits10 is indeed 3).
+ QVERIFY(below == above);
+ QCOMPARE(int(Bounds::max_digits10), 5); // we need 5 to distinguish these two:
+ QVERIFY(qfloat16(1000.5f) != qfloat16(1001.4f));
+
+ // Actual limiting values of the type:
+ const qfloat16 rose(one + Bounds::epsilon());
+ QVERIFY(rose > one);
+ if (overOptimised)
+ QEXPECT_FAIL("", "Over-optimised on QEMU", Continue);
+ QVERIFY(one + Bounds::epsilon() / rose == one);
+ QVERIFY(qIsInf(Bounds::infinity()));
+ QVERIFY(!qIsNaN(Bounds::infinity()));
+ QVERIFY(!qIsFinite(Bounds::infinity()));
+ // QCOMPARE(Bounds::infinity(), Bounds::infinity());
+ QVERIFY(Bounds::infinity() > -Bounds::infinity());
+ QVERIFY(Bounds::infinity() > zero);
+ QVERIFY(qIsInf(-Bounds::infinity()));
+ QVERIFY(!qIsNaN(-Bounds::infinity()));
+ QVERIFY(!qIsFinite(-Bounds::infinity()));
+ // QCOMPARE(-Bounds::infinity(), -Bounds::infinity());
+ QVERIFY(-Bounds::infinity() < zero);
+ QVERIFY(qIsNaN(Bounds::quiet_NaN()));
+ QVERIFY(!qIsInf(Bounds::quiet_NaN()));
+ QVERIFY(!qIsFinite(Bounds::quiet_NaN()));
+ QVERIFY(!(Bounds::quiet_NaN() == Bounds::quiet_NaN()));
+ // QCOMPARE(Bounds::quiet_NaN(), Bounds::quiet_NaN());
+ QVERIFY(Bounds::max() > zero);
+ QVERIFY(qIsFinite(Bounds::max()));
+ QVERIFY(!qIsInf(Bounds::max()));
+ QVERIFY(!qIsNaN(Bounds::max()));
+ QVERIFY(qIsInf(Bounds::max() * rose));
+ QVERIFY(Bounds::lowest() < zero);
+ QVERIFY(qIsFinite(Bounds::lowest()));
+ QVERIFY(!qIsInf(Bounds::lowest()));
+ QVERIFY(!qIsNaN(Bounds::lowest()));
+ QVERIFY(qIsInf(Bounds::lowest() * rose));
+ QVERIFY(Bounds::min() > zero);
+ QVERIFY(Bounds::min().isNormal());
+ QVERIFY(!(Bounds::min() / rose).isNormal());
+ QVERIFY(qIsFinite(Bounds::min()));
+ QVERIFY(!qIsInf(Bounds::min()));
+ QVERIFY(!qIsNaN(Bounds::min()));
+ QVERIFY(Bounds::denorm_min() > zero);
+ QVERIFY(!Bounds::denorm_min().isNormal());
+ QVERIFY(qIsFinite(Bounds::denorm_min()));
+ QVERIFY(!qIsInf(Bounds::denorm_min()));
+ QVERIFY(!qIsNaN(Bounds::denorm_min()));
+ if (overOptimised)
+ QEXPECT_FAIL("", "Over-optimised on QEMU", Continue);
+ QCOMPARE(Bounds::denorm_min() / rose, zero);
+}
+
QTEST_APPLESS_MAIN(tst_qfloat16)
#include "tst_qfloat16.moc"