@@ -290,17 +290,17 @@ class FloatingPoint {
290
290
// around may change its bits, although the new value is guaranteed
291
291
// to be also a NAN. Therefore, don't expect this constructor to
292
292
// preserve the bits in x when x is a NAN.
293
- explicit FloatingPoint (const RawType& x) { u_. value_ = x ; }
293
+ explicit FloatingPoint (RawType x) { memcpy (&bits_, &x, sizeof (x)) ; }
294
294
295
295
// Static methods
296
296
297
297
// Reinterprets a bit pattern as a floating-point number.
298
298
//
299
299
// This function is needed to test the AlmostEquals() method.
300
- static RawType ReinterpretBits (const Bits bits) {
301
- FloatingPoint fp ( 0 ) ;
302
- fp. u_ . bits_ = bits;
303
- return fp. u_ . value_ ;
300
+ static RawType ReinterpretBits (Bits bits) {
301
+ RawType fp;
302
+ memcpy (&fp, & bits, sizeof (fp)) ;
303
+ return fp;
304
304
}
305
305
306
306
// Returns the floating-point number that represent positive infinity.
@@ -309,16 +309,16 @@ class FloatingPoint {
309
309
// Non-static methods
310
310
311
311
// Returns the bits that represents this number.
312
- const Bits& bits () const { return u_. bits_ ; }
312
+ const Bits& bits () const { return bits_; }
313
313
314
314
// Returns the exponent bits of this number.
315
- Bits exponent_bits () const { return kExponentBitMask & u_. bits_ ; }
315
+ Bits exponent_bits () const { return kExponentBitMask & bits_; }
316
316
317
317
// Returns the fraction bits of this number.
318
- Bits fraction_bits () const { return kFractionBitMask & u_. bits_ ; }
318
+ Bits fraction_bits () const { return kFractionBitMask & bits_; }
319
319
320
320
// Returns the sign bit of this number.
321
- Bits sign_bit () const { return kSignBitMask & u_. bits_ ; }
321
+ Bits sign_bit () const { return kSignBitMask & bits_; }
322
322
323
323
// Returns true if and only if this is NAN (not a number).
324
324
bool is_nan () const {
@@ -332,23 +332,16 @@ class FloatingPoint {
332
332
//
333
333
// - returns false if either number is (or both are) NAN.
334
334
// - treats really large numbers as almost equal to infinity.
335
- // - thinks +0.0 and -0.0 are 0 DLP 's apart.
335
+ // - thinks +0.0 and -0.0 are 0 ULP 's apart.
336
336
bool AlmostEquals (const FloatingPoint& rhs) const {
337
337
// The IEEE standard says that any comparison operation involving
338
338
// a NAN must return false.
339
339
if (is_nan () || rhs.is_nan ()) return false ;
340
340
341
- return DistanceBetweenSignAndMagnitudeNumbers (u_.bits_ , rhs.u_ .bits_ ) <=
342
- kMaxUlps ;
341
+ return DistanceBetweenSignAndMagnitudeNumbers (bits_, rhs.bits_ ) <= kMaxUlps ;
343
342
}
344
343
345
344
private:
346
- // The data type used to store the actual floating-point number.
347
- union FloatingPointUnion {
348
- RawType value_; // The raw floating-point number.
349
- Bits bits_; // The bits that represent the number.
350
- };
351
-
352
345
// Converts an integer from the sign-and-magnitude representation to
353
346
// the biased representation. More precisely, let N be 2 to the
354
347
// power of (kBitCount - 1), an integer x is represented by the
@@ -364,7 +357,7 @@ class FloatingPoint {
364
357
//
365
358
// Read https://en.wikipedia.org/wiki/Signed_number_representations
366
359
// for more details on signed number representations.
367
- static Bits SignAndMagnitudeToBiased (const Bits& sam) {
360
+ static Bits SignAndMagnitudeToBiased (Bits sam) {
368
361
if (kSignBitMask & sam) {
369
362
// sam represents a negative number.
370
363
return ~sam + 1 ;
@@ -376,14 +369,13 @@ class FloatingPoint {
376
369
377
370
// Given two numbers in the sign-and-magnitude representation,
378
371
// returns the distance between them as an unsigned number.
379
- static Bits DistanceBetweenSignAndMagnitudeNumbers (const Bits& sam1,
380
- const Bits& sam2) {
372
+ static Bits DistanceBetweenSignAndMagnitudeNumbers (Bits sam1, Bits sam2) {
381
373
const Bits biased1 = SignAndMagnitudeToBiased (sam1);
382
374
const Bits biased2 = SignAndMagnitudeToBiased (sam2);
383
375
return (biased1 >= biased2) ? (biased1 - biased2) : (biased2 - biased1);
384
376
}
385
377
386
- FloatingPointUnion u_;
378
+ Bits bits_; // The bits that represent the number.
387
379
};
388
380
389
381
// Typedefs the instances of the FloatingPoint template class that we
0 commit comments