by mtklein · 3y ago · 0 comments
if (is_pow2_or_zero(len)) {
int grown = len ? len*2 : 1;
ptr = realloc(ptr, (size_t)grown * sizeof *ptr);
}
compiling into this sort of disassembly to calculate the value of grown: lsl w8, w19, #1 // w8 = len*2
cmp w19, #0x0 // is len zero?
csinc w8, w8, wzr, ne // w8 = (w8 if len != 0) or (0+1 if len == 0)
Pretty clever to create that 1 constant using csinc on the wzr zero register. cmp wzr, w19 // set the carry flag if w19 is zero
adc w8, w19, w19 // w8 = w19 + w19 + carry add eax, eax
setz al add eax, eax
jnz skipinc
inc eax
skipinc:
Also 5 bytes.