LSU.h File Reference
Linear Scaling Utilities for unsigned 32-bit integers.
More...
|
Classes |
struct | _LSU_factors |
| Structure to hold the calculated scale factors for a given unsigned numerator and denominator. More...
|
Typedefs |
typedef struct _LSU_factors | LSU_factors |
| Typedef for struct _LSU_factors.
|
Functions |
void | LSU_atomicCalc (LSU_factors *factors, unsigned int numerator, unsigned int denominator) |
| Calculates the scaling factors based on numerator and denominator.
|
unsigned int | LSU_atomicScale (const LSU_factors *factors, unsigned int dx) |
| Returns the scaled value of dx.
|
void | LSU_calc (LSU_factors *factors, unsigned int numerator, unsigned int denominator) |
| Calculates the scaling factors based on numerator and denominator.
|
unsigned int | LSU_scale (const LSU_factors *factors, unsigned int dx) |
| Returns the scaled value of dx.
|
Detailed Description
Linear Scaling Utilities for unsigned 32-bit integers.
- Author:
- JJRussell - russell@slac.stanford.edu
CVS $Id: LSU.h,v 1.2 2005/05/02 15:39:39 russell Exp $
- Basic Operation
- These routines perform integer linear scaling, i.e. the following operation, on unsigned 32-bit integers.
y = x * Numerator / Denominator
- Goals
- They seek to satisfy two goals
- Avoid overflow and round off error.
- Efficiency
- Overflow and Round Off Error
- Arthimetically there are three naive ways to compute this
- Convert everything to floating point
- As y = (x * Numerator) / Denominator
- As y = x * (Numerator / Denominator)
- Floating Point Option
- The first of these solutions, converting to floating point is ruled out as impractical. Many times the embedded execution environment does not allow the use of floating point.
- Multiple, then Divide
- If done in straight 32-bit arthimetic, this solution runs the very real risk of overflowing. The alternative would be to perform the multiple as a 64-bit operation. This would lead to a very lengthy 64-bit divide by 32-bit.
- Divide, then Multiple
- If done in straight 32-bit arthimetic, the value of the division Numerator / Denominator would certainly lose precision, and in the very real case of Numerator < Denominator, the value would be zero.
- The Solution
- The solution is to first scaling up Numerator by some power of 2, then doing the division as a 64-bit by 32-bit value. With this tactic not much has been gained over the first method. However, one can save the result of this calculation plus the power of 2 used. Subsequent calculations can then be reduced to something like
y = (x * factor) >> shift
- This is not precisely what is done. For the truly stout of heart, the implementation is fully documented in the code.
- Efficiency
- The final feather in the cap, is that once computed, the multipication and scale factors can be used over and over again, provided numerator and denominator do not change. In many applications, this is precisely the case, x keeps changing, but numerator and denominator do not. This prescription allows the very expensive 64-bit by 32-bit divide to be amortized over many operations
- Variants
- On occasion, one may find that the values in the scaling factor structure, LSU_factors, are being used and updated in an asynchonous fashion. In these cases the routines LSU_atomicCalc and LSU_atomicScale should used to ensure coherent access and usage
Typedef Documentation
Typedef for struct _LSU_factors.
- Usage
- The members of this structure should never be accessed directly by user code. In C++, language, this are private members. The structure is presented in the public interface for sole convenience of allowing the user to drop compiler instances of this structure, avoiding the need to allocate them from the heap;
Function Documentation
void LSU_atomicCalc |
( |
LSU_factors * |
factors, |
|
|
unsigned int |
numerator, |
|
|
unsigned int |
denominator | |
|
) |
| | |
Calculates the scaling factors based on numerator and denominator.
- Parameters:
-
| factors | The structure to receive the calculated scaling factors. |
| numerator | The numerator of the scale factor |
| denominator | The denominator of the scale factor |
- This routines calculates a multiply and a shift factor that can be used by LSU_scale or LSU_atomicScale to scale a variable by the ratio of numerator to denominator.
- This routine ensures atomic access to the scaling numbers. If your application does update the scale factors asynchronous to their use or your application has already arranged its own protection (perhaps be virtue of being called in interrupt code), then consider using the somewhat lighter-weight routines, LSU_calc and LSU_scale.
References INT__lock(), INT__unlock(), and lsu_calc().
unsigned int LSU_atomicScale |
( |
const LSU_factors * |
factors, |
|
|
unsigned int |
dx | |
|
) |
| | |
Returns the scaled value of dx.
- Returns:
- The scaled value of dx
- Parameters:
-
| factors | The scaling factors |
| dx | The value to be scaled |
- What It Does
- This routine scales dx by the ratio of numerator and denominator values used to calculate the scaling factor. The main claim to fame here is speed on the PowerPC platforms, operating about 10 times faster than a 32 x 32 to 64 bit multiple followed by a 32-bit divide. It achieves this by precalculating factors that merely multiple and shift dx, avoiding the expensive divide operation everywhere but in the initial calculation of the multiple and shift factors.
- Atomic Operation
- On Power PC platforms only, the scaling factors are extracted in an indivisable fashion, protecting the routine from having these values recomputed out from underneath it. No such protection is offered on other platforms.
- Note:
- If your application does not update these parameters asynchonously to their use, consider using the lighter-weight routines LSU_calc and LSU_scale.
References INT__lock(), INT__unlock(), lsu__scale(), _LSU_factors::mult, and _LSU_factors::shift.
void LSU_calc |
( |
LSU_factors * |
factors, |
|
|
unsigned int |
numerator, |
|
|
unsigned int |
denominator | |
|
) |
| | |
Calculates the scaling factors based on numerator and denominator.
- Parameters:
-
| factors | The structure to receive the calculated scaling factors. |
| numerator | The numerator of the scale factor |
| denominator | The denominator of the scale factor |
- What It Does
- This routines calculates a multiply and a shift factor that can be used by LSU_scale or LSU_atomicScale to scale a variable by the ratio of numerator to denominator.
- Warning:
- If the scale factors are being update asynchronous of their use, then consider using LSU_atomicCalc and LSU_atomicScale or be sure to provide some locking/protection scheme of your own.
References lsu_calc().
Referenced by WCT_set().
unsigned int LSU_scale |
( |
const LSU_factors * |
factors, |
|
|
unsigned int |
dx | |
|
) |
| | |
Returns the scaled value of dx.
- Returns:
- The scaled value of dx
- Parameters:
-
| factors | The scaling factors |
| dx | The value to be scaled |
- What It Does
- This routine scales dx by the ratio of numerator and denominator values used to calculate the scaling factor. The main claim to fame here is speed on the PowerPC platforms, operating about 10 times faster than a 32 x 32 to 64 bit multiple followed by a 32-bit divide. It achieves this by precalculating factors that merely multiple and shift dx, avoiding the expensive divide operation everywhere but in the initial calculation of the multiple and shift factors.
- Warning:
- If your application does updates these parameters asynchonously to their use, then either consider using the routines LSU_atomicCalc and LSU_atomicScale or arranging your own protection.
References lsu__scale(), _LSU_factors::mult, and _LSU_factors::shift.