[cfe-dev] int-to-bool conversion issues

Alexandre Isoard via cfe-dev cfe-dev at lists.llvm.org
Fri Sep 13 14:19:56 PDT 2019


Yes, you are right, I just double checked, it's not allowed to pick any
other choice.

The issue is that we are maintaining an arbitrary precision library. We
provide a method that returns a range of bits, and a method that returns a
single bit. The method that returns a range of bits, when used to pick a
single bit, was behaving much better than the method that returns a single
bit. The reason is the method that returns a single bit uses bool, while
the other one stay in the realm of the library (aka. returns an arbitrary
precision integer range).

It's not the first time we see poor behavior from C++ native types, we
should have expected it... (in general, we should move away from integral
types smaller than int to avoid those pesky integer conversion/promotion)

On Fri, Sep 13, 2019 at 1:54 PM Stephen Canon <scanon at apple.com> wrote:

> But: you can use ! Instead of ~ to avoid this issue.
>
> – Steve
>
> On Sep 13, 2019, at 4:52 PM, Stephen Canon <scanon at apple.com> wrote:
>
> The C and C++ language standards fully specify this behavior. There’s no
> flexibility here to change it.
>
> – Steve
>
> On Sep 13, 2019, at 4:49 PM, Alexandre Isoard via cfe-dev <
> cfe-dev at lists.llvm.org> wrote:
>
> Sorry for the typo:
>
> false → 0b0000 → 0b1111 → true
> true → 0b0001 → 0b1110 → true
>
> On Fri, Sep 13, 2019 at 1:38 PM Alexandre Isoard <
> alexandre.isoard at gmail.com> wrote:
>
>> Hello,
>>
>> I encountered a really annoying issue, I am not sure it is Clang or C++
>> language issue.
>>
>> But basically, the following code:
>>
>> bool foo(bool a) {
>> return ~a;
>> }
>>
>> Collapse into:
>>
>> bool foo(bool a) {
>> return true;
>> }
>>
>> The explanation is simple:
>> - a is implicitly converted to int using zext
>> - int operator~(int) has the unexpected behavior described below
>> - then icmp ne 0 is used to convert to bool and is always true
>>
>> Here is the truth table of bool-to-int + operator~(int) + int-to-bool:
>> false → 0b0000 → 0b0000 → true
>> true → 0b0001 → 0b1110 → true
>>
>> Note that we can solve the issue by using sext for bool-to-int
>> conversions.
>> Should we? Is this a known problem?
>>
>> --
>> *Alexandre Isoard*
>>
>
>
> --
> *Alexandre Isoard*
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
>
>
>

-- 
*Alexandre Isoard*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20190913/30ecd53a/attachment.html>


More information about the cfe-dev mailing list