Joshua
open source statistical hierarchical phrase-based machine translation system
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
src/kenlm/util/double-conversion/diy-fp.h
00001 // Copyright 2010 the V8 project authors. All rights reserved.
00002 // Redistribution and use in source and binary forms, with or without
00003 // modification, are permitted provided that the following conditions are
00004 // met:
00005 //
00006 //     * Redistributions of source code must retain the above copyright
00007 //       notice, this list of conditions and the following disclaimer.
00008 //     * Redistributions in binary form must reproduce the above
00009 //       copyright notice, this list of conditions and the following
00010 //       disclaimer in the documentation and/or other materials provided
00011 //       with the distribution.
00012 //     * Neither the name of Google Inc. nor the names of its
00013 //       contributors may be used to endorse or promote products derived
00014 //       from this software without specific prior written permission.
00015 //
00016 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00017 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00018 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00019 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00020 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00021 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00026 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 
00028 #ifndef DOUBLE_CONVERSION_DIY_FP_H_
00029 #define DOUBLE_CONVERSION_DIY_FP_H_
00030 
00031 #include "utils.h"
00032 
00033 namespace double_conversion {
00034 
00035 // This "Do It Yourself Floating Point" class implements a floating-point number
00036 // with a uint64 significand and an int exponent. Normalized DiyFp numbers will
00037 // have the most significant bit of the significand set.
00038 // Multiplication and Subtraction do not normalize their results.
00039 // DiyFp are not designed to contain special doubles (NaN and Infinity).
00040 class DiyFp {
00041  public:
00042   static const int kSignificandSize = 64;
00043 
00044   DiyFp() : f_(0), e_(0) {}
00045   DiyFp(uint64_t f, int e) : f_(f), e_(e) {}
00046 
00047   // this = this - other.
00048   // The exponents of both numbers must be the same and the significand of this
00049   // must be bigger than the significand of other.
00050   // The result will not be normalized.
00051   void Subtract(const DiyFp& other) {
00052     ASSERT(e_ == other.e_);
00053     ASSERT(f_ >= other.f_);
00054     f_ -= other.f_;
00055   }
00056 
00057   // Returns a - b.
00058   // The exponents of both numbers must be the same and this must be bigger
00059   // than other. The result will not be normalized.
00060   static DiyFp Minus(const DiyFp& a, const DiyFp& b) {
00061     DiyFp result = a;
00062     result.Subtract(b);
00063     return result;
00064   }
00065 
00066 
00067   // this = this * other.
00068   void Multiply(const DiyFp& other);
00069 
00070   // returns a * b;
00071   static DiyFp Times(const DiyFp& a, const DiyFp& b) {
00072     DiyFp result = a;
00073     result.Multiply(b);
00074     return result;
00075   }
00076 
00077   void Normalize() {
00078     ASSERT(f_ != 0);
00079     uint64_t f = f_;
00080     int e = e_;
00081 
00082     // This method is mainly called for normalizing boundaries. In general
00083     // boundaries need to be shifted by 10 bits. We thus optimize for this case.
00084     const uint64_t k10MSBits = UINT64_2PART_C(0xFFC00000, 00000000);
00085     while ((f & k10MSBits) == 0) {
00086       f <<= 10;
00087       e -= 10;
00088     }
00089     while ((f & kUint64MSB) == 0) {
00090       f <<= 1;
00091       e--;
00092     }
00093     f_ = f;
00094     e_ = e;
00095   }
00096 
00097   static DiyFp Normalize(const DiyFp& a) {
00098     DiyFp result = a;
00099     result.Normalize();
00100     return result;
00101   }
00102 
00103   uint64_t f() const { return f_; }
00104   int e() const { return e_; }
00105 
00106   void set_f(uint64_t new_value) { f_ = new_value; }
00107   void set_e(int new_value) { e_ = new_value; }
00108 
00109  private:
00110   static const uint64_t kUint64MSB = UINT64_2PART_C(0x80000000, 00000000);
00111 
00112   uint64_t f_;
00113   int e_;
00114 };
00115 
00116 }  // namespace double_conversion
00117 
00118 #endif  // DOUBLE_CONVERSION_DIY_FP_H_