www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Wanted: 128 bit integers

reply "Paul D. Anderson" <paul.d.removethis.anderson comcast.andthis.net> writes:
I'm working on a decimal arithmetic project and I need 128 bit 
integers in order to implement the decimal128 type. (The decimal 
value is stored in 128 bits; the coefficient field is 114 bits, 
to hold values with 34 decimal digits.)

I could use BigInt (but that's overkill) or I could code them up 
myself, which I'm willing to do if no alternative exists. But 
surely someone has already created this type, either as part of a 
larger project or as some kind of test problem, etc. Or perhaps 
someone would like to tackle it as a project of their own.

I specifically need unsigned 128 bit integers, but both signed 
and unsigned versions would probably have more general 
applications. Better yet, if it checked for overflow, like 
Andrei's CheckedInt class in TDPL, it would be more immediately 
useful.

This could also be the basis for a general fixed-size integer 
type, where the size is specified: CheckedInt!128, CheckedInt!96, 
etc. I recall having seen something like this mentioned in this 
forum.

So has anyone already done this? Does anyone want to take it on?

Thanks,

Paul
Mar 13 2012
next sibling parent reply =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <xtzgzorex gmail.com> writes:
On 14-03-2012 01:34, Paul D. Anderson wrote:
 I'm working on a decimal arithmetic project and I need 128 bit integers
 in order to implement the decimal128 type. (The decimal value is stored
 in 128 bits; the coefficient field is 114 bits, to hold values with 34
 decimal digits.)

 I could use BigInt (but that's overkill) or I could code them up myself,
 which I'm willing to do if no alternative exists. But surely someone has
 already created this type, either as part of a larger project or as some
 kind of test problem, etc. Or perhaps someone would like to tackle it as
 a project of their own.

 I specifically need unsigned 128 bit integers, but both signed and
 unsigned versions would probably have more general applications. Better
 yet, if it checked for overflow, like Andrei's CheckedInt class in TDPL,
 it would be more immediately useful.

 This could also be the basis for a general fixed-size integer type,
 where the size is specified: CheckedInt!128, CheckedInt!96, etc. I
 recall having seen something like this mentioned in this forum.

 So has anyone already done this? Does anyone want to take it on?

 Thanks,

 Paul

The D language specifies 128-bit integers: cent and ucent. They just aren't implemented yet... -- - Alex
Mar 13 2012
parent =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <xtzgzorex gmail.com> writes:
On 14-03-2012 17:49, Jonathan M Davis wrote:
 On Wednesday, March 14, 2012 10:57:03 Manu wrote:
 On 14 March 2012 02:37, Alex Rønne Petersen<xtzgzorex gmail.com>  wrote:
 On 14-03-2012 01:34, Paul D. Anderson wrote:
 The D language specifies 128-bit integers: cent and ucent. They just
 aren't implemented yet...

Why aren't they implemented in a library for the time being at least, so code can compile and work?

I believe that what it really comes down to is that cent and ucent were set aside just in case they would be needed with no specific plans to do anything with them. So, they'll probably be implemented eventually, but they're definitely not a priority. And if you really need larger integers, then there's BigInt. Prior to the 64-bit ports of dmd (which are fairly recent), dmd wasn't even on any architectures that supported 128 bit integers anyway. - Jonathan M Davis

There aren't really any platforms that natively support 128-bit integers, even today. The most "support" you'll get is SIMD-like extensions, but those aren't necessarily useful for implementing 128-bit integers. So, most likely, the compiler would just have to unroll 128-bit operations just as it does for 64-bit operations on 32-bit targets. -- - Alex
Mar 14 2012
prev sibling next sibling parent reply Manu <turkeyman gmail.com> writes:
--20cf300faca1c8bdd604bb30282b
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

On 14 March 2012 02:37, Alex R=C3=B8nne Petersen <xtzgzorex gmail.com> wrot=
e:

 On 14-03-2012 01:34, Paul D. Anderson wrote:

 I'm working on a decimal arithmetic project and I need 128 bit integers
 in order to implement the decimal128 type. (The decimal value is stored
 in 128 bits; the coefficient field is 114 bits, to hold values with 34
 decimal digits.)

 I could use BigInt (but that's overkill) or I could code them up myself,
 which I'm willing to do if no alternative exists. But surely someone has
 already created this type, either as part of a larger project or as some
 kind of test problem, etc. Or perhaps someone would like to tackle it as
 a project of their own.

 I specifically need unsigned 128 bit integers, but both signed and
 unsigned versions would probably have more general applications. Better
 yet, if it checked for overflow, like Andrei's CheckedInt class in TDPL,
 it would be more immediately useful.

 This could also be the basis for a general fixed-size integer type,
 where the size is specified: CheckedInt!128, CheckedInt!96, etc. I
 recall having seen something like this mentioned in this forum.

 So has anyone already done this? Does anyone want to take it on?

 Thanks,

 Paul

aren't implemented yet...

Why aren't they implemented in a library for the time being at least, so code can compile and work? --20cf300faca1c8bdd604bb30282b Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On 14 March 2012 02:37, Alex R=C3=B8nne Petersen= <span dir=3D"ltr">&lt;<a href=3D"mailto:xtzgzorex gmail.com">xtzgzorex gma= il.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"= margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> <div class=3D"HOEnZb"><div class=3D"h5">On 14-03-2012 01:34, Paul D. Anders= on wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> I&#39;m working on a decimal arithmetic project and I need 128 bit integers= <br> in order to implement the decimal128 type. (The decimal value is stored<br> in 128 bits; the coefficient field is 114 bits, to hold values with 34<br> decimal digits.)<br> <br> I could use BigInt (but that&#39;s overkill) or I could code them up myself= ,<br> which I&#39;m willing to do if no alternative exists. But surely someone ha= s<br> already created this type, either as part of a larger project or as some<br=


<br> I specifically need unsigned 128 bit integers, but both signed and<br> unsigned versions would probably have more general applications. Better<br> yet, if it checked for overflow, like Andrei&#39;s CheckedInt class in TDPL= ,<br> it would be more immediately useful.<br> <br> This could also be the basis for a general fixed-size integer type,<br> where the size is specified: CheckedInt!128, CheckedInt!96, etc. I<br> recall having seen something like this mentioned in this forum.<br> <br> So has anyone already done this? Does anyone want to take it on?<br> <br> Thanks,<br> <br> Paul<br> <br> </blockquote> <br></div></div> The D language specifies 128-bit integers: cent and ucent. They just aren&#= 39;t implemented yet...</blockquote><div><br></div><div>Why aren&#39;t they= implemented in a library for the time being at least, so code can compile = and work?</div> </div> --20cf300faca1c8bdd604bb30282b--
Mar 14 2012
parent reply "Daniel Murphy" <yebblies nospamgmail.com> writes:
"Manu" <turkeyman gmail.com> wrote in message 
news:mailman.657.1331715447.4860.digitalmars-d puremagic.com...

 Why aren't they implemented in a library for the time being at least, so 
 code can compile and work?

The obvious answer is because nobody's done it yet. I have a branch that partially implements ucent in the dmd backend (somewhere in my fork on github), but it will likely be at least a month before I have time to work on it again. It looks like it will be pretty easy on 64 bit.
Mar 14 2012
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 3/15/12 1:42 AM, Daniel Murphy wrote:
 "Manu"<turkeyman gmail.com>  wrote in message
 news:mailman.657.1331715447.4860.digitalmars-d puremagic.com...

 Why aren't they implemented in a library for the time being at least, so
 code can compile and work?

The obvious answer is because nobody's done it yet. I have a branch that partially implements ucent in the dmd backend (somewhere in my fork on github), but it will likely be at least a month before I have time to work on it again. It looks like it will be pretty easy on 64 bit.

My suggestion is to focus on fixed arbitrary-sized integers in Phobos, and then add optimizations for 64-bit integers on architectures that support it. Andrei
Mar 15 2012
parent "Daniel Murphy" <yebblies nospamgmail.com> writes:
"Andrei Alexandrescu" <SeeWebsiteForEmail erdani.org> wrote in message 
news:jjt1lh$pfb$1 digitalmars.com...
 My suggestion is to focus on fixed arbitrary-sized integers in Phobos, and 
 then add optimizations for 64-bit integers on architectures that support 
 it.

 Andrei

Yeah. A lot of the BigInt code can probably be adapted, if someone wants to give it a go.
Mar 15 2012
prev sibling next sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Wednesday, March 14, 2012 10:57:03 Manu wrote:
 On 14 March 2012 02:37, Alex Rønne Petersen <xtzgzorex gmail.com> wrote:
 On 14-03-2012 01:34, Paul D. Anderson wrote:
 The D language specifies 128-bit integers: cent and ucent. They just
 aren't implemented yet...

Why aren't they implemented in a library for the time being at least, so code can compile and work?

I believe that what it really comes down to is that cent and ucent were set aside just in case they would be needed with no specific plans to do anything with them. So, they'll probably be implemented eventually, but they're definitely not a priority. And if you really need larger integers, then there's BigInt. Prior to the 64-bit ports of dmd (which are fairly recent), dmd wasn't even on any architectures that supported 128 bit integers anyway. - Jonathan M Davis
Mar 14 2012
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Jonathan M Davis:

 Prior to the 64-bit ports of dmd (which are fairly recent), dmd 
 wasn't
 even on any architectures that supported 128 bit integers 
 anyway.

You are able to support 128 bit numbers even on a 16 bit system :-) Bye, bearophile
Mar 14 2012
prev sibling next sibling parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Wednesday, March 14, 2012 17:51:49 Alex Rønne Petersen wrote:
 On 14-03-2012 17:49, Jonathan M Davis wrote:
 On Wednesday, March 14, 2012 10:57:03 Manu wrote:
 On 14 March 2012 02:37, Alex Rønne Petersen<xtzgzorex gmail.com> wrote:
 On 14-03-2012 01:34, Paul D. Anderson wrote:
 The D language specifies 128-bit integers: cent and ucent. They just
 aren't implemented yet...

Why aren't they implemented in a library for the time being at least, so code can compile and work?

I believe that what it really comes down to is that cent and ucent were set aside just in case they would be needed with no specific plans to do anything with them. So, they'll probably be implemented eventually, but they're definitely not a priority. And if you really need larger integers, then there's BigInt. Prior to the 64-bit ports of dmd (which are fairly recent), dmd wasn't even on any architectures that supported 128 bit integers anyway. - Jonathan M Davis

There aren't really any platforms that natively support 128-bit integers, even today. The most "support" you'll get is SIMD-like extensions, but those aren't necessarily useful for implementing 128-bit integers. So, most likely, the compiler would just have to unroll 128-bit operations just as it does for 64-bit operations on 32-bit targets.

long long is 128 bits on 64-bit Linux. That's what I meant by support. 32-bit doesn't have that with any C type on any platform that I know of. I have no idea how it's implemented though. - Jonathan M Davis
Mar 14 2012
parent "Daniel Murphy" <yebblies nospamgmail.com> writes:
"Jonathan M Davis" <jmdavisProg gmx.com> wrote in message 
news:mailman.666.1331749219.4860.digitalmars-d puremagic.com...
 long long is 128 bits on 64-bit Linux.

Are you sure about this? I think we already had this discussion...
Mar 14 2012
prev sibling next sibling parent James Miller <james aatch.net> writes:
On 15 March 2012 13:55, Daniel Murphy <yebblies nospamgmail.com> wrote:
 "Jonathan M Davis" <jmdavisProg gmx.com> wrote in message
 news:mailman.666.1331749219.4860.digitalmars-d puremagic.com...
 long long is 128 bits on 64-bit Linux.

Are you sure about this? =C2=A0I think we already had this discussion...

Same discussion, different topic. I think the one before was sparked by size_t issues. -- James Miller
Mar 14 2012
prev sibling next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Thursday, March 15, 2012 11:55:46 Daniel Murphy wrote:
 "Jonathan M Davis" <jmdavisProg gmx.com> wrote in message
 news:mailman.666.1331749219.4860.digitalmars-d puremagic.com...
 
 long long is 128 bits on 64-bit Linux.

Are you sure about this? I think we already had this discussion...

If it isn't, then whatever that discussion was obviously didn't stick in my head. - Jonathan M Davis
Mar 14 2012
parent "Daniel Murphy" <yebblies nospamgmail.com> writes:
"Jonathan M Davis" <jmdavisProg gmx.com> wrote in message 
news:mailman.685.1331774722.4860.digitalmars-d puremagic.com...
 On Thursday, March 15, 2012 11:55:46 Daniel Murphy wrote:
 "Jonathan M Davis" <jmdavisProg gmx.com> wrote in message
 news:mailman.666.1331749219.4860.digitalmars-d puremagic.com...

 long long is 128 bits on 64-bit Linux.

Are you sure about this? I think we already had this discussion...

If it isn't, then whatever that discussion was obviously didn't stick in my head. - Jonathan M Davis

long double (aka real, a much better name) is 128 bits (including padding) on some platforms/compilers, I'm pretty sure long long is 64 bits in every x86-64 memory model. Some compilers do however support 128 bit integer types on some platforms.
Mar 14 2012
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Andrei Alexandrescu:

 My suggestion is to focus on fixed arbitrary-sized integers in 
 Phobos, and then add optimizations for 64-bit integers on 
 architectures that support it.

I think library-defined fixed arbitrary-sized integers and built-in cent/ucent types are both useful to have. One doesn't fully replace the other. cent/ucent is more efficient (even on 32 bit systems), while FixedInt is more flexible. But both are low-priority. Bye, bearophile
Mar 15 2012
prev sibling parent "Paul D. Anderson" <paul.d.removethis.anderson comcast.andthis.net> writes:
On Thursday, 15 March 2012 at 15:39:47 UTC, Daniel Murphy wrote:
 "Andrei Alexandrescu" <SeeWebsiteForEmail erdani.org> wrote in 
 message
 news:jjt1lh$pfb$1 digitalmars.com...
 My suggestion is to focus on fixed arbitrary-sized integers in 
 Phobos, and then add optimizations for 64-bit integers on 
 architectures that support it.

 Andrei

Yeah. A lot of the BigInt code can probably be adapted, if someone wants to give it a go.

I was really only checking to see if anyone had already implemented 128-bit integers. It seems like the answer is no, but Daniel may have some code. It looks like the quickest solution is for me to do it myself. I'll let you all know when it's ready. Paul
Mar 15 2012