/src/mozilla-central/mfbt/FloatingPoint.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | /* Implementations of FloatingPoint functions */ |
8 | | |
9 | | #include "mozilla/FloatingPoint.h" |
10 | | |
11 | | #include <cfloat> // for FLT_MAX |
12 | | |
13 | | namespace mozilla { |
14 | | |
15 | | bool |
16 | | IsFloat32Representable(double aValue) |
17 | 0 | { |
18 | 0 | // NaNs and infinities are representable. |
19 | 0 | if (!IsFinite(aValue)) { |
20 | 0 | return true; |
21 | 0 | } |
22 | 0 | |
23 | 0 | // If it exceeds finite |float| range, casting to |double| is always undefined |
24 | 0 | // behavior per C++11 [conv.double]p1 last sentence. |
25 | 0 | if (Abs(aValue) > FLT_MAX) { |
26 | 0 | return false; |
27 | 0 | } |
28 | 0 | |
29 | 0 | // But if it's within finite range, then either it's 1) an exact value and so |
30 | 0 | // representable, or 2) it's "between two adjacent destination values" and |
31 | 0 | // safe to cast to "an implementation-defined choice of either of those |
32 | 0 | // values". |
33 | 0 | auto valueAsFloat = static_cast<float>(aValue); |
34 | 0 |
|
35 | 0 | // Per [conv.fpprom] this never changes value. |
36 | 0 | auto valueAsFloatAsDouble = static_cast<double>(valueAsFloat); |
37 | 0 |
|
38 | 0 | // Finally, in 1) exact representable value equals exact representable value, |
39 | 0 | // or 2) *changed* value does not equal original value, ergo unrepresentable. |
40 | 0 | return valueAsFloatAsDouble == aValue; |
41 | 0 | } |
42 | | |
43 | | } /* namespace mozilla */ |