1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2006 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "test.h"
21 #include "abort.h"
22 #include "assert.h"
23 #include "log.h"
24 #include <cmath>
25 #include <iostream>
26 #include "int64x64-cairo.h"
27 
28 // Include directly to allow optimizations within this compilation unit.
29 extern "C" {
30 #include "cairo-wideint.c"
31 }
32 
33 /**
34  * \file
35  * \ingroup highprec
36  * Implementation of the ns3::int64x64_t type using the Cairo implementation.
37  */
38 
39 namespace ns3 {
40 
41 // Note:  Logging in this file is largely avoided due to the
42 // number of calls that are made to these functions and the possibility
43 // of causing recursions leading to stack overflow
44 NS_LOG_COMPONENT_DEFINE ("int64x64-cairo");
45 
46 /**
47  * \ingroup highprec
48  * Compute the sign of the result of multiplying or dividing
49  * Q64.64 fixed precision operands.
50  *
51  * \param [in]  sa The signed value of the first operand.
52  * \param [in]  sb The signed value of the second operand.
53  * \param [out] ua The unsigned magnitude of the first operand.
54  * \param [out] ub The unsigned magnitude of the second operand.
55  * \returns True if the result will be negative.
56  */
57 static inline
58 bool
output_sign(const cairo_int128_t sa,const cairo_int128_t sb,cairo_uint128_t & ua,cairo_uint128_t & ub)59 output_sign (const cairo_int128_t sa,
60              const cairo_int128_t sb,
61              cairo_uint128_t & ua,
62              cairo_uint128_t & ub)
63 {
64   bool negA = _cairo_int128_negative (sa);
65   bool negB = _cairo_int128_negative (sb);
66   ua = _cairo_int128_to_uint128 (sa);
67   ub = _cairo_int128_to_uint128 (sb);
68   ua = negA ? _cairo_uint128_negate (ua) : ua;
69   ub = negB ? _cairo_uint128_negate (ub) : ub;
70   return (negA && !negB) || (!negA && negB);
71 }
72 
73 void
Mul(const int64x64_t & o)74 int64x64_t::Mul (const int64x64_t & o)
75 {
76   cairo_uint128_t a, b;
77   bool sign = output_sign (_v, o._v, a, b);
78   cairo_uint128_t result = Umul (a, b);
79   _v = sign ? _cairo_uint128_negate (result) : result;
80 }
81 
82 cairo_uint128_t
Umul(const cairo_uint128_t a,const cairo_uint128_t b)83 int64x64_t::Umul (const cairo_uint128_t a, const cairo_uint128_t b)
84 {
85   cairo_uint128_t result;
86   cairo_uint128_t hiPart, loPart, midPart;
87   cairo_uint128_t res1, res2;
88 
89   // Multiplying (a.h 2^64 + a.l) x (b.h 2^64 + b.l) =
90   //			2^128 a.h b.h + 2^64*(a.h b.l+b.h a.l) + a.l b.l
91   // get the low part a.l b.l
92   // multiply the fractional part
93   loPart = _cairo_uint64x64_128_mul (a.lo, b.lo);
94   // compute the middle part 2^64*(a.h b.l+b.h a.l)
95   midPart = _cairo_uint128_add (_cairo_uint64x64_128_mul (a.lo, b.hi),
96                                 _cairo_uint64x64_128_mul (a.hi, b.lo));
97   // compute the high part 2^128 a.h b.h
98   hiPart = _cairo_uint64x64_128_mul (a.hi, b.hi);
99   // if the high part is not zero, put a warning
100   NS_ABORT_MSG_IF (hiPart.hi != 0,
101                    "High precision 128 bits multiplication error: multiplication overflow.");
102 
103   // Adding 64-bit terms to get 128-bit results, with carries
104   res1 = _cairo_uint64_to_uint128 (loPart.hi);
105   res2 = _cairo_uint64_to_uint128 (midPart.lo);
106   result  = _cairo_uint128_add (res1, res2);
107 
108   res1 = _cairo_uint64_to_uint128 (midPart.hi);
109   res2 = _cairo_uint64_to_uint128 (hiPart.lo);
110   res1 = _cairo_uint128_add (res1, res2);
111   res1 = _cairo_uint128_lsl (res1, 64);
112 
113   result  = _cairo_uint128_add (result, res1);
114 
115   return result;
116 }
117 
118 void
Div(const int64x64_t & o)119 int64x64_t::Div (const int64x64_t & o)
120 {
121   cairo_uint128_t a, b;
122   bool sign = output_sign (_v, o._v, a, b);
123   cairo_uint128_t result = Udiv (a, b);
124   _v = sign ? _cairo_uint128_negate (result) : result;
125 }
126 
127 cairo_uint128_t
Udiv(const cairo_uint128_t a,const cairo_uint128_t b)128 int64x64_t::Udiv (const cairo_uint128_t a, const cairo_uint128_t b)
129 {
130   cairo_uint128_t den = b;
131   cairo_uquorem128_t qr = _cairo_uint128_divrem (a, b);
132   cairo_uint128_t result = qr.quo;
133   cairo_uint128_t rem = qr.rem;
134 
135   // Now, manage the remainder
136   const uint64_t DIGITS = 64;  // Number of fraction digits (bits) we need
137   const cairo_uint128_t ZERO = _cairo_uint32_to_uint128 ((uint32_t)0);
138 
139   NS_ASSERT_MSG (_cairo_uint128_lt (rem, den),
140                  "Remainder not less than divisor");
141 
142   uint64_t digis = 0;          // Number of digits we have already
143   uint64_t shift = 0;          // Number we are going to get this round
144 
145   // Skip trailing zeros in divisor
146   while ( (shift < DIGITS) && !(den.lo & 0x1))
147     {
148       ++shift;
149       den = _cairo_uint128_rsl (den, 1);
150     }
151 
152   while ( (digis < DIGITS) && !(_cairo_uint128_eq (rem, ZERO)) )
153     {
154       // Skip leading zeros in remainder
155       while ( (digis + shift < DIGITS)
156               && !(rem.hi & HPCAIRO_MASK_HI_BIT) )
157         {
158           ++shift;
159           rem = _cairo_int128_lsl (rem, 1);
160         }
161 
162       // Cast off denominator bits if:
163       //   Need more digits and
164       //     LSB is zero or
165       //     rem < den
166       while ( (digis + shift < DIGITS)
167               && ( !(den.lo & 0x1) || _cairo_uint128_lt (rem, den) ) )
168         {
169           ++shift;
170           den = _cairo_uint128_rsl (den, 1);
171         }
172 
173       // Do the division
174       qr = _cairo_uint128_divrem (rem, den);
175 
176       // Add in the quotient as shift bits of the fraction
177       result = _cairo_uint128_lsl (result, static_cast<int> (shift));
178       result = _cairo_uint128_add (result, qr.quo);
179       rem = qr.rem;
180       digis += shift;
181       shift = 0;
182     }
183   // Did we run out of remainder?
184   if (digis < DIGITS)
185     {
186       shift = DIGITS - digis;
187       result = _cairo_uint128_lsl (result, static_cast<int> (shift));
188     }
189 
190   return result;
191 }
192 
193 void
MulByInvert(const int64x64_t & o)194 int64x64_t::MulByInvert (const int64x64_t & o)
195 {
196   bool sign = _cairo_int128_negative (_v);
197   cairo_uint128_t a = sign ? _cairo_int128_negate (_v) : _v;
198   cairo_uint128_t result = UmulByInvert (a, o._v);
199 
200   _v = sign ? _cairo_int128_negate (result) : result;
201 }
202 
203 cairo_uint128_t
UmulByInvert(const cairo_uint128_t a,const cairo_uint128_t b)204 int64x64_t::UmulByInvert (const cairo_uint128_t a, const cairo_uint128_t b)
205 {
206   cairo_uint128_t result;
207   cairo_uint128_t hi, mid;
208   hi = _cairo_uint64x64_128_mul (a.hi, b.hi);
209   mid = _cairo_uint128_add (_cairo_uint64x64_128_mul (a.hi, b.lo),
210                             _cairo_uint64x64_128_mul (a.lo, b.hi));
211   mid.lo = mid.hi;
212   mid.hi = 0;
213   result = _cairo_uint128_add (hi,mid);
214   return result;
215 }
216 
217 int64x64_t
Invert(const uint64_t v)218 int64x64_t::Invert (const uint64_t v)
219 {
220   NS_ASSERT (v > 1);
221   cairo_uint128_t a, factor;
222   a.hi = 1;
223   a.lo = 0;
224   factor.hi = 0;
225   factor.lo = v;
226   int64x64_t result;
227   result._v = Udiv (a, factor);
228   int64x64_t tmp = int64x64_t (v, 0);
229   tmp.MulByInvert (result);
230   if (tmp.GetHigh () != 1)
231     {
232       cairo_uint128_t one = { 1, 0};
233       result._v = _cairo_uint128_add (result._v, one);
234     }
235   return result;
236 }
237 
238 
239 } // namespace ns3
240