]>
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 are not designed to contain special doubles (NaN and Infinity).
56 static const int kSignificandSize
= 64;
58 DiyFp() : f_(0), e_(0) {}
59 DiyFp(uint64_t significand
, int exponent
) : f_(significand
), e_(exponent
) {}
61 // this = this - other.
62 // The exponents of both numbers must be the same and the significand of this
63 // must be bigger than the significand of other.
64 // The result will not be normalized.
65 void Subtract(const DiyFp
& other
) {
66 ASSERT(e_
== other
.e_
);
67 ASSERT(f_
>= other
.f_
);
72 // The exponents of both numbers must be the same and this must be bigger
73 // than other. The result will not be normalized.
74 static DiyFp
Minus(const DiyFp
& a
, const DiyFp
& b
) {
81 // this = this * other.
82 void Multiply(const DiyFp
& other
);
85 static DiyFp
Times(const DiyFp
& a
, const DiyFp
& b
) {
93 uint64_t significand
= f_
;
96 // This method is mainly called for normalizing boundaries. In general
97 // boundaries need to be shifted by 10 bits. We thus optimize for this case.
98 const uint64_t k10MSBits
= UINT64_2PART_C(0xFFC00000, 00000000);
99 while ((significand
& k10MSBits
) == 0) {
103 while ((significand
& kUint64MSB
) == 0) {
111 static DiyFp
Normalize(const DiyFp
& a
) {
117 uint64_t f() const { return f_
; }
118 int e() const { return e_
; }
120 void set_f(uint64_t new_value
) { f_
= new_value
; }
121 void set_e(int new_value
) { e_
= new_value
; }
124 static const uint64_t kUint64MSB
= UINT64_2PART_C(0x80000000, 00000000);
130 } // namespace double_conversion
132 // ICU PATCH: Close ICU namespace
135 #endif // DOUBLE_CONVERSION_DIY_FP_H_
136 #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING