* Re: A microoptimization of isnegative or greaterthan2millions.
2007-04-10 17:53 A microoptimization of isnegative or greaterthan2millions J.C. Pizarro
@ 2007-04-10 17:53 ` J.C. Pizarro
2007-04-10 17:54 ` Ian Lance Taylor
1 sibling, 0 replies; 4+ messages in thread
From: J.C. Pizarro @ 2007-04-10 17:53 UTC (permalink / raw)
To: gcc
I'm sorry, i did want to say 2 billions, not 2 millions.
J.C. Pizarro
^ permalink raw reply [flat|nested] 4+ messages in thread
* A microoptimization of isnegative or greaterthan2millions.
@ 2007-04-10 17:53 J.C. Pizarro
2007-04-10 17:53 ` J.C. Pizarro
2007-04-10 17:54 ` Ian Lance Taylor
0 siblings, 2 replies; 4+ messages in thread
From: J.C. Pizarro @ 2007-04-10 17:53 UTC (permalink / raw)
To: gcc
[-- Attachment #1: Type: text/plain, Size: 3535 bytes --]
/* Given X an unsigned of 32 bits, and Y a bool. Try to translate optimizing
*
* Y = X > 2147483647; to Y = ((signed)X) < 0;
* Y = X >= 2147483648; to Y = ((signed)X) < 0;
*
* [ Another optimization is to Y = (X >> 31) ]
*
* The opposite (ELSE):
*
* Y = X <= 2147483647; to Y = ((signed)X) >= 0;
* Y = X < 2147483648; to Y = ((signed)X) >= 0;
*
* [ Another optimization is to Y = ((~X) >> 31) ]
*
* 2147483647=0x7FFFFFFF 2147483648=0x80000000
*
* The unsigned comparison is become to signed comparison.
*
* by J.C. Pizarro */
#include <stdio.h>
/* isnegative means greaterthan2millions */
int isnegative_1(unsigned int X) {
int result; // Y is the conditional expression of if-else.
if (X > 2147483647) result = 1;
else result = 0;
return result;
}
int isnegative_2(unsigned int X) {
int result; // Y is the conditional expression of if-else.
if (X >= 2147483648U) result = 1;
else result = 0;
return result;
}
int isnegative_3(unsigned int X) {
int result; // Y is the conditional expression of if-else.
if (X <= 2147483647) result = 0;
else result = 1;
return result;
}
int isnegative_4(unsigned int X) {
int result; // Y is the conditional expression of if-else.
if (X < 2147483648U) result = 0;
else result = 1;
return result;
}
int isnegative_optimized_1(unsigned int X) {
int result; // Y is the conditional expression of if-else.
if (((signed)X) < 0) result = 1;
else result = 0;
return result;
}
int isnegative_optimized_2(unsigned int X) {
int result; // Y is the conditional expression of if-else.
if (((signed)X) >= 0) result = 0;
else result = 1;
return result;
}
int isnegative_optimized_3(unsigned int X) {
int result; // Y is the conditional expression of if-else.
if (X >> 31) result = 1;
else result = 0;
return result;
}
int isnegative_optimized_4(unsigned int X) {
int result; // Y is the conditional expression of if-else.
if ((~X) >> 31) result = 0;
else result = 1;
return result;
}
int are_equivalent_isnegative(unsigned int X) {
int equiv=1,isneg;
isneg = isnegative_1(X);
equiv = equiv && (isnegative_2(X) == isneg);
equiv = equiv && (isnegative_3(X) == isneg);
equiv = equiv && (isnegative_4(X) == isneg);
equiv = equiv && (isnegative_optimized_1(X) == isneg);
equiv = equiv && (isnegative_optimized_2(X) == isneg);
equiv = equiv && (isnegative_optimized_3(X) == isneg);
equiv = equiv && (isnegative_optimized_4(X) == isneg);
return equiv;
}
int main(int argc,char *argv[]) {
long long X;
int testOK=1;
for (X=0LL;(X<=0x0FFFFFFFFLL)&&testOK;X++) {
testOK = are_equivalent_isnegative((unsigned int)(X&0xFFFFFFFF));
}
if (testOK) printf("Full test of isnegative is PASSED.\n");
else printf("Full test of isnegative is FAILED.\n");
return 0;
}
------------------------------------------------------------------------------
# gcc version 4.1.3 20070326 (prerelease)
Full test of isnegative is PASSED.
notl %eax
shrl $31, %eax
xorl $1, %eax
IS WORSE THAN
shrl $31, %eax
---------------------
xorl %eax, %eax
cmpl $0, 4(%esp)
sets %al
IS WORSE THAN
movl 4(%esp), %eax
shrl $31, %eax
------------------------------------------------------------------------------
J.C. Pizarro
[-- Attachment #2: isnegative_20070410-1.tar.gz --]
[-- Type: application/x-gzip, Size: 1832 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: A microoptimization of isnegative or greaterthan2millions.
2007-04-10 17:53 A microoptimization of isnegative or greaterthan2millions J.C. Pizarro
2007-04-10 17:53 ` J.C. Pizarro
@ 2007-04-10 17:54 ` Ian Lance Taylor
2007-04-10 18:30 ` J.C. Pizarro
1 sibling, 1 reply; 4+ messages in thread
From: Ian Lance Taylor @ 2007-04-10 17:54 UTC (permalink / raw)
To: J.C. Pizarro; +Cc: gcc
"J.C. Pizarro" <jcpiza@gmail.com> writes:
> /* Given X an unsigned of 32 bits, and Y a bool. Try to translate optimizing
> *
> * Y = X > 2147483647; to Y = ((signed)X) < 0;
> * Y = X >= 2147483648; to Y = ((signed)X) < 0;
> *
> * [ Another optimization is to Y = (X >> 31) ]
As far as I can tell, you are recommending that gcc generate a
different code sequence than it currently does. The most helpful
approach you can use for such a suggestion is to open a bug report
marked as an enhancement. See http://gcc.gnu.org/bugs.html.
Postings to gcc@gcc.gnu.org are not wrong, but they will almost
certainly get lost. An entry in the bug database will not get lost.
Thanks.
Ian
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: A microoptimization of isnegative or greaterthan2millions.
2007-04-10 17:54 ` Ian Lance Taylor
@ 2007-04-10 18:30 ` J.C. Pizarro
0 siblings, 0 replies; 4+ messages in thread
From: J.C. Pizarro @ 2007-04-10 18:30 UTC (permalink / raw)
To: Ian Lance Taylor; +Cc: gcc
10 Apr 2007 10:53:08 -0700, Ian Lance Taylor <iant@google.com> wrote:
> As far as I can tell, you are recommending that gcc generate a
> different code sequence than it currently does. The most helpful
> approach you can use for such a suggestion is to open a bug report
> marked as an enhancement. See http://gcc.gnu.org/bugs.html.
>
> Postings to gcc@gcc.gnu.org are not wrong, but they will almost
> certainly get lost. An entry in the bug database will not get lost.
>
> Thanks.
>
> Ian
>
Thanks, bug reported as enhancement in
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31531
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2007-04-10 18:22 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-04-10 17:53 A microoptimization of isnegative or greaterthan2millions J.C. Pizarro
2007-04-10 17:53 ` J.C. Pizarro
2007-04-10 17:54 ` Ian Lance Taylor
2007-04-10 18:30 ` J.C. Pizarro
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).