]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/PureNaN.h
JavaScriptCore-7600.1.4.16.1.tar.gz
[apple/javascriptcore.git] / runtime / PureNaN.h
CommitLineData
81345200
A
1/*
2 * Copyright (C) 2014 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef PureNaN_h
27#define PureNaN_h
28
29#include <wtf/Assertions.h>
30#include <wtf/StdLibExtras.h>
31
32namespace JSC {
33
34// NaN (not-a-number) double values are central to how JavaScriptCore encodes JavaScript
35// values (JSValues). All values, including integers and non-numeric values, are always
36// encoded using the IEEE 854 binary double format. Non-double values are encoded using
37// a NaN with the sign bit set. The 51-bit payload is then used for encoding the actual
38// value - be it an integer or a pointer to an object, or something else. But we only
39// make use of the low 49 bits and the top 15 bits being all set to 1 is the indicator
40// that a value is not a double. Top 15 bits being set to 1 also indicate a signed
41// signaling NaN with some additional NaN payload bits.
42//
43// Our use of NaN encoding means that we have to be careful with how we use NaNs for
44// ordinary doubles. For example, it would be wrong to ever use a NaN that has the top
45// 15 bits set, as that would look like a non-double value to JSC.
46//
47// We can trust that on all of the hardware/OS combinations that we care about,
48// NaN-producing math operations never produce a NaN that looks like a tagged value. But
49// if we're ever in a situation where we worry about it, we can use purifyNaN() to get a
50// NaN that doesn't look like a tagged non-double value. The JavaScript language doesn't
51// distinguish between different flavors of NaN and there is no way to detect what kind
52// of NaN you have - hence so long as all double NaNs are purified then our tagging
53// scheme remains sound.
54//
55// It's worth noting that there are cases, like sin(), that will almost produce a NaN
56// that breaks us. sin(-inf) returns 0xfff8000000000000. This doesn't break us because
57// not all of the top 15 bits are set. But it's very close. Hence our assumptions about
58// NaN are just about the most aggressive assumptions we could possibly make without
59// having to call purifyNaN() in surprising places.
60//
61// For naming purposes, we say that a NaN is "pure" if it is safe to tag, in the sense
62// that doing so would result in a tagged value that would pass the "are you a double"
63// test. We say that a NaN is "impure" if attempting to tag it would result in a value
64// that would look like something other than a double.
65
66// Returns some kind of pure NaN.
67inline double pureNaN()
68{
69 // Be sure that we return exactly the kind of NaN that is safe. We engineer the bits
70 // ourselves to ensure that it's !isImpureNaN(). FWIW, this is what
71 // numeric_limits<double>::quiet_NaN() returns on Mac/X86_64. But AFAICT there is
72 // no guarantee that quiet_NaN would return a pureNaN on all platforms. For example,
73 // the docs appear to imply that quiet_NaN could even return a double with the
74 // signaling bit set on hardware that doesn't do signaling. That would probably
75 // never happen, but it's healthy to be paranoid.
76 return bitwise_cast<double>(0x7ff8000000000000ll);
77}
78
79#define PNaN (pureNaN())
80
81inline bool isImpureNaN(double value)
82{
83 // Tests if the double value would break JSVALUE64 encoding, which is the most
84 // aggressive kind of encoding that we currently use.
85 return bitwise_cast<uint64_t>(value) >= 0xfffe000000000000llu;
86}
87
88// If the given value is NaN then return a NaN that is known to be pure.
89inline double purifyNaN(double value)
90{
91 if (value != value)
92 return PNaN;
93 return value;
94}
95
96} // namespace JSC
97
98#endif // PureNaN_h