All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ieee.h
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 #ifndef DOUBLE_CONVERSION_DOUBLE_H_
29 #define DOUBLE_CONVERSION_DOUBLE_H_
30 
31 #include "diy-fp.h"
32 
33 #include "pxr/pxr.h"
34 
35 PXR_NAMESPACE_OPEN_SCOPE
36 
37 namespace pxr_double_conversion {
38 
39 // We assume that doubles and uint64_t have the same endianness.
40 static uint64_t double_to_uint64(double d) { return BitCast<uint64_t>(d); }
41 static double uint64_to_double(uint64_t d64) { return BitCast<double>(d64); }
42 static uint32_t float_to_uint32(float f) { return BitCast<uint32_t>(f); }
43 static float uint32_to_float(uint32_t d32) { return BitCast<float>(d32); }
44 
45 // Helper functions for doubles.
46 class Double {
47  public:
48  static const uint64_t kSignMask = UINT64_2PART_C(0x80000000, 00000000);
49  static const uint64_t kExponentMask = UINT64_2PART_C(0x7FF00000, 00000000);
50  static const uint64_t kSignificandMask = UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
51  static const uint64_t kHiddenBit = UINT64_2PART_C(0x00100000, 00000000);
52  static const int kPhysicalSignificandSize = 52; // Excludes the hidden bit.
53  static const int kSignificandSize = 53;
54 
55  Double() : d64_(0) {}
56  explicit Double(double d) : d64_(double_to_uint64(d)) {}
57  explicit Double(uint64_t d64) : d64_(d64) {}
58  explicit Double(DiyFp diy_fp)
59  : d64_(DiyFpToUint64(diy_fp)) {}
60 
61  // The value encoded by this Double must be greater or equal to +0.0.
62  // It must not be special (infinity, or NaN).
63  DiyFp AsDiyFp() const {
64  ASSERT(Sign() > 0);
65  ASSERT(!IsSpecial());
66  return DiyFp(Significand(), Exponent());
67  }
68 
69  // The value encoded by this Double must be strictly greater than 0.
70  DiyFp AsNormalizedDiyFp() const {
71  ASSERT(value() > 0.0);
72  uint64_t f = Significand();
73  int e = Exponent();
74 
75  // The current double could be a denormal.
76  while ((f & kHiddenBit) == 0) {
77  f <<= 1;
78  e--;
79  }
80  // Do the final shifts in one go.
81  f <<= DiyFp::kSignificandSize - kSignificandSize;
82  e -= DiyFp::kSignificandSize - kSignificandSize;
83  return DiyFp(f, e);
84  }
85 
86  // Returns the double's bit as uint64.
87  uint64_t AsUint64() const {
88  return d64_;
89  }
90 
91  // Returns the next greater double. Returns +infinity on input +infinity.
92  double NextDouble() const {
93  if (d64_ == kInfinity) return Double(kInfinity).value();
94  if (Sign() < 0 && Significand() == 0) {
95  // -0.0
96  return 0.0;
97  }
98  if (Sign() < 0) {
99  return Double(d64_ - 1).value();
100  } else {
101  return Double(d64_ + 1).value();
102  }
103  }
104 
105  double PreviousDouble() const {
106  if (d64_ == (kInfinity | kSignMask)) return -Infinity();
107  if (Sign() < 0) {
108  return Double(d64_ + 1).value();
109  } else {
110  if (Significand() == 0) return -0.0;
111  return Double(d64_ - 1).value();
112  }
113  }
114 
115  int Exponent() const {
116  if (IsDenormal()) return kDenormalExponent;
117 
118  uint64_t d64 = AsUint64();
119  int biased_e =
120  static_cast<int>((d64 & kExponentMask) >> kPhysicalSignificandSize);
121  return biased_e - kExponentBias;
122  }
123 
124  uint64_t Significand() const {
125  uint64_t d64 = AsUint64();
126  uint64_t significand = d64 & kSignificandMask;
127  if (!IsDenormal()) {
128  return significand + kHiddenBit;
129  } else {
130  return significand;
131  }
132  }
133 
134  // Returns true if the double is a denormal.
135  bool IsDenormal() const {
136  uint64_t d64 = AsUint64();
137  return (d64 & kExponentMask) == 0;
138  }
139 
140  // We consider denormals not to be special.
141  // Hence only Infinity and NaN are special.
142  bool IsSpecial() const {
143  uint64_t d64 = AsUint64();
144  return (d64 & kExponentMask) == kExponentMask;
145  }
146 
147  bool IsNan() const {
148  uint64_t d64 = AsUint64();
149  return ((d64 & kExponentMask) == kExponentMask) &&
150  ((d64 & kSignificandMask) != 0);
151  }
152 
153  bool IsInfinite() const {
154  uint64_t d64 = AsUint64();
155  return ((d64 & kExponentMask) == kExponentMask) &&
156  ((d64 & kSignificandMask) == 0);
157  }
158 
159  int Sign() const {
160  uint64_t d64 = AsUint64();
161  return (d64 & kSignMask) == 0? 1: -1;
162  }
163 
164  // Precondition: the value encoded by this Double must be greater or equal
165  // than +0.0.
166  DiyFp UpperBoundary() const {
167  ASSERT(Sign() > 0);
168  return DiyFp(Significand() * 2 + 1, Exponent() - 1);
169  }
170 
171  // Computes the two boundaries of this.
172  // The bigger boundary (m_plus) is normalized. The lower boundary has the same
173  // exponent as m_plus.
174  // Precondition: the value encoded by this Double must be greater than 0.
175  void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
176  ASSERT(value() > 0.0);
177  DiyFp v = this->AsDiyFp();
178  DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
179  DiyFp m_minus;
180  if (LowerBoundaryIsCloser()) {
181  m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
182  } else {
183  m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
184  }
185  m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
186  m_minus.set_e(m_plus.e());
187  *out_m_plus = m_plus;
188  *out_m_minus = m_minus;
189  }
190 
191  bool LowerBoundaryIsCloser() const {
192  // The boundary is closer if the significand is of the form f == 2^p-1 then
193  // the lower boundary is closer.
194  // Think of v = 1000e10 and v- = 9999e9.
195  // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
196  // at a distance of 1e8.
197  // The only exception is for the smallest normal: the largest denormal is
198  // at the same distance as its successor.
199  // Note: denormals have the same exponent as the smallest normals.
200  bool physical_significand_is_zero = ((AsUint64() & kSignificandMask) == 0);
201  return physical_significand_is_zero && (Exponent() != kDenormalExponent);
202  }
203 
204  double value() const { return uint64_to_double(d64_); }
205 
206  // Returns the significand size for a given order of magnitude.
207  // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude.
208  // This function returns the number of significant binary digits v will have
209  // once it's encoded into a double. In almost all cases this is equal to
210  // kSignificandSize. The only exceptions are denormals. They start with
211  // leading zeroes and their effective significand-size is hence smaller.
212  static int SignificandSizeForOrderOfMagnitude(int order) {
213  if (order >= (kDenormalExponent + kSignificandSize)) {
214  return kSignificandSize;
215  }
216  if (order <= kDenormalExponent) return 0;
217  return order - kDenormalExponent;
218  }
219 
220  static double Infinity() {
221  return Double(kInfinity).value();
222  }
223 
224  static double NaN() {
225  return Double(kNaN).value();
226  }
227 
228  private:
229  static const int kExponentBias = 0x3FF + kPhysicalSignificandSize;
230  static const int kDenormalExponent = -kExponentBias + 1;
231  static const int kMaxExponent = 0x7FF - kExponentBias;
232  static const uint64_t kInfinity = UINT64_2PART_C(0x7FF00000, 00000000);
233  static const uint64_t kNaN = UINT64_2PART_C(0x7FF80000, 00000000);
234 
235  const uint64_t d64_;
236 
237  static uint64_t DiyFpToUint64(DiyFp diy_fp) {
238  uint64_t significand = diy_fp.f();
239  int exponent = diy_fp.e();
240  while (significand > kHiddenBit + kSignificandMask) {
241  significand >>= 1;
242  exponent++;
243  }
244  if (exponent >= kMaxExponent) {
245  return kInfinity;
246  }
247  if (exponent < kDenormalExponent) {
248  return 0;
249  }
250  while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) {
251  significand <<= 1;
252  exponent--;
253  }
254  uint64_t biased_exponent;
255  if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) {
256  biased_exponent = 0;
257  } else {
258  biased_exponent = static_cast<uint64_t>(exponent + kExponentBias);
259  }
260  return (significand & kSignificandMask) |
261  (biased_exponent << kPhysicalSignificandSize);
262  }
263 
264  DISALLOW_COPY_AND_ASSIGN(Double);
265 };
266 
267 class Single {
268  public:
269  static const uint32_t kSignMask = 0x80000000;
270  static const uint32_t kExponentMask = 0x7F800000;
271  static const uint32_t kSignificandMask = 0x007FFFFF;
272  static const uint32_t kHiddenBit = 0x00800000;
273  static const int kPhysicalSignificandSize = 23; // Excludes the hidden bit.
274  static const int kSignificandSize = 24;
275 
276  Single() : d32_(0) {}
277  explicit Single(float f) : d32_(float_to_uint32(f)) {}
278  explicit Single(uint32_t d32) : d32_(d32) {}
279 
280  // The value encoded by this Single must be greater or equal to +0.0.
281  // It must not be special (infinity, or NaN).
282  DiyFp AsDiyFp() const {
283  ASSERT(Sign() > 0);
284  ASSERT(!IsSpecial());
285  return DiyFp(Significand(), Exponent());
286  }
287 
288  // Returns the single's bit as uint64.
289  uint32_t AsUint32() const {
290  return d32_;
291  }
292 
293  int Exponent() const {
294  if (IsDenormal()) return kDenormalExponent;
295 
296  uint32_t d32 = AsUint32();
297  int biased_e =
298  static_cast<int>((d32 & kExponentMask) >> kPhysicalSignificandSize);
299  return biased_e - kExponentBias;
300  }
301 
302  uint32_t Significand() const {
303  uint32_t d32 = AsUint32();
304  uint32_t significand = d32 & kSignificandMask;
305  if (!IsDenormal()) {
306  return significand + kHiddenBit;
307  } else {
308  return significand;
309  }
310  }
311 
312  // Returns true if the single is a denormal.
313  bool IsDenormal() const {
314  uint32_t d32 = AsUint32();
315  return (d32 & kExponentMask) == 0;
316  }
317 
318  // We consider denormals not to be special.
319  // Hence only Infinity and NaN are special.
320  bool IsSpecial() const {
321  uint32_t d32 = AsUint32();
322  return (d32 & kExponentMask) == kExponentMask;
323  }
324 
325  bool IsNan() const {
326  uint32_t d32 = AsUint32();
327  return ((d32 & kExponentMask) == kExponentMask) &&
328  ((d32 & kSignificandMask) != 0);
329  }
330 
331  bool IsInfinite() const {
332  uint32_t d32 = AsUint32();
333  return ((d32 & kExponentMask) == kExponentMask) &&
334  ((d32 & kSignificandMask) == 0);
335  }
336 
337  int Sign() const {
338  uint32_t d32 = AsUint32();
339  return (d32 & kSignMask) == 0? 1: -1;
340  }
341 
342  // Computes the two boundaries of this.
343  // The bigger boundary (m_plus) is normalized. The lower boundary has the same
344  // exponent as m_plus.
345  // Precondition: the value encoded by this Single must be greater than 0.
346  void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
347  ASSERT(value() > 0.0);
348  DiyFp v = this->AsDiyFp();
349  DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
350  DiyFp m_minus;
351  if (LowerBoundaryIsCloser()) {
352  m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
353  } else {
354  m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
355  }
356  m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
357  m_minus.set_e(m_plus.e());
358  *out_m_plus = m_plus;
359  *out_m_minus = m_minus;
360  }
361 
362  // Precondition: the value encoded by this Single must be greater or equal
363  // than +0.0.
364  DiyFp UpperBoundary() const {
365  ASSERT(Sign() > 0);
366  return DiyFp(Significand() * 2 + 1, Exponent() - 1);
367  }
368 
369  bool LowerBoundaryIsCloser() const {
370  // The boundary is closer if the significand is of the form f == 2^p-1 then
371  // the lower boundary is closer.
372  // Think of v = 1000e10 and v- = 9999e9.
373  // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
374  // at a distance of 1e8.
375  // The only exception is for the smallest normal: the largest denormal is
376  // at the same distance as its successor.
377  // Note: denormals have the same exponent as the smallest normals.
378  bool physical_significand_is_zero = ((AsUint32() & kSignificandMask) == 0);
379  return physical_significand_is_zero && (Exponent() != kDenormalExponent);
380  }
381 
382  float value() const { return uint32_to_float(d32_); }
383 
384  static float Infinity() {
385  return Single(kInfinity).value();
386  }
387 
388  static float NaN() {
389  return Single(kNaN).value();
390  }
391 
392  private:
393  static const int kExponentBias = 0x7F + kPhysicalSignificandSize;
394  static const int kDenormalExponent = -kExponentBias + 1;
395  static const int kMaxExponent = 0xFF - kExponentBias;
396  static const uint32_t kInfinity = 0x7F800000;
397  static const uint32_t kNaN = 0x7FC00000;
398 
399  const uint32_t d32_;
400 
401  DISALLOW_COPY_AND_ASSIGN(Single);
402 };
403 
404 } // namespace pxr_double_conversion
405 
406 PXR_NAMESPACE_CLOSE_SCOPE
407 
408 #endif // DOUBLE_CONVERSION_DOUBLE_H_