llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

float to unsigned char conversion looses zero-extend

Open dobbelaj-snps opened this issue 8 years ago • 3 comments

Bugzilla Link 36152
Resolution INVALID
Resolved on Jan 30, 2018 08:51
Version 5.0
OS Windows NT
Attachments example.c, llvm-ir output (for xscale, as an example of an architecture without i8/i16 registers), annotated xscale assembly
CC @efriedma-quic

Extended Description

On architectures where i8/i16 are not valid register types, legalization wrongly omits a 'zext i8 to i32' for code like 'unsigned char foo(float a) { return a; }'

dobbelaj-snps avatar Jan 30 '18 15:01 dobbelaj-snps

It seems that for both c and c++, the conversion of a float that does not fit into the destination type results in either an unspecified value (+exception, see Aneex F.4) or undefined behavior (See n4296, 4.9). This means that llvm's behavior is indeed according to the standard.

dobbelaj-snps avatar Jan 30 '18 16:01 dobbelaj-snps

fptoui returns undef if the result would be out of range, so the high bits are naturally zero, so the backend is doing the right thing.

The interesting question is what the C and C++ standards say about this... are we supposed to promote the unsigned char to an int before we do the implicit conversion? clang's behavior here is a bit inconsistent.

efriedma-quic avatar Jan 30 '18 16:01 efriedma-quic

In DAGTypeLegalizer::PromoteIntRes_FP_TO_XInt(...), it is probably sufficient to just return the 'Res', without adding the AssertZext/AssertSext.

dobbelaj-snps avatar Jan 30 '18 16:01 dobbelaj-snps