Coder Social home page Coder Social logo

bigint's Introduction

Efficient BigInteger Implementation

This is an improved version of java.math.BigInteger that uses fast algorithms for multiplying and dividing large numbers. It is based on Alan Eliasen's BigInteger patch which provides the Karatsuba and Toom-Cook implementations.

Depending on the input size, numbers are multiplied using Long Multiplication, Karatsuba, Toom-Cook, or Schönhage-Strassen. For division, Long Division, Burnikel-Ziegler Division, or Barrett Division is used.

This code has been merged into OpenJDK 8 except for the Schönhage-Strassen and Barrett algorithms which are planned for OpenJDK 9.

Benchmark results for multiplication of two n-digit numbers (Intel i3 @3.1 GHz, 64-bit mode):

nOpenJDK 7 BigIntegerOpenJDK 8 BigIntegerImproved BigIntegerSpeedup vs JDK8Algorithm
10.00006ms.00006ms.00006ms1.0Long
25.00008ms.00008ms.00008ms1.0Long
50.00016ms.00015ms.00015ms1.0Long
75.00022ms.00020ms.00020ms1.0Long
100.00037ms.00032ms.00033ms1.0Long
250.0016ms.00016ms.0016ms1.0Long
500.0063ms.00053ms.0055ms1.0Kara
750.014ms.012ms.012ms1.0Toom
1,000.024ms.018ms.018ms1.0Toom
2,500.15ms.080ms.082ms1.0Toom
5,000.57ms.23ms.23ms1.0Toom
7,5001.3ms.43ms.44ms1.0Toom
10,0002.3ms.64ms.66ms1.0Toom
25,00014ms2.5ms2.6ms1.0Toom
50,00057ms7.2ms7.0ms1.0Toom
75,000.13s13ms6.5ms2.0SS
100,000.23s20ms14ms1.4SS
250,0001.4s76ms30ms2.5SS
500,0005.7s.22s77ms2.9SS
750,00013s.38s.16s2.4SS
1,000,00023s.62s.16s3.9SS
2,500,000151s2.3s.44s5.2SS
5,000,000620s6.7s.89s7.5SS
7,500,0001395s12s2.3s5.2SS
10,000,0002488s18s2.3s7.8SS
25,000,00067s12s5.6SS
50,000,000181s25s7.2SS
75,000,000339s25s14SS
100,000,000454s61s7.4SS

Benchmark results for division of a 2n-digit number by a n-digit number (Intel i3 @3.1 GHz, 64-bit mode):

nOpenJDK 7 BigIntegerOpenJDK 8 BigIntegerImproved BigIntegerSpeedup vs JDK8Algorithm
10.00016ms.00016ms.000016ms1.0Long
25.00030ms.00031ms.00031ms1.0Long
50.00052ms.00054ms.00054ms1.0Long
75.00072ms.00074ms.00074ms1.0Long
100.0011ms.0011ms.0011ms1.0Long
250.0037ms.0036ms.0037ms1.0Long
500.013ms.012ms.012ms1.0Long
750.026ms.23ms.022ms1.0BZ
1,000.045ms.036ms.035ms1.0BZ
2,500.26ms.15ms.15ms1.0BZ
5,0001.0ms.45ms.44ms1.0BZ
7,5002.3ms.82ms.83ms1.0BZ
10,0004.0ms1.3ms1.3ms1.0BZ
25,00025ms5.5ms5.4ms1.0BZ
50,00099ms15ms15ms1.0BZ
75,000.22s29ms25ms1.2BZ
100,000.40s45ms42ms1.1BZ
250,0002.5s.17s.12s1.4Barr
500,0009.9s.48s.29s1.7Barr
750,00022s.88s.66s1.3BZ
1,000,00040s1.4s.64s2.2Barr
2,500,000250s5.2s1.6s3.2Barr
5,000,0001066s15s3.5s4.3Barr
7,500,0002346s26s8.3s3.1Barr
10,000,0004464s41s8.4s4.9Barr
25,000,000156s45s3.5Barr
50,000,000421s96s4.4Barr
75,000,000800s96s8.3Barr
100,000,0001151s247s4.7Barr

bigint's People

Contributors

tbuktu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bigint's Issues

Override Java's BigInteger

Is is possible to use this code on our code in order to override the default BigInteger?
I just added it to my project but I am still using the default implementation.

ToomCook will not activate in certain situations?

At line 1399 you have code like this:

        if ((xlen < KARATSUBA_THRESHOLD) || (ylen < KARATSUBA_THRESHOLD))
        {
            int resultSign = signum == val.signum ? 1 : -1;
            if (val.mag.length == 1) {
                return multiplyByInt(mag,val.mag[0], resultSign);
            }
            if(mag.length == 1) {
                return multiplyByInt(val.mag,mag[0], resultSign);
            }
            int[] result = multiplyToLen(mag, xlen,
                                         val.mag, ylen, null);
            result = trustedStripLeadingZeroInts(result);
            return new BigInteger(result, resultSign);
        }
        else
            if ((xlen < TOOM_COOK_THRESHOLD) && (ylen < TOOM_COOK_THRESHOLD))
                return multiplyKaratsuba(this, val);
            else

I've been implementing my own bigint for scala based on your code, and I noticed that even though the TOOM_COOK algorithm is supposed to activate if either xlen or ylen is at or over 75, the KARATSUBA threshold is not passed if xlen AND ylen are equal to or over 50. This means that if you are multiplying a humongous BigInteger(like one with a magnitude of 1000) against one with a magnitude of 49, it will use the multiplyToLen method. Is this behaviour intended?

Schönhage-Strassen Java 9

Schönhage-Strassen and Barrett algorithms are planned for Java 9. Are they already there? if not, is it still planned?

Burnikel Division threshold on ARM Cortex-A53

I'm trying to tune Burnikel Division Threshold on my Samsung J7 Pro that has ARM Cortex-A53 processor, the burnikel division outperform knuth division when the mag length is 225, why it takes so large value? Should I change the Burnikel offset value too?

Line 1558 in BigInteger.java has a bug.

        // result = p1 * 2^(32*2*half) + (p3 - p1 - p2) * 2^(32*half) + p2
        BigInteger result = p1.shiftLeft(32*half).add(p3.subtract(p1).subtract(p2)).shiftLeft(32*half).add(p2);

should be

        BigInteger result = p1.shiftLeft(64*half).add(p3.subtract(p1).subtract(p2)).shiftLeft(32*half).add(p2);

as per your comment. I have confirmed the current code does not give the correct results when compared to the original BigInteger.

java.lang.ArrayIndexOutOfBoundsException: 10

Hi, I am using your BigInt to implement a encryption algorithm. By testing, your BigInt has a better performance than BigInteger, so thanks for you creating a so excellent BigInt. But I occured one problem.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at com.zqx.ivhe.BigInt.subMag(BigInt.java:1153) at com.zqx.ivhe.BigInt.add(BigInt.java:1172)

I need to calcuate multiplication between a vector and a matrix, the size of them is (1025) (1025,1025). The element of the vector and matrix is a BigInt Object, and every element is a very big integer. I test for another vector and matrix with size of (65), (65,65) and successfully. But the size of them is (1025) (1025,1025) occur the above problem.
Please help me.
Thansk.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.