Why don't we switch to C like floating pointed arithmetic instead of automatic expansion to reals?

John Colvin via Digitalmars-d digitalmars-d at puremagic.com
Fri Aug 5 02:34:42 PDT 2016


On Friday, 5 August 2016 at 08:43:48 UTC, deadalnix wrote:
> On Friday, 5 August 2016 at 08:17:00 UTC, Ilya Yaroshenko wrote:
>> 1. Could you please provide an assembler example with clang or 
>> recent gcc?
>
> I have better: compile your favorite project with 
> -Wdouble-promotion and enjoy the rain of warnings.
>
> But try it yourself:
>
> float foo(float a, float b) {
>   return 3.0 * a / b;
> }
>
> GCC 5.3 gives me
>
> foo(float, float):
>         cvtss2sd        xmm0, xmm0
>         cvtss2sd        xmm1, xmm1
>         mulsd   xmm0, QWORD PTR .LC0[rip]
>         divsd   xmm0, xmm1
>         cvtsd2ss        xmm0, xmm0
>         ret
> .LC0:
>         .long   0
>         .long   1074266112
>

Gotta be careful with those examples. See this: 
https://godbolt.org/g/0yNUSG

float foo1(float a, float b) {
   return 3.42 * (a / b);
}

float foo2(float a, float b) {
   return 3.0 * (a / b);
}

float foo3(float a, float b) {
   return 3.0 * a / b;
}

float foo4(float a, float b) {
   return 3.0f * a / b;
}

foo1(float, float):
         divss   xmm0, xmm1
         cvtss2sd        xmm0, xmm0
         mulsd   xmm0, QWORD PTR .LC0[rip]
         cvtsd2ss        xmm0, xmm0
         ret
foo2(float, float):
         divss   xmm0, xmm1
         mulss   xmm0, DWORD PTR .LC2[rip]
         ret
foo3(float, float):
         cvtss2sd        xmm0, xmm0
         cvtss2sd        xmm1, xmm1
         mulsd   xmm0, QWORD PTR .LC4[rip]
         divsd   xmm0, xmm1
         cvtsd2ss        xmm0, xmm0
         ret
foo4(float, float):
         mulss   xmm0, DWORD PTR .LC2[rip]
         divss   xmm0, xmm1
         ret
.LC0:
         .long   4123168604
         .long   1074486312
.LC2:
         .long   1077936128
.LC4:
         .long   0
         .long   1074266112


It depends on the literal value, not just the type.


More information about the Digitalmars-d mailing list