which seems to be acceptable to me on Cortex-M0
It's pretty horrid that its loading the result values from memory instead of reusing the values it already has.
I think mine compiles to better code...
I agree, although the difference is lost whenever one supplies a compile-time constant
sat:
https://godbolt.org/z/rvY7bdrcn.
I looked at
the Cortex-M0 Instruction Set, and remembered that while it does not have any conditional assignment operators, it does have 16-to-32-bit sign extension instruction (
sxth). This means that
((int32_t)n != (int32_t)(int16_t)n) gets compiled to a pair of instructions, one to sign-extend the parameter, and the other to compare the two; and this comparison is only true if the value does not fit in a signed 16-bit integer. In other words,
https://godbolt.org/z/cYWdPf3zb:
#include <stdint.h>
int_fast16_t saturate16(int_fast32_t v)
{
if ((int32_t)v == (int32_t)(int16_t)v)
return v;
const int_fast16_t m = 32768; // constexpr int_fast16_t m = 32768; for C++
if (v > 0)
return m-1;
else
return -m;
}which compiles (
gcc -Os -mcpu=cortex-m0 -mfloat-abi=soft) to
saturate16(int):
sxth r3, r0
cmp r3, r0
beq .L5
asrs r3, r0, #31
subs r3, r3, r0
asrs r3, r3, #31
uxth r0, r3
ldr r3, .L7
adds r0, r0, r3
.L5:
bx lr
.L7:
.word -32768
Whether this is faster than brucehoults, I dunno; but I do expect it to be in practice.
Edited: It looks like
#include <stdint.h>
static inline int_fast16_t u32767(void)
{
return 32767;
}
int_fast16_t saturate16(int_fast32_t v)
{
if ((int32_t)v == (int32_t)(int16_t)v)
return v;
else
return (int16_t)((v >= 0) - 1) ^ u32767();
}is even faster, as it compiles to (assuming zero wait states, according to
ARM Cortex-M0 Technical Reference)
saturate16:
sxth r3, r0 ; 1 cycle
cmp r3, r0 ; 1 cycle
beq .L2 ; 2 cycles if taken, 1 cycle if not taken
ldr r3, .L3 ; 2 cycles
asrs r0, r0, #31 ; 1 cycle
eors r0, r3 ; 1 cycle
.L2:
bx lr
.L3:
.word 32767
taking 4 cycles if the value is within
int16_t range, and 7 cycles if clamped; but it does get into the obfuscated C code territory...
I did verify it works, of course. (The expression
(int16_t)((v >= 0) - 1) evaluates to
0xFFFF if
v is negative, and to
0x0000 otherwise. Exclusive-OR that with 32767, and you'll get
0x8000 = -32768 if
v is negative, and
0x7FFF = 32767 otherwise.)
If you want to avoid the memory load, you can replace the constant with inline assembly, e.g.
static inline int_fast16_t u32767(void)
{
int_fast16_t value;
asm volatile ( "mov\t%0, #128\n\t"
"lsl\t%0, #8\n\t"
"sub\t%0, #1\n\t"
: "=r" (value));
return value;
}
in which case you replace the 2-cycle memory load (slower, if you have wait states when running code from e.g. Flash) with three single-cycle instructions, and end up with
saturate16(int):
sxth r3, r0 ; 1 cycle
cmp r3, r0 ; 1 cycle
beq .L2 ; 2 cycles if taken, 1 if not taken
mov r3, #128 ; 1 cycle
lsl r3, #8 ; 1 cycle
sub r3, #1 ; 1 cycle
asrs r0, r0, #31 ; 1 cycle
eors r0, r3 ; 1 cycle
.L2:
bx lr
which has no memory accesses and only dirties one register, takes 4 cycles for valid values and 8 cycles for clamped values, when inlined (i.e., ignoring function call overhead).