Go Back

What is the Truncation/Narrowing Behavior of i8 to i4?

Keywords: truncation, narrowing, i4, i8, wrap around, overflow of int
Topics: 32-bit, 64-bit, Assignment Operator

Consider the following Traditional Synergy program:
 

main

record
myi8, i8
myi4, i4

proc

myi8 = -1
myi4 = myi8

nop

myi8 = 4294967296
myi4 = myi8

nop

endmain

When compiled in 64 bit Traditional Synergy what is the expected behavior of this narrowing operation?
 

4 Answers
1   | Posted by Ace Olszowka to Synergy DBL on 11/21/2019 9:43 PM
Best Answer chosen by Ace Olszowka
Ace Olszowka
The behavior of the above program (when compiled in either 32 or 64bit Traditional Synergy) appears to be documented deep within the documentation under Operators under the Numeric operations sub-heading: https://synergex.com/docs/versions/v111/index.htm#lrm/lrmChap3Operators.htm#Numeric%20operations

To quote the current version of the documentation:
 
For performance reasons, Synergy uses the platform's native integer size for expression intermediates. On 32-bit platforms, if an i8 is explicitly used in an integer expression, an i8 intermediate is used. Otherwise, an integer expression not using i8s that results in a value exceeding the native i4 will wrap (not be promoted to an i8), and no error will be generated. If necessary, you can avoid this by using i8s instead but at a slight cost to integer performance. On 64-bit platforms, all integer expressions use native i8 intermediates.

Pay close attention to this statement:
 
integer expression not using i8s that results in a value exceeding the native i4 will wrap (not be promoted to an i8), and no error will be generated.

Therefore for the above program the output will be:
 
myi8=-1
myi4=-1

and
 
myi8=4294967296
myi4=-1

As documented above.

11/21/2019 9:44 PM   1  
Mark Vinten
I haven't read through the documentation or tested the scenarios, but I feel like that should be picked up by bounds checking.  After all, if you try to put a D8 into a D6 you get an error.  This seems counter-intuitive.

11/22/2019 8:16 AM   0  
Ace Olszowka
@Mark Vinten You get a NARROWING Warning ( 696 https://www.synergex.com/docs/index.htm#errors/comp/warn/compNARROWING.htm) but only when you compile with -W4; this warning may have been converted to an error by your build system.

For us we have something like 12,000+ NARROWING warnings because it was (ab)used to perform a quick and dirty cast. This for us means that this warning is basically useless.

I suspect, but cannot prove, that the behavior was probably put in to help ease some conversion for some customer (I do not believe us; but it may have been long before my time), but without the historical knowledge it is difficult to decipher Synergex's intent. @Synergex any historical background for this behavior?

From CU's perspective the wrapping behavior is probably going to be relied upon heavily, most places return a ^VAL function which when you convert to 64bit returns a Native Int (which is an I8 as per the documentation). However sloppy coding resulted in many of these values being pushed into I4's; without this wrapping logic, a naive truncation implementation would truncate the high-bit, meaning a -1 would become a 1; changing the meaning of most if not all of the return codes.

11/22/2019 2:52 PM   0  
Ace Olszowka
Double Post (because we cannot edit comments) here is the warning from the above program:

Severity    Code    Description    Project    File    Line    Suppression State
Warning        %DBL-W-NARROWING, Narrowing conversion could cause loss of data : myi4 = myi8    NarrowingTests    C:\Users\aceo\source\repos\NarrowingTests\NarrowingTests\Program.dbl    15    
Warning        %DBL-W-NARROWING, Narrowing conversion could cause loss of data : myi4 = myi8    NarrowingTests    C:\Users\aceo\source\repos\NarrowingTests\NarrowingTests\Program.dbl    10    

 

11/22/2019 2:53 PM   0  
Please log in to comment or answer this question.