Re: [fpc-devel] Division nodes

2023-05-11 Thread J. Gareth Moreton via fpc-devel
Fair enough, but I would like an explanation as to why it's necessary, because the reason for testing -1 in particular is very unclear, and I wonder if there's a known misbehaviour with a particular division function with -1. Kit On 11/05/2023 21:27, Wayne Sherman wrote: On Thu, May 11,

Re: [fpc-devel] Division nodes

2023-05-11 Thread Wayne Sherman via fpc-devel
On Thu, May 11, 2023 at 11:42 AM J. Gareth Moreton wrote: > This is the code block in question (ncnv.pas, starting at line 3397) The git "blame" function shows who last made changes: https://gitlab.com/freepascal.org/fpc/source/-/blame/main/compiler/ncnv.pas?page=4#L3396 Most of that code was

Re: [fpc-devel] Division nodes

2023-05-11 Thread J. Gareth Moreton via fpc-devel
This is the code block in question (ncnv.pas, starting at line 3397) - if anyone can explain why it has to be set up this way, or add comments to the code, I will be most grateful (it's run for the following node types: subn, addn, muln, divn, modn, xorn, andn, orn, shln, shrn):  

Re: [fpc-devel] Division nodes

2023-05-11 Thread J. Gareth Moreton via fpc-devel
P.S. I found the code that adds the conditional checks; it's "doremoveinttypeconvs" in the ncnv unit.  However, it's very unclear as to WHY it's doing it as there's no comments around the code block. Kit On 11/05/2023 15:39, J. Gareth Moreton via fpc-devel wrote: It does seem odd.  In a

Re: [fpc-devel] Division nodes

2023-05-11 Thread J. Gareth Moreton via fpc-devel
It does seem odd.  In a practical sense, the only time I can see -1 being a common input among other random numbers is if it's an error value, in which case you would most likely do special handling rather than pass it through a division operation.  With the slowdown that comes from additional

Re: [fpc-devel] Division nodes

2023-05-11 Thread Stefan Glienke via fpc-devel
Looks like a rather disadvantageous way to avoid the idiv instruction because x div -1 = -x and x mod -1 = 0. I ran a quick benchmark doing a lot of integer divisions where sometimes (randomly) the divisor was -1. When the occurence was rare enough (~5%) the performance was not impacted, the

[fpc-devel] Division nodes

2023-05-11 Thread J. Gareth Moreton via fpc-devel
Hi everyone, I need to ask a question about how division nodes are set up (I'm looking at possible optimisation techniques).  I've written the following procedure: procedure DoDivMod(N, D: Integer; out Q, R: Integer); begin   Q := N div D;   R := N mod D; end; Fairly simple and to the