File tree Expand file tree Collapse file tree 2 files changed +34
-3
lines changed Expand file tree Collapse file tree 2 files changed +34
-3
lines changed Original file line number Diff line number Diff line change @@ -53,7 +53,7 @@ IOUAmount::normalize()
53
53
54
54
if (*stNumberSwitchover)
55
55
{
56
- Number v{mantissa_, exponent_};
56
+ const Number v{mantissa_, exponent_};
57
57
mantissa_ = v.mantissa ();
58
58
exponent_ = v.exponent ();
59
59
if (exponent_ > maxExponent)
Original file line number Diff line number Diff line change 18
18
// ==============================================================================
19
19
20
20
#include < ripple/basics/Number.h>
21
+ #include < boost/predef.h>
21
22
#include < algorithm>
22
23
#include < cassert>
23
24
#include < numeric>
@@ -335,6 +336,34 @@ Number::operator+=(Number const& y)
335
336
return *this ;
336
337
}
337
338
339
+ // Optimization equivalent to:
340
+ // auto r = static_cast<unsigned>(u % 10);
341
+ // u /= 10;
342
+ // return r;
343
+ // Derived from Hacker's Delight Second Edition Chapter 10
344
+ // by Henry S. Warren, Jr.
345
+ static inline unsigned
346
+ divu10 (uint128_t & u)
347
+ {
348
+ // q = u * 0.75
349
+ auto q = (u >> 1 ) + (u >> 2 );
350
+ // iterate towards q = u * 0.8
351
+ q += q >> 4 ;
352
+ q += q >> 8 ;
353
+ q += q >> 16 ;
354
+ q += q >> 32 ;
355
+ q += q >> 64 ;
356
+ // q /= 8 approximately == u / 10
357
+ q >>= 3 ;
358
+ // r = u - q * 10 approximately == u % 10
359
+ auto r = static_cast <unsigned >(u - ((q << 3 ) + (q << 1 )));
360
+ // correction c is 1 if r >= 10 else 0
361
+ auto c = (r + 6 ) >> 4 ;
362
+ u = q + c;
363
+ r -= c * 10 ;
364
+ return r;
365
+ }
366
+
338
367
Number&
339
368
Number::operator *=(Number const & y)
340
369
{
@@ -370,8 +399,10 @@ Number::operator*=(Number const& y)
370
399
g.set_negative ();
371
400
while (zm > maxMantissa)
372
401
{
373
- g.push (static_cast <unsigned >(zm % 10 ));
374
- zm /= 10 ;
402
+ // The following is optimization for:
403
+ // g.push(static_cast<unsigned>(zm % 10));
404
+ // zm /= 10;
405
+ g.push (divu10 (zm));
375
406
++ze;
376
407
}
377
408
xm = static_cast <rep>(zm);
You can’t perform that action at this time.
0 commit comments