boxed-float.h 3.22 KB
Newer Older
1 2 3 4
// 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.

5 6
#ifndef V8_BOXED_FLOAT_H_
#define V8_BOXED_FLOAT_H_
7

8
#include <cmath>
9
#include "src/base/macros.h"
10
#include "src/globals.h"
11 12 13 14 15 16 17 18 19 20

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:
21
  Float32() = default;
22 23 24

  // This constructor does not guarantee that bit pattern of the input value
  // is preserved if the input is a NaN.
25 26 27 28 29
  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));
  }
30 31 32 33 34

  uint32_t get_bits() const { return bit_pattern_; }

  float get_scalar() const { return bit_cast<float>(bit_pattern_); }

35 36 37 38 39 40
  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());
  }

41 42 43 44
  // 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_; }

45
  static constexpr Float32 FromBits(uint32_t bits) { return Float32(bits); }
46 47

 private:
48 49 50 51
  uint32_t bit_pattern_ = 0;

  explicit constexpr Float32(uint32_t bit_pattern)
      : bit_pattern_(bit_pattern) {}
52 53
};

54
ASSERT_TRIVIALLY_COPYABLE(Float32);
55

56
// Safety wrapper for a 64-bit floating-point value to make sure we don't lose
57
// the exact bit pattern during deoptimization when passing this value.
58 59 60
// TODO(ahaas): Unify this class with Double in double.h
class Float64 {
 public:
61
  Float64() = default;
62 63 64 65 66 67 68 69 70

  // 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));
  }

71 72 73
  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; }
74 75 76 77 78
  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());
  }
79 80 81 82 83

  // 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_; }

84
  static constexpr Float64 FromBits(uint64_t bits) { return Float64(bits); }
85 86

 private:
87 88
  uint64_t bit_pattern_ = 0;

89 90
  explicit constexpr Float64(uint64_t bit_pattern)
      : bit_pattern_(bit_pattern) {}
91 92
};

93
ASSERT_TRIVIALLY_COPYABLE(Float64);
94

95 96 97
}  // namespace internal
}  // namespace v8

98
#endif  // V8_BOXED_FLOAT_H_