Long signed integer multiplication issue?

27 views
Skip to first unread message

Leonardo Hirokazu De Souza Hamada

unread,
Aug 16, 2021, 2:39:02 PM8/16/21
to jallist

Hi,

Currently I am messing around a JAL adapted PID code from Arduino library (FastPID to be precise). I'm using a pic 18f4620.

So, it came to my attention that a multiplication between two SDWORD value does not give a correct value, when one of then is negative.

-----Code section
delay_1s(1)
print_string(serial_hw_data, "Results:\r\n")

var sdword E = -2
var sdword c, p
p = 50
c = p * E
print_sdword_dec(serial_hw_data, c)

print_crlf(serial_hw_data)

-- This code gives correct results
E = -2
p = 50
if E < 0 then 
   E = -E
   c = p * E
   c = -c
end if
print_sdword_dec(serial_hw_data, c)

for 32 loop
    print_crlf(serial_hw_data)
end loop

---End code

----Terminal output:
Results:
3276700
-100

Rob Hamerling

unread,
Aug 17, 2021, 7:37:05 AM8/17/21
to jal...@googlegroups.com

Try with the  -fastmath  compiler option (see compiler docs for its effects).



On 16/08/2021 20.25, 'Leonardo Hirokazu De Souza Hamada' via jallist wrote:

Hi,

Currently I am messing around a JAL adapted PID code from Arduino library (FastPID to be precise). I'm using a pic 18f4620.

So, it came to my attention that a multiplication between two SDWORD value does not give a correct value, when one of then is negative.

-----Code section
delay_1s(1)
print_string(serial_hw_data, "Results:\r\n")

var sdword E = -2
var sdword c, p
p = 50
c = p * E
print_sdword_dec(serial_hw_data, c)
Try with the  -fastmath  compiler option (see compiler docs for effects).

print_crlf(serial_hw_data)

-- This code gives correct results
E = -2
p = 50
if E < 0 then 
   E = -E
   c = p * E
   c = -c
end if
print_sdword_dec(serial_hw_data, c)

for 32 loop
    print_crlf(serial_hw_data)
end loop

---End code

----Terminal output:
Results:
3276700
-100

--
You received this message because you are subscribed to the Google Groups "jallist" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallist+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jallist/e714ddfc-97d7-4bdc-81e5-f1b3d3ff0b9en%40googlegroups.com.

--
Rob Hamerling, Vianen, NL

Leonardo Hirokazu De Souza Hamada

unread,
Aug 18, 2021, 10:29:18 AM8/18/21
to jallist
So, just to complement, I did a diff against the two generated .asm files, one with -fastmath option and the other one with -no-fastmath option.

The only thing effectively changed was the name of multiplication subroutine, see:


$ diff 18f4620_integer_debug_stripped.asm_fm 18f4620_integer_debug_stripped.asm_no-fm
3c3
< ; command line:  C:\\JallibWorkplace\\compiler\\jalv2_64.exe d:\Users\Hamada\Google Drive\Controle-Forno-Eletrico\VALIDATION\18f4620_integer_debug_stripped.jal -s C://JallibWorkplace//lib -fastmath
---
> ; command line:  C:\\JallibWorkplace\\compiler\\jalv2_64.exe d:\Users\Hamada\Google Drive\Controle-Forno-Eletrico\VALIDATION\18f4620_integer_debug_stripped.jal -s C://JallibWorkplace//lib -no-fastmath
57c57
< l__pic_multiply_4_4
---
> l__pic_multiply
112c112
<                                call     l__pic_multiply_4_4
---
>                                call     l__pic_multiply
154c154
<                                call     l__pic_multiply_4_4
---
>                                call     l__pic_multiply


Greetings,

Leonardo

Rob CJ

unread,
Aug 18, 2021, 1:41:32 PM8/18/21
to jal...@googlegroups.com
Hi Leonardo,

I did the following tests (next time also mention the PIC):
-) Ran the program on a PIC18F26K42. Incorrect results with and without -fastmath
-) Ran the program on a PIC16F1825. Correct results.

So the problem seems to be for the PIC18F. I did not test this for a PIC12F but at least this narrows the problem down a bit.

I registered the issue under.

Given the following program. The results should in both cases be -100 but the first result shows 3276700 and the second one the correct -100. This seems to be the case for PIC18F types, not PIC16F ...
I am also working on some other stuff so it might take some time before I can have a look at this issue and the other issue you reported (I am not a very good multi-tasker).

Kind regards,

Rob


Van: 'Leonardo Hirokazu De Souza Hamada' via jallist <jal...@googlegroups.com>
Verzonden: woensdag 18 augustus 2021 16:29
Aan: jallist <jal...@googlegroups.com>
Onderwerp: Re: [jallist] Long signed integer multiplication issue?
 

Rob Hamerling

unread,
Aug 18, 2021, 3:07:02 PM8/18/21
to jal...@googlegroups.com

Hi Leonardo,

Found out why I have different asm files between with and without -fastmath!
I copied your program, but left out the second part with the good result of the multiply (reason: "why debug good code?"). 
So  in my program there is only a single multiply. And the doc text of -fastpath explains that in that case this single multiply is generated 'inline'. Therefore I get different asm files for cases with and without -fastpath.
I did a check: when I add the second multiply I get idential asm files (apart from the name of the procedure), exactly like with you.
I just didn't realize this code difference would have this effect, sorry for the confusion.

Remains the 'puzzle' why the not-inline routine gives a wrong result.

Regards, Rob.



 



On 18/08/2021 16.29, 'Leonardo Hirokazu De Souza Hamada' via jallist wrote:
So, just to complement, I did a diff against the two generated .asm files, one with -fastmath option and the other one with -no-fastmath option.

The only thing effectively changed was the name of multiplication subroutine, see:


$ diff 18f4620_integer_debug_stripped.asm_fm 18f4620_integer_debug_stripped.asm_no-fm
3c3
< ; command line:  C:\\JallibWorkplace\\co
--

Rob Hamerling

unread,
Aug 19, 2021, 4:21:55 AM8/19/21
to 'Leonardo Hirokazu De Souza Hamada' via jallist

Hi Leonardo,

Section 3.2 of the compiler docs (jalv2.pdf) contains a warning concerning multiplications: casting might be required!
Following the recommendation and modifying the multiplication into:

    c = sbyte*8(p) * E

gives me a warning that the value of c may be truncated (because c is smaller than sbyte*8), but in this case there is no truncation and the value of c is correct!

The compiler might be a little more user-friendly for these occasions, but at least it is documented!

Regards, Rob.






On 16/08/2021 20.25, 'Leonardo Hirokazu De Souza Hamada' via jallist wrote:
--
You received this message because you are subscribed to the Google Groups "jallist" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallist+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jallist/e714ddfc-97d7-4bdc-81e5-f1b3d3ff0b9en%40googlegroups.com.

Leonardo Hirokazu De Souza Hamada

unread,
Aug 19, 2021, 11:03:46 AM8/19/21
to jallist
Hi Rob,

Thanks for the tips and attention given to this.

Best regards,

Leonardo
Reply all
Reply to author
Forward
0 new messages