Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
"Mastering C Pointers"....
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Reply to thread
Message
[QUOTE="Chris Torek, post: 1686403"] Yes, this is a special case of the code I suggested. We want "adj = (x & SIGNBIT) ? (2 - 1) : 0", and of course 2 - 1 is 1. I had suggested the general case would use (for x86) "sarl $31, %eax" and then "and $1, %eax", but note that if we use a logical shift instead of an arithmetic shift, the values we get for "is signed" and "is not signed" are 1 and 0 respectively, instead of -1 and 0. In (nonportable) C this would be: /* adj = (x >> 31) & (divisor - 1); -- general case */ adj = (unsigned)x >> 31; /* specific case when divisor==2 */ This allows the compiler to omit the "and" instruction. (The nonportable parts are twofold: this assumes x is a 32 bit two's complement value; and it assumes that signed ">>" sign-extends. The first part is easy to parameterize, but the second is not.) (The "leal" instead of "add" is another fairly disgusting trick that avoids pipeline constraints in some cases, I believe.) [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
"Mastering C Pointers"....
Top