5 ms·
Zero or Sign Extend
- eqvinox 2y ago> ... this explicitly relies on shifting something into the sign bit, which depending on the exact flavor of language standard you’re using is either not allowed or at best fairly recently .. An unsigned has no sign bit, so the left shift just needs to be unsigned to make it "technically correct". (Remember to not use smaller than int types though, due to integer promotion issues)
- fluoridation 2y agoYup. When you're twiddling bits you're better off using unsigned types in general anyway, and leaving converting to a signed type at the very end.
- jchw 2y agoOf course doing the undefined thing works on almost any platform except DS9k, but that last formulation is quite elegant. It's a bit like byteswapping in that it's fairly simple to do but it's even simpler to not do by just never relying on the machine endianness.
- Sesse__ 2y agoAlso shifts, especially variable-length shifts, are frequently slower than xor and add/sub (e.g., on x86, shl only works with cl and shlx has high latency), so that's another score for the xor variant.
- CalChris 2y agoMaybe for variable variable-length shifts but for constant variable-length shifts, SHL reg, imm8 is single cycle on recent x86_64 microarchitectures.
- Sesse__ 2y agoBut xor and sub can go in way more ports, giving you higher throughput.
- dzaima 2y ago"way more" is 2 vs 4 ports (→5 for ≥alderlake); 1/cycle via shifts is probably good enough for most use-cases (though perhaps the more focused port pressure could be an issue with larger context). And with hard-coded immediates xor+sub also ends up at twice the code size as shl+shr, so there's some trade-off. (but yeah if code size isn't a concern, xor+sub wins out)
- Sesse__ 2y agoTwice or more is pretty significant, wouldn't you say? :-) Code size is a valid concern, though.
- Findecanor 2y agoWhen compiling for e.g. ARM, however, the two shifts tends to be compiled into a single bitfield-extract instruction. RISC-V doesn't have such an instruction but there are cores that do macro-op fusion of just that sequence.
- Neywiny 2y agoThis is the perfect spot to use a bitfield. You can tell it signed or unsigned, and the compiler will deal with it all and optimize. No bit ops to get wrong or maintain. Very readable and scalable.
- edflsafoiewq 2y agoBut the width and signedness of a bitfield are defined at compile-time, while in this example they need to come from a format read at runtime.
- cryptonector 2y agoSo? The author knows a priori the size of the int on the wire.
- almostgotcaught 2y ago> the compiler I love when people say this as if there's exactly one compiler with a fixed implementation for whatever opt pass.
- AlotOfReading 2y agoThat's not how this phrase is used. It usually encompasses any reasonably advanced compiler like clang, GCC, and sometimes MSVC.
- Joker_vD 2y agoBut not including any of the slightly broken C compilers that the embedded hardware manufacturers provide (also, ICC neither)?
- AlotOfReading 2y agoI'm just providing examples, not excluding everything unmentioned.
- 2y ago
- IshKebab 2y agoEh the author's suggestions only seem better because C++ is insane. The last one is definitely nice though!
- vlovich123 2y agoCan you post examples in other languages where this would be easier?
- IshKebab 2y agoSure, in Rust: fn sign_extend_u11(x: u32) -> u32 { (((x as i32) << (32-11)) >> (32-11)) as u32 } Doesn't have any of the C++ issues he mentions. And it will be faster than the alternative since it's just two instructions. (Ok this is never going to matter in practice but still...)
- dan-robertson 2y agoIsn’t the alternative also two instructions: x ^ 0x400 - 0x400
- TinkersW 2y agoShifts run on less ports than xor/sub, so should be avoided if possible when performance is important, especially when using SIMD where the shifts are often quite subpar.
- vlovich123 2y agoBut the C++20 code doesn't have this issue. That's my point. C++20 is 4 years old by this point.
- samatman 2y agoZig: fn signExtend(raw: u11) i32 { return @as(i11, @bitCast(raw)); } test "signExtend" { try expectEqual(1023, signExtend(1023)); try expectEqual(-1, signExtend(2047)); }
- 2y ago
- deleted 2y ago[deleted]
- notepad0x90 2y agoThere are explicit instructions on x64 and aarch64 for sign and zero extensions. There are also common patterns like 'xor r64, r64' to clear out a reg (and the zero-register arm variant). Why are there no higher level language abstractions for these types of patterns? Or maybe there are and I'm just unaware. I would really like to see single operand operators, similar to 'i++'. '!!i' to do 'i~=i', '<<i' to do 'i=i<<1'. The 'rep' instructions are also nice: https://www.felixcloutier.com/x86/rep:repe:repz:repne:repnz https://www.felixcloutier.com/x86/rep:repe:repz:repne:repnz Imagine doing something like this in C or Rust: 'int i=10;rep i printf("called %u times", i);' where rep would store the value of i in rcx, sets it to zero and stores in rax and jmp's to whatever function or codeblock you specified (could be inline code, or lambda expression), rcx (i) times, passing 'i''s current value optionally to the target code block. It would essentially be a shorthand form of 'for(int i=0;i<10;i++){printf("called %u times",i);}' except it's easier to use for simpler constructs like 'rep 8 <<i;' (just an example, you can just do 'i = i << 8;') if you combine it with my earlier proposed left shift operator.
- toast0 2y ago> There are explicit instructions on x64 and aarch64 for sign and zero extensions. There are also common patterns like 'xor r64, r64' to clear out a reg (and the zero-register arm variant). Why are there no higher level language abstractions for these types of patterns? Or maybe there are and I'm just unaware. The high level abstraction for xor r64, r64 is foo = 0. High level abstraction for sign/zero extension is casting to a larger type.
- notepad0x90 2y agoI guess I was just being lazy and wanting something like '~foo'. If you won't want to change the type and are fine with losing bits, it's slightly more verbose, but I admit to being lazy again with that as well :(.
- adrian_b 2y agoIf you define in C/C++ a structure with bit fields, and you declare the bit fields as being either signed or unsigned, assigning the bit fields to a bigger integer, which is appropriately signed or unsigned, will perform an optimal sign extension or zero extension, as necessary, without having to write any expression. When you need sign extension or zero extension for conversion between standard integer types, you have to write only the type casting operator. Having to use any of the inefficient tricks presented in the parent article is necessary only when you do not declare the correct types for your variables.