Re: Range of 32 bit values
Re: Range of 32 bit values
- Subject: Re: Range of 32 bit values
- From: Ben Weiss <email@hidden>
- Date: Mon, 9 Apr 2007 12:29:22 -0700
Luigi,
You could try dicing up the floating-point format by hand using
integer instructions. The float format is 1:8:23, with a bias of 127,
so you'd have something like this (which should handle denormal/
infinity values as well, since you want them all to produce 0 output):
unsigned int CastToIntWrap(real myfloat) {
unsigned int v = *(unsigned*)&myfloat; // load raw floating-point
bits into integer register
if (int(v) < 0) return -CastToIntWrap(-myfloat); // deal with
negative values
unsigned int exponent = v >> 23 & 0xFF; // biased exponent
if (exponent < 127 || exponent > 181) return 0; // no significant
bits in 32-bit integer range
v = 0x00800000 | (v & 0x007FFFFF); // this value is exactly correct
if the biased exponent is 150
return exponent >= 150 ? v << (exponent - 150) : v >> (150 - exponent);
}
Since these are all integer instructions, it should still run several
times faster than a single call to fmod().
Hope this helps,
Ben
On Apr 9, 2007, at 11:58 AM, Luigi Castelli wrote:
Hey guys,
first of all, thanks to all who replied.
Here is the problem:
the casting with wrapping is an optimization trick that the algorithm
uses.
So adding an instruction (or several) by using fmod() really defeats
the purpose.
Also, I cannot use only integers. The parameters before casting
must be
floats.
These are the restriction that I have to deal with.
So my question remains as if there is a way to make the range of an
integer wrap after casting or if it's an operation entirely dependant
on hardware and the programmer has no control over it.
- Luigi
--- Greg Guerin <email@hidden> wrote:
Luigi Castelli wrote:
I multiply big float values and - after being appropriately cast -
assign them to the above variable.
Example:
int value = (int)(32768. * 65536.) // (and bigger)
I just so happens that when the result of the multiplication exceed
the
size of the value that a 32 bit int type variable is able to
represent
the value is clipped to -2147483648. I have noticed that the cast
(either explicit or implicit) is what causes the value to be
clipped,
because if I cast to a bigger value (i.e. 64 bit) then the value is
wrapped until the 64 bit maximum is reached. Then the value is
clipped
again.
In any case for the algorithm I am developing I would like the int
value to be wrapped, not clipped. I would like after reaching the
value's maximum limit to go back and start again from its minimum.
This seems like a simple answer: don't use float arithmetic.
Only use operands that are 32-bit ints, not floats.
You might also look into the Intel-specific behavior of casting
floats to
ints (overrange type-narrowing conversions). If CodeWarrior-compiled
code
wrapped around, it may be a side-effect of the CPU arch (PPC), and
not at
all related to CW vs. Xcode. As I vaguely recall, these kinds of
conversions are unspecified by the C spec, but you really should
check a
spec, or just use ints.
-- GG
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40yahoo.com
This email sent to email@hidden
------------------------------------------------------------
THIS E-MAIL MESSAGE IS FOR THE SOLE USE OF THE INTENDED RECIPIENT
AND MAY CONTAIN CONFIDENTIAL AND/OR PRIVILEGED INFORMATION. ANY
UNAUTHORIZED REVIEW, USE, DISCLOSURE OR DISTRIBUTION IS
PROHIBITED. IF YOU ARE NOT THE INTENDED RECIPIENT, CONTACT THE
SENDER BY E-MAIL AT email@hidden AND DESTROY ALL COPIES OF
THE ORIGINAL MESSAGE. WITHOUT PREJUDICE UCC1-207.
------------------------------------------------------------
______________________________________________________________________
______________
Food fight? Enjoy some healthy debate
in the Yahoo! Answers Food & Drink Q&A.
http://answers.yahoo.com/dir/?link=list&sid=396545367
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40benweiss.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden