123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730 |
- /*___________________________________________________________________________
- | |
- | Function Name : extract_h |
- | |
- | Purpose : |
- | |
- | Return the 16 MSB of L_var1. |
- | |
- | Complexity weight : 1 |
- | |
- | Inputs : |
- | |
- | L_var1 |
- | 32 bit long signed integer (Word32 ) whose value falls in the |
- | range : 0x8000 0000 <= L_var1 <= 0x7fff ffff. |
- | |
- | Outputs : |
- | |
- | none |
- | |
- | Return Value : |
- | |
- | var_out |
- | 16 bit short signed integer (Word16) whose value falls in the |
- | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
- |___________________________________________________________________________|
- */
- PJ_INLINE(Word16) extract_h (Word32 L_var1)
- {
- return (Word16) (L_var1 >> 16);
- }
- /* ------------------------- End of extract_h() ------------------------- */
- /*___________________________________________________________________________
- | |
- | Function Name : extract_l |
- | |
- | Purpose : |
- | |
- | Return the 16 LSB of L_var1. |
- | |
- | Complexity weight : 1 |
- | |
- | Inputs : |
- | |
- | L_var1 |
- | 32 bit long signed integer (Word32 ) whose value falls in the |
- | range : 0x8000 0000 <= L_var1 <= 0x7fff ffff. |
- | |
- | Outputs : |
- | |
- | none |
- | |
- | Return Value : |
- | |
- | var_out |
- | 16 bit short signed integer (Word16) whose value falls in the |
- | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
- |___________________________________________________________________________|
- */
- PJ_INLINE(Word16) extract_l (Word32 L_var1)
- {
- return (Word16) L_var1;
- }
- /* ------------------------- End of extract_l() ------------------------- */
- /*___________________________________________________________________________
- | |
- | Function Name : saturate |
- | |
- | Purpose : |
- | |
- | Limit the 32 bit input to the range of a 16 bit word. |
- | |
- | Inputs : |
- | |
- | L_var1 |
- | 32 bit long signed integer (Word32) whose value falls in the |
- | range : 0x8000 0000 <= L_var1 <= 0x7fff ffff. |
- | |
- | Outputs : |
- | |
- | none |
- | |
- | Return Value : |
- | |
- | var_out |
- | 16 bit short signed integer (Word16) whose value falls in the |
- | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
- |___________________________________________________________________________|
- */
- PJ_INLINE(Word16) saturate (Word32 L_var1)
- {
- Word16 val16 = (Word16) L_var1;
- if (val16 == L_var1)
- return val16;
- if (L_var1 > MAX_16)
- return MAX_16;
- return MIN_16;
- }
- /* ------------------------- End of saturate() ------------------------- */
- #if PJ_HAS_INT64
- PJ_INLINE(Word32) L_saturate (pj_int64_t LL_var1)
- {
- pj_int32_t L_var1 = (pj_int32_t)LL_var1;
- if (LL_var1 == L_var1)
- return L_var1;
- else if (LL_var1 > MAX_32)
- return MAX_32;
- else
- return MIN_32;
- }
- #endif
- /*___________________________________________________________________________
- | |
- | Function Name : add |
- | |
- | Purpose : |
- | |
- | Performs the addition (var1+var2) with overflow control and saturation;|
- | the 16 bit result is set at +32767 when overflow occurs or at -32768 |
- | when underflow occurs. |
- | |
- | Complexity weight : 1 |
- | |
- | Inputs : |
- | |
- | var1 |
- | 16 bit short signed integer (Word16) whose value falls in the |
- | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
- | |
- | var2 |
- | 16 bit short signed integer (Word16) whose value falls in the |
- | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
- | |
- | Outputs : |
- | |
- | none |
- | |
- | Return Value : |
- | |
- | var_out |
- | 16 bit short signed integer (Word16) whose value falls in the |
- | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
- |___________________________________________________________________________|
- */
- PJ_INLINE(Word16) add (Word16 var1, Word16 var2)
- {
- return saturate (var1 + var2);
- }
- /* ------------------------- End of add() ------------------------- */
- /*___________________________________________________________________________
- | |
- | Function Name : sub |
- | |
- | Purpose : |
- | |
- | Performs the subtraction (var1+var2) with overflow control and satu- |
- | ration; the 16 bit result is set at +32767 when overflow occurs or at |
- | -32768 when underflow occurs. |
- | |
- | Complexity weight : 1 |
- | |
- | Inputs : |
- | |
- | var1 |
- | 16 bit short signed integer (Word16) whose value falls in the |
- | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
- | |
- | var2 |
- | 16 bit short signed integer (Word16) whose value falls in the |
- | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
- | |
- | Outputs : |
- | |
- | none |
- | |
- | Return Value : |
- | |
- | var_out |
- | 16 bit short signed integer (Word16) whose value falls in the |
- | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
- |___________________________________________________________________________|
- */
- PJ_INLINE(Word16) sub (Word16 var1, Word16 var2)
- {
- return saturate ((Word32) var1 - var2);
- }
- /* ------------------------- End of sub() ------------------------- */
- /*___________________________________________________________________________
- | |
- | Function Name : negate |
- | |
- | Purpose : |
- | |
- | Negate var1 with saturation, saturate in the case where input is -32768:|
- | negate(var1) = sub(0,var1). |
- | |
- | Complexity weight : 1 |
- | |
- | Inputs : |
- | |
- | var1 |
- | 16 bit short signed integer (Word16) whose value falls in the |
- | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
- | |
- | Outputs : |
- | |
- | none |
- | |
- | Return Value : |
- | |
- | var_out |
- | 16 bit short signed integer (Word16) whose value falls in the |
- | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
- |___________________________________________________________________________|
- */
- PJ_INLINE(Word16) negate (Word16 var1)
- {
- return (Word16)((var1 == MIN_16) ? MAX_16 : -var1);
- }
- /* ------------------------- End of negate() ------------------------- */
- /*___________________________________________________________________________
- | |
- | Function Name : L_add |
- | |
- | Purpose : |
- | |
- | 32 bits addition of the two 32 bits variables (L_var1+L_var2) with |
- | overflow control and saturation; the result is set at +2147483647 when |
- | overflow occurs or at -2147483648 when underflow occurs. |
- | |
- | Complexity weight : 2 |
- | |
- | Inputs : |
- | |
- | L_var1 32 bit long signed integer (Word32) whose value falls in the |
- | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
- | |
- | L_var2 32 bit long signed integer (Word32) whose value falls in the |
- | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
- | |
- | Outputs : |
- | |
- | none |
- | |
- | Return Value : |
- | |
- | L_var_out |
- | 32 bit long signed integer (Word32) whose value falls in the |
- | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
- |___________________________________________________________________________|
- */
- PJ_INLINE(Word32) L_add (Word32 L_var1, Word32 L_var2)
- {
- #if PJ_HAS_INT64
- return L_saturate(((pj_int64_t)L_var1) + L_var2);
- #else
- Word32 L_var_out;
- L_var_out = L_var1 + L_var2;
- if (((L_var1 ^ L_var2) & MIN_32) == 0)
- {
- if ((L_var_out ^ L_var1) & MIN_32)
- {
- SET_OVERFLOW(1);
- L_var_out = (L_var1 < 0) ? MIN_32 : MAX_32;
- }
- }
- return (L_var_out);
- #endif
- }
- /* ------------------------- End of L_add() ------------------------- */
- /*___________________________________________________________________________
- | |
- | Function Name : L_sub |
- | |
- | Purpose : |
- | |
- | 32 bits subtraction of the two 32 bits variables (L_var1-L_var2) with |
- | overflow control and saturation; the result is set at +2147483647 when |
- | overflow occurs or at -2147483648 when underflow occurs. |
- | |
- | Complexity weight : 2 |
- | |
- | Inputs : |
- | |
- | L_var1 32 bit long signed integer (Word32) whose value falls in the |
- | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
- | |
- | L_var2 32 bit long signed integer (Word32) whose value falls in the |
- | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
- | |
- | Outputs : |
- | |
- | none |
- | |
- | Return Value : |
- | |
- | L_var_out |
- | 32 bit long signed integer (Word32) whose value falls in the |
- | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
- |___________________________________________________________________________|
- */
- PJ_INLINE(Word32) L_sub (Word32 L_var1, Word32 L_var2)
- {
- #if PJ_HAS_INT64
- return L_saturate((pj_int64_t)L_var1 - L_var2);
- #else
- Word32 L_var_out;
- L_var_out = L_var1 - L_var2;
- if (((L_var1 ^ L_var2) & MIN_32) != 0)
- {
- if ((L_var_out ^ L_var1) & MIN_32)
- {
- L_var_out = (L_var1 < 0L) ? MIN_32 : MAX_32;
- SET_OVERFLOW(1);
- }
- }
- return (L_var_out);
- #endif
- }
- /* ------------------------- End of L_sub() ------------------------- */
- /*___________________________________________________________________________
- | |
- | Function Name : L_mult |
- | |
- | Purpose : |
- | |
- | L_mult is the 32 bit result of the multiplication of var1 times var2 |
- | with one shift left i.e.: |
- | L_mult(var1,var2) = L_shl((var1 times var2),1) and |
- | L_mult(-32768,-32768) = 2147483647. |
- | |
- | Complexity weight : 1 |
- | |
- | Inputs : |
- | |
- | var1 |
- | 16 bit short signed integer (Word16) whose value falls in the |
- | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
- | |
- | var2 |
- | 16 bit short signed integer (Word16) whose value falls in the |
- | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
- | |
- | Outputs : |
- | |
- | none |
- | |
- | Return Value : |
- | |
- | L_var_out |
- | 32 bit long signed integer (Word32) whose value falls in the |
- | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
- |___________________________________________________________________________|
- */
- PJ_INLINE(Word32) L_mult (Word16 var1, Word16 var2)
- {
- Word32 L_var_out;
- L_var_out = (Word32) var1 *(Word32) var2;
- if (L_var_out != (Word32) 0x40000000L)
- {
- return L_var_out << 1;
- }
- else
- {
- SET_OVERFLOW(1);
- return MAX_32;
- }
- }
- /* ------------------------- End of L_mult() ------------------------- */
- /*___________________________________________________________________________
- | |
- | Function Name : L_mac |
- | |
- | Purpose : |
- | |
- | Multiply var1 by var2 and shift the result left by 1. Add the 32 bit |
- | result to L_var3 with saturation, return a 32 bit result: |
- | L_mac(L_var3,var1,var2) = L_add(L_var3,L_mult(var1,var2)). |
- | |
- | Complexity weight : 1 |
- | |
- | Inputs : |
- | |
- | L_var3 32 bit long signed integer (Word32) whose value falls in the |
- | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
- | |
- | var1 |
- | 16 bit short signed integer (Word16) whose value falls in the |
- | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
- | |
- | var2 |
- | 16 bit short signed integer (Word16) whose value falls in the |
- | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
- | |
- | Outputs : |
- | |
- | none |
- | |
- | Return Value : |
- | |
- | L_var_out |
- | 32 bit long signed integer (Word32) whose value falls in the |
- | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
- |___________________________________________________________________________|
- */
- PJ_INLINE(Word32) L_mac (Word32 L_var3, Word16 var1, Word16 var2)
- {
- return L_add (L_var3, L_mult (var1, var2));
- }
- /* ------------------------- End of L_mac() ------------------------- */
- /*___________________________________________________________________________
- | |
- | Function Name : round |
- | |
- | Purpose : |
- | |
- | Round the lower 16 bits of the 32 bit input number into the MS 16 bits |
- | with saturation. Shift the resulting bits right by 16 and return the 16 |
- | bit number: |
- | round(L_var1) = extract_h(L_add(L_var1,32768)) |
- | |
- | Complexity weight : 1 |
- | |
- | Inputs : |
- | |
- | L_var1 |
- | 32 bit long signed integer (Word32 ) whose value falls in the |
- | range : 0x8000 0000 <= L_var1 <= 0x7fff ffff. |
- | |
- | Outputs : |
- | |
- | none |
- | |
- | Return Value : |
- | |
- | var_out |
- | 16 bit short signed integer (Word16) whose value falls in the |
- | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
- |___________________________________________________________________________|
- */
- PJ_INLINE(Word16) itu_round (Word32 L_var1)
- {
- return extract_h (L_add (L_var1, (Word32) 0x00008000L));
- }
- /* ------------------------- End of round() ------------------------- */
- /*___________________________________________________________________________
- | |
- | Function Name : L_shr |
- | |
- | Purpose : |
- | |
- | Arithmetically shift the 32 bit input L_var1 right var2 positions with |
- | sign extension. If var2 is negative, arithmetically shift L_var1 left |
- | by -var2 and zero fill the -var2 LSB of the result. Saturate the result |
- | in case of underflows or overflows. |
- | |
- | Complexity weight : 2 |
- | |
- | Inputs : |
- | |
- | L_var1 32 bit long signed integer (Word32) whose value falls in the |
- | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
- | |
- | var2 |
- | 16 bit short signed integer (Word16) whose value falls in the |
- | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
- | |
- | Outputs : |
- | |
- | none |
- | |
- | Return Value : |
- | |
- | L_var_out |
- | 32 bit long signed integer (Word32) whose value falls in the |
- | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
- |___________________________________________________________________________|
- */
- PJ_INLINE(Word32) L_shr_nocheck(Word32 L_var1, Word16 var2)
- {
- #if 1
- return L_var1 >> var2;
- #else
- if (var2 >= 31)
- {
- return (L_var1 < 0L) ? -1 : 0;
- }
- else
- {
- if (L_var1 < 0)
- {
- return ~((~L_var1) >> var2);
- }
- else
- {
- return L_var1 >> var2;
- }
- }
- #endif
- }
- PJ_INLINE(Word32) L_shl_nocheck (Word32 L_var1, Word16 var2)
- {
- #if PJ_HAS_INT64
- return L_saturate( ((pj_int64_t)L_var1) << var2 );
- #else
- for (; var2 > 0; var2--)
- {
- if (L_var1 > (Word32) 0X3fffffffL)
- {
- SET_OVERFLOW(1);
- return MAX_32;
- }
- else
- {
- if (L_var1 < (Word32) 0xc0000000L)
- {
- SET_OVERFLOW(1);
- return MIN_32;
- }
- }
- L_var1 <<= 1;
- }
- return (L_var1);
- #endif
- }
- PJ_INLINE(Word32) L_shr (Word32 L_var1, Word16 var2)
- {
- if (var2 < 0)
- {
- if (var2 < -32)
- var2 = -32;
- return L_shl_nocheck (L_var1, (Word16) -var2);
- }
- else
- {
- return L_shr_nocheck(L_var1, var2);
- }
- }
- /* ------------------------- End of L_shr() ------------------------- */
- /*___________________________________________________________________________
- | |
- | Function Name : L_shl |
- | |
- | Purpose : |
- | |
- | Arithmetically shift the 32 bit input L_var1 left var2 positions. Zero |
- | fill the var2 LSB of the result. If var2 is negative, arithmetically |
- | shift L_var1 right by -var2 with sign extension. Saturate the result in |
- | case of underflows or overflows. |
- | |
- | Complexity weight : 2 |
- | |
- | Inputs : |
- | |
- | L_var1 32 bit long signed integer (Word32) whose value falls in the |
- | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
- | |
- | var2 |
- | 16 bit short signed integer (Word16) whose value falls in the |
- | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
- | |
- | Outputs : |
- | |
- | none |
- | |
- | Return Value : |
- | |
- | L_var_out |
- | 32 bit long signed integer (Word32) whose value falls in the |
- | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
- |___________________________________________________________________________|
- */
- PJ_INLINE(Word32) L_shl (Word32 L_var1, Word16 var2)
- {
- if (var2 <= 0)
- {
- if (var2 < -32)
- var2 = -32;
- return L_shr_nocheck(L_var1, (Word16) -var2);
- }
- else
- {
- return L_shl_nocheck(L_var1, var2);
- }
- }
- /* ------------------------- End of L_shl() ------------------------- */
- /*___________________________________________________________________________
- | |
- | Function Name : abs_s |
- | |
- | Purpose : |
- | |
- | Absolute value of var1; abs_s(-32768) = 32767. |
- | |
- | Complexity weight : 1 |
- | |
- | Inputs : |
- | |
- | var1 |
- | 16 bit short signed integer (Word16) whose value falls in the |
- | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
- | |
- | Outputs : |
- | |
- | none |
- | |
- | Return Value : |
- | |
- | var_out |
- | 16 bit short signed integer (Word16) whose value falls in the |
- | range : 0x0000 0000 <= var_out <= 0x0000 7fff. |
- |___________________________________________________________________________|
- */
- PJ_INLINE(Word16) abs_s (Word16 var1)
- {
- #if 1
- if (var1 >= 0)
- return var1;
- else if (var1 == MIN_16)
- return MAX_16;
- else
- return (Word16)-var1;
- #else
- if (var1 == MIN_16)
- {
- return MAX_16;
- }
- else
- {
- if (var1 < 0)
- {
- return (Word16)-var1;
- }
- else
- {
- return var1;
- }
- }
- #endif
- }
- /* ------------------------- End of abs_s() ------------------------- */
- PJ_INLINE(Word16) shl_nocheck(Word16 var1, Word16 var2)
- {
- #if 1
- /* blp: this should be more optimized */
- return saturate (((Word32)var1) << var2);
- #else
- /* Original algorithm */
- Word32 result = (Word32) var1 *((Word32) 1 << var2);
- if ((var2 > 15 && var1 != 0) || (result != (Word32) ((Word16) result)))
- {
- SET_OVERFLOW(1);
- return (Word16) ((var1 > 0) ? MAX_16 : MIN_16);
- }
- else
- {
- return extract_l (result);
- }
- #endif
- }
- PJ_INLINE(Word16) shr_nocheck(Word16 var1, Word16 var2)
- {
- #if 1
- /* blp: this should yield the same value */
- return (Word16) (var1 >> var2);
- #else
- /* Original algorithm */
- if (var2 >= 15)
- {
- return (Word16)((var1 < 0) ? -1 : 0);
- }
- else
- {
- if (var1 < 0)
- {
- return (Word16) (~((~var1) >> var2));
- }
- else
- {
- return (Word16)(var1 >> var2);
- }
- }
- #endif
- }
|