none
BigInteger source code

    Question

  • Is the source code for System.Numerics.BigInteger posted anywhere?

    I am developing a real-time data acquisition and graphing application where we will have thousands to millions of Int128s and possibly Int256s.  I have reviewed several others and found everything else slower, in some cases considerably, than this class.  My testing has shown NET's BigInteger  to be the clear performance winner with only some undesirable memory handling since it is variable size, so I was wondering if I could get it's source to implement a modified version with the different memory characteristics we need.

    For others:  I've already reviewed MPIR/GMP, SoftFluent's Int128, XInt, Mono's, and the CodeProject big integer.  All are slower, though Mono's is nearly as good, and many hugely so.

    David L-

    Wednesday, August 22, 2012 7:00 PM

Answers

  • Is the source code for System.Numerics.BigInteger posted anywhere?

    [...]

    so I was wondering if I could get it's source to implement a modified version with the different memory characteristics we need.

    The answer to your first question is "No" (except for the obvious impossibility to prove that large a negative).

    If your business case really justifies the effort, you'll be better off asking someone from microsoft directly. I would suggest http://blogs.msdn.com/b/bclteam/archive/2007/01/16/introducing-system-numeric-biginteger-inbar-gazit.aspx as a starting point.


    Volker

    • Proposed as answer by Hetzi Monday, August 27, 2012 11:40 AM
    • Marked as answer by Mike FengModerator Thursday, September 06, 2012 3:47 AM
    Monday, August 27, 2012 9:22 AM

All replies

  • The .NET 4.5 framework just release reference source.  See: http://blogs.msdn.com/b/dotnet/archive/2012/08/15/announcing-the-release-of-net-framework-4-5-rtm-product-and-source-code.aspx

    You can download from: http://referencesource.microsoft.com/netframework.aspx

    This will include BigInteger's source.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, August 22, 2012 7:02 PM
    Moderator
  • My apologies, I forgot to mention I have already tried downloading the NET 4.5 and 4.0 framework source.  Neither contained the source for System.Numerics.BigInteger.  There is a similarly-named BigInteger class, but it is in System.Xml.Xsl.XPathConvert.  It works quite differently and is private, anyway.

    Wednesday, August 22, 2012 7:10 PM
  • Don't know why it isn't there.  Isn't the reflected code from ILSpy adequate?
    Wednesday, August 22, 2012 8:04 PM
  • The original source has more comments and is more understandable.  I want to make sure not to break anything in my revision and the best way is to have the original source code.  Fast multiple-element arithmetic algorithms are not easy to understand; I have to do some algorithm adaptation to make this work.
    Wednesday, August 22, 2012 10:10 PM
  • "The original source has more comments and is more understandable."

    I wasn't aware that you had the original source code.  What exactly is your question?

    • Edited by JohnWein Wednesday, August 22, 2012 10:51 PM
    Wednesday, August 22, 2012 10:14 PM
  • I do not have the source code for BigInteger.  As I said above, I *want* the source code for BigInteger so I can create a modified version that occupies much less space (16 bytes) and is limited to 128-bit values as I will have hundreds of thousands and millions of these.


    David L-

    Thursday, August 23, 2012 2:26 PM
  • I do not have the source code for BigInteger.  As I said above, I *want* the source code for BigInteger so I can create a modified version that occupies much less space (16 bytes) and is limited to 128-bit values as I will have hundreds of thousands and millions of these.


    David L-

    By reflection, you have the source code.  I guess the problem is that you don't have the original comments for the source code and therefore can't understand the code.  If you ask questions about the particular code blocks that you don't understand, I'm sure you'll get help on these forums.  Using BigInteger as a basis for a fixed length interger type doesn't seem very performant.  Intel chips have native 128 bit registers and instructions.
    Thursday, August 23, 2012 3:40 PM
  • Just out of curiosity I tried this with 10 million 256 bit integers:

    static void Main(string[] args)
    	{
    	const int Size = 10000000;
    	BigInteger[] All = new BigInteger[Size];
    	System.Random Rnd = new Random();
    	for (int I = 0; I < Size; I++)
    		{
    		byte[] B=new byte[32];
    		Rnd.NextBytes(B);
    		B[31]=255;
    		All[I] = new BigInteger(B);
    		}
    	GC.Collect();
    	Console.WriteLine("Waiting...");
    	Console.ReadLine();
    	GC.KeepAlive(All);
    	}

    This uses about half a gig and, as x64 app, 750MB.

    If we assume 32 bit pointers I get about 350MB minimum memory size. I think 510MB isn't that bad. Are you programming really that close to the edge that the difference justifies the extra coding?


    Volker




    • Edited by Hetzi Thursday, August 23, 2012 4:24 PM
    Thursday, August 23, 2012 4:01 PM
  • Hi David,

    Did you tried this link:

    http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx


    Regards, http://www.shwetalodha.blogspot.in/

    Thursday, August 23, 2012 4:57 PM
  • David,

    During net surf, I came across this below article:

    "I just happened to find a BigInteger class in the source code of SharpDevelop (a free .NET IDE). Haven't got a clue whether it's the same as the .NET 4.0 BigInteger (it won't be exactly the same but it might work the same way), but perhaps worth a look.



    Here's another implementation:
    http://www.codeproject.com/KB/cs/biginteger.aspx"


    Regards, http://www.shwetalodha.blogspot.in/

    Thursday, August 23, 2012 5:04 PM
  • John, reflection does not provide source code, it is a decompilation representation from machine code.  The source code is not embedded within the assembly, and machine:source is not 1:1.  Intel and AMD processors do not have instructions to deal with 128-bit ints.  These are MMX/SIMD/AVE and do not deal with 128-bit values, they deal with groups of smaller values.

    Your responses appear to presuppose that I don't need what I'm asking for.  It would be more helpful if you instead responded with something helpful.

    Friday, August 24, 2012 1:48 PM
  • Thankyou, I did see that although I forgot to mention it in my list above.  The performance wasn't as good as Microsoft's, which is why I was seeking their implementation to learn how they did it better.
    Friday, August 24, 2012 1:50 PM
  • John, reflection does not provide source code, it is a decompilation representation from machine code.  The source code is not embedded within the assembly, and machine:source is not 1:1.  Intel and AMD processors do not have instructions to deal with 128-bit ints.  These are MMX/SIMD/AVE and do not deal with 128-bit values, they deal with groups of smaller values.

    Your responses appear to presuppose that I don't need what I'm asking for.  It would be more helpful if you instead responded with something helpful.

    My reponses suppose that you don't accept suggestions from responders.  I definitely don't think that you looked at the source code provided by ILSpy nor have you studied the SSE registers and instruction in the Intel software manuals. 

    It seems that the answer you are looking for to your question has already been given.  It's not available.  Mark the answer and close the thread.

    Friday, August 24, 2012 2:01 PM
  • Actually, I have a set of printed Intel IA-32 manuals right here.  Also, if you review the CLR capabilities, you will find there are no 128-bit integer instructions, and the CLR is what I'm really targetting.  The CIL instruction list is at http://en.wikipedia.org/wiki/List_of_CIL_instructions.

    Your responses sound a bit arrogant.  Maybe your time would be better spent elsewhere.  Why do I have to prove anything to you?

    Friday, August 24, 2012 7:43 PM
  • Is the source code for System.Numerics.BigInteger posted anywhere?

    [...]

    so I was wondering if I could get it's source to implement a modified version with the different memory characteristics we need.

    The answer to your first question is "No" (except for the obvious impossibility to prove that large a negative).

    If your business case really justifies the effort, you'll be better off asking someone from microsoft directly. I would suggest http://blogs.msdn.com/b/bclteam/archive/2007/01/16/introducing-system-numeric-biginteger-inbar-gazit.aspx as a starting point.


    Volker

    • Proposed as answer by Hetzi Monday, August 27, 2012 11:40 AM
    • Marked as answer by Mike FengModerator Thursday, September 06, 2012 3:47 AM
    Monday, August 27, 2012 9:22 AM
  • Hi David. I just happened to pass here by and noticed your question.

    I don't know if you find an answer but one of the reason the implementations you can find the source are slower than .NET's BigInteger is BigInteger has been NGen'd (http://msdn.microsoft.com/en-us/library/6t9t5wcf(v=vs.80).aspx). I wrote SoftFluent Int128 but I never tried to NGen it. That would be worth a try.


    Simon Mourier


    Monday, December 03, 2012 4:41 PM