/usr/include/spandsp/saturated.h is in libspandsp-dev 0.0.6+dfsg-0.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | /*
* SpanDSP - a series of DSP components for telephony
*
* saturated.h - General saturated arithmetic routines.
*
* Written by Steve Underwood <steveu@coppice.org>
*
* Copyright (C) 2001, 2008 Steve Underwood
*
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 2.1,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*! \file */
#if !defined(_SPANDSP_SATURATED_H_)
#define _SPANDSP_SATURATED_H_
/*! \page saturated_page Saturated arithmetic
\section saturated_page_sec_1 What does it do?
\section saturated_page_sec_2 How does it work?
*/
#if defined(__cplusplus)
extern "C"
{
#endif
/* This is the same as saturate16(), but is here for historic reasons */
static __inline__ int16_t saturate(int32_t amp)
{
int16_t amp16;
/* Hopefully this is optimised for the common case - not clipping */
amp16 = (int16_t) amp;
if (amp == amp16)
return amp16;
if (amp > INT16_MAX)
return INT16_MAX;
return INT16_MIN;
}
/*- End of function --------------------------------------------------------*/
static __inline__ int16_t saturate16(int32_t amp)
{
int16_t amp16;
/* Hopefully this is optimised for the common case - not clipping */
amp16 = (int16_t) amp;
if (amp == amp16)
return amp16;
if (amp > INT16_MAX)
return INT16_MAX;
return INT16_MIN;
}
/*- End of function --------------------------------------------------------*/
/*! Saturate to 15 bits, rather than the usual 16 bits. This is often a useful function. */
static __inline__ int16_t saturate15(int32_t amp)
{
if (amp > 16383)
return 16383;
if (amp < -16384)
return -16384;
return (int16_t) amp;
}
/*- End of function --------------------------------------------------------*/
static __inline__ uint16_t saturateu16(int32_t amp)
{
uint16_t amp16;
/* Hopefully this is optimised for the common case - not clipping */
amp16 = (uint16_t) amp;
if (amp == amp16)
return amp16;
if (amp > UINT16_MAX)
return UINT16_MAX;
return 0;
}
/*- End of function --------------------------------------------------------*/
static __inline__ uint8_t saturateu8(int32_t amp)
{
uint8_t amp8;
/* Hopefully this is optimised for the common case - not clipping */
amp8 = (uint8_t) amp;
if (amp == amp8)
return amp8;
if (amp > UINT8_MAX)
return UINT8_MAX;
return 0;
}
/*- End of function --------------------------------------------------------*/
static __inline__ int16_t fsaturatef(float famp)
{
if (famp > (float) INT16_MAX)
return INT16_MAX;
if (famp < (float) INT16_MIN)
return INT16_MIN;
return (int16_t) lrintf(famp);
}
/*- End of function --------------------------------------------------------*/
static __inline__ int16_t fsaturate(double damp)
{
if (damp > (double) INT16_MAX)
return INT16_MAX;
if (damp < (double) INT16_MIN)
return INT16_MIN;
return (int16_t) lrint(damp);
}
/*- End of function --------------------------------------------------------*/
/* Saturate to a 16 bit integer, using the fastest float to int conversion */
static __inline__ int16_t ffastsaturatef(float famp)
{
if (famp > (float) INT16_MAX)
return INT16_MAX;
if (famp < (float) INT16_MIN)
return INT16_MIN;
return (int16_t) lfastrintf(famp);
}
/*- End of function --------------------------------------------------------*/
/* Saturate to a 16 bit integer, using the fastest double to int conversion */
static __inline__ int16_t ffastsaturate(double damp)
{
if (damp > (double) INT16_MAX)
return INT16_MAX;
if (damp < (double) INT16_MIN)
return INT16_MIN;
return (int16_t) lfastrint(damp);
}
/*- End of function --------------------------------------------------------*/
/* Saturate to a 16 bit integer, using the closest float to int conversion */
static __inline__ float ffsaturatef(float famp)
{
if (famp > (float) INT16_MAX)
return (float) INT16_MAX;
if (famp < (float) INT16_MIN)
return (float) INT16_MIN;
return famp;
}
/*- End of function --------------------------------------------------------*/
/* Saturate to a 16 bit integer, using the closest double to int conversion */
static __inline__ double ffsaturate(double famp)
{
if (famp > (double) INT16_MAX)
return (double) INT16_MAX;
if (famp < (double) INT16_MIN)
return (double) INT16_MIN;
return famp;
}
/*- End of function --------------------------------------------------------*/
static __inline__ int16_t saturated_add16(int16_t a, int16_t b)
{
#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
__asm__ __volatile__(
" addw %2,%0;\n"
" jno 0f;\n"
" movw $0x7fff,%0;\n"
" adcw $0,%0;\n"
"0:"
: "=r" (a)
: "0" (a), "ir" (b)
: "cc"
);
return a;
#elif defined(__GNUC__) && defined(__arm5__)
int16_t result;
__asm__ __volatile__(
" sadd16 %0,%1,%2;\n"
: "=r" (result)
: "0" (a), "ir" (b)
);
return result;
#else
return saturate((int32_t) a + (int32_t) b);
#endif
}
/*- End of function --------------------------------------------------------*/
static __inline__ int32_t saturated_add32(int32_t a, int32_t b)
{
#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
__asm__ __volatile__(
" addl %2,%0;\n"
" jno 0f;\n"
" movl $0x7fffffff,%0;\n"
" adcl $0,%0;\n"
"0:"
: "=r" (a)
: "0" (a), "ir" (b)
: "cc"
);
return a;
#elif defined(__GNUC__) && defined(__arm5__)
int32_t result;
__asm__ __volatile__(
" qadd %0,%1,%2;\n"
: "=r" (result)
: "0" (a), "ir" (b)
);
return result;
#else
int32_t sum;
sum = a + b;
if ((a ^ b) >= 0)
{
if ((sum ^ a) < 0)
sum = (a < 0) ? INT32_MIN : INT32_MAX;
}
return sum;
#endif
}
/*- End of function --------------------------------------------------------*/
static __inline__ int16_t saturated_sub16(int16_t a, int16_t b)
{
#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
__asm__ __volatile__(
" subw %2,%0;\n"
" jno 0f;\n"
" movw $0x8000,%0;\n"
" sbbw $0,%0;\n"
"0:"
: "=r" (a)
: "0" (a), "ir" (b)
: "cc"
);
return a;
#elif defined(__GNUC__) && defined(__arm5__)
int16_t result;
__asm__ __volatile__(
" ssub16 %0,%1,%2;\n"
: "=r" (result)
: "0" (a), "ir" (b)
);
return result;
#else
return saturate((int32_t) a - (int32_t) b);
#endif
}
/*- End of function --------------------------------------------------------*/
static __inline__ int32_t saturated_sub32(int32_t a, int32_t b)
{
#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
__asm__ __volatile__(
" subl %2,%0;\n"
" jno 0f;\n"
" movl $0x80000000,%0;\n"
" sbbl $0,%0;\n"
"0:"
: "=r" (a)
: "0" (a), "ir" (b)
: "cc"
);
return a;
#elif defined(__GNUC__) && defined(__arm5__)
int32_t result;
__asm__ __volatile__(
" qsub %0,%1,%2;\n"
: "=r" (result)
: "0" (a), "ir" (b)
);
return result;
#else
int32_t diff;
diff = a - b;
if ((a ^ b) < 0)
{
if ((diff ^ a) & INT32_MIN)
diff = (a < 0L) ? INT32_MIN : INT32_MAX;
}
return diff;
#endif
}
/*- End of function --------------------------------------------------------*/
static __inline__ int16_t saturated_mul16(int16_t a, int16_t b)
{
if (a == INT16_MIN && b == INT16_MIN)
return INT16_MAX;
/*endif*/
return (int16_t) (((int32_t) a*(int32_t) b) >> 15);
}
/*- End of function --------------------------------------------------------*/
static __inline__ int32_t saturated_mul16_32(int16_t a, int16_t b)
{
return ((int32_t) a*(int32_t) b) << 1;
}
/*- End of function --------------------------------------------------------*/
static __inline__ int16_t saturated_abs16(int16_t a)
{
return (a == INT16_MIN) ? INT16_MAX : (int16_t) abs(a);
}
/*- End of function --------------------------------------------------------*/
#if defined(__cplusplus)
}
#endif
#endif
/*- End of file ------------------------------------------------------------*/
|