Thread overview
[Issue 24646] `is` test broken for invalid bools
Jul 03
Bolpat
Jul 03
Bolpat
[Issue 24646] reading a bool generates redundant masking code
Jul 03
Dennis
July 03
https://issues.dlang.org/show_bug.cgi?id=24646

--- Comment #1 from Bolpat <qs.il.paperinik@gmail.com> ---
Masking the `bool` for the `is` test is wrong. A struct with padding bits has its padding bits compared by `is` as well. For all intents and purposes, a `bool` is a struct around a `ubyte` with the invariant that its value is only ever 0 or 1.

--
July 03
https://issues.dlang.org/show_bug.cgi?id=24646

Bolpat <qs.il.paperinik@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |qs.il.paperinik@gmail.com

--
July 03
https://issues.dlang.org/show_bug.cgi?id=24646

Dennis <dkorpel@live.nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |performance
                 CC|                            |dkorpel@live.nl
            Summary|`is` test broken for        |reading a bool generates
                   |invalid bools               |redundant masking code
           Severity|normal                      |enhancement

--- Comment #2 from Dennis <dkorpel@live.nl> ---
There is no defined behavior for `is` on invalid bools, so you can't say it's broken when dmd doesn't do a particular thing. It is true that dmd still does redundant masking for bool from a previous safety fix, which can now be removed since safe values for `bool` have been agreed upon and specified since, so I'll reframe this as a performance issue.

This code:
```D
bool eq(bool a, bool b)
{
    return a is b;
}
```

Generates this code with `-O`:
```
mov     CL,SIL
and     CL,1
mov     DL,DIL
and     DL,1
cmp     CL,DL
setz    AL
pop     RBP
```

Which could have the `and` instructions removed.

--