Moving the kernel to modern C
Moving the kernel to modern C
Posted Mar 3, 2022 0:07 UTC (Thu) by HenrikH (subscriber, #31152)In reply to: Moving the kernel to modern C by dvdeug
Parent article: Moving the kernel to modern C
For your example at hand with "a + b" where we have "int a" and "unsigned int b" and a have a negative value then since both are of the same rank (integers) but different types (signed vs unsigned) the signed integer is implicitly converted to an unsigned integer. Overflow is only undefined for the signed integer case while defined to wrap around for the unsigned integer which is why most of the "a + b" code works as intended.
Aka if a is -1 and b is 2 then -1 is "converted" to 0xFFFFFFFF, add +2 and we wrap around to 1. In practice the compiler simply moves both a register and does the x86 ADD instruction since converting signed to unsigned of the same size is a no op and a binary add in particular is signedness agnostic.
