cglm icon indicating copy to clipboard operation
cglm copied to clipboard

Error in glm_sign()

Open pm4gh opened this issue 3 years ago • 2 comments

glm_sign(), as ((val >> 31) - (-val >> 31)) fails if val is INT_MIN. In fact, it has undefinded behaviour (shifting signed 32-bit value by 31 bits).

Using shifts, or bitwise operations in general, on signed values is a bit iffy. It would be surprising if this function was so performance-critical that this kind of branchless hack is worth it. It would be more sensible to implement it the same way as glm_signf() later inthe same source file.

pm4gh avatar Mar 05 '22 21:03 pm4gh

Hi @pm4gh ,

Thanks, any feedback would be awesome to find the sign of number.

It would be more sensible to implement it the same way as glm_signf() later inthe same source file.

yes it could be, we can update it to that way.

recp avatar Mar 07 '22 10:03 recp

getting the sign bit of 32-bit integer is as simple as: return val&0x80000000 (1 if signed, 0 otherwise)

same apply for sp-fp.