The Fibonacci sequence is a series of numbers where each term is the sum of the two preceding ones, starting from 0 and 1. Mathematically, it's defined as
fibonacci(nthNumber):
nMinus2 = 0
nMinus1 = 0
n = 1
for i from 1 to nthNumber - 1:
nMinus2 = nMinus1
nMinus1 = n
n = nMinus2 + nMinus1
return n
The Fibonacci sequence grows exponentially, meaning that Fibonacci numbers increase in size very quickly. Calculating higher Fibonacci terms can rapidly exceed the capacity of standard integer sizes. In this implementation, 128-bit integers are used to handle the overflow risk associated with large Fibonacci values. By using 128-bit operations, the program can correctly calculate much larger Fibonacci terms without overflow, ensuring accurate results even for high input values that would otherwise exceed the limits of 64-bit integers
A total of six 64-bit registers are used, organized in pairs to represent nMinus2
, nMinus1
, and n
as 128-bit values.
- Register assignments for each 128-bit value:
nMinus2
:- Lower 64 bits:
r8
- Upper 64 bits:
r9
- Lower 64 bits:
nMinus1
:- Lower 64 bits:
r10
- Upper 64 bits:
r11
- Lower 64 bits:
n
(current Fibonacci term):- Lower 64 bits:
rax
- Upper 64 bits:
rdx
- Lower 64 bits:
This setup allows efficient handling of large Fibonacci values by pairing 64-bit registers to represent each term as a 128-bit number.
Details in implementation can be found in the comments of fibo.S
.
I just used ChatGPT to generate this part as it wasn't the focus of the task.
void uint128_to_string(unsigned __int128 num, char *str)
allows the 128-Bit integers to be printed as a string.void uint128_to_hex_string(unsigned __int128 num, char *str)
allows the 128-Bit integers to be printed in hex as a string.