[llvm-dev] [compiler-rt] Undefined negation in float emulation functions

Stephen Canon via llvm-dev llvm-dev at lists.llvm.org
Tue Oct 20 06:15:52 PDT 2015


Yup, this is UB.  If you want to propose a patch, I would do something like the following:

	rep_t sign = 0;
	unsigned int aAbs = a;
	if (a < 0) {
		sign = signBit;
		aAbs = -aAbs;
	}
	// Now use aAbs instead of a.

– Steve

> On Oct 20, 2015, at 6:38 AM, Matthew Fernandez via llvm-dev <llvm-dev at lists.llvm.org> wrote:
> 
> Hi,
> 
> I recently came across the following in __floatsidf in compiler-rt:
> 
>    __floatsidf(int a) {
>        ...
>        if (a < 0) {
>            ...
>            a = -a;
> 
> In the case where a == INT_MIN, is this negation not undefined behaviour? AIUI this function is used for software emulation on targets that have no hardware floating point support. Perhaps there is an in-built assumption that this code is never called with INT_MIN, though I don't immediately see anything to indicate this. Indeed there is a later comment in this function that indicates INT_MIN is an anticipated input, but the negation has already occurred by this point.
> 
> I am not a floating point expert, so perhaps I am missing some subtlety here. If so, apologies for the noise. The above refers to r218935 and similar code is present in __floatsisf.
> 
> Thanks,
> Matthew
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev



More information about the llvm-dev mailing list