Recent

Author Topic: [CLOSED] 128 bit integers  (Read 768 times)

julkas

  • Sr. Member
  • ****
  • Posts: 416
  • KISS principle / Lazarus 2.0.0 / FPC 3.0.4
[CLOSED] 128 bit integers
« on: June 16, 2019, 04:15:07 pm »
Is there any type operations (helpers) on int128rec?
Thanks.
« Last Edit: June 20, 2019, 03:16:57 pm by julkas »
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;

jamie

  • Hero Member
  • *****
  • Posts: 2087
Re: 128 bit integers
« Reply #1 on: June 16, 2019, 04:27:46 pm »
http://www.delphiforfun.org/programs/Library/big_integers.htm

Look there. There some source files at the end.

I believe there is a TBCD lib in Fpc somewhere that can do this.
Number 1 at blue screen app creations!

Thaddy

  • Hero Member
  • *****
  • Posts: 9187
Re: 128 bit integers
« Reply #2 on: June 16, 2019, 04:38:02 pm »
This is already present in fpc. Operations on that are not (yet) fully supported, but I have a full set of operators if you need them.
 (Only UNSIGNED integers, see: https://bugs.freepascal.org/view.php?id=35371 and https://www.freepascal.org/docs-html/rtl/sysutils/int128rec.html )
Just let me know. The bug resolve is not to my liking, but at least Florian has a proposed solution. I also implemented that.
« Last Edit: June 16, 2019, 04:44:57 pm by Thaddy »
also related to equus asinus.

julkas

  • Sr. Member
  • ****
  • Posts: 416
  • KISS principle / Lazarus 2.0.0 / FPC 3.0.4
Re: 128 bit integers
« Reply #3 on: June 16, 2019, 04:53:28 pm »
@Thaddy Can you share your code? Is there support for  __builtin_clz GNU C family operations?
« Last Edit: June 16, 2019, 05:43:06 pm by julkas »
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;

Thaddy

  • Hero Member
  • *****
  • Posts: 9187
Re: 128 bit integers
« Reply #4 on: June 16, 2019, 06:48:45 pm »
I have to ask for permission, but it it is pure Pascal. The clz support depends on the FPU support and so the compiler modes..
I can probably share it.
« Last Edit: June 16, 2019, 06:50:48 pm by Thaddy »
also related to equus asinus.

julkas

  • Sr. Member
  • ****
  • Posts: 416
  • KISS principle / Lazarus 2.0.0 / FPC 3.0.4
Re: 128 bit integers
« Reply #5 on: June 17, 2019, 02:38:48 pm »
Thanks @Thaddy.
I want RTL bit helpers (operations) (CPU optimized) on 16, 32, 64, 128 bit ordinal types.
I found only PopCnt function.
GCC __builtin family has
Code: C  [Select]
  1. int __builtin_ffs (int x)
  2. int __builtin_clz (unsigned int x)
  3. int __builtin_ctz (unsigned int x)
  4. ...
  5.  
and long, long long analogs.

What's going with Free Pascal?



 
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;

Thaddy

  • Hero Member
  • *****
  • Posts: 9187
Re: 128 bit integers
« Reply #6 on: June 17, 2019, 02:53:30 pm »
Freepascal will optimize based on the FPU settings with which you compile. E.g. -CfAVX/ -CfAVX2 etc  on windows and e.g. - CfVFPv4 on armhf. Results may vary, so I usually do a bit of inline assembler when it is absolutely necessary.
But i didn't do that with the code I referred to: that's pure Pascal.
also related to equus asinus.

howardpc

  • Hero Member
  • *****
  • Posts: 3181
Re: 128 bit integers
« Reply #7 on: June 17, 2019, 02:56:11 pm »
I want RTL bit helpers (operations) (CPU optimized) on 16, 32, 64, 128 bit ordinal types.
I found only PopCnt function.
FPC has RorByte, RolByte, BsfByte, BsrByte, all with Word, DWord and QWord analogs.
There is also SarShortint, with SmallInt, Longint and Int64 analogs.
« Last Edit: June 17, 2019, 02:58:04 pm by howardpc »

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7506
Re: 128 bit integers
« Reply #8 on: June 17, 2019, 02:59:36 pm »
Afaik there are

function BsfByte(Const AValue: Byte): Byte;[internproc:fpc_in_bsf_x];
function BsrByte(Const AValue: Byte): Byte;[internproc:fpc_in_bsr_x];
function BsfWord(Const AValue: Word): cardinal;[internproc:fpc_in_bsf_x];
function BsrWord(Const AValue: Word): cardinal;[internproc:fpc_in_bsr_x];

and also dword, qword in trunk versions which are afaik somewhat like clz/ctz, though the error on no bit is different. See attached help page. ffs might be clz on not value.

But in general intrinsics are only added when they are actually used.

« Last Edit: June 17, 2019, 03:01:35 pm by marcov »

julkas

  • Sr. Member
  • ****
  • Posts: 416
  • KISS principle / Lazarus 2.0.0 / FPC 3.0.4
Re: 128 bit integers
« Reply #9 on: June 17, 2019, 03:06:45 pm »
@howardpc, @marcov. Great. +5.
Thanks.
« Last Edit: June 18, 2019, 09:23:03 am by julkas »
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;