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: Usage of UnsignedFixed type



 > #define Float64ToUnsignedFixed(f64SampleRate) ((UnsignedFixed)
 > (((f64SampleRate) * 65536.0) + 0.5))

uses fraction. I missed, that the multiplication is done with floating point precision...

Therefore I suppose, you can calculate the fraction part of an UnsignedFixed with something like

#define FractionOfUnsignedFixed(f64SampleRate) (((Float64) ( (f64SampleRate) & 0xffff)) / 65536 )

Is that correct ?

Fixed is a 32b data type that presents a number in the fixed point representation. Upper half (in big endian) holds its integer part, while the lower one holds the fractional part and therefore there is a scale of s=65536=2^16. This scale is used when converting a number to its Fixed representation and during several operations such as multiplication or division. That's why there's MulDiv because it is more effective because scales cancel.


Whether the data type is signed or unsigned depends barely on YOU and you should just use different arithmetic operations at the assembly level (compiler does that for you). That is a reason why there are some UnsignedFixed functions as these different arithmetic operations has to be used.

If you want to get a number to its Fixed representation, you JUST multiply it by the scale (s) and suppose it does not overflow you obtain its fixed point representation after rounding to the nearest that is often solved as adding 1/2 ulp.

Based on the previous statement, Float64ToUnsignedFixed macro first multiplies a number with the scale s that gets it to a double representation of a fixed data type and then adds a 1/2 ulp [unit in the last place] to get the number rounded to its nearest Fixed representation. Once that is done, the number gets cast to UnsignedFixed. If you cast it to a Fixed instead, it will work the same way.

The second macro, FractionOfUnsignedFixed simply omits the high (integer) portion of an UnsignedFixed so the only fractional portion of the UnsignedFixed remains and you obtain an absolute value of the fractional part. That is absolutely OK because the entire number is an absolute value anyway. Then the macro casts fractional part as a double to get it into the floating point set and divides it by the scale to get its unscaled value. I would probably use 65536.0 instead of 65536 to tell the compiler that this number is a floating point number but it should be able to find out automatically.

Concluding, I recommend to put there 65536.0 to be absolutely sure what kind of division happens but otherwise the macro is OK.

It might be interesting to write FractionOfFixed because it would not work to just mask off the high portion because we lose the sign. Which way do you think is better?

#define sgn(x) (((x)==0.0)?0:((x)>0)?1:-1)
#define FractionOfFixed(fixed) sgn(fixed)*FractionOfUnsignedFixed(fixed)

or

#define FractionOfFixed(fixed) ((((Float64)fixed)/65536.0)-floor((((Float64)fixed)/65536.0)))


Cheers,

  Tomas
--
# Ing. Tomas Zahradnicky, Jr.
# The Czech Technical University
# Dept of Computer Science, FEE-CTU Prague
_______________________________________________
Do not post admin requests to the list. They will be ignored.
QuickTime-API mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/quicktime-api/email@hidden

This email sent to email@hidden
References: 
 >Usage of UnsignedFixed type (From: "Martin Wilz" <email@hidden>)
 >Re: Usage of UnsignedFixed type (From: Brad Ford <email@hidden>)
 >Re: Usage of UnsignedFixed type (From: "Martin Wilz" <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.