Recent

Author Topic: 128 bit integer support for 64 bit CPUs  (Read 1363 times)

uart

  • New Member
  • *
  • Posts: 40
128 bit integer support for 64 bit CPUs
« on: January 17, 2020, 12:59:41 pm »
Given that a lot of 128 bit operations can be handled fairly easily on modern x64 processors, I'm wondering if anyone is looking at adding 128 bit integer support to free pascal. (Or has it already been done and I'm just not looking hard enough?)

I noticed an "Int128Rec" type exists, https://www.freepascal.org/docs-html/rtl/sysutils/int128rec.html, but I haven't been able to find any methods/procedures that implement any arithmetic operations on it.

winni

  • Hero Member
  • *****
  • Posts: 1903
Re: 128 bit integer support for 64 bit CPUs
« Reply #1 on: January 17, 2020, 01:44:54 pm »
Hi!

AFAIK there are no procedures or classes to handle the Int128Rec.

The only source where I found the rec is rtl/x86_64/cpu.pp

Winni

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8793
  • FPC developer.
Re: 128 bit integer support for 64 bit CPUs
« Reply #2 on: January 17, 2020, 02:20:33 pm »
Given that a lot of 128 bit operations can be handled fairly easily on modern x64 processors, I'm wondering if anyone is looking at adding 128 bit integer support to free pascal. (Or has it already been done and I'm just not looking hard enough?)

I noticed an "Int128Rec" type exists, https://www.freepascal.org/docs-html/rtl/sysutils/int128rec.html, but I haven't been able to find any methods/procedures that implement any arithmetic operations on it.

That subdivision is effectively an SSE2 register. And SSE2 does not feature 128-bit integers, and neither does this record.

So the record existence doesn't mean that much

Thaddy

  • Hero Member
  • *****
  • Posts: 10516
Re: 128 bit integer support for 64 bit CPUs
« Reply #3 on: January 17, 2020, 02:42:33 pm »
That subdivision is effectively an SSE2 register. And SSE2 does not feature 128-bit integers, and neither does this record.

So the record existence doesn't mean that much
SSE3 and above support xmm128 and fpc supports that too, so for inline assembler purposes this record is valuable. So it means: no Pascal (yet) but you can use it with inline assembler on modern Intel/Amd..
Actually, for that purpose 256 and 512 records should be added too...And it is absolutely platform.
« Last Edit: January 17, 2020, 02:46:02 pm by Thaddy »

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8793
  • FPC developer.
Re: 128 bit integer support for 64 bit CPUs
« Reply #4 on: January 17, 2020, 03:33:17 pm »
That subdivision is effectively an SSE2 register. And SSE2 does not feature 128-bit integers, and neither does this record.

So the record existence doesn't mean that much
SSE3 and above support xmm128 and fpc supports that too, so for inline assembler purposes this record is valuable.

SSE2. But 128bit xmm (fairly redundant since 64-bit is called just "mm", and 256-bit are ymm) registers don't contain a 128-bit integer.

There are primitives for large integers (the so called ADX ) instructions, but these are Broadwell (Intel Core 5x00) and +.
« Last Edit: January 17, 2020, 03:42:56 pm by marcov »

Thaddy

  • Hero Member
  • *****
  • Posts: 10516
Re: 128 bit integer support for 64 bit CPUs
« Reply #5 on: January 17, 2020, 03:48:10 pm »
yes, ymm. But fpc supports that too for inline assembler.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8793
  • FPC developer.
Re: 128 bit integer support for 64 bit CPUs
« Reply #6 on: January 17, 2020, 04:53:15 pm »
yes, ymm. But fpc supports that too for inline assembler.

True, and even zmm for avx512. But that is not what this thread is about.

Thaddy

  • Hero Member
  • *****
  • Posts: 10516
Re: 128 bit integer support for 64 bit CPUs
« Reply #7 on: January 17, 2020, 04:56:09 pm »
True, and even zmm for avx512. But that is not what this thread is about.
Well, it is, because someone noted the structure. For now it is there just for inline assembler purpose I guess....

PascalDragon

  • Hero Member
  • *****
  • Posts: 2262
  • Compiler Developer
Re: 128 bit integer support for 64 bit CPUs
« Reply #8 on: January 18, 2020, 11:24:23 am »
Support for CPU optimized 128 bit types is currently work in progress.

uart

  • New Member
  • *
  • Posts: 40
Re: 128 bit integer support for 64 bit CPUs
« Reply #9 on: January 18, 2020, 03:30:16 pm »
Thanks everyone.  :)

I was looking at writing a simple (static) object with data fields for the high and low qwords and some basic I/O and arithmetic methods for 128 bit integers.

I've tested this by doing the equivalent on a 32 bit machine with 64 bit integers (not needed of course as 64 bit integers are already supported, but just to test the concept).

No problems with this on my old 32 bit system, but I'm currently stuck because I can't work out how to get assembler access to the object (self) with the 64 bit targeted assembler.

This is going off topic for this thread so I've posted it as a new question here:
https://forum.lazarus.freepascal.org/index.php/topic,48176.0.html

avk

  • Sr. Member
  • ****
  • Posts: 309
    • my self-education project
Re: 128 bit integer support for 64 bit CPUs
« Reply #10 on: January 18, 2020, 03:41:38 pm »
You might be interested this one.

uart

  • New Member
  • *
  • Posts: 40
Re: 128 bit integer support for 64 bit CPUs
« Reply #11 on: January 18, 2020, 04:56:01 pm »
You might be interested this one.

No no no, I don't want to look at someone else's code. :D

Ok I had a peek. He called his object the same name as mine - "TuInt128".  I didn't copy, honest! 8)

I've already scratched out an outline of my code, it's much less ambitious than that project. Just methods for basic conversion like ToString, FromString and few other initialization methods,  and then the basic arithmetic stuff like Add, Neg, Div Mult and Compare. A couple of hundred lines at most. The only thing I haven't already scratched out is some power/modulo stuff I'll probably add later.

This is just a simple project I set myself to get back into programming. I used to program in Pascal back in the turbo pascal days, but haven't done much recently. And last time I tried to optimize asm code it was on and AMD k6-2, Haha, so that's how far out of touch I am.

Also wanted to learn a bit about x64 asm. But don't worry I'm not under any illusions, I know that I'm out of my depth when it comes to optimizing. This is just a simple learning project.


 

TinyPortal © 2005-2018