2 * qmath functions used in arithmatic and DSP operations where
3 * fractional operations, saturation support is needed.
5 * Copyright (C) 2009, Broadcom Corporation
8 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Broadcom Corporation;
9 * the contents of this file may not be disclosed to third parties, copied
10 * or duplicated in any form, in whole or in part, without the prior
11 * written permission of Broadcom Corporation.
13 * $Id: qmath.c,v 1.3 2008/01/12 05:06:51 Exp $
19 Description: This function saturate input 32 bit number into a 16 bit number.
20 If input number is greater than 0x7fff then output is saturated to 0x7fff.
21 else if input number is less than 0xffff8000 then output is saturated to 0xffff8000
22 else output is same as input.
28 if (op
> (int32
)0x7fff) {
31 else if (op
< (int32
)0xffff8000) {
32 result
= (int16
)(0x8000);
41 Description: This function multiply two input 16 bit numbers and return the 32 bit result.
42 This multiplication is similar to compiler multiplication. This operation is defined if
43 16 bit multiplication on the processor platform is cheaper than 32 bit multiplication (as
44 the most of qmath functions can be replaced with processor intrinsic instructions).
47 qm_mul321616(int16 op1
, int16 op2
)
49 return ((int32
)(op1
)*(int32
)(op2
));
53 Description: This function make 16 bit multiplication and return the result in 16 bits.
54 To fit the result into 16 bits the 32 bit multiplication result is right
58 qm_mul16(int16 op1
, int16 op2
)
61 result
= ((int32
)(op1
)*(int32
)(op2
));
62 return ((int16
)(result
>> 16));
66 Description: This function multiply two 16 bit numbers and return the result in 32 bits.
67 This function remove the extra sign bit created by the multiplication by leftshifting the
68 32 bit multiplication result by 1 bit before returning the result. So the output is
69 twice that of compiler multiplication. (i.e. qm_muls321616(2,3)=12).
70 When both input 16 bit numbers are 0x8000, then the result is saturated to 0x7fffffff.
73 qm_muls321616(int16 op1
, int16 op2
)
76 if (op1
== (int16
)(0x8000) && op2
== (int16
)(0x8000)) {
80 result
= ((int32
)(op1
)*(int32
)(op2
));
87 Description: This function make 16 bit unsigned multiplication. To fit the output into
88 16 bits the 32 bit multiplication result is right shifted by 16 bits.
91 qm_mulu16(uint16 op1
, uint16 op2
)
93 return (uint16
)(((uint32
)op1
* (uint32
)op2
)>>16);
97 Description: This function make 16 bit multiplication and return the result in 16 bits.
98 To fit the multiplication result into 16 bits the multiplication result is right shifted by
99 15 bits. Right shifting 15 bits instead of 16 bits is done to remove the extra sign bit formed
100 due to the multiplication.
101 When both the 16bit inputs are 0x8000 then the output is saturated to 0x7fffffff.
104 qm_muls16(int16 op1
, int16 op2
)
107 if (op1
== (int16
)0x8000 && op2
== (int16
)0x8000) {
111 result
= ((int32
)(op1
)*(int32
)(op2
));
113 return ((int16
)(result
>> 15));
117 Description: This function add two 32 bit numbers and return the 32bit result.
118 If the result overflow 32 bits, the output will be saturated to 32bits.
121 qm_add32(int32 op1
, int32 op2
)
125 if (op1
< 0 && op2
< 0 && result
> 0) {
127 } else if (op1
> 0 && op2
> 0 && result
< 0) {
134 Description: This function add two 16 bit numbers and return the 16bit result.
135 If the result overflow 16 bits, the output will be saturated to 16bits.
138 qm_add16(int16 op1
, int16 op2
)
141 int32 temp
= (int32
)op1
+ (int32
)op2
;
142 if (temp
> (int32
)0x7fff) {
143 result
= (int16
)0x7fff;
144 } else if (temp
< (int32
)0xffff8000) {
145 result
= (int16
)0xffff8000;
147 result
= (int16
) temp
;
153 Description: This function make 16 bit subtraction and return the 16bit result.
154 If the result overflow 16 bits, the output will be saturated to 16bits.
157 qm_sub16(int16 op1
, int16 op2
)
160 int32 temp
= (int32
)op1
- (int32
)op2
;
161 if (temp
> (int32
)0x7fff) {
162 result
= (int16
)0x7fff;
163 } else if (temp
< (int32
)0xffff8000) {
164 result
= (int16
)0xffff8000;
166 result
= (int16
) temp
;
172 Description: This function make 32 bit subtraction and return the 32bit result.
173 If the result overflow 32 bits, the output will be saturated to 32bits.
176 qm_sub32(int32 op1
, int32 op2
)
180 if (op1
>= 0 && op2
< 0 && result
< 0) {
183 else if (op1
< 0 && op2
> 0 && result
> 0) {
190 Description: This function multiply input 16 bit numbers and accumulate the result
191 into the input 32 bit number and return the 32 bit accumulated result.
192 If the accumulation result in overflow, then the output will be saturated.
195 qm_mac321616(int32 acc
, int16 op1
, int16 op2
)
198 result
= qm_add32(acc
, qm_mul321616(op1
, op2
));
203 Description: This function make a 32 bit saturated left shift when the specified shift
204 is +ve. This function will make a 32 bit right shift when the specified shift is -ve.
205 This function return the result after shifting operation.
208 qm_shl32(int32 op
, int shift
)
213 if (shift
> 31) shift
= 31;
214 else if (shift
< -31) shift
= -31;
216 for (i
= 0; i
< shift
; i
++) {
217 result
= qm_add32(result
, result
);
221 result
= result
>> (-shift
);
227 Description: This function make a 32 bit right shift when shift is +ve.
228 This function make a 32 bit saturated left shift when shift is -ve. This function
229 return the result of the shift operation.
232 qm_shr32(int32 op
, int shift
)
234 return qm_shl32(op
, -shift
);
238 Description: This function make a 16 bit saturated left shift when the specified shift
239 is +ve. This function will make a 16 bit right shift when the specified shift is -ve.
240 This function return the result after shifting operation.
243 qm_shl16(int16 op
, int shift
)
248 if (shift
> 15) shift
= 15;
249 else if (shift
< -15) shift
= -15;
251 for (i
= 0; i
< shift
; i
++) {
252 result
= qm_add16(result
, result
);
256 result
= result
>> (-shift
);
262 Description: This function make a 16 bit right shift when shift is +ve.
263 This function make a 16 bit saturated left shift when shift is -ve. This function
264 return the result of the shift operation.
267 qm_shr16(int16 op
, int shift
)
269 return qm_shl16(op
, -shift
);
273 Description: This function return the number of redundant sign bits in a 16 bit number.
274 Example: qm_norm16(0x0080) = 7.
279 uint16 u16extraSignBits
;
284 u16extraSignBits
= 0;
285 while ((op
>> 15) == (op
>> 14)) {
290 return u16extraSignBits
;
294 Description: This function return the number of redundant sign bits in a 32 bit number.
295 Example: qm_norm32(0x00000080) = 23
300 uint16 u16extraSignBits
;
305 u16extraSignBits
= 0;
306 while ((op
>> 31) == (op
>> 30)) {
311 return u16extraSignBits
;
315 Description: This function divide two 16 bit unsigned numbers.
316 The numerator should be less than denominator. So the quotient is always less than 1.
317 This function return the quotient in q.15 format.
320 qm_div_s(int16 num
, int16 denom
)
327 L_denom
= (denom
) << 15;
328 for (iteration
= 0; iteration
< 15; iteration
++) {
330 if (L_num
>= L_denom
) {
331 L_num
= qm_sub32(L_num
, L_denom
);
332 L_num
= qm_add32(L_num
, 1);
335 var_out
= (int16
)(L_num
& 0x7fff);
340 Description: This function compute the absolute value of a 16 bit number.
346 if (op
== (int16
)0xffff8000) {
358 Description: This function divide two 16 bit numbers.
359 The quotient is returned through return value.
360 The qformat of the quotient is returned through the pointer (qQuotient) passed
361 to this function. The qformat of quotient is adjusted appropriately such that
362 the quotient occupies all 16 bits.
365 qm_div16(int16 num
, int16 denom
, int16
*qQuotient
)
371 denom
= qm_abs16(denom
);
372 nNum
= qm_norm16(num
);
373 nDenom
= qm_norm16(denom
);
374 num
= qm_shl16(num
, nNum
-1);
375 denom
= qm_shl16(denom
, nDenom
);
376 *qQuotient
= nNum
-1-nDenom
+15;
378 return qm_div_s(num
, denom
);
381 return -qm_div_s(num
, denom
);
386 Description: This function compute absolute value of a 32 bit number.
392 if (op
== (int32
)0x80000000) {
404 Description: This function divide two 32 bit numbers. The division is performed
405 by considering only important 16 bits in 32 bit numbers.
406 The quotient is returned through return value.
407 The qformat of the quotient is returned through the pointer (qquotient) passed
408 to this function. The qformat of quotient is adjusted appropriately such that
409 the quotient occupies all 16 bits.
412 qm_div163232(int32 num
, int32 denom
, int16
*qquotient
)
418 denom
= qm_abs32(denom
);
419 nNum
= qm_norm32(num
);
420 nDenom
= qm_norm32(denom
);
421 num
= qm_shl32(num
, nNum
- 1);
422 denom
= qm_shl32(denom
, nDenom
);
423 *qquotient
= nNum
- 1 - nDenom
+ 15;
425 return qm_div_s((int16
)(num
>> 16), (int16
)(denom
>> 16));
428 return -qm_div_s((int16
)(num
>> 16), (int16
)(denom
>> 16));
433 Description: This function multiply a 32 bit number with a 16 bit number.
434 The multiplicaton result is right shifted by 16 bits to fit the result
438 qm_mul323216(int32 op1
, int16 op2
)
444 lo
= (int16
)(op1
& 0xffff);
445 result
= qm_mul321616(hi
, op2
);
446 result
= result
+ (qm_mulsu321616(op2
, lo
) >> 16);
451 Description: This function multiply signed 16 bit number with unsigned 16 bit number and return
452 the result in 32 bits.
455 qm_mulsu321616(int16 op1
, uint16 op2
)
457 return (int32
)(op1
) * op2
;
461 Description: This function multiply 32 bit number with 16 bit number. The multiplication result is
462 right shifted by 15 bits to fit the result into 32 bits. Right shifting by only 15 bits instead of
463 16 bits is done to remove the extra sign bit formed by multiplication from the return value.
464 When the input numbers are 0x80000000, 0x8000 the return value is saturated to 0x7fffffff.
467 qm_muls323216(int32 op1
, int16 op2
)
473 lo
= (int16
)(op1
& 0xffff);
474 result
= qm_muls321616(hi
, op2
);
475 result
= qm_add32(result
, (qm_mulsu321616(op2
, lo
) >> 15));
480 Description: This function multiply two 32 bit numbers. The multiplication result is right
481 shifted by 32 bits to fit the multiplication result into 32 bits. The right shifted
482 multiplication result is returned as output.
485 qm_mul32(int32 a
, int32 b
)
492 lo1
= (uint16
)(a
& 0xffff);
493 lo2
= (uint16
)(b
& 0xffff);
494 result
= qm_mul321616(hi1
, hi2
);
495 result
= result
+ (qm_mulsu321616(hi1
, lo2
) >> 16);
496 result
= result
+ (qm_mulsu321616(hi2
, lo1
) >> 16);
501 Description: This function multiply two 32 bit numbers. The multiplication result is
502 right shifted by 31 bits to fit the multiplication result into 32 bits. The right
503 shifted multiplication result is returned as output. Right shifting by only 31 bits
504 instead of 32 bits is done to remove the extra sign bit formed by multiplication.
505 When the input numbers are 0x80000000, 0x80000000 the return value is saturated to
509 qm_muls32(int32 a
, int32 b
)
516 lo1
= (uint16
)(a
& 0xffff);
517 lo2
= (uint16
)(b
& 0xffff);
518 result
= qm_muls321616(hi1
, hi2
);
519 result
= qm_add32(result
, (qm_mulsu321616(hi1
, lo2
) >> 15));
520 result
= qm_add32(result
, (qm_mulsu321616(hi2
, lo1
) >> 15));
521 result
= qm_add32(result
, (qm_mulu16(lo1
, lo2
) >> 15));
525 /* This table is log2(1+(i/32)) where i=[0:1:31], in q.15 format */
526 int16 log_table
[] = {
560 #define LOG_TABLE_SIZE 32 /* log_table size */
561 #define LOG2_LOG_TABLE_SIZE 5 /* log2(log_table size) */
562 #define Q_LOG_TABLE 15 /* qformat of log_table */
563 #define LOG10_2 19728 /* log10(2) in q.16 */
567 This routine takes the input number N and its q format qN and compute
568 the log10(N). This routine first normalizes the input no N. Then N is in mag*(2^x) format.
569 mag is any number in the range 2^30-(2^31 - 1). Then log2(mag * 2^x) = log2(mag) + x is computed.
570 From that log10(mag * 2^x) = log2(mag * 2^x) * log10(2) is computed.
571 This routine looks the log2 value in the table considering LOG2_LOG_TABLE_SIZE+1 MSBs.
572 As the MSB is always 1, only next LOG2_OF_LOG_TABLE_SIZE MSBs are used for table lookup.
573 Next 16 MSBs are used for interpolation.
575 N - number to which log10 has to be found.
577 log10N - address where log10(N) will be written.
578 qLog10N - address where log10N qformat will be written.
580 For accurate results input should be in normalized or near normalized form.
583 qm_log10(int32 N
, int16 qN
, int16
*log10N
, int16
*qLog10N
)
585 int16 s16norm
, s16tableIndex
, s16errorApproximation
;
589 /* Logerithm of negative values is undefined.
590 * assert N is greater than 0.
594 /* normalize the N. */
595 s16norm
= qm_norm32(N
);
598 /* The qformat of N after normalization.
599 * -30 is added to treat the no as between 1.0 to 2.0
600 * i.e. after adding the -30 to the qformat the decimal point will be
601 * just rigtht of the MSB. (i.e. after sign bit and 1st MSB). i.e.
602 * at the right side of 30th bit.
604 qN
= qN
+ s16norm
- 30;
606 /* take the table index as the LOG2_OF_LOG_TABLE_SIZE bits right of the MSB */
607 s16tableIndex
= (int16
)(N
>> (32-(2+LOG2_LOG_TABLE_SIZE
)));
609 /* remove the MSB. the MSB is always 1 after normalization. */
610 s16tableIndex
= s16tableIndex
& (int16
)((1<<LOG2_LOG_TABLE_SIZE
)-1);
612 /* remove the (1+LOG2_OF_LOG_TABLE_SIZE) MSBs in the N. */
613 N
= N
& ((1<<(32-(2+LOG2_LOG_TABLE_SIZE
))) - 1);
615 /* take the offset as the 16 MSBS after table index.
617 u16offset
= (uint16
)(N
>> (32-(2+LOG2_LOG_TABLE_SIZE
+16)));
619 /* look the log value in the table. */
620 s32log
= log_table
[s16tableIndex
]; /* q.15 format */
622 /* interpolate using the offset. */
623 s16errorApproximation
= (int16
)qm_mulu16(u16offset
, \
624 (uint16
)(log_table
[s16tableIndex
+1]-log_table
[s16tableIndex
])); /* q.15 */
626 s32log
= qm_add16((int16
)s32log
, s16errorApproximation
); /* q.15 format */
628 /* adjust for the qformat of the N as
629 * log2(mag * 2^x) = log2(mag) + x
631 s32log
= qm_add32(s32log
, ((int32
)-qN
)<<15); /* q.15 format */
633 /* normalize the result. */
634 s16norm
= qm_norm32(s32log
);
636 /* bring all the important bits into lower 16 bits */
637 s32log
= qm_shl32(s32log
, s16norm
-16); /* q.15+s16norm-16 format */
639 /* compute the log10(N) by multiplying log2(N) with log10(2).
640 * as log10(mag * 2^x) = log2(mag * 2^x) * log10(2)
641 * log10N in q.15+s16norm-16+1 (LOG10_2 is in q.16)
643 *log10N
= qm_muls16((int16
)s32log
, (int16
)LOG10_2
);
645 /* write the q format of the result. */
646 *qLog10N
= 15 + s16norm
- 16 + 1;
653 This routine compute 1/N.
654 This routine reformates the given no N as N * 2^qN where N is in between 0.5 and 1.0
655 in q.15 format in 16 bits. So the problem now boils down to finding the inverse of a
656 q.15 no in 16 bits which is in the range of 0.5 to 1.0. The output is always between
657 2.0 to 1. So the output is 2.0 to 1.0 in q.30 format. Once the final output format is found
658 by taking the qN into account. Inverse is found with newton rapson method. Initially
659 inverse (x) is guessed as 1/0.75 (with appropriate sign). The new guess is calculated
660 using the formula x' = 2*x - N*x*x. After 4 or 5 iterations the inverse is very close to
663 N - number to which 1/N has to be found.
665 sqrtN - address where 1/N has to be written.
666 qsqrtN - address where q format of 1/N has to be written.
670 qm_1byN(int32 N
, int16 qN
, int32
*result
, int16
*qResult
)
673 int32 s32firstTerm
, s32secondTerm
, x
;
676 normN
= qm_norm32(N
);
678 /* limit N to least significant 16 bits. 15th bit is the sign bit. */
679 N
= qm_shl32(N
, normN
-16);
680 qN
= qN
+ normN
- 16 - 15;
681 /* -15 is added to treat N as 16 bit q.15 number in the range from 0.5 to 1 */
683 /* Take the initial guess as 1/0.75 in qx format with appropriate sign. */
686 x
= (int32
)((1/0.75)*(1<<qx
));
687 /* input no is in the range 0.5 to 1. So 1/0.75 is taken as initial guess. */
691 x
= (int32
)((1/-0.75)*(1<<qx
));
692 /* input no is in the range -0.5 to -1. So 1/-0.75 is taken as initial guess. */
695 /* iterate the equation x = 2*x - N*x*x for 4 times. */
696 for (i
= 0; i
< 4; i
++)
698 s32firstTerm
= qm_shl32(x
, 1); /* s32firstTerm = 2*x in q.29 */
699 s32secondTerm
= qm_muls321616((int16
)(s32firstTerm
>>16), (int16
)(s32firstTerm
>>16));
700 /* s32secondTerm = x*x in q.(29+1-16)*2+1 */
701 s32secondTerm
= qm_muls321616((int16
)(s32secondTerm
>>16), (int16
)N
);
702 /* s32secondTerm = N*x*x in q.((29+1-16)*2+1)-16+15+1 i.e. in q.29 */
703 x
= qm_sub32(s32firstTerm
, s32secondTerm
);
704 /* can be added directly as both are in q.29 */
707 /* Bring the x to q.30 format. */
708 *result
= qm_shl32(x
, 1);
709 /* giving the output in q.30 format for q.15 input in 16 bits. */
711 /* compute the final q format of the result. */
712 *qResult
= -qN
+ 30; /* adjusting the q format of actual output */