Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
diy-fp.h
Go to the documentation of this file.
1// Copyright 2010 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef DOUBLE_CONVERSION_DIY_FP_H_
29#define DOUBLE_CONVERSION_DIY_FP_H_
30
31#include "utils.h"
32
33namespace double_conversion {
34
35// This "Do It Yourself Floating Point" class implements a floating-point number
36// with a uint64 significand and an int exponent. Normalized DiyFp numbers will
37// have the most significant bit of the significand set.
38// Multiplication and Subtraction do not normalize their results.
39// DiyFp store only non-negative numbers and are not designed to contain special
40// doubles (NaN and Infinity).
41class DiyFp {
42 public:
43 static const int kSignificandSize = 64;
44
45 DiyFp() : f_(0), e_(0) {}
46 DiyFp(const uint64_t significand, const int32_t exponent) : f_(significand), e_(exponent) {}
47
48 // this -= other.
49 // The exponents of both numbers must be the same and the significand of this
50 // must be greater or equal than the significand of other.
51 // The result will not be normalized.
52 void Subtract(const DiyFp& other) {
53 DOUBLE_CONVERSION_ASSERT(e_ == other.e_);
54 DOUBLE_CONVERSION_ASSERT(f_ >= other.f_);
55 f_ -= other.f_;
56 }
57
58 // Returns a - b.
59 // The exponents of both numbers must be the same and a must be greater
60 // or equal than b. The result will not be normalized.
61 static DiyFp Minus(const DiyFp& a, const DiyFp& b) {
62 DiyFp result = a;
63 result.Subtract(b);
64 return result;
65 }
66
67 // this *= other.
68 void Multiply(const DiyFp& other) {
69 // Simply "emulates" a 128 bit multiplication.
70 // However: the resulting number only contains 64 bits. The least
71 // significant 64 bits are only used for rounding the most significant 64
72 // bits.
73 const uint64_t kM32 = 0xFFFFFFFFU;
74 const uint64_t a = f_ >> 32;
75 const uint64_t b = f_ & kM32;
76 const uint64_t c = other.f_ >> 32;
77 const uint64_t d = other.f_ & kM32;
78 const uint64_t ac = a * c;
79 const uint64_t bc = b * c;
80 const uint64_t ad = a * d;
81 const uint64_t bd = b * d;
82 // By adding 1U << 31 to tmp we round the final result.
83 // Halfway cases will be rounded up.
84 const uint64_t tmp = (bd >> 32) + (ad & kM32) + (bc & kM32) + (1U << 31);
85 e_ += other.e_ + 64;
86 f_ = ac + (ad >> 32) + (bc >> 32) + (tmp >> 32);
87 }
88
89 // returns a * b;
90 static DiyFp Times(const DiyFp& a, const DiyFp& b) {
91 DiyFp result = a;
92 result.Multiply(b);
93 return result;
94 }
95
96 void Normalize() {
98 uint64_t significand = f_;
99 int32_t exponent = e_;
100
101 // This method is mainly called for normalizing boundaries. In general,
102 // boundaries need to be shifted by 10 bits, and we optimize for this case.
103 const uint64_t k10MSBits = DOUBLE_CONVERSION_UINT64_2PART_C(0xFFC00000, 00000000);
104 while ((significand & k10MSBits) == 0) {
105 significand <<= 10;
106 exponent -= 10;
107 }
108 while ((significand & kUint64MSB) == 0) {
109 significand <<= 1;
110 exponent--;
111 }
112 f_ = significand;
113 e_ = exponent;
114 }
115
116 static DiyFp Normalize(const DiyFp& a) {
117 DiyFp result = a;
118 result.Normalize();
119 return result;
120 }
121
122 uint64_t f() const { return f_; }
123 int32_t e() const { return e_; }
124
125 void set_f(uint64_t new_value) { f_ = new_value; }
126 void set_e(int32_t new_value) { e_ = new_value; }
127
128 private:
129 static const uint64_t kUint64MSB = DOUBLE_CONVERSION_UINT64_2PART_C(0x80000000, 00000000);
130
131 uint64_t f_;
132 int32_t e_;
133};
134
135} // namespace double_conversion
136
137#endif // DOUBLE_CONVERSION_DIY_FP_H_
int32_t e() const
Definition diy-fp.h:123
static DiyFp Minus(const DiyFp &a, const DiyFp &b)
Definition diy-fp.h:61
void Subtract(const DiyFp &other)
Definition diy-fp.h:52
DiyFp(const uint64_t significand, const int32_t exponent)
Definition diy-fp.h:46
uint64_t f() const
Definition diy-fp.h:122
void set_f(uint64_t new_value)
Definition diy-fp.h:125
void Multiply(const DiyFp &other)
Definition diy-fp.h:68
static DiyFp Normalize(const DiyFp &a)
Definition diy-fp.h:116
void set_e(int32_t new_value)
Definition diy-fp.h:126
static const int kSignificandSize
Definition diy-fp.h:43
static DiyFp Times(const DiyFp &a, const DiyFp &b)
Definition diy-fp.h:90
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition main.cc:19
static bool b
struct MyStruct a[10]
GAsyncResult * result
#define DOUBLE_CONVERSION_ASSERT(condition)
Definition utils.h:46
#define DOUBLE_CONVERSION_UINT64_2PART_C(a, b)
Definition utils.h:195