Portable replacement

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.
 
S

suresh shenoy

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.

You still use a 2 byte mask, what if unsigned u represents 32 bits?

Suresh M. Shenoy
 
T

thomas.mertes

I can't help but wonder, what code does it output for:

int greater_or_equal3(unsigned u, unsigned v)
{
return u >= v;

}

Maybe the code of the OP will be used in a obfuscated
C contest...

Greetings Thomas Mertes

Seed7 Homepage: http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.
 
T

Thad Smith

Noob said:
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;
}

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.
 
N

Noob

Thad said:
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: <[email protected]>
http://groups.google.com/group/comp.lang.c/browse_frm/thread/dceeb7c981bf2113

For example, 2 is considered "greater than" 65530, because there is
a high probability that 2 is, in fact, 65538 in disguise.

Regards.
 
N

Noob

Suresh said:
You still use a 2 byte mask,

I think you wrote "byte" where you meant "octet" :)
what if unsigned u represents 32 bits?

I don't understand the question. What did you mean?

The range of legal values for u and v is that of an uint16_t
i.e. 0 to 65535.

Regards.
 
N

Noob

pete said:
I can't help but wonder, what code does it output for:

int greater_or_equal3(unsigned u, unsigned v)
{
return u >= v;
}

Why are you wondering?

greater_or_equal3:
movl 8(%esp), %edx
cmpl %edx, 4(%esp)
setae %al
movzbl %al, %eax
ret

But this is irrelevant, as greater_or_equal3 is /not/ equivalent
to greater_or_equal2. (It doesn't deal with wrap-around.)

Consider u=65000 and v=10

greater_or_equal2(65000, 10) returns 0.
greater_or_equal3(65000, 10) returns 1.

Regards.
 
B

Ben Bacarisse

Noob said:
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;
}

There is one difference (which I though had already been pointed out,
but I may be miss-remembering) which is that in foo1, u-v may not be
representable as in int16_t, so the conversion is either undefined or,
implementation defined depending on which C standard one is using.
 
N

Noob

Ben said:
There is one difference (which I though had already been pointed out,
but I may be miss-remembering) which is that in foo1, u-v may not be
representable as in int16_t, so the conversion is either undefined or,
implementation defined depending on which C standard one is using.

I've already pointed out that foo1 relies on impl-defined behavior.
In fact, that is the very reason why I wrote foo2.

The problem statement was:

<quote>
I've rewritten a function that relies on implementation-defined
behavior and availability of exact-width integers, with the goal
of making the new implementation portable across any platform.
</quote>

On a related subject, I don't think the conversion to int16_t is
ever undefined. (AFAIU, both C89 and C99 say it is impl-defined.)

Regards.
 
T

Thad Smith

Noob said:
(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.)

For the cases that are well-defined by the standard, the results appear
identical. If they give the results you want for the
implementation-defined situations, then you have a good replacement.
 
B

Ben Bacarisse

Noob said:
I've already pointed out that foo1 relies on impl-defined behavior.
In fact, that is the very reason why I wrote foo2.

Ah, right. I thought you were asking a new question.
On a related subject, I don't think the conversion to int16_t is
ever undefined. (AFAIU, both C89 and C99 say it is impl-defined.)

Yes, you are right. For some reason, I though the conversion to int
was undefined in C89 (as it is from floating types) but it is indeed
only implementation defined.

To address your question... I'd like to be definitive, but I have no
"calculus" for covering all the possible options. It looks the same,
(assuming one typical behaviour for the implementation-defined cases)
but you know that already. I can't prove it.
 
T

thomas.mertes

Why are you wondering?

greater_or_equal3:
movl 8(%esp), %edx
cmpl %edx, 4(%esp)
setae %al
movzbl %al, %eax
ret

But this is irrelevant, as greater_or_equal3 is /not/ equivalent
to greater_or_equal2. (It doesn't deal with wrap-around.)

Consider u=65000 and v=10

greater_or_equal2(65000, 10) returns 0.
greater_or_equal3(65000, 10) returns 1.

Maybe greater_or_equal3 returns 1 because 65000 is
greater than or equal 10.

Can it be that your function has unsigned parameters,
but you really want to do a signed comparison.
What about something like:

int greater_or_equal4 (unsigned u, unsigned v)
{
return ((int) u) >= ((int) v);
}

that way greater_or_equal4(65000, 10) would return 0
(at least when the size of int and unsigned is 2).

Greetings Thomas Mertes

Seed7 Homepage: http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.
 
N

Noob

Thomas said:
What about something like:

int greater_or_equal4 (unsigned u, unsigned v)
{
return ((int) u) >= ((int) v);
}

that way greater_or_equal4(65000, 10) would return 0
(at least when the size of int and unsigned is 2).

For the record, the original functions, renamed foo1 and foo2
to prevent people from focusing on their result.

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

On my platform, foo1 and foo2 are equivalent.
My claim is that foo2 is portable, while foo1 is not.

greater_or_equal4 is not equivalent to foo2.
(Consider u=32000 and v=33000)

greater_or_equal4 has even worse shortcomings than foo1, as it
requires int and unsigned int to be 16 bits wide, which is not
true on my platform.
 
B

Barry Schwarz

For the record, the original functions, renamed foo1 and foo2
to prevent people from focusing on their result.

int foo1(uint16_t u, uint16_t v)
{
return (int16_t)(u-v) >= 0;

What happens when u is 0, v is UINT16_MAX, and UINT16_MAX > INT16_MAX?
}

int foo2(unsigned u, unsigned v)
{
return ((u-v) & 0xffffU) <= 0x7fffU;
}

On my platform, foo1 and foo2 are equivalent.
My claim is that foo2 is portable, while foo1 is not.

greater_or_equal4 is not equivalent to foo2.
(Consider u=32000 and v=33000)

greater_or_equal4 has even worse shortcomings than foo1, as it
requires int and unsigned int to be 16 bits wide, which is not
true on my platform.


Remove del for email
 
N

Noob

Barry said:
What happens when u is 0, v is UINT16_MAX, and UINT16_MAX > INT16_MAX?

(NB: UINT16_MAX = 65535 and INT16_MAX = 32767)

If u=0 and v=65535 then (int16_t)(u-v) evaluates to 1.
And 1 >= 0 evaluates to 1. Therefore foo1 returns 1.

The logic is: If we receive seqno 0 after seqno 65535, then, with high
probability, seqno 0 is, in fact, seqno 65536 in disguise, which is
newer than seqno 65535; which translates to : 0 "is greater than" 65535.

http://groups.google.com/group/comp.lang.c/browse_frm/thread/dceeb7c981bf2113

foo2(0, 65535) also returns 1.

Regards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top