MCPcopy Create free account
hub / github.com/Clarionos/clarion / divide

Function divide

libraries/fc/src/uint128.cpp:14–45  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12
13 template <typename T>
14 static void divide(const T &numerator, const T &denominator, T &quotient, T &remainder)
15 {
16 static const int bits = sizeof(T) * 8;//CHAR_BIT;
17
18 if(denominator == 0) {
19 throw std::domain_error("divide by zero");
20 } else {
21 T n = numerator;
22 T d = denominator;
23 T x = 1;
24 T answer = 0;
25
26
27 while((n >= d) && (((d >> (bits - 1)) & 1) == 0)) {
28 x <<= 1;
29 d <<= 1;
30 }
31
32 while(x != 0) {
33 if(n >= d) {
34 n -= d;
35 answer |= x;
36 }
37
38 x >>= 1;
39 d >>= 1;
40 }
41
42 quotient = answer;
43 remainder = n;
44 }
45 }
46
47 uint128::uint128(const std::string &sz)
48 :hi(0), lo(0)

Callers 1

uint128.cppFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected