mfbt/double-conversion/diy-fp.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial