Forum > Embedded
Multiply operation for xtensa (lx6/esp32)
(1/1)
irfanbagus:
I try this code
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TestSmallInt(A,B: SmallInt): SmallInt;begin Result:= SmallInt(5)+A*B;end;
and i get this assembly code
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---# [90] begin entry a1,64 mov a10,a2# Var A located in register a10 mov a11,a3# Var B located in register a11# Var B located in register a11# Var A located in register a10# [91] Result:= SmallInt(5)+A*B; call8 fpc_mul_longint addi a2,a10,5 sext a2,a2,15# Var $result located in register a2# [92] end; sext a2,a2,15 retw
i have almost zero knowledge in this xtensa cpu. Is there any reason why fpc does not generate MUL* instruction, instead calling multiply helper function? missing native MUL instruction perhaps?
ccrause:
--- Quote from: irfanbagus on August 07, 2020, 05:58:57 pm ---...
i have almost zero knowledge in this xtensa cpu. Is there any reason why fpc does not generate MUL* instruction, instead calling multiply helper function? missing native MUL instruction perhaps?
--- End quote ---
The both ESP32 and ESP8266 have hardware MULL and MUL16 type instructions. The necessary code to generate the CPU specific instructions hasn't been implemented yet [1] - this target is relatively new so not much time has been invested yet into optimizing the generated code.
Edit: [1] Not quite true, there is actually some code that can generate a hardware MULL instruction, but it is disabled by default (probably because it lacks error checking).
irfanbagus:
--- Quote from: ccrause on August 07, 2020, 09:02:01 pm ---...
Edit: [1] Not quite true, there is actually some code that can generate a hardware MULL instruction, but it is disabled by default (probably because it lacks error checking).
--- End quote ---
is this what you mean (see my attachment) ?
ccrause:
Yes. Since the MUL instructions are optional, I would rather follow the m68k example and declare a capability in cpuinfo.pas and check if this capability exists for the selected subarch. taddnode.use_mul_helper can be overridden to perform this check. See attached patch.
Note that the generated code does not check for overflow, this still needs to be implemented. Test example:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program test; procedure vTaskDelay(xTicksToDelay: uint32); external; var a, b, c: uint32; begin for a := $FFFF to $7FFFFF do for b := $FFF0 to $7FFFFF do begin c := a * b; writeln(a, ' x ', b, ' = ', c); vTaskDelay(100); end; repeat until false;end.
--- Code: ASM [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---# [12] c := a * b; l32r a2,.Lj13 l32i a3,a2,0 l32r a2,.Lj14 l32i a2,a2,0 mull a2,a2,a3 l32r a3,.Lj15 s32i a2,a3,0.Ll5:
And the output with overflow checking enabled:
--- Code: Text [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---65535 x 65535 = 429483622565535 x 65536 = 429490176065535 x 65537 = 429496729565535 x 65538 = 6553465535 x 65539 = 13106965535 x 65540 = 196604
Navigation
[0] Message Index