Skip to content

Commit 59c924b

Browse files
laramielcopybara-github
authored andcommitted
Use std::memcpy instead of a union to access floating point bits.
PiperOrigin-RevId: 752789717 Change-Id: I20d17677bc75d12fa50328feb2bdc5c25e852cc4
1 parent 00b2154 commit 59c924b

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

googletest/include/gtest/internal/gtest-internal.h

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -290,17 +290,17 @@ class FloatingPoint {
290290
// around may change its bits, although the new value is guaranteed
291291
// to be also a NAN. Therefore, don't expect this constructor to
292292
// 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)); }
294294

295295
// Static methods
296296

297297
// Reinterprets a bit pattern as a floating-point number.
298298
//
299299
// 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;
304304
}
305305

306306
// Returns the floating-point number that represent positive infinity.
@@ -309,16 +309,16 @@ class FloatingPoint {
309309
// Non-static methods
310310

311311
// Returns the bits that represents this number.
312-
const Bits& bits() const { return u_.bits_; }
312+
const Bits& bits() const { return bits_; }
313313

314314
// Returns the exponent bits of this number.
315-
Bits exponent_bits() const { return kExponentBitMask & u_.bits_; }
315+
Bits exponent_bits() const { return kExponentBitMask & bits_; }
316316

317317
// Returns the fraction bits of this number.
318-
Bits fraction_bits() const { return kFractionBitMask & u_.bits_; }
318+
Bits fraction_bits() const { return kFractionBitMask & bits_; }
319319

320320
// Returns the sign bit of this number.
321-
Bits sign_bit() const { return kSignBitMask & u_.bits_; }
321+
Bits sign_bit() const { return kSignBitMask & bits_; }
322322

323323
// Returns true if and only if this is NAN (not a number).
324324
bool is_nan() const {
@@ -338,17 +338,10 @@ class FloatingPoint {
338338
// a NAN must return false.
339339
if (is_nan() || rhs.is_nan()) return false;
340340

341-
return DistanceBetweenSignAndMagnitudeNumbers(u_.bits_, rhs.u_.bits_) <=
342-
kMaxUlps;
341+
return DistanceBetweenSignAndMagnitudeNumbers(bits_, rhs.bits_) <= kMaxUlps;
343342
}
344343

345344
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-
352345
// Converts an integer from the sign-and-magnitude representation to
353346
// the biased representation. More precisely, let N be 2 to the
354347
// power of (kBitCount - 1), an integer x is represented by the
@@ -364,7 +357,7 @@ class FloatingPoint {
364357
//
365358
// Read https://en.wikipedia.org/wiki/Signed_number_representations
366359
// for more details on signed number representations.
367-
static Bits SignAndMagnitudeToBiased(const Bits& sam) {
360+
static Bits SignAndMagnitudeToBiased(Bits sam) {
368361
if (kSignBitMask & sam) {
369362
// sam represents a negative number.
370363
return ~sam + 1;
@@ -376,14 +369,13 @@ class FloatingPoint {
376369

377370
// Given two numbers in the sign-and-magnitude representation,
378371
// 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) {
381373
const Bits biased1 = SignAndMagnitudeToBiased(sam1);
382374
const Bits biased2 = SignAndMagnitudeToBiased(sam2);
383375
return (biased1 >= biased2) ? (biased1 - biased2) : (biased2 - biased1);
384376
}
385377

386-
FloatingPointUnion u_;
378+
Bits bits_; // The bits that represent the number.
387379
};
388380

389381
// Typedefs the instances of the FloatingPoint template class that we

0 commit comments

Comments
 (0)