Recent

Author Topic: Can't wrap around  (Read 436 times)

LemonParty

  • Sr. Member
  • ****
  • Posts: 457
Can't wrap around
« on: May 02, 2026, 01:37:38 pm »
Hello.
I create this topic because of this problem https://forum.lazarus.freepascal.org/index.php/topic,73945.msg581824.html#msg581824.

I simplified the problem in this code:
Code: Pascal  [Select][+][-]
  1. {$mode ObjFPC}{$H+}{$R-}
  2.  
  3. const
  4.   PRIME64_1 =  Uint64(11400714785074694791);
  5.   PRIME64_2 =  Uint64(14029467366897019727);
  6.  
  7. var
  8.   a: UInt64;
  9. begin
  10.   a:= PRIME64_1 + PRIME64_2;
  11. end.
This code not compile with error:
Quote
(10,16) Error: Overflow in arithmetic operation
Desirable to have a wrap around of values PRIME64_1 and PRIME64_2 in this situation.
Checked in trunk and 3.0.4+dfsg-22 [2019/01/24] of FPC. In both gives an error. In Lazarus 4.4 this code compiles with no problem (at least topic starter say that).

Why it doesn't wrap around in pointed versions of compiler?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Lutz Mändle

  • Jr. Member
  • **
  • Posts: 88
Re: Can't wrap around
« Reply #1 on: May 02, 2026, 02:39:14 pm »
With FPC 3.2.2 on Linux 64bit it doesn't work either.
It gives the same error (Error: Overflow in arithmetic operation).

Thaddy

  • Hero Member
  • *****
  • Posts: 19115
  • Glad to be alive.
Re: Can't wrap around
« Reply #2 on: May 02, 2026, 02:56:08 pm »
This is only the case when the consts are untyped, because of the internal representation of untyped consts as signed types.
But this works:
Code: Pascal  [Select][+][-]
  1. {$mode ObjFPC}{$H+}
  2. const
  3.   PRIME64_1:Uint64 =  11400714785074694791;
  4.   PRIME64_2:Uint64 =  14029467366897019727;
  5.  
  6. var
  7.   a: UInt64;
  8. begin
  9.   a:= PRIME64_1 + PRIME64_2;
  10. end.

« Last Edit: May 02, 2026, 03:08:51 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

LemonParty

  • Sr. Member
  • ****
  • Posts: 457
Re: Can't wrap around
« Reply #3 on: May 02, 2026, 04:06:16 pm »
Thank you for checking, Lutz Mändle.
Great Thaddy, I hope this will cure the problem.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12335
  • Debugger - SynEdit - and more
    • wiki
Re: Can't wrap around
« Reply #4 on: May 02, 2026, 04:16:41 pm »
Thaddy's example might want a {$WriteableConst off}

Or as a workaround:
Code: Pascal  [Select][+][-]
  1. {$mode ObjFPC}{$H+}{$R-}
  2.  
  3. const
  4.   PRIME64_1 =  Uint64(11400714785074694791);
  5.   PRIME64_2 =  Uint64(14029467366897019727);
  6.  
  7. var
  8.   a: UInt64;
  9. begin
  10.   a:= qword(pointer( PRIME64_1) + PRIME64_2);
  11. end.



I don't know, it might be worth opening an issue on this.

As for the example {$R-} is range checking. What should have been there is overflow checking {$Q-}. But that doesn't help either.

LemonParty

  • Sr. Member
  • ****
  • Posts: 457
Re: Can't wrap around
« Reply #5 on: May 03, 2026, 07:27:55 pm »
I can't for some reason create an issue on gitlab. Previously I was able to do that.

Can someone post this issue?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

LemonParty

  • Sr. Member
  • ****
  • Posts: 457
Re: Can't wrap around
« Reply #6 on: May 06, 2026, 01:56:28 pm »
I successfully created an issue in bugtracker.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

 

TinyPortal © 2005-2018