v8/src/boxed-float.h
Clemens Hammacher 9dd6f0d089 Fix is_trivially_copyable check for MSVC and older stdlibc++
MSVC 2015 and 2017 implement std::is_trivially_copyable, but not
correctly. Hence, reimplement it using more low-level primitives.

For stdlibc++ versions below 5.0, we already have a workaround for the
missing support of std::is_trivially_copyable, but this is an unsound
approximation, because it is ignoring move constructor, move assignment
and copy assignment. Therefore, do not use this approximation for
asserting trivial copyability of a type.

Finally, add unittests for the new is_trivially_copyable
implementations.

R=mstarzinger@chromium.org
CC=loorongjie@gmail.com

Change-Id: I9ee56a65882e8c94b72c9a2d484edd27963a5d89
Reviewed-on: https://chromium-review.googlesource.com/941521
Reviewed-by: Michael Starzinger <mstarzinger@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#51651}
2018-03-01 13:05:12 +00:00

99 lines
3.2 KiB
C++

// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_BOXED_FLOAT_H_
#define V8_BOXED_FLOAT_H_
#include <cmath>
#include "src/base/macros.h"
#include "src/globals.h"
namespace v8 {
namespace internal {
// TODO(ahaas): Make these classes with the one in double.h
// Safety wrapper for a 32-bit floating-point value to make sure we don't lose
// the exact bit pattern during deoptimization when passing this value.
class Float32 {
public:
Float32() = default;
// This constructor does not guarantee that bit pattern of the input value
// is preserved if the input is a NaN.
explicit Float32(float value) : bit_pattern_(bit_cast<uint32_t>(value)) {
// Check that the provided value is not a NaN, because the bit pattern of a
// NaN may be changed by a bit_cast, e.g. for signalling NaNs on ia32.
DCHECK(!std::isnan(value));
}
uint32_t get_bits() const { return bit_pattern_; }
float get_scalar() const { return bit_cast<float>(bit_pattern_); }
bool is_nan() const {
// Even though {get_scalar()} might flip the quiet NaN bit, it's ok here,
// because this does not change the is_nan property.
return std::isnan(get_scalar());
}
// Return a pointer to the field storing the bit pattern. Used in code
// generation tests to store generated values there directly.
uint32_t* get_bits_address() { return &bit_pattern_; }
static constexpr Float32 FromBits(uint32_t bits) { return Float32(bits); }
private:
uint32_t bit_pattern_ = 0;
explicit constexpr Float32(uint32_t bit_pattern)
: bit_pattern_(bit_pattern) {}
};
ASSERT_TRIVIALLY_COPYABLE(Float32);
// Safety wrapper for a 64-bit floating-point value to make sure we don't lose
// the exact bit pattern during deoptimization when passing this value.
// TODO(ahaas): Unify this class with Double in double.h
class Float64 {
public:
Float64() = default;
// This constructor does not guarantee that bit pattern of the input value
// is preserved if the input is a NaN.
explicit Float64(double value) : bit_pattern_(bit_cast<uint64_t>(value)) {
// Check that the provided value is not a NaN, because the bit pattern of a
// NaN may be changed by a bit_cast, e.g. for signalling NaNs on ia32.
DCHECK(!std::isnan(value));
}
uint64_t get_bits() const { return bit_pattern_; }
double get_scalar() const { return bit_cast<double>(bit_pattern_); }
bool is_hole_nan() const { return bit_pattern_ == kHoleNanInt64; }
bool is_nan() const {
// Even though {get_scalar()} might flip the quiet NaN bit, it's ok here,
// because this does not change the is_nan property.
return std::isnan(get_scalar());
}
// Return a pointer to the field storing the bit pattern. Used in code
// generation tests to store generated values there directly.
uint64_t* get_bits_address() { return &bit_pattern_; }
static constexpr Float64 FromBits(uint64_t bits) { return Float64(bits); }
private:
uint64_t bit_pattern_ = 0;
explicit constexpr Float64(uint64_t bit_pattern)
: bit_pattern_(bit_pattern) {}
};
ASSERT_TRIVIALLY_COPYABLE(Float64);
} // namespace internal
} // namespace v8
#endif // V8_BOXED_FLOAT_H_