Fast Exponentiation Algorithm
Computing exponentiation efficiently using fold or forward, particularly using exponentiation by squaring.
| Section | Description | Key Concepts |
|---|---|---|
| Naive Implementation of Exponentiation | A naive implementation of base^n by
performing n multiplications. |
|
| Fast Exponentiation by Squaring | Fast exponentiation using at most 2 * log₂(n) multiplications, with a partial forward. |
|
| Optimization of Fast Exponentiation for Embedded Systems | Optimization for embedded systems of fast exponentiation implementation with forward (non-partial). |
|
In this illustrative example, we implement different algorithms for computing exponentiation by an integer (that is, basen with n being an integer) to show different usages of fold and forward. As a reminder, here is the definition of the exponentiation (n is called the exponent):
In safety-critical embedded systems, handling arithmetic overflow is essential for preserving data integrity and system stability. To focus on the fundamental concepts of the language in our example without being sidetracked by edge cases like arithmetic overflow, we assume that the inputs do not cause such overflow.
We begin with an explanation of a naive approach to exponentiation, which involves multiplying base by itself n times with a fold; this method requires n multiplications, making it a costly algorithm.
Next, we explore the fast exponentiation technique called Exponentiation by Squaring. In this method, we only need to perform at most 2*q iterations for q-bit exponent values (for instance, 2*16 iterations for exponents being a 16-bits integer). In this case, we use a partial forward block.
Finally, we optimize this approach for embedded systems with a fixed computation time of fixed q iterations for q-bit exponent values and a suppression of comparison for the loop termination condition at each iteration. For this last approach, we use a forward block.
In each step, we discuss the underlying concepts, their advantages and limitations to help you gain a deeper understanding of fold and forward through algorithms for exponentiation.