2

I need to store and use numeric values bigger than ULLONG_MAX.

I will need to make arithmetic operations with those values, so I think store as char** is not a option.

Is there a way to dynamically create an additional long prefix in those cases?


Thank you all. Based on responses, Very helpful but I doubt about the best performance option. piokuc quote a performance link, but it's unclear. In this time I'm between GMP or MPIR, there is some one faster?

Jens
  • 69,818
  • 15
  • 125
  • 179
ton
  • 3,827
  • 1
  • 42
  • 40

2 Answers2

4

You need to use one of the arbitrary precision arithmetic libraries, like GMP, see also What's the best (for speed) arbitrary-precision library for C++?

Community
  • 1
  • 1
piokuc
  • 25,594
  • 11
  • 72
  • 102
  • I don't think it can ben a new post, but.. The link about performance seems ineffective, there is no benchmark reference to say that GMP is the better... seems a filling, what about MPIR, it's a fork with GPU intents, what's the better for a new software ? GMP or MPIR? – ton Aug 06 '13 at 08:28
  • 1
    I didn't know MPIR. It seems to be a lively project with a momentum, there is a community of users and contributors behind it, which is great. Apparently they added a lot of stuff on top of GMP. It depends on your requirements and set up, for examples if you are on Linux then I guess you have GMP already installed and so it will be easier for you to start with it. On the other hand I presume the basic API for arbitrary precision maths should be the same or very similar in both libs so it should be easy to switch from one to the other if you start with the common subset. – piokuc Aug 06 '13 at 09:02
3

In addition to multi-precision libraries such as GMP, if you are using GCC on a 64-bit architecture, you also have the option of using __int128 (documentation).

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
  • 2
    clang also supports `__int128_t` (and `__uint128_t`). – Stephen Canon Aug 05 '13 at 15:45
  • 1
    The code won't be portable in any way, but if you only need a quick and dirty job - then this is great – rabensky Aug 05 '13 at 16:03
  • dirty, understood, but what about performance? is (__uint128 or __uint128_t) better than GMP ? – ton Aug 06 '13 at 08:30
  • 1
    @ton I wouldn't say it “dirty” (that was cluracan's comment). It fills a different niche. It is much faster if you only need 128 bits. GMP spends its time loading lengths from dynamically allocated objects and looping unknown numbers of iterations (varying according to operands' lengths). 128-bit operations are straight executions paths, 2 64-bit additions to simulate one 128-bit addition, 4 multiplications to simulate one 128-bit multiplication, this kind of thing. Don't get me wrong, GMP is well-written and optimized, but you can't expect it to fight against __int128 on its own ground – Pascal Cuoq Aug 06 '13 at 15:33