A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
int64x64-cairo.cc
Go to the documentation of this file.
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 // Note: Logging in this file is largely avoided due to the
29 // number of calls that are made to these functions and the possibility
30 // of causing recursions leading to stack overflow
31 
32 NS_LOG_COMPONENT_DEFINE ("int64x64-cairo");
33 
34 // Include directly to allow optimizations within this compilation unit.
35 extern "C" {
36 #include "cairo-wideint.c"
37 }
38 
39 
40 namespace ns3 {
41 
42 static inline
43 bool
45  const cairo_int128_t sb,
46  cairo_uint128_t & ua,
47  cairo_uint128_t & ub)
48 {
49  bool negA = _cairo_int128_negative (sa);
50  bool negB = _cairo_int128_negative (sb);
51  ua = _cairo_int128_to_uint128 (sa);
52  ub = _cairo_int128_to_uint128 (sb);
53  ua = negA ? _cairo_uint128_negate (ua) : ua;
54  ub = negB ? _cairo_uint128_negate (ub) : ub;
55  return (negA && !negB) || (!negA && negB);
56 }
57 
58 void
59 int64x64_t::Mul (const int64x64_t & o)
60 {
61  cairo_uint128_t a, b;
62  bool sign = output_sign (_v, o._v, a, b);
63  cairo_uint128_t result = Umul (a, b);
64  _v = sign ? _cairo_uint128_negate (result) : result;
65 }
66 
67 cairo_uint128_t
68 int64x64_t::Umul (const cairo_uint128_t a, const cairo_uint128_t b)
69 {
70  cairo_uint128_t result;
71  cairo_uint128_t hiPart, loPart, midPart;
72  cairo_uint128_t res1, res2;
73 
74  // Multiplying (a.h 2^64 + a.l) x (b.h 2^64 + b.l) =
75  // 2^128 a.h b.h + 2^64*(a.h b.l+b.h a.l) + a.l b.l
76  // get the low part a.l b.l
77  // multiply the fractional part
78  loPart = _cairo_uint64x64_128_mul (a.lo, b.lo);
79  // compute the middle part 2^64*(a.h b.l+b.h a.l)
80  midPart = _cairo_uint128_add (_cairo_uint64x64_128_mul (a.lo, b.hi),
81  _cairo_uint64x64_128_mul (a.hi, b.lo));
82  // compute the high part 2^128 a.h b.h
83  hiPart = _cairo_uint64x64_128_mul (a.hi, b.hi);
84  // if the high part is not zero, put a warning
85  NS_ABORT_MSG_IF (hiPart.hi != 0,
86  "High precision 128 bits multiplication error: multiplication overflow.");
87 
88  // Adding 64-bit terms to get 128-bit results, with carries
89  res1 = _cairo_uint64_to_uint128 (loPart.hi);
90  res2 = _cairo_uint64_to_uint128 (midPart.lo);
91  result = _cairo_uint128_add (res1, res2);
92 
93  res1 = _cairo_uint64_to_uint128 (midPart.hi);
94  res2 = _cairo_uint64_to_uint128 (hiPart.lo);
95  res1 = _cairo_uint128_add (res1, res2);
96  res1 = _cairo_uint128_lsl (res1, 64);
97 
98  result = _cairo_uint128_add (result, res1);
99 
100  return result;
101 }
102 
103 void
104 int64x64_t::Div (const int64x64_t & o)
105 {
106  cairo_uint128_t a, b;
107  bool sign = output_sign (_v, o._v, a, b);
108  cairo_uint128_t result = Udiv (a, b);
109  _v = sign ? _cairo_uint128_negate (result) : result;
110 }
111 
112 cairo_uint128_t
113 int64x64_t::Udiv (const cairo_uint128_t a, const cairo_uint128_t b)
114 {
115  cairo_uint128_t den = b;
117  cairo_uint128_t result = qr.quo;
118  cairo_uint128_t rem = qr.rem;
119 
120  // Now, manage the remainder
121  const uint64_t DIGITS = 64; // Number of fraction digits (bits) we need
122  const cairo_uint128_t ZERO = _cairo_uint32_to_uint128 ((uint32_t)0);
123 
124  NS_ASSERT_MSG (_cairo_uint128_lt (rem, den),
125  "Remainder not less than divisor");
126 
127  uint64_t digis = 0; // Number of digits we have already
128  uint64_t shift = 0; // Number we are going to get this round
129 
130  // Skip trailing zeros in divisor
131  while ( (shift < DIGITS) && !(den.lo & 0x1))
132  {
133  ++shift;
134  den = _cairo_uint128_rsl (den, 1);
135  }
136 
137  while ( (digis < DIGITS) && !(_cairo_uint128_eq(rem, ZERO)) )
138  {
139  // Skip leading zeros in remainder
140  while ( (digis + shift < DIGITS) &&
141  !(rem.hi & HPCAIRO_MASK_HI_BIT) )
142  {
143  ++shift;
144  rem = _cairo_int128_lsl (rem, 1);
145  }
146 
147  // Cast off denominator bits if:
148  // Need more digits and
149  // LSB is zero or
150  // rem < den
151  while ( (digis + shift < DIGITS) &&
152  ( !(den.lo & 0x1) || _cairo_uint128_lt (rem, den) ) )
153  {
154  ++shift;
155  den = _cairo_uint128_rsl (den, 1);
156  }
157 
158  // Do the division
159  qr = _cairo_uint128_divrem (rem, den);
160 
161  // Add in the quotient as shift bits of the fraction
162  result = _cairo_uint128_lsl (result, shift);
163  result = _cairo_uint128_add (result, qr.quo);
164  rem = qr.rem;
165  digis += shift;
166  shift = 0;
167  }
168  // Did we run out of remainder?
169  if (digis < DIGITS)
170  {
171  shift = DIGITS - digis;
172  result = _cairo_uint128_lsl (result, shift);
173  }
174 
175  return result;
176 }
177 
178 void
179 int64x64_t::MulByInvert (const int64x64_t & o)
180 {
181  bool sign = _cairo_int128_negative (_v);
182  cairo_uint128_t a = sign ? _cairo_int128_negate (_v) : _v;
183  cairo_uint128_t result = UmulByInvert (a, o._v);
184 
185  _v = sign ? _cairo_int128_negate (result) : result;
186 }
187 
188 cairo_uint128_t
189 int64x64_t::UmulByInvert (const cairo_uint128_t a, const cairo_uint128_t b)
190 {
191  cairo_uint128_t result;
192  cairo_uint128_t hi, mid;
193  hi = _cairo_uint64x64_128_mul (a.hi, b.hi);
194  mid = _cairo_uint128_add (_cairo_uint64x64_128_mul (a.hi, b.lo),
195  _cairo_uint64x64_128_mul (a.lo, b.hi));
196  mid.lo = mid.hi;
197  mid.hi = 0;
198  result = _cairo_uint128_add (hi,mid);
199  return result;
200 }
201 
202 int64x64_t
203 int64x64_t::Invert (const uint64_t v)
204 {
205  NS_ASSERT (v > 1);
206  cairo_uint128_t a, factor;
207  a.hi = 1;
208  a.lo = 0;
209  factor.hi = 0;
210  factor.lo = v;
211  int64x64_t result;
212  result._v = Udiv (a, factor);
213  int64x64_t tmp = int64x64_t (v, 0);
214  tmp.MulByInvert (result);
215  if (tmp.GetHigh () != 1)
216  {
217  cairo_uint128_t one = { 1, 0};
218  result._v = _cairo_uint128_add (result._v, one);
219  }
220  return result;
221 }
222 
223 
224 } // namespace ns3
cairo_uint128_t I _cairo_uint64_to_uint128(cairo_uint64_t i)
static uint128_t Udiv(const uint128_t a, const uint128_t b)
Unsigned division of Q64.64 values.
Definition: int64x64-128.cc:87
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:170
cairo_uint128_t I _cairo_uint128_negate(cairo_uint128_t a)
int I _cairo_uint128_eq(cairo_uint128_t a, cairo_uint128_t b)
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.
cairo_uquorem128_t I _cairo_uint128_divrem(cairo_uint128_t num, cairo_uint128_t den)
int I _cairo_uint128_lt(cairo_uint128_t a, cairo_uint128_t b)
cairo_uint128_t I _cairo_uint128_rsl(cairo_uint128_t a, int shift)
cairo_uint128_t I _cairo_uint32_to_uint128(uint32_t i)
cairo_uint128_t I _cairo_uint64x64_128_mul(cairo_uint64_t a, cairo_uint64_t b)
static uint128_t Umul(const uint128_t a, const uint128_t b)
Unsigned multiplication of Q64.64 values.
Definition: int64x64-128.cc:38
int128_t _v
The Q64.64 value.
Definition: int64x64-128.h:310
void Mul(const int64x64_t &o)
Implement *=.
Definition: int64x64-128.cc:29
int64x64_t()
Default constructor.
Definition: int64x64-128.h:60
cairo_uint128_t I _cairo_uint128_lsl(cairo_uint128_t a, int shift)
static bool output_sign(const int128_t sa, const int128_t sb, uint128_t &ua, uint128_t &ub)
Definition: int64x64-128.cc:16
#define _cairo_int128_negative(a)
#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
#define _cairo_int128_lsl(a, b)
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if cond is true.
Definition: abort.h:101
void Div(const int64x64_t &o)
Implement /=.
Definition: int64x64-128.cc:78
#define _cairo_int128_negate(a)
#define _cairo_int128_to_uint128(i)
cairo_uint128_t I _cairo_uint128_add(cairo_uint128_t a, cairo_uint128_t b)