v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
diy-fp.h
Go to the documentation of this file.
1// Copyright 2011 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_BASE_NUMBERS_DIY_FP_H_
6#define V8_BASE_NUMBERS_DIY_FP_H_
7
8#include <stdint.h>
9
10#include "src/base/logging.h"
11
12namespace v8 {
13namespace base {
14
15// This "Do It Yourself Floating Point" class implements a floating-point number
16// with a uint64 significand and an int exponent. Normalized DiyFp numbers will
17// have the most significant bit of the significand set.
18// Multiplication and Subtraction do not normalize their results.
19// DiyFp are not designed to contain special doubles (NaN and Infinity).
20class DiyFp {
21 public:
22 static const int kSignificandSize = 64;
23
24 constexpr DiyFp() : f_(0), e_(0) {}
25 constexpr DiyFp(uint64_t f, int e) : f_(f), e_(e) {}
26
27 // this = this - other.
28 // The exponents of both numbers must be the same and the significand of this
29 // must be bigger than the significand of other.
30 // The result will not be normalized.
31 void Subtract(const DiyFp& other) {
32 DCHECK(e_ == other.e_);
33 DCHECK(f_ >= other.f_);
34 f_ -= other.f_;
35 }
36
37 // Returns a - b.
38 // The exponents of both numbers must be the same and this must be bigger
39 // than other. The result will not be normalized.
40 static DiyFp Minus(const DiyFp& a, const DiyFp& b) {
41 DiyFp result = a;
43 return result;
44 }
45
46 // this = this * other.
47 V8_BASE_EXPORT void Multiply(const DiyFp& other);
48
49 // returns a * b;
50 static DiyFp Times(const DiyFp& a, const DiyFp& b) {
51#ifdef __SIZEOF_INT128__
52 // If we have compiler-assisted 64x64 -> 128 muls (e.g. x86-64 and
53 // aarch64), we can use that for a faster, inlined implementation.
54 // This rounds the same way as Multiply().
55 uint64_t hi = (a.f_ * static_cast<unsigned __int128>(b.f_)) >> 64;
56 uint64_t lo = (a.f_ * static_cast<unsigned __int128>(b.f_));
57 return {hi + (lo >> 63), a.e_ + b.e_ + 64};
58#else
59 DiyFp result = a;
61 return result;
62#endif
63 }
64
65 void Normalize() {
66 DCHECK_NE(f_, 0);
67 uint64_t f = f_;
68 int e = e_;
69
70 // This method is mainly called for normalizing boundaries. In general
71 // boundaries need to be shifted by 10 bits. We thus optimize for this case.
72 const uint64_t k10MSBits = static_cast<uint64_t>(0x3FF) << 54;
73 while ((f & k10MSBits) == 0) {
74 f <<= 10;
75 e -= 10;
76 }
77 while ((f & kUint64MSB) == 0) {
78 f <<= 1;
79 e--;
80 }
81 f_ = f;
82 e_ = e;
83 }
84
85 static DiyFp Normalize(const DiyFp& a) {
86 DiyFp result = a;
88 return result;
89 }
90
91 constexpr uint64_t f() const { return f_; }
92 constexpr int e() const { return e_; }
93
94 constexpr void set_f(uint64_t new_value) { f_ = new_value; }
95 constexpr void set_e(int new_value) { e_ = new_value; }
96
97 private:
98 static const uint64_t kUint64MSB = static_cast<uint64_t>(1) << 63;
99
100 uint64_t f_;
101 int e_;
102};
103
104} // namespace base
105} // namespace v8
106
107#endif // V8_BASE_NUMBERS_DIY_FP_H_
#define V8_BASE_EXPORT
Definition base-export.h:26
static DiyFp Minus(const DiyFp &a, const DiyFp &b)
Definition diy-fp.h:40
constexpr DiyFp()
Definition diy-fp.h:24
static const int kSignificandSize
Definition diy-fp.h:22
static DiyFp Normalize(const DiyFp &a)
Definition diy-fp.h:85
static DiyFp Times(const DiyFp &a, const DiyFp &b)
Definition diy-fp.h:50
constexpr uint64_t f() const
Definition diy-fp.h:91
static const uint64_t kUint64MSB
Definition diy-fp.h:98
uint64_t f_
Definition diy-fp.h:100
void Subtract(const DiyFp &other)
Definition diy-fp.h:31
constexpr int e() const
Definition diy-fp.h:92
void Normalize()
Definition diy-fp.h:65
V8_BASE_EXPORT void Multiply(const DiyFp &other)
Definition diy-fp.cc:12
constexpr void set_f(uint64_t new_value)
Definition diy-fp.h:94
constexpr void set_e(int new_value)
Definition diy-fp.h:95
constexpr DiyFp(uint64_t f, int e)
Definition diy-fp.h:25
std::optional< TNode< JSArray > > a
ZoneVector< RpoNumber > & result
#define DCHECK_NE(v1, v2)
Definition logging.h:486
#define DCHECK(condition)
Definition logging.h:482