Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: float to int (kinda OT)



A naive compiler will branch when generating (remainder255 >= (255 + 128)); that's a comparison, and comparisons generate branches.
A smarter compiler might have a clever way of avoiding the branch, perhaps involving a subtract followed by a cntlzw (?). That's just off the top of my head; I haven't tested it.

And sho'nuff, it does:

tmp = alpha * red;
remainder256 = tmp & 0xFF; // get alpha * red (mod 256)
dividend = tmp >> 8; // compute alpha * red / 256
remainder255 = dividend + remainder256; // compute alpha * red (mod 255)
dividend += ((remainder255 >= (255 + 128)) & 1) + ((remainder255 >= 128) & 1); // add 0, 1, 2 depending on how large the remainder (mod 255) is

00000000 mullw r4,r3,r4 00000004 li r2,0x1 00000008 rlwinm r0,r4,0,24,31 0000000c srawi r3,r4,8 00000010 add r0,r3,r0 00000014 cmpwi cr7,r0,0x17e 00000018 bgt cr7,0x20 0000001c li r2,_test 00000020 cmpwi cr7,r0,0x7f 00000024 li r0,0x1 00000028 bgt cr7,0x30 0000002c li r0,_test 00000030 add r0,r0,r2 00000034 add r3,r3,r0

gcc -O2; gcc -fast produces very similar code:

00000000        mullw   r7,r3,r4
00000004        li      r0,0x1
00000008        li      r2,0x1
0000000c        srawi   r3,r7,8
00000010        rlwinm  r6,r7,0,24,31
00000014        add     r5,r3,r6
00000018        cmpwi   r5,0x17e
0000001c        cmpwi   cr6,r5,0x7f
00000020        bgt     0x28
00000024        li      r2,_test
00000028        bgt     cr6,0x30
0000002c        li      r0,_test
00000030        add     r4,r0,r2
00000034        add     r3,r3,r4

I always think of (x >= y) as an int which happens to be either 0 or 1, but I guess that's not the case on the PowerPC at least, since the comparison operators don't put the results in the GPRs. Phooey :)

-Keith
_______________________________________________
Do not post admin requests to the list. They will be ignored.
PerfOptimization-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/perfoptimization-dev/email@hidden

This email sent to email@hidden
References: 
 >Graphics card tricks (From: "Edward K. Chew" <email@hidden>)
 >Re: Graphics card tricks (From: Holger Bettag <email@hidden>)
 >Re: Graphics card tricks (From: Niall Dalton <email@hidden>)
 >float to int (kinda OT) (From: Ando Sonenblick <email@hidden>)
 >Re: float to int (kinda OT) (From: Brendan Younger <email@hidden>)
 >Re: float to int (kinda OT) (From: Chris Cox <email@hidden>)
 >Re: float to int (kinda OT) (From: Brendan Younger <email@hidden>)
 >Re: float to int (kinda OT) (From: Chris Cox <email@hidden>)
 >Re: float to int (kinda OT) (From: Keith Bauer <email@hidden>)
 >Re: float to int (kinda OT) (From: John Stiles <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.