C++ Logo

std-discussion

Advanced search

Re: small map instruction

From: Thiago Macieira <thiago_at_[hidden]>
Date: Tue, 05 Dec 2023 15:38:27 -0800
On Tuesday, 5 December 2023 14:55:39 PST Bongo Ferno via Std-Discussion wrote:
> 1. An if
>
> double mapped = uintFlag ? double(-1.0) : double(1.0);
> // It's slow, because it introduces a branch

Says who?

See https://gcc.godbolt.org/z/MKfvMThaf, where:

1) Clang used an indexed memory array (like your #3)
2) GCC and the old ICC used a branch
3) MSVC implemented the comparison in the SIMD register

Clang and MSVC were branchless.

But all four loaded from memory because neither the value of 1.0 or -1.0 are
easy to make from plain instructions (unlike 0.0 or the integer -1).

In any case, the summary is: write readable code and let the compiler decide
how to best implement it. I don't see a need to provide a function to do this
simple ternary-operator operation.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-12-05 23:38:30