A Discrete-Event Network Simulator
API
int64x64-128.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 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  */
19 
20 #include "int64x64-128.h"
21 #include "abort.h"
22 #include "assert.h"
23 #include "log.h"
24 
25 namespace ns3 {
26 
27 // Note: Logging in this file is largely avoided due to the
28 // number of calls that are made to these functions and the possibility
29 // of causing recursions leading to stack overflow
30 NS_LOG_COMPONENT_DEFINE ("int64x64-128");
31 
43 static inline
44 bool
46  const int128_t sb,
47  uint128_t & ua,
48  uint128_t & ub)
49 {
50  bool negA = sa < 0;
51  bool negB = sb < 0;
52  ua = negA ? -sa : sa;
53  ub = negB ? -sb : sb;
54  return (negA && !negB) || (!negA && negB);
55 }
56 
57 void
59 {
60  uint128_t a, b;
61  bool negative = output_sign (_v, o._v, a, b);
62  uint128_t result = Umul (a, b);
63  _v = negative ? -result : result;
64 }
65 
68 {
69  uint128_t aL = a & HP_MASK_LO;
70  uint128_t bL = b & HP_MASK_LO;
71  uint128_t aH = (a >> 64) & HP_MASK_LO;
72  uint128_t bH = (b >> 64) & HP_MASK_LO;
73 
74  uint128_t result;
75  uint128_t hiPart, loPart, midPart;
76  uint128_t res1, res2;
77 
78  // Multiplying (a.h 2^64 + a.l) x (b.h 2^64 + b.l) =
79  // 2^128 a.h b.h + 2^64*(a.h b.l+b.h a.l) + a.l b.l
80  // get the low part a.l b.l
81  // multiply the fractional part
82  loPart = aL * bL;
83  // compute the middle part 2^64*(a.h b.l+b.h a.l)
84  midPart = aL * bH + aH * bL;
85  // compute the high part 2^128 a.h b.h
86  hiPart = aH * bH;
87  // if the high part is not zero, put a warning
88  NS_ABORT_MSG_IF ((hiPart & HP_MASK_HI) != 0,
89  "High precision 128 bits multiplication error: multiplication overflow.");
90 
91  // Adding 64-bit terms to get 128-bit results, with carries
92  res1 = loPart >> 64;
93  res2 = midPart & HP_MASK_LO;
94  result = res1 + res2;
95 
96  res1 = midPart >> 64;
97  res2 = hiPart & HP_MASK_LO;
98  res1 += res2;
99  res1 <<= 64;
100 
101  result += res1;
102 
103  return result;
104 }
105 
106 void
108 {
109  uint128_t a, b;
110  bool negative = output_sign (_v, o._v, a, b);
111  int128_t result = Udiv (a, b);
112  _v = negative ? -result : result;
113 }
114 
115 uint128_t
117 {
118 
119  uint128_t rem = a;
120  uint128_t den = b;
121  uint128_t quo = rem / den;
122  rem = rem % den;
123  uint128_t result = quo;
124 
125  // Now, manage the remainder
126  const uint64_t DIGITS = 64; // Number of fraction digits (bits) we need
127  const uint128_t ZERO = 0;
128 
129  NS_ASSERT_MSG (rem < den,
130  "Remainder not less than divisor");
131 
132  uint64_t digis = 0; // Number of digits we have already
133  uint64_t shift = 0; // Number we are going to get this round
134 
135  // Skip trailing zeros in divisor
136  while ( (shift < DIGITS) && !(den & 0x1))
137  {
138  ++shift;
139  den >>= 1;
140  }
141 
142  while ( (digis < DIGITS) && (rem != ZERO) )
143  {
144  // Skip leading zeros in remainder
145  while ( (digis + shift < DIGITS) &&
146  !(rem & HP128_MASK_HI_BIT))
147  {
148  ++shift;
149  rem <<= 1;
150  }
151 
152  // Cast off denominator bits if:
153  // Need more digits and
154  // LSB is zero or
155  // rem < den
156  while ( (digis + shift < DIGITS) &&
157  ( !(den & 0x1) || (rem < den) ) )
158  {
159  ++shift;
160  den >>= 1;
161  }
162 
163  // Do the division
164  quo = rem / den;
165  rem = rem % den;
166 
167  // Add in the quotient as shift bits of the fraction
168  result <<= shift;
169  result += quo;
170 
171  digis += shift;
172  shift = 0;
173  }
174  // Did we run out of remainder?
175  if (digis < DIGITS)
176  {
177  shift = DIGITS - digis;
178  result <<= shift;
179  }
180 
181  return result;
182 }
183 
184 void
186 {
187  bool negResult = _v < 0;
188  uint128_t a = negResult ? -_v : _v;
189  uint128_t result = UmulByInvert (a, o._v);
190 
191  _v = negResult ? -result : result;
192 }
193 
194 uint128_t
196 {
197  uint128_t result, ah, bh, al, bl;
198  uint128_t hi, mid;
199  ah = a >> 64;
200  bh = b >> 64;
201  al = a & HP_MASK_LO;
202  bl = b & HP_MASK_LO;
203  hi = ah * bh;
204  mid = ah * bl + al * bh;
205  mid >>= 64;
206  result = hi + mid;
207  return result;
208 }
209 
210 int64x64_t
211 int64x64_t::Invert (const uint64_t v)
212 {
213  NS_ASSERT (v > 1);
214  uint128_t a;
215  a = 1;
216  a <<= 64;
217  int64x64_t result;
218  result._v = Udiv (a, v);
219  int64x64_t tmp = int64x64_t (v, false);
220  tmp.MulByInvert (result);
221  if (tmp.GetHigh () != 1)
222  {
223  result._v += 1;
224  }
225  return result;
226 }
227 
228 } // namespace ns3
229 
High precision numerical type, implementing Q64.64 fixed precision.
Definition: int64x64-128.h:39
static uint128_t Udiv(const uint128_t a, const uint128_t b)
Unsigned division of Q64.64 values.
static int64x64_t Invert(const uint64_t v)
Compute the inverse of an integer value.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
__int128_t int128_t
Definition: int64x64-128.h:30
void MulByInvert(const int64x64_t &o)
Multiply this value by a Q0.128 value, presumably representing an inverse, completing a division oper...
static uint128_t UmulByInvert(const uint128_t a, const uint128_t b)
Unsigned multiplication of Q64.64 and Q0.128 values.
__uint128_t uint128_t
Definition: int64x64-128.h:29
static uint128_t Umul(const uint128_t a, const uint128_t b)
Unsigned multiplication of Q64.64 values.
Definition: int64x64-128.cc:67
int128_t _v
The Q64.64 value.
Definition: int64x64-128.h:333
void Mul(const int64x64_t &o)
Implement *=.
Definition: int64x64-128.cc:58
int64x64_t()
Default constructor.
Definition: int64x64-128.h:80
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static const uint128_t HP128_MASK_HI_BIT
uint128_t high bit (sign bit).
Definition: int64x64-128.h:42
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:84
static const uint64_t HP_MASK_LO
Mask for fraction part.
Definition: int64x64-128.h:44
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
void Div(const int64x64_t &o)
Implement /=.
int64_t GetHigh(void) const
Get the integer portion.
Definition: int64x64-128.h:213
Debug message logging.
static const uint64_t HP_MASK_HI
Mask for sign + integer part.
Definition: int64x64-128.h:46
NS_ABORT_x macro definitions.
static bool output_sign(const int128_t sa, const int128_t sb, uint128_t &ua, uint128_t &ub)
Compute the sign of the result of multiplying or dividing Q64.64 fixed precision operands.
Definition: int64x64-128.cc:45