]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/double-conversion-diy-fp.h
1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 // From the double-conversion library. Original license:
6 // Copyright 2010 the V8 project authors. All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following
15 // disclaimer in the documentation and/or other materials provided
16 // with the distribution.
17 // * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // ICU PATCH: ifdef around UCONFIG_NO_FORMATTING
34 #include "unicode/utypes.h"
35 #if !UCONFIG_NO_FORMATTING
37 #ifndef DOUBLE_CONVERSION_DIY_FP_H_
38 #define DOUBLE_CONVERSION_DIY_FP_H_
40 // ICU PATCH: Customize header file paths for ICU.
42 #include "double-conversion-utils.h"
44 // ICU PATCH: Wrap in ICU namespace
47 namespace double_conversion
{
49 // This "Do It Yourself Floating Point" class implements a floating-point number
50 // with a uint64 significand and an int exponent. Normalized DiyFp numbers will
51 // have the most significant bit of the significand set.
52 // Multiplication and Subtraction do not normalize their results.
53 // DiyFp store only non-negative numbers and are not designed to contain special
54 // doubles (NaN and Infinity).
57 static const int kSignificandSize
= 64;
59 DiyFp() : f_(0), e_(0) {}
60 DiyFp(const uint64_t significand
, const int32_t exponent
) : f_(significand
), e_(exponent
) {}
63 // The exponents of both numbers must be the same and the significand of this
64 // must be greater or equal than the significand of other.
65 // The result will not be normalized.
66 void Subtract(const DiyFp
& other
) {
67 DOUBLE_CONVERSION_ASSERT(e_
== other
.e_
);
68 DOUBLE_CONVERSION_ASSERT(f_
>= other
.f_
);
73 // The exponents of both numbers must be the same and a must be greater
74 // or equal than b. The result will not be normalized.
75 static DiyFp
Minus(const DiyFp
& a
, const DiyFp
& b
) {
82 void Multiply(const DiyFp
& other
) {
83 // Simply "emulates" a 128 bit multiplication.
84 // However: the resulting number only contains 64 bits. The least
85 // significant 64 bits are only used for rounding the most significant 64
87 const uint64_t kM32
= 0xFFFFFFFFU
;
88 const uint64_t a
= f_
>> 32;
89 const uint64_t b
= f_
& kM32
;
90 const uint64_t c
= other
.f_
>> 32;
91 const uint64_t d
= other
.f_
& kM32
;
92 const uint64_t ac
= a
* c
;
93 const uint64_t bc
= b
* c
;
94 const uint64_t ad
= a
* d
;
95 const uint64_t bd
= b
* d
;
96 // By adding 1U << 31 to tmp we round the final result.
97 // Halfway cases will be rounded up.
98 const uint64_t tmp
= (bd
>> 32) + (ad
& kM32
) + (bc
& kM32
) + (1U << 31);
100 f_
= ac
+ (ad
>> 32) + (bc
>> 32) + (tmp
>> 32);
104 static DiyFp
Times(const DiyFp
& a
, const DiyFp
& b
) {
111 DOUBLE_CONVERSION_ASSERT(f_
!= 0);
112 uint64_t significand
= f_
;
113 int32_t exponent
= e_
;
115 // This method is mainly called for normalizing boundaries. In general,
116 // boundaries need to be shifted by 10 bits, and we optimize for this case.
117 const uint64_t k10MSBits
= DOUBLE_CONVERSION_UINT64_2PART_C(0xFFC00000, 00000000);
118 while ((significand
& k10MSBits
) == 0) {
122 while ((significand
& kUint64MSB
) == 0) {
130 static DiyFp
Normalize(const DiyFp
& a
) {
136 uint64_t f() const { return f_
; }
137 int32_t e() const { return e_
; }
139 void set_f(uint64_t new_value
) { f_
= new_value
; }
140 void set_e(int32_t new_value
) { e_
= new_value
; }
143 static const uint64_t kUint64MSB
= DOUBLE_CONVERSION_UINT64_2PART_C(0x80000000, 00000000);
149 } // namespace double_conversion
151 // ICU PATCH: Close ICU namespace
154 #endif // DOUBLE_CONVERSION_DIY_FP_H_
155 #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING