Bug 1601406 [wpt PR 20618] - Advertise DocumentPolicy & Network Err when receive...
[gecko.git] / mfbt / FloatingPoint.cpp
blob96fc320f9c6d7a21c2717d56c9f63cd0181744a5
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/. */
7 /* Implementations of FloatingPoint functions */
9 #include "mozilla/FloatingPoint.h"
11 #include <cfloat> // for FLT_MAX
13 namespace mozilla {
15 bool IsFloat32Representable(double aValue) {
16 // NaNs and infinities are representable.
17 if (!IsFinite(aValue)) {
18 return true;
21 // If it exceeds finite |float| range, casting to |double| is always undefined
22 // behavior per C++11 [conv.double]p1 last sentence.
23 if (Abs(aValue) > FLT_MAX) {
24 return false;
27 // But if it's within finite range, then either it's 1) an exact value and so
28 // representable, or 2) it's "between two adjacent destination values" and
29 // safe to cast to "an implementation-defined choice of either of those
30 // values".
31 auto valueAsFloat = static_cast<float>(aValue);
33 // Per [conv.fpprom] this never changes value.
34 auto valueAsFloatAsDouble = static_cast<double>(valueAsFloat);
36 // Finally, in 1) exact representable value equals exact representable value,
37 // or 2) *changed* value does not equal original value, ergo unrepresentable.
38 return valueAsFloatAsDouble == aValue;
41 } /* namespace mozilla */