mathhalf.h
上传用户:csczyc
上传日期:2021-02-19
资源大小:1051k
文件大小:7k
- /*
- 2.4 kbps MELP Proposed Federal Standard speech coder
- Fixed-point C code, version 1.0
- Copyright (c) 1998, Texas Instruments, Inc.
- Texas Instruments has intellectual property rights on the MELP
- algorithm. The Texas Instruments contact for licensing issues for
- commercial and non-government use is William Gordon, Director,
- Government Contracts, Texas Instruments Incorporated, Semiconductor
- Group (phone 972 480 7442).
- The fixed-point version of the voice codec Mixed Excitation Linear
- Prediction (MELP) is based on specifications on the C-language software
- simulation contained in GSM 06.06 which is protected by copyright and
- is the property of the European Telecommunications Standards Institute
- (ETSI). This standard is available from the ETSI publication office
- tel. +33 (0)4 92 94 42 58. ETSI has granted a license to United States
- Department of Defense to use the C-language software simulation contained
- in GSM 06.06 for the purposes of the development of a fixed-point
- version of the voice codec Mixed Excitation Linear Prediction (MELP).
- Requests for authorization to make other use of the GSM 06.06 or
- otherwise distribute or modify them need to be addressed to the ETSI
- Secretariat fax: +33 493 65 47 16.
- */
- #include "typedefs.h"
- #ifndef __MATHHALF
- #define __MATHHALF
- /* addition */
- /************/
- Shortword add(Shortword var1, Shortword var2); /* 1 ops */
- /* Shortword sub(Shortword var1, Shortword var2); 1 ops */
- Longword L_add(Longword L_var1, Longword L_var2); /* 2 ops */
- Longword L_sub(Longword L_var1, Longword L_var2); /* 2 ops */
- __inline Shortword sub(Shortword var1, Shortword var2) /* 1 ops */
- {
- Longword L_diff;
- Shortword swOut;
- extern int saturation;
- L_diff = (Longword) var1 - var2;
- if (L_diff > SW_MAX) {
- swOut = SW_MAX;
- saturation = saturation + 1;
- }
- else if (L_diff < SW_MIN) {
- swOut = SW_MIN;
- saturation = saturation + 1;
- }
- else {
- swOut = (Shortword) L_diff; /* automatic type conversion */
- }
- return (swOut);
- }
- /* multiplication */
- /******************/
- Shortword mult(Shortword var1, Shortword var2); /* 1 ops */
- Longword L_mult(Shortword var1, Shortword var2); /* 1 ops */
- Shortword mult_r(Shortword var1, Shortword var2); /* 2 ops */
- /* arithmetic shifts */
- /*********************/
- Shortword shr(Shortword var1, Shortword var2); /* 1 ops */
- Shortword shl(Shortword var1, Shortword var2); /* 1 ops */
- Longword L_shr(Longword L_var1, Shortword var2); /* 2 ops */
- Longword L_shl(Longword L_var1, Shortword var2); /* 2 ops */
- Shortword shift_r(Shortword var, Shortword var2); /* 2 ops */
- Longword L_shift_r(Longword L_var, Shortword var2); /* 3 ops */
- __inline Shortword shl_a(Shortword var1, Shortword var2)
- {
- Shortword swOut;
- Longword L_Out;
- // extern int complexity;mark del
- extern int saturation;
- // int old_complexity;mark del
-
- // old_complexity = complexity;mark del
- if (var2 == 0 || var1 == 0) {
- swOut = var1;
- }
- else {
- /* var2 > 0 */
- if (var2 >= 15) {
- swOut = (var1 > 0) ? SW_MAX : SW_MIN; /* saturate */
- saturation = saturation + 1;
- }
- else {
- L_Out = (Longword) var1 *(1 << var2);
- swOut = (Shortword) L_Out; /* copy low portion to swOut,
- * overflow could have hpnd */
- if (swOut != L_Out) {
- /* overflow */
- swOut = (var1 > 0) ? SW_MAX : SW_MIN; /* saturate */
- saturation = saturation + 1;
- }
- }
- }
- // complexity = old_complexity + 1;mark del
- return (swOut);
- }
- __inline Shortword shr_a(Shortword var1, Shortword var2)
- {
- Shortword swMask,
- swOut;
- // extern int complexity;mark del
- extern int saturation;
- // int old_complexity;mark del
-
- // old_complexity = complexity;mark del
- if (var2 == 0 || var1 == 0)
- swOut = var1;
- else {
- /* positive shift count */
- /*----------------------*/
- if (var2 >= 15)
- swOut = (var1 < 0) ? (Shortword) 0xffff : 0x0;
- else {
- /* take care of sign extension */
- /*-----------------------------*/
- swMask = 0;
- if (var1 < 0) {
- swMask = ~swMask << (16 - var2);
- }
- var1 >>= var2;
- swOut = swMask | var1;
- }
- }
- // complexity = old_complexity + 1;mark del
- return (swOut);
- }
- /* absolute value */
- /*******************/
- Shortword abs_s(Shortword var1); /* 1 ops */
- Longword L_abs(Longword var1); /* 3 ops */
- /* multiply accumulate */
- /************************/
- Longword L_mac(Longword L_var3,
- Shortword var1, Shortword var2); /* 1 op */
- Shortword mac_r(Longword L_var3,
- Shortword var1, Shortword var2); /* 2 op */
- Longword L_msu(Longword L_var3,
- Shortword var1, Shortword var2); /* 1 op */
- Shortword msu_r(Longword L_var3,
- Shortword var1, Shortword var2); /* 2 op */
- /* negation */
- /*************/
- /* Shortword negate(Shortword var1); 1 ops */
- Longword L_negate(Longword L_var1); /* 2 ops */
- __inline Shortword negate(Shortword var1)
- {
- Shortword swOut;
- // extern int complexity;mark del
- extern int saturation;
- // int old_complexity;mark del
-
- // old_complexity = complexity;mark del
- if (var1 == SW_MIN) {
- saturation = saturation + 1;
- swOut = SW_MAX;
- }
- else {
- swOut = -var1;
- }
- // complexity = old_complexity + 1;mark del
- return (swOut);
- }
- /* Accumulator manipulation */
- /****************************/
- Longword L_deposit_l(Shortword var1); /* 1 ops */
- Longword L_deposit_h(Shortword var1); /* 1 ops */
- Shortword extract_l(Longword L_var1); /* 1 ops */
- Shortword extract_h(Longword L_var1); /* 1 ops */
- /* Round */
- /*********/
- Shortword round(Longword L_var1); /* 1 ops */
- /* Normalization */
- /*****************/
- Shortword norm_l(Longword L_var1); /* 30 ops */
- Shortword norm_s(Shortword var1); /* 15 ops */
- /* Division */
- /************/
- /* Shortword divide_s(Shortword var1, Shortword var2); 18 ops */
- __inline Shortword divide_s(Shortword var1, Shortword var2)
- {
- Longword L_div;
- Shortword swOut;
- extern int saturation;
- // extern int complexity;mark del
- // int old_complexity;mark del
-
- // old_complexity = complexity;Longword
- if (var1 < 0 || var2 < 0 || var1 > var2) {
- /* undefined output for invalid input into divide_s */
- return ((Shortword)0);
- }
- if (var1 == var2)
- return ((Shortword)0x7fff);
- L_div = ((0x00008000L * (Longword) var1) / (Longword) var2);
-
- /* swOut = saturate(L_div); */
- if (L_div> SW_MAX) {
- swOut = SW_MAX;
- saturation = saturation + 1;
- }
- else if (L_div< SW_MIN) {
- swOut = SW_MIN;
- saturation = saturation + 1;
- }
- else {
- swOut = (Shortword) L_div; /* automatic type conversion */
- }
- // complexity = old_complexity + 18;mark del
- return (swOut);
- }
- /* Non-saturating instructions */
- /*******************************/
- Longword L_add_c(Longword L_Var1, Longword L_Var2); /* 2 ops */
- Longword L_sub_c(Longword L_Var1, Longword L_Var2); /* 2 ops */
- Longword L_sat(Longword L_var1); /* 4 ops */
- Longword L_macNs(Longword L_var3,
- Shortword var1, Shortword var2); /* 1 ops */
- Longword L_msuNs(Longword L_var3,
- Shortword var1, Shortword var2); /* 1 ops */
- #endif