saturated.h

Go to the documentation of this file.
00001 /*
00002  * SpanDSP - a series of DSP components for telephony
00003  *
00004  * saturated.h - General saturated arithmetic routines.
00005  *
00006  * Written by Steve Underwood <steveu@coppice.org>
00007  *
00008  * Copyright (C) 2001, 2008 Steve Underwood
00009  *
00010  * All rights reserved.
00011  *
00012  * This program is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU Lesser General Public License version 2.1,
00014  * as published by the Free Software Foundation.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Lesser General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public
00022  * License along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00024  *
00025  * $Id: saturated.h,v 1.1 2008/09/19 14:02:05 steveu Exp $
00026  */
00027 
00028 /*! \file */
00029 
00030 #if !defined(_SPANDSP_SATURATED_H_)
00031 #define _SPANDSP_SATURATED_H_
00032 
00033 /*! \page saturated_page Saturated arithmetic
00034 
00035 \section saturated_page_sec_1 What does it do?
00036 
00037 
00038 \section saturated_page_sec_2 How does it work?
00039 
00040 */
00041 
00042 #if defined(__cplusplus)
00043 extern "C"
00044 {
00045 #endif
00046 
00047 static __inline__ int16_t saturate(int32_t amp)
00048 {
00049     int16_t amp16;
00050 
00051     /* Hopefully this is optimised for the common case - not clipping */
00052     amp16 = (int16_t) amp;
00053     if (amp == amp16)
00054         return amp16;
00055     if (amp > INT16_MAX)
00056         return INT16_MAX;
00057     return INT16_MIN;
00058 }
00059 /*- End of function --------------------------------------------------------*/
00060 
00061 /*! Saturate to 15 bits, rather than the usual 16 bits. This is often a useful function. */
00062 static __inline__ int16_t saturate15(int32_t amp)
00063 {
00064     if (amp > 16383)
00065         return 16383;
00066     if (amp < -16384)
00067         return -16384;
00068     return (int16_t) amp;
00069 }
00070 /*- End of function --------------------------------------------------------*/
00071 
00072 static __inline__ int16_t fsaturatef(float famp)
00073 {
00074     if (famp > (float) INT16_MAX)
00075         return INT16_MAX;
00076     if (famp < (float) INT16_MIN)
00077         return INT16_MIN;
00078     return (int16_t) rintf(famp);
00079 }
00080 /*- End of function --------------------------------------------------------*/
00081 
00082 static __inline__ int16_t fsaturate(double damp)
00083 {
00084     if (damp > (double) INT16_MAX)
00085         return INT16_MAX;
00086     if (damp < (double) INT16_MIN)
00087         return INT16_MIN;
00088     return (int16_t) rint(damp);
00089 }
00090 /*- End of function --------------------------------------------------------*/
00091 
00092 static __inline__ float ffsaturatef(float famp)
00093 {
00094     if (famp > (float) INT16_MAX)
00095         return (float) INT16_MAX;
00096     if (famp < (float) INT16_MIN)
00097         return (float) INT16_MIN;
00098     return famp;
00099 }
00100 /*- End of function --------------------------------------------------------*/
00101 
00102 static __inline__ double ffsaturate(double famp)
00103 {
00104     if (famp > (double) INT16_MAX)
00105         return (double) INT16_MAX;
00106     if (famp < (double) INT16_MIN)
00107         return (double) INT16_MIN;
00108     return famp;
00109 }
00110 /*- End of function --------------------------------------------------------*/
00111 
00112 static __inline__ int16_t saturated_add16(int16_t a, int16_t b)
00113 {
00114 #if defined(__GNUC__)  &&  defined(__i386__)
00115     __asm__ __volatile__(
00116         " addw %2,%0;\n"
00117         " jno 0f;\n"
00118         " movw $0x7fff,%0;\n"
00119         " adcw $0,%0;\n"
00120         "0:"
00121         : "=r" (a)
00122         : "0" (a), "ir" (b)
00123         : "cc"
00124     );
00125     return a;
00126 #else
00127     return saturate((int32_t) a + (int32_t) b);
00128 #endif
00129 }
00130 /*- End of function --------------------------------------------------------*/
00131 
00132 static __inline__ int32_t saturated_add32(int32_t a, int32_t b)
00133 {
00134 #if defined(__GNUC__)  &&  defined(__i386__)
00135     __asm__ __volatile__(
00136         " addl %2,%0;\n"
00137         " jno 0f;\n"
00138         " movl $0x7fffffff,%0;\n"
00139         " adcl $0,%0;\n"
00140         "0:"
00141         : "=r" (a)
00142         : "0" (a), "ir" (b)
00143         : "cc"
00144     );
00145     return a;
00146 #else
00147     uint32_t A;
00148 
00149     if (a < 0)
00150     {
00151         if (b >= 0)
00152             return  a + b;
00153         /*endif*/
00154         A = (uint32_t) -(a + 1) + (uint32_t) -(b + 1);
00155         return (A >= INT32_MAX)  ?  INT32_MIN  :  -(int32_t) A - 2;
00156     }
00157     /*endif*/
00158     if (b <= 0)
00159         return  a + b;
00160     /*endif*/
00161     A = (uint32_t) a + (uint32_t) b;
00162     return (A > INT32_MAX)  ?  INT32_MAX  :  A;
00163 #endif
00164 }
00165 /*- End of function --------------------------------------------------------*/
00166 
00167 static __inline__ int16_t saturated_sub16(int16_t a, int16_t b)
00168 {
00169     return saturate((int32_t) a - (int32_t) b);
00170 }
00171 /*- End of function --------------------------------------------------------*/
00172 
00173 static __inline__ int16_t saturated_mul16(int16_t a, int16_t b)
00174 {
00175     if (a == INT16_MIN  &&  b == INT16_MIN)
00176         return INT16_MAX;
00177     /*endif*/
00178     return (int16_t) (((int32_t) a*(int32_t) b) >> 15);
00179 }
00180 /*- End of function --------------------------------------------------------*/
00181 
00182 static __inline__ int32_t saturated_mul_16_32(int16_t a, int16_t b)
00183 {
00184     return ((int32_t) a*(int32_t) b) << 1;
00185 }
00186 /*- End of function --------------------------------------------------------*/
00187 
00188 static __inline__ int16_t saturated_abs16(int16_t a)
00189 {
00190     return (a == INT16_MIN)  ?  INT16_MAX  :  (int16_t) abs(a);
00191 }
00192 /*- End of function --------------------------------------------------------*/
00193 
00194 #if defined(__cplusplus)
00195 }
00196 #endif
00197 
00198 #endif
00199 /*- End of file ------------------------------------------------------------*/

Generated on Tue Oct 7 20:25:47 2008 for spandsp by  doxygen 1.5.6