Thad Smith wrote:

> Noob wrote:

>

>> 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;

>> }

>

> Neither implementation is correct without an exact definition of what it

> does. A function that evaluates greater_or_equal2(60000,0) as 0 would

> be surprising to me without a definition to the contrary.
(I agree that I have given these functions unintuitive names, but

I didn't ask whether the two implementations were correct.)

What matters to me is whether the two implementations are equivalent.

That is, given identical input, do they produce identical output?

(The range of legal values for u and v is that of an uint16_t,

i.e. 0 to 65535.)

I should have named the two functions foo1 and foo2, and asked:

"Are foo1 and foo2 equivalent? and is foo2 portable?"

int foo1(uint16_t u, uint16_t v)

{

return (int16_t)(u-v) >= 0;

}

int foo2(unsigned u, unsigned v)

{

return ((u-v) & 0xffffU) <= 0x7fffU;

}

For those wondering what they're supposed to compute, I provided

more details in an earlier thread.

Message-ID: <480f0d58$0$5410$(E-Mail Removed)>

http://groups.google.com/group/comp....eeb7c981bf2113
For example, 2 is considered "greater than" 65530, because there is

a high probability that 2 is, in fact, 65538 in disguise.

Regards.