SphinxBase  5prealpha
fixpoint.h
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3  * Copyright (c) 2005 Carnegie Mellon University. All rights
4  * reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * This work was supported in part by funding from the Defense Advanced
19  * Research Projects Agency and the National Science Foundation of the
20  * United States of America, and the CMU Sphinx Speech Consortium.
21  *
22  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * ==================================================================== */
35 
36 /* Fixed-point arithmetic macros.
37  *
38  * Author: David Huggins-Daines <dhuggins@cs.cmu.edu>
39  */
40 
41 #ifndef _FIXPOINT_H_
42 #define _FIXPOINT_H_
43 
44 #include <limits.h>
45 
46 /* Win32/WinCE DLL gunk */
47 #include <sphinxbase/sphinxbase_export.h>
48 #include <sphinxbase/prim_type.h>
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 #if 0
54 /* Fool Emacs. */
55 }
56 #endif
57 
58 #ifndef DEFAULT_RADIX
59 #define DEFAULT_RADIX 12
60 #endif
61 
63 typedef int32 fixed32;
64 
66 #define FLOAT2FIX_ANY(x,radix) \
67  (((x)<0.0) ? \
68  ((fixed32)((x)*(float32)(1<<(radix)) - 0.5)) \
69  : ((fixed32)((x)*(float32)(1<<(radix)) + 0.5)))
70 #define FLOAT2FIX(x) FLOAT2FIX_ANY(x,DEFAULT_RADIX)
71 
72 #define FIX2FLOAT_ANY(x,radix) ((float32)(x)/(1<<(radix)))
73 #define FIX2FLOAT(x) FIX2FLOAT_ANY(x,DEFAULT_RADIX)
74 
82 #if defined(__arm__) && !defined(__thumb__)
83 /*
84  * This works on most modern ARMs but *only* in ARM mode (for obvious
85  * reasons), so don't use it in Thumb mode (but why are you building
86  * signal processing code in Thumb mode?!)
87  */
88 #define FIXMUL(a,b) FIXMUL_ANY(a,b,DEFAULT_RADIX)
89 #define FIXMUL_ANY(a,b,r) ({ \
90  int cl, ch, _a = a, _b = b; \
91  __asm__ ("smull %0, %1, %2, %3\n" \
92  "mov %0, %0, lsr %4\n" \
93  "orr %0, %0, %1, lsl %5\n" \
94  : "=&r" (cl), "=&r" (ch) \
95  : "r" (_a), "r" (_b), "i" (r), "i" (32-(r)));\
96  cl; })
97 
98 #elif defined(_MSC_VER) || (defined(HAVE_LONG_LONG) && SIZEOF_LONG_LONG == 8)
99 /* Standard systems*/
100 #define FIXMUL(a,b) FIXMUL_ANY(a,b,DEFAULT_RADIX)
101 #define FIXMUL_ANY(a,b,radix) ((fixed32)(((int64)(a)*(b))>>(radix)))
102 
103 #else
104 /* Most general case where 'long long' doesn't exist or is slow. */
105 #define FIXMUL(a,b) FIXMUL_ANY(a,b,DEFAULT_RADIX)
106 #define FIXMUL_ANY(a,b,radix) ({ \
107  int32 _ah, _bh; \
108  uint32 _al, _bl, _t, c; \
109  _ah = ((int32)(a)) >> 16; \
110  _bh = ((int32)(b)) >> 16; \
111  _al = ((uint32)(a)) & 0xffff; \
112  _bl = ((uint32)(b)) & 0xffff; \
113  _t = _ah * _bl + _al * _bh; \
114  c = (fixed32)(((_al * _bl) >> (radix)) \
115  + ((_ah * _bh) << (32 - (radix))) \
116  + ((radix) > 16 ? (_t >> (radix - 16)) : (_t << (16 - radix)))); \
117  c;})
118 #endif
119 
120 /* Various fixed-point logarithmic functions that we need. */
122 #define MIN_FIXLOG -2829416 /* log(1e-300) * (1<<DEFAULT_RADIX) */
123 #define MIN_FIXLOG2 -4081985 /* log2(1e-300) * (1<<DEFAULT_RADIX) */
124 
125 #define FIXLN_2 ((fixed32)(0.693147180559945 * (1<<DEFAULT_RADIX)))
126 
127 #define FIXLN(x) (fixlog(x) - (FIXLN_2 * DEFAULT_RADIX))
128 
133 int32 fixlog(uint32 x);
138 int32 fixlog2(uint32 x);
139 
140 #ifdef __cplusplus
141 }
142 #endif
143 
144 
145 #endif /* _FIXPOINT_H_ */
Basic type definitions used in Sphinx.