[fpc-devel] Division nodes

J. Gareth Moreton gareth at moreton-family.com
Fri May 19 21:14:31 CEST 2023

```So I need to ask... should the check for a divisor of -1 still be
performed? The case of doing "min_int div -1", even with
unsigned-to-signed typecasting, seems very contrived and the programmer
should expect problems if "min_int" and "-1" appear as the operands.  Is
there a specific example where this implicit check is absolutely
necessary?  As others have pointed out, silently returning "min_int" as
the answer seems more unexpected (granted this is just the behaviour of
an optimisation that converts the nodes equating to "x div -1" to "-x",
and Intel's NEG instruction doesn't return an error if min_int is its
input operand, but I can't be sure if the same applies to non-Intel
processors and their equivalent instructions).

Kit

On 17/05/2023 09:51, J. Gareth Moreton via fpc-devel wrote:
> Logically yes, but using 16-bit as an example, min_int is -32,768, and
> signed 16-bit integers range from -32,768 to 32,767. So -32,768 ÷ -1 =
> 32,768, which is out of range.  This is where the problem lies.
>
> Internally, negation involves inverting all of the bits and then
> adding 1 (essentially how you subtract a number using two's
> complement), so min_int, which is 1000000000000000, becomes
> 0111111111111111 and then, after incrementing, 1000000000000000, which
> is min_int again.
>
> Kit
>
> On 16/05/2023 13:13, Jean SUZINEAU via fpc-devel wrote:
>> Le 16/05/2023 à 01:47, Stefan Glienke via fpc-devel a écrit :
>>> min_int div -1
>>
>> "min_int div -1"  should give  "- min_int" ?
>> _______________________________________________
>> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
>> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
>>
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
>
```