- -1 when x < 0
- 0 when x = 0,
- +1 when x > 0.
; input: eax = X movl %eax, %ebx sarl $31, %eax // eax = -1 if X less then zero, 0 otherwise andl $0x7fffffff, %ebx addl $0x7fffffff, %ebx // MSB is set if any lower bits were set shrl $31, $ebx // eax = +1 if X greater then zero, 0 otherwise orl %ebx, %eax // eax = result
C99 implementation:
int32_t sign(int32_t x) { int32_t y; y = (x & 0x7fffffff) + 0x7fffffff; return (x >> 31) | ((uint32_t)y >> 31); }
Brak komentarzy:
Prześlij komentarz