|
| 1 | +# Binary Exponentiation by Factoring |
| 2 | + |
| 3 | +Binary exponentiation by factoring is a binary trick (*) which allows to |
| 4 | +calculate $a^n$ using only $O(1)$ multiplications, instead of $O(log n)$ |
| 5 | +multiplications required by the classical binary exponentiation approach. |
| 6 | + |
| 7 | +## Algorithm |
| 8 | + |
| 9 | +Raising $a$ to the power of $n$ is expressed naively as the exponent |
| 10 | +of the logarithm of $a$ multiplied by $n$ being the exponent. In the |
| 11 | +following examples all base numbers, $a$, are assumed to be odd. |
| 12 | + |
| 13 | +## Implementation for 32-bit integers |
| 14 | + |
| 15 | +### The logarithm table needed |
| 16 | + |
| 17 | +```cpp |
| 18 | +static const uint32_t mbin_log_32_table[32] = { |
| 19 | + 0x00000000, |
| 20 | + 0x00000000, |
| 21 | + 0xd3cfd984, |
| 22 | + 0x9ee62e18, |
| 23 | + 0xe83d9070, |
| 24 | + 0xb59e81e0, |
| 25 | + 0xa17407c0, |
| 26 | + 0xce601f80, |
| 27 | + 0xf4807f00, |
| 28 | + 0xe701fe00, |
| 29 | + 0xbe07fc00, |
| 30 | + 0xfc1ff800, |
| 31 | + 0xf87ff000, |
| 32 | + 0xf1ffe000, |
| 33 | + 0xe7ffc000, |
| 34 | + 0xdfff8000, |
| 35 | + 0xffff0000, |
| 36 | + 0xfffe0000, |
| 37 | + 0xfffc0000, |
| 38 | + 0xfff80000, |
| 39 | + 0xfff00000, |
| 40 | + 0xffe00000, |
| 41 | + 0xffc00000, |
| 42 | + 0xff800000, |
| 43 | + 0xff000000, |
| 44 | + 0xfe000000, |
| 45 | + 0xfc000000, |
| 46 | + 0xf8000000, |
| 47 | + 0xf0000000, |
| 48 | + 0xe0000000, |
| 49 | + 0xc0000000, |
| 50 | + 0x80000000, |
| 51 | +}; |
| 52 | +``` |
| 53 | + |
| 54 | +### The logarithmic function |
| 55 | + |
| 56 | +```cpp |
| 57 | +static uint32_t |
| 58 | +mbin_log_32(uint32_t r, uint32_t x) |
| 59 | +{ |
| 60 | + uint8_t n; |
| 61 | + |
| 62 | + for (n = 2; n != 16; n++) { |
| 63 | + if (x & (1 << n)) { |
| 64 | + x = x + (x << n); |
| 65 | + r -= mbin_log_32_table[n]; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + r -= (x & 0xFFFF0000); |
| 70 | + |
| 71 | + return (r); |
| 72 | +} |
| 73 | +``` |
| 74 | +
|
| 75 | +### The exponent function |
| 76 | +
|
| 77 | +```cpp |
| 78 | +static uint32_t |
| 79 | +mbin_exp_32(uint32_t r, uint32_t x) |
| 80 | +{ |
| 81 | + uint8_t n; |
| 82 | +
|
| 83 | + for (n = 2; n != 16; n++) { |
| 84 | + if (x & (1 << n)) { |
| 85 | + r = r + (r << n); |
| 86 | + x -= mbin_log_32_table[n]; |
| 87 | + } |
| 88 | + } |
| 89 | +
|
| 90 | + r *= 1 - (x & 0xFFFF0000); |
| 91 | +
|
| 92 | + return (r); |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +### The exponentiation function |
| 97 | + |
| 98 | +```cpp |
| 99 | +static uint32_t |
| 100 | +mbin_power_odd_32(uint32_t rem, uint32_t base, uint32_t exp) |
| 101 | +{ |
| 102 | + if (base & 2) { |
| 103 | + /* divider is considered negative */ |
| 104 | + base = -base; |
| 105 | + /* check if result should be negative */ |
| 106 | + if (exp & 1) |
| 107 | + rem = -rem; |
| 108 | + } |
| 109 | + return (mbin_exp_32(rem, mbin_log_32(0, base) * exp)); |
| 110 | +} |
| 111 | +``` |
| 112 | +
|
| 113 | +* [M30, Hans Petter Selasky, 2009](https://books.google.no/books?id=IkuEBAAAQBAJ&pg=PT310&lpg=PT310&dq=M30+selasky&source=bl&ots=W86b0P-yfw&sig=ACfU3U0Z7orvRJyZSGuUbOQ8H85sSpZHpQ&hl=no&sa=X&ved=2ahUKEwiC87OO1Zn3AhXrtYsKHTcIAgUQ6AF6BAgCEAM#v=onepage&q=M30%20selasky&f=false) |
0 commit comments