Should you be able to initialize a float with a char?

Steven Schveighoffer schveiguy at gmail.com
Fri May 20 00:06:27 UTC 2022


On 5/19/22 5:44 PM, Walter Bright wrote:
> On 5/19/2022 1:01 PM, Steven Schveighoffer wrote:
>> And let the compiler come up with whatever funky stuff it wants to in 
>> order to make it fast.
> 
> You never write things like:
> 
>     a += (b < c);
> 
> ? I do.

I have written these kinds of things *sometimes*, but also would be fine 
writing `b < c ? 1 : 0` if required, or even `int(b < c)`. I'd happily 
write that in exchange for not having this happen:

```d
enum A : int { a }

Json j = A.a;
writeln(j); // false
```

> And, as I remarked before, GPUs favor this style of coding, as 
> does SIMD code, as does cryto code.

If the optimizer can't see through the ternary expression with 2 
constants, then maybe it needs updating.

> Hoping the compiler will transform the code into this style, if it is 
> not specified to, is just that, hope :-/

It's just use a better compiler. And if it doesn't, so what? Just write 
it with casts if a) it doesn't do what you want, and b) it's critically 
important. I just write it the "normal" way and move on.

What if the compiler rewrites it back to the ternary expression? Either 
way, if you are paranoid the compiler isn't doing the right thing, you 
check the assembly.

> Sometimes this style is not necessarily faster, either, even though the 
> user may desire it for crypto reasons.

Which is exactly why I leave it to the experts.

I want to write the clearest code possible, and let the optimizer 
wizards do their magic.

If for some reason, the compiler isn't smart enough to figure out the 
right thing to do (and it bothers me to the point of investigation), 
then D provides so many tools to get it to spit out what you want, all 
the way down to inline assembly. Anyone who thinks they can predict 
exactly what the compiler will output for any given code is fooling 
themselves. If you care, check the assembly.

-Steve


More information about the Digitalmars-d mailing list