On Tue, 29 Oct 2013, Richard Biener wrote:

> LLVM covers addition, subtraction and multiply on signed and unsigned
> int, long and long long types.  Not sure why they offer anything for
> unsigned - possibly for size_t arithmetic and security concerns with
> malloc?  For practicability and to be less error-prone I'd have done
> the builtins in a type-generic way (like tgmath) as using the
> wrong typed builtin can lead both to undetected overflow, unwanted
> truncation of arguments and possibly memory overflow of 'result'
> (if you ignore warnings about incompatible pointer types).

One difficulty of doing it as type-generic is that you might sometimes 
want to detect overflow on types smaller than int, which C is rather prone 
to promote to int.  (Of course in such cases, you can also write naive 
code computing e.g. short_1 * short_2 as an int value then comparing with 
SHRT_MIN and SHRT_MAX, but it seems reasonable to want to use the same 
interface as for wider types.)  In general, the type that arithmetic would 
happen on is not necessarily the same as the type you then store the 
result in, and it's the latter type you want to know if the arithmetic 
overflows.  Maybe it could be type-generic on the result pointer only 
(i.e., say "compute the result as if the arguments are infinite-precision 
signed integers, store the result reduced modulo 2^N and return overflow 
status if that isn't the same as the infinite-precision result", where the 
first two arguments could have arbitrary integer types).

-- 
Joseph S. Myers
jos...@codesourcery.com

Reply via email to