Skip to content

Repository files navigation

BigInt Multiplication

BigInt Multiplication provides an example for multiplying two 64-bit arrays. Performs a total of leftLength x rightLength multiplication operations. It does not require the result array to be reset while the calculation is performed and sequential access is provided while reaching the result. Performing multiplication sequentially may be more amenable to parallel computation in the future. Especially if features like AVX, 64 bit multiplication and writing the result as low and high bits and addition with carry are included. Of course, loading the registers (ymm, zmm) from memory in reverse order can bring an extra performance increase.

Decimal digits count of 64-bit arrays

When performing mathematical operations, we may need operations with higher precision than numbers such as double or float. For example, if we want to find the roots of a quartic function as a double, the input of the function must be greater than the double precision.

LengthByte CountDecimal Count
864155
16128309
32256617
645121234
12810242467
25620484933
51240969865
1024819219719
20481638439447

Calculation Process

For example, if leftLength is 8 and rightLength is 5, the order of operations is listed in the table below. Result Index is the index where the multiplication result will be written. Left Index, Right Index values are multiplied and each product is added and written to Result Index. Rows can be divided into 3 regions. Start [0-4], middle [5-7], finish [8, 11]. Thus, the code is run without making too many checks in loops.

Result IndexLeft Index, Right Index
0 0,0
1 0,1 1,0
2 0,2 1,1 2,0
3 0,3 1,2 2,1 3,0
4 0,4 1,3 2,2 3,1 4,0
5 X 1,4 2,3 3,2 4,1 5,0
6 X X 2,4 3,3 4,2 5,1 6,0
7 X X X 3,4 4,3 5,2 6,1 7,0
8 X X X X 4,4 5,3 6,2 7,1 X
9 X X X X X 5,4 6,3 7,2 X X
10 X X X X X X 6,4 7,3 X X X
11 X X X X X X X 7,4 X X X X

Performance Results

In the performance table below, two random 64-bit numbers of length leftLength and rightLength were multiplied. Each line was run 300 times and the minimum ElapsedTicks values were written with StopWatch(.NET). ExpectedTick(max) belongs to BigInteger(.NET).

leftLengthrightLengthexpectedTick(max)actualTickPercent
16169666%
1632181266%
1664362158%
16128724055%
162561417855%
1651227814451%
16102456126547%
162048112852846%
3216181266%
3232302376%
3264584577%
321281168270%
3225623317072%
3251246733471%
32102493164068%
3220481871132370%
6416482347%
6432854654%
64641448559%
6412828918363%
64256330356107%
64512654696106%
64102413081390106%
64204826122709103%
12816613760%
128321008484%
12864162176108%
128128253355140%
128256503723143%
12851210091405139%
128102419962834141%
128204840395794143%
256161237258%
2563220016984%
25664325351108%
256128504707140%
2562567741467189%
25651215432963192%
256102430465860192%
2562048579611690201%
5121624314358%
5123240031779%
51264650686105%
51212810101451143%
51225615452954191%
51251223415972255%
5121024437712077275%
5122048876023290265%
10241648826654%
10243280367183%
10246412781398109%
102412820242849140%
102425630835869190%
1024512446711852265%
10241024660323285352%
102420481297447030362%
20481693049353%
2048321602129380%
20486426072777106%
204812840325703141%
2048256577011730203%
2048512873122610258%
204810241318646620353%
204820482023994075464%

Conclusion

This method can provide better performance than BigInteger for values smaller than 1234 digits. Perhaps using this method, two 64-bit numbers can be multiplied simultaneously with AVX-256 and all performance lines can be achieved.

About

BigInt Multiplication

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages