C++ Logo

std-discussion

Advanced search

small map instruction

From: Bongo Ferno <bongoferno_at_[hidden]>
Date: Tue, 5 Dec 2023 19:55:39 -0300
Should this be a new feature for the c++ language,or is it already
implemented?

I need to map a 64 bit unsigned integer from {0,1} to 64 bit double {1,-1}

The available solutions are

1. An if

double mapped = uintFlag ? double(-1.0) : double(1.0);
// It's slow, because it introduces a branch

2. An arithmetic expression
double mapped = 1 - 2 * int(uintFlag);
// It's slow, because it needs type conversion

3. A constant array
constexpr double mapArray[2] = {1.0, -1.0};
double answer = mapArray[uintFlag];
// It's slow, because it needs 10 cycles to fetch from memory, but tends to
be faster, because it's branchless, and doesn't convert types.

Some bitwise hacks are possible, but they depend on formats for negative
integers, which are not portable between architectures.

¿is possible to create, or already exists, a smallMap instruction that
emulates a little array inside the ALU, to select a small number of
constants from a small integer index, or at least a boolean flag?

For example, two constants are in 2 registers, and a flag decides which one
is copied.

Or else, an instruction to convert a boolean flag into a sign switcher
operator?

Or maybe a GOTO to an integer label.

It would be useful in physics, geometry, graphics, and geometric algebra,
where bitwise hacks over unsigned integers translate to a multitude of
floating point sign switches, but the mapping of boolean to floating point
is unnecessarily expensive.

Received on 2023-12-05 22:55:53