On Thu, 19 Jul 2018, Davin McCall wrote:

> So, this POSIX requirement doesn't actually impose any extra requirements on
> the C compiler, if I understand correctly - just on the implementation of the
> abs() functions.

In practice (and again for 20 years or more), compilers do not generate 
out-of-line calls to abs functions when optimizing - they expand those 
operations inline instead, so the implementation of the out-of-line abs 
function is essentially irrelevant (only ever used in unusual cases such 
as a function pointer the compiler can't tell points to abs).  And it's 
not even the inline expansion of a single abs call in isolation that's 
relevant - it's the optimization of a larger expression based on the known 
semantics of abs (such as returning a nonnegative value from any valid 
call).

(There are processors that use two's complement arithmetic, but provide an 
absolute value instruction that uses saturation on overflow rather than 
returning the negative argument value in that case.  For example, the TI 
C6X family of DSPs, which even has a Linux kernel port.  On such 
processors, an expansion of an isolated abs call without such optimization 
based on undefined behavior would naturally return INT_MAX not INT_MIN for 
an argument of INT_MIN.)

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

Reply via email to