C vs ASM math. (again)

Just starting out? Need help? Post your questions and find answers here.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

C vs ASM math. (again)

Post by jassing »

(I'm using 6.11b3 x64, didn't try x86)
run this under C backend, then ASM backend. Will the backends ever agree on math, or are workarounds the norm?

Code: Select all

Define t
Debug @t % 32
r-i-v-e-r
User
User
Posts: 20
Joined: Thu May 09, 2024 5:18 pm

Re: C vs ASM math. (again)

Post by r-i-v-e-r »

Where is the disagreement in terms of math operations?

You're computing modulo 32 of a random memory address, so you can't really expect agreement even when using the same BE.

This isn't a typical thing you'd expect codified in language guarantees (unlike, e.g., that distinct variables have distinct memory addresses, stack is 16-byte aligned, etc.).
PBJim
Enthusiast
Enthusiast
Posts: 293
Joined: Fri Jan 19, 2024 11:56 pm

Re: C vs ASM math. (again)

Post by PBJim »

r-i-v-e-r wrote: Sun Jun 02, 2024 7:54 pm Where is the disagreement in terms of math operations?

You're computing modulo 32 of a random memory address, so you can't really expect agreement even when using the same BE.

This isn't a typical thing you'd expect guarantees from the language for (unlike, e.g., that distinct variables have distinct memory addresses, stack is 16-byte aligned, etc.).
... and if you change between C compiler and C-optimised compiler, it shows that. Optimisation in that case is outside PureBasic.
r-i-v-e-r
User
User
Posts: 20
Joined: Thu May 09, 2024 5:18 pm

Re: C vs ASM math. (again)

Post by r-i-v-e-r »

PBJim wrote: Sun Jun 02, 2024 8:00 pm
r-i-v-e-r wrote: Sun Jun 02, 2024 7:54 pm Where is the disagreement in terms of math operations?

You're computing modulo 32 of a random memory address, so you can't really expect agreement even when using the same BE.

This isn't a typical thing you'd expect guarantees from the language for (unlike, e.g., that distinct variables have distinct memory addresses, stack is 16-byte aligned, etc.).
... and if you change between C compiler and C-optimised compiler, it shows that. Optimisation in that case is outside PureBasic.
Exactly :D

We can better illustrate this with something like:

Code: Select all

Define t
OpenConsole()
PrintN(Str(@t % 32))
Input()
This makes it easier to see totally varying results even with the same backend, by changing other compiler parameters, e.g.:
  • ASM Debugger ON: 4
  • ASM Debugger OFF: 16
  • C Debugger ON: 16
  • C Debugger OFF: 0
  • C Debugger ON (Optimised): 8
  • C Debugger OFF (Optimised): 0
(and these results can of course vary, because as above, no guarantees exist for the variable's address)

TLDR: Not a bug. Both backends appear to be performing proper modulo of a signed 64-bit integer.
plouf
Enthusiast
Enthusiast
Posts: 281
Joined: Fri Apr 25, 2003 6:35 pm
Location: Athens,Greece

Re: C vs ASM math. (again)

Post by plouf »

on top of previous comments, i just want to "judge" the "Agreement part"

for reason already explained in detail here, we all understand that in the long go, ONLY C backend will exist
so in general we have to focus a bit only in "is C backend correct? " ... and leave ASM backend as a sweet memory ;)
Christos
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: C vs ASM math. (again)

Post by luis »

plouf wrote: Sun Jun 02, 2024 8:10 pm so in general we have to focus a bit only in "is C backend correct? "
Probably only the C backend will remain yes, but until then both must behave the same.
The same source must produce equivalent binary code resulting in the same behaviour.
If not the affected backend must be fixed or removed or not used anymore, because what would be the point of having it ?

edit: personally I hope Fred will keep maintaining the ASM backend as long as possible because it's so much quicker to use, and if nothing changed recently the purifier also works better with it.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Lord
Addict
Addict
Posts: 898
Joined: Tue May 26, 2009 2:11 pm

Re: C vs ASM math. (again)

Post by Lord »

Image
Post Reply