| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2003-2010, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: bmth_fix.h $ |
|---|
| 11 | * $brcm_Revision: Hydra_Software_Devel/9 $ |
|---|
| 12 | * $brcm_Date: 8/26/10 7:46p $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: /magnum/commonutils/mth/bmth_fix.h $ |
|---|
| 19 | * |
|---|
| 20 | * Hydra_Software_Devel/9 8/26/10 7:46p albertl |
|---|
| 21 | * SW7405-4556, SW7405-4515: Changed multiplication to use 64-bit |
|---|
| 22 | * intermediate. Added fix to float macro. |
|---|
| 23 | * |
|---|
| 24 | * Hydra_Software_Devel/8 8/4/08 1:44p tdo |
|---|
| 25 | * PR45389: Make inline user definable because it is not compile for some |
|---|
| 26 | * std. |
|---|
| 27 | * |
|---|
| 28 | * Hydra_Software_Devel/7 4/2/08 5:17p albertl |
|---|
| 29 | * PR35135: Changed BMTH_FIX_SIGNED_FTOFIX to round float values to |
|---|
| 30 | * nearest fixed point value. |
|---|
| 31 | * |
|---|
| 32 | * Hydra_Software_Devel/6 9/20/07 1:26a albertl |
|---|
| 33 | * PR35135: Cleaned up color space matrices and changed them to use same |
|---|
| 34 | * macro system. Added color space conversion functionality to graphics |
|---|
| 35 | * windows. |
|---|
| 36 | * |
|---|
| 37 | * Hydra_Software_Devel/5 8/9/07 12:27p tdo |
|---|
| 38 | * PR33574: Convert more macro defines into functions to avoid compiling |
|---|
| 39 | * errors in WinCE build |
|---|
| 40 | * |
|---|
| 41 | * Hydra_Software_Devel/4 8/3/07 5:19p tdo |
|---|
| 42 | * PR33574: Make BMTH_FIX_SIGNED_MUL into function call to eliminate |
|---|
| 43 | * compiling errors in WinCE platform build |
|---|
| 44 | * |
|---|
| 45 | * Hydra_Software_Devel/3 6/12/07 7:17p albertl |
|---|
| 46 | * PR31093: Reverted to older macros that handled signed bits properly. |
|---|
| 47 | * |
|---|
| 48 | * Hydra_Software_Devel/2 6/11/07 6:56p albertl |
|---|
| 49 | * PR31093: Fixed sign errors in macros. |
|---|
| 50 | * |
|---|
| 51 | * Hydra_Software_Devel/1 2/22/06 6:10p albertl |
|---|
| 52 | * PR18913: Initial revision of BMTH_FIX fixed point math library. |
|---|
| 53 | * |
|---|
| 54 | ***************************************************************************/ |
|---|
| 55 | #ifndef BMTH_FIX_H__ |
|---|
| 56 | #define BMTH_FIX_H__ |
|---|
| 57 | |
|---|
| 58 | #ifdef __cplusplus |
|---|
| 59 | extern "C" { |
|---|
| 60 | #endif |
|---|
| 61 | |
|---|
| 62 | #ifndef BMTH_INLINE |
|---|
| 63 | #define BMTH_INLINE |
|---|
| 64 | #endif |
|---|
| 65 | |
|---|
| 66 | #include "bstd.h" |
|---|
| 67 | #include "bmth_fix_priv.h" |
|---|
| 68 | |
|---|
| 69 | /*************************************************************************** |
|---|
| 70 | Summary: |
|---|
| 71 | convert one fixed point value to another |
|---|
| 72 | ***************************************************************************/ |
|---|
| 73 | BMTH_INLINE uint32_t BMTH_FIX_SIGNED_CONVERT(uint32_t x, uint32_t inint, uint32_t infract, uint32_t outint, uint32_t outfract); |
|---|
| 74 | |
|---|
| 75 | /*************************************************************************** |
|---|
| 76 | Summary: |
|---|
| 77 | convert fixed point to integer |
|---|
| 78 | ***************************************************************************/ |
|---|
| 79 | #define BMTH_FIX_SIGNED_FIXTOI(x, intbits, fractbits) \ |
|---|
| 80 | (((x) & BMTH_P_FIX_SIGN_BIT(intbits, fractbits)) ? \ |
|---|
| 81 | ((x) >> (fractbits) | ~BMTH_P_FIX_SIGNED_MASK(intbits, fractbits)) : \ |
|---|
| 82 | ((x) >> (fractbits))) |
|---|
| 83 | |
|---|
| 84 | /*************************************************************************** |
|---|
| 85 | Summary: |
|---|
| 86 | convert integer to fixed point |
|---|
| 87 | ***************************************************************************/ |
|---|
| 88 | #define BMTH_FIX_SIGNED_ITOFIX(x, intbits, fractbits) \ |
|---|
| 89 | (((x) << fractbits) & BMTH_P_FIX_SIGNED_MASK(intbits, fractbits)) |
|---|
| 90 | |
|---|
| 91 | /*************************************************************************** |
|---|
| 92 | Summary: |
|---|
| 93 | convert float to fixed point |
|---|
| 94 | ***************************************************************************/ |
|---|
| 95 | #define BMTH_FIX_SIGNED_FTOFIX(x, intbits, fractbits) \ |
|---|
| 96 | ((int32_t)(((x) * (1 << fractbits)) + ((x > 0) ? 0.5f : -0.5f)) & BMTH_P_FIX_SIGNED_MASK(intbits, fractbits)) |
|---|
| 97 | |
|---|
| 98 | /*************************************************************************** |
|---|
| 99 | Summary: |
|---|
| 100 | convert fixed to float point |
|---|
| 101 | ***************************************************************************/ |
|---|
| 102 | #define BMTH_FIX_SIGNED_FIXTOF(x, intbits, fractbits) \ |
|---|
| 103 | ((int32_t)((BMTH_P_FIX_SIGN_BIT(intbits, fractbits) & x) ? \ |
|---|
| 104 | -((~BMTH_P_FIX_SIGN_BIT(intbits, fractbits) & ~x) + 1) : x ) / (float)(1 << fractbits)) |
|---|
| 105 | |
|---|
| 106 | /*************************************************************************** |
|---|
| 107 | Summary: |
|---|
| 108 | fixed point operation multiply |
|---|
| 109 | ***************************************************************************/ |
|---|
| 110 | BMTH_INLINE uint32_t BMTH_FIX_SIGNED_MUL(uint32_t x, uint32_t y, uint32_t xint, uint32_t xfract, uint32_t yint, uint32_t yfract, uint32_t outint, uint32_t outfract); |
|---|
| 111 | |
|---|
| 112 | /*************************************************************************** |
|---|
| 113 | Summary: |
|---|
| 114 | fixed point operation divide |
|---|
| 115 | ***************************************************************************/ |
|---|
| 116 | #define BMTH_FIX_SIGNED_DIV(x, y, xint, xfract, yint, yfract, outint, outfract) \ |
|---|
| 117 | ( \ |
|---|
| 118 | (uint32_t)(((outfract > ((xfract) - (yfract))) ? \ |
|---|
| 119 | ((int32_t)BMTH_FIX_SIGNED_CONVERT(x, xint, xfract, BMTH_P_FIX_SIGNED_MAX_BITS - (xfract), xfract) << (outfract - (xfract - yfract))) : \ |
|---|
| 120 | ((int32_t)BMTH_FIX_SIGNED_CONVERT(x, xint, xfract, BMTH_P_FIX_SIGNED_MAX_BITS - (xfract), xfract) >> ((xfract - yfract) - outfract))) / \ |
|---|
| 121 | (int32_t)BMTH_FIX_SIGNED_CONVERT(y, yint, yfract, BMTH_P_FIX_SIGNED_MAX_BITS - yfract, yfract)) \ |
|---|
| 122 | ) |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | /*************************************************************************** |
|---|
| 126 | Summary: |
|---|
| 127 | fixed point operation modulus |
|---|
| 128 | ***************************************************************************/ |
|---|
| 129 | uint32_t BMTH_FIX_SIGNED_MOD(uint32_t x, uint32_t y, uint32_t xint, uint32_t xfract, uint32_t yint, uint32_t yfract, uint32_t outint, uint32_t outfract); |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | /*************************************************************************** |
|---|
| 133 | Summary: |
|---|
| 134 | fixed point log 2 |
|---|
| 135 | ***************************************************************************/ |
|---|
| 136 | BMTH_INLINE uint32_t BMTH_FIX_LOG2(uint32_t x); |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | /*************************************************************************** |
|---|
| 140 | Summary: |
|---|
| 141 | gets pi in fixed point format |
|---|
| 142 | ****************************************************************************/ |
|---|
| 143 | #define BMTH_FIX_SIGNED_GET_PI(intbits, fractbits) \ |
|---|
| 144 | ((BMTH_P_FIX_UNSIGNED_PI >> (BMTH_P_FIX_UNSIGNED_RAD_FRACT_BITS - fractbits)) & \ |
|---|
| 145 | BMTH_P_FIX_SIGNED_MASK(intbits, fractbits)) |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | /*************************************************************************** |
|---|
| 149 | Summary: |
|---|
| 150 | gets pi/2 in fixed point format |
|---|
| 151 | ****************************************************************************/ |
|---|
| 152 | #define BMTH_FIX_SIGNED_GET_HALF_PI(intbits, fractbits) \ |
|---|
| 153 | (BMTH_FIX_SIGNED_GET_PI(intbits, fractbits) / 2) |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | /*************************************************************************** |
|---|
| 157 | Summary: |
|---|
| 158 | converts a radian to a value between -2 pi and 2 pi |
|---|
| 159 | ****************************************************************************/ |
|---|
| 160 | #define BMTH_FIX_SIGNED_RADTO2PI(x, intbits, fractbits, outint, outfract) \ |
|---|
| 161 | (BMTH_FIX_SIGNED_MOD(x, (2 * BMTH_FIX_SIGNED_GET_PI(outint, outfract)), \ |
|---|
| 162 | intbits, fractbits, \ |
|---|
| 163 | BMTH_P_FIX_SIGNED_RAD_INT_BITS, BMTH_P_FIX_SIGNED_RAD_FRACT_BITS, \ |
|---|
| 164 | outint, outfract)) |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | /*************************************************************************** |
|---|
| 168 | Summary: |
|---|
| 169 | sin, with linear interpolation |
|---|
| 170 | ****************************************************************************/ |
|---|
| 171 | uint32_t BMTH_FIX_SIGNED_SIN(uint32_t x, uint32_t xint, uint32_t xfract, uint32_t sinint, uint32_t sinfract); |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | /*************************************************************************** |
|---|
| 175 | Summary: |
|---|
| 176 | cos, with linear interpolation |
|---|
| 177 | ****************************************************************************/ |
|---|
| 178 | uint32_t BMTH_FIX_SIGNED_COS(uint32_t x, uint32_t xint, uint32_t xfract, uint32_t sinint, uint32_t sinfract); |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | /*************************************************************************** |
|---|
| 182 | Summary: |
|---|
| 183 | finds the minimum number of bits needed to represent integer value |
|---|
| 184 | ****************************************************************************/ |
|---|
| 185 | BMTH_INLINE uint32_t BMTH_P_FIX_SIGNED_MININTBITS(uint32_t x, uint32_t intbits, uint32_t fractbits); |
|---|
| 186 | |
|---|
| 187 | #ifdef __cplusplus |
|---|
| 188 | } |
|---|
| 189 | #endif |
|---|
| 190 | |
|---|
| 191 | #endif /* #ifndef BMTH_FIX_H__ */ |
|---|
| 192 | /* End of File */ |
|---|