N
Noob
Hello,
I've rewritten a function (greater_or_equal) that relies on
implementation-defined behavior and availability of exact-width
integers, with the goal of making the new implementation
(greater_or_equal2) portable across any platform.
What do you think of the new implementation?
(Suggestions and comments are welcome.)
int greater_or_equal(uint16_t u, uint16_t v)
{
return (int16_t)(u-v) >= 0;
}
int greater_or_equal2(unsigned u, unsigned v)
{
return ((u-v) & 0xffffU) <= 0x7fffU;
}
<OT>
GCC seems to "understand" the source as it outputs the following code.
greater_or_equal2:
movl 8(%esp), %edx
cmpw %dx, 4(%esp)
setns %al
movzbl %al, %eax
ret
</OT>
Regards.
I've rewritten a function (greater_or_equal) that relies on
implementation-defined behavior and availability of exact-width
integers, with the goal of making the new implementation
(greater_or_equal2) portable across any platform.
What do you think of the new implementation?
(Suggestions and comments are welcome.)
int greater_or_equal(uint16_t u, uint16_t v)
{
return (int16_t)(u-v) >= 0;
}
int greater_or_equal2(unsigned u, unsigned v)
{
return ((u-v) & 0xffffU) <= 0x7fffU;
}
<OT>
GCC seems to "understand" the source as it outputs the following code.
greater_or_equal2:
movl 8(%esp), %edx
cmpw %dx, 4(%esp)
setns %al
movzbl %al, %eax
ret
</OT>
Regards.