News:

;) This forum is the property of Proton software developers

Main Menu

Unsigned casting during parameter passing

Started by CPR, Nov 13, 2026, 01:03 PM

Previous topic - Next topic

CPR

I have a procedure
checkSum(pMinT As Long, pNowT As Long, pMaxT As Long, pMinH As Long, pNowH As Long, pMaxH As Long), Word
When I call it like so -
checkSum(sminTO + 300, snowTO + 300, ...)

(sminTO, snowTO etc., are all dimensioned as SWord)

with sminTO = 150, the procedure sees 150 + 300 = 450 <- correct!

But... when sminTO is negative (e.g -12) sminTO then holds a 16-bit signed hex value 0xFFF4 and because 300 is an unsigned constant,
I think the compiler may be treating 0xFFF4 as an unsigned word during the addition? So it calculates 65524 + 300 = 65824 (incorrect)

Is this the case or have I miss-interpreted something?


Pepe

demo proteus without error

CPR

Quote from: Pepe on Nov 13, 2026, 03:06 PMdemo proteus without error

Thanks Pepe, but that's for a single value. I'm asking the compiler to prepare six values for the procedure. Try it with six and keep optimiser=3
where sminTO, snowTO etc., (SWord) have at least a couple of negative values
checkSum(sminTO+300, snowTO+300, smaxTO+300, sminHO+300, snowHO+300, smaxHO+300)

Pepe

with 6 parameters demo proteus

top204

#4
Remember... Long variables are always unsigned, so if a signed variable is sent to it, the raw value will be loaded into it, not the signed value.

So if the value -12 is sent to a Long variable from a 16-bit variable, it will be loaded with the value 65524 ($FFF4), which is the raw 2's complement value for -12 in a 16-bit variable.

You cannot just mix signed and unsigned variables, because signed are carrying 2's complement values, which are sign extended for negative values.

Regards
Les

CPR

#5
Quote from: top204 on Nov 13, 2026, 06:23 PMRemember Long variables are always unsigned, so if a signed variable is sent to it, the raw value will be loaded into it, not the signed value.

So if the value -12 is sent to a Long variable, it will be loaded with the value 65524 ($FFF4), which is the raw 2's complement value for -12.

You cannot just mix signed and unsigned variables, because signed are carrying 2's complement values, which are sign extended for negative values.

Regards
Les


Ah! That explains it. Thanks Les. I've listed the checkSum() procedure below.

'//____________________________________

Proc checkSum(pMinT As Long, pNowT As Long, pMaxT As Long, pMinH As Long, pNowH As Long, pMaxH As Long), Word
                                                                                '// Weighted sum mod 4093 (prime). Result is 0..4092, fits in 12 bits.
    Dim sum As Long

    sum = pMinT * 31
    sum = sum + (pNowT * 89)
    sum = sum + (pMaxT * 43)
    sum = sum + (pMinH * 71)
    sum = sum + (pNowH * 113)
    sum = sum + (pMaxH * 53)
                               
    While sum >= 4093                                                           '// Max possible sum is 400 * 1023 = 409,200
        sum = sum - 4093
    Wend

    Result = sum.LowWord

EndProc

'//____________________________________

Les, perhaps this topic is better moved to Proton 8 threads ?

▲