1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3  * Copyright (c) 1999-2010 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  */
37 
38 /**
39  * @file tied_mgau_common.h
40  * @brief Common code shared between SC and PTM (tied-state) models.
41  */
42 
43 #ifndef __TIED_MGAU_COMMON_H__
44 #define __TIED_MGAU_COMMON_H__
45 
46 #include <sphinxbase/logmath.h>
47 #include <sphinxbase/fixpoint.h>
48 
49 #define MGAU_MIXW_VERSION	"1.0"   /* Sphinx-3 file format version for mixw */
50 #define MGAU_PARAM_VERSION	"1.0"   /* Sphinx-3 file format version for mean/var */
51 #define NONE		-1
52 #define WORST_DIST	(int32)(0x80000000)
53 
54 /** Subtract GMM component b (assumed to be positive) and saturate */
55 #ifdef FIXED_POINT
56 #define GMMSUB(a,b) \
57 	(((a)-(b) > a) ? (INT_MIN) : ((a)-(b)))
58 /** Add GMM component b (assumed to be positive) and saturate */
59 #define GMMADD(a,b) \
60 	(((a)+(b) < a) ? (INT_MAX) : ((a)+(b)))
61 #else
62 #define GMMSUB(a,b) ((a)-(b))
63 #define GMMADD(a,b) ((a)+(b))
64 #endif
65 
66 #ifndef MIN
67 #define MIN(a,b) ((a) < (b) ? (a) : (b))
68 #endif
69 
70 
71 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
72 #define LOGMATH_INLINE static inline
73 #elif defined(_MSC_VER)
74 #define LOGMATH_INLINE __inline
75 #else
76 #define LOGMATH_INLINE static
77 #endif
78 
79 /* Allocate 0..159 for negated quantized mixture weights and 0..96 for
80  * negated normalized acoustic scores, so that the combination of the
81  * two (for a single mixture) can never exceed 255. */
82 #define MAX_NEG_MIXW 159 /**< Maximum negated mixture weight value. */
83 #define MAX_NEG_ASCR 96  /**< Maximum negated acoustic score value. */
84 
85 /**
86  * Quickly log-add two negated log probabilities.
87  *
88  * @param lmath The log-math object
89  * @param mlx A negative log probability (0 < mlx < 255)
90  * @param mly A negative log probability (0 < mly < 255)
91  * @return -log(exp(-mlx)+exp(-mly))
92  *
93  * We can do some extra-fast log addition since we know that
94  * mixw+ascr is always less than 256 and hence x-y is also always less
95  * than 256.  This relies on some cooperation from logmath_t which
96  * will never produce a logmath table smaller than 256 entries.
97  *
98  * Note that the parameters are *negated* log probabilities (and
99  * hence, are positive numbers), as is the return value.  This is the
100  * key to the "fastness" of this function.
101  */
102 LOGMATH_INLINE int
fast_logmath_add(logmath_t * lmath,int mlx,int mly)103 fast_logmath_add(logmath_t *lmath, int mlx, int mly)
104 {
105     logadd_t *t = LOGMATH_TABLE(lmath);
106     int d, r;
107 
108     /* d must be positive, obviously. */
109     if (mlx > mly) {
110         d = (mlx - mly);
111         r = mly;
112     }
113     else {
114         d = (mly - mlx);
115         r = mlx;
116     }
117 
118     return r - (((uint8 *)t->table)[d]);
119 }
120 
121 #endif /* __TIED_MGAU_COMMON_H__ */
122