I have the following code:
#include <math.h> #include <stdio.h> using u16 = unsigned short int; using s16 = signed short int; using s32 = signed int; s16 Algorithm(s16 sample) { s32 absSample = static_cast<s32>(abs(sample)); s32 sampleBits = absSample >> 7; s32 sampleMasked = absSample & 0x7F; s16 result = (u16)(sampleMasked << sampleBits) | (u16)(1 << (sampleBits - 2)); if (sample < 0) { result = -result; } return result; } int main() { s16 result = Algorithm(-63); if (result == -63) { printf("OK!\n"); return 0; } else { printf("BUG!?\n"); return 1; } } In x64 debug it prints "OK!" but in x64 release it prints "BUG!?", is there a problem with my code or is the compiler doing something wrong here? How can I fix or workaround this issue?
uint16_t,int16_t, andint32_t, right? Sincesigned intisn't guaranteed to be 32 bits?OK.