DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Line Code
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
/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* ====================================================================
 * Copyright (c) 2007 Carnegie Mellon University.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * This work was supported in part by funding from the Defense Advanced 
 * Research Projects Agency and the National Science Foundation of the 
 * United States of America, and the CMU Sphinx Speech Consortium.
 *
 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 
 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 *
 */
/**
 * @file logmath.h
 * @brief Fast integer logarithmic addition operations.
 *
 * In evaluating HMM models, probability values are often kept in log
 * domain, to avoid overflow.  To enable these logprob values to be
 * held in int32 variables without significant loss of precision, a
 * logbase of (1+epsilon) (where epsilon < 0.01 or so) is used.  This
 * module maintains this logbase (B).
 * 
 * However, maintaining probabilities in log domain creates a problem
 * when adding two probability values.  This problem can be solved by
 * table lookup.  Note that:
 *
 *  - \f$ b^z = b^x + b^y \f$
 *  - \f$ b^z = b^x(1 + b^{y-x})     = b^y(1 + e^{x-y}) \f$
 *  - \f$ z   = x + log_b(1 + b^{y-x}) = y + log_b(1 + b^{x-y}) \f$
 *
 * So:
 * 
 *  - when \f$ y > x, z = y + logadd\_table[-(x-y)] \f$
 *  - when \f$ x > y, z = x + logadd\_table[-(y-x)] \f$
 *  - where \f$ logadd\_table[n] = log_b(1 + b^{-n}) \f$
 *
 * The first entry in <i>logadd_table</i> is
 * simply \f$ log_b(2.0) \f$, for
 * the case where \f$ y = x \f$ and thus
 * \f$ z = log_b(2x) = log_b(2) + x \f$.  The last entry is zero,
 * where \f$ log_b(x+y) = x = y \f$ due to loss of precision.
 *
 * Since this table can be quite large particularly for small
 * logbases, an option is provided to compress it by dropping the
 * least significant bits of the table.
 */

#ifndef __LOGMATH_H__
#define __LOGMATH_H__

#include <sphinxbase/sphinxbase_export.h>
#include <sphinxbase/prim_type.h>
#include <sphinxbase/cmd_ln.h>


#ifdef __cplusplus
extern "C" {
#endif
#if 0
/* Fool Emacs. */
}
#endif

/**
 * Integer log math computation table.
 *
 * This is exposed here to allow log-add computations to be inlined.
 */
typedef struct logadd_s logadd_t;
struct logadd_s {
    /** Table, in unsigned integers of (width) bytes. */
    void *table;
    /** Number of elements in (table).  This is never smaller than 256 (important!) */
    uint32 table_size;
    /** Width of elements of (table). */
    uint8 width;
    /** Right shift applied to elements in (table). */
    int8 shift;
};

/**
 * Integer log math computation class.
 */
typedef struct logmath_s logmath_t;

/**
 * Obtain the log-add table from a logmath_t *
 */
#define LOGMATH_TABLE(lm) ((logadd_t *)lm)

/**
 * Initialize a log math computation table.
 * @param base The base B in which computation is to be done.
 * @param shift Log values are shifted right by this many bits.
 * @param use_table Whether to use an add table or not
 * @return The newly created log math table.
 */
SPHINXBASE_EXPORT
logmath_t *logmath_init(float64 base, int shift, int use_table);

/**
 * Memory-map (or read) a log table from a file.
 */
SPHINXBASE_EXPORT
logmath_t *logmath_read(const char *filename);

/**
 * Write a log table to a file.
 */
SPHINXBASE_EXPORT
int32 logmath_write(logmath_t *lmath, const char *filename);

/**
 * Get the log table size and dimensions.
 */
SPHINXBASE_EXPORT
int32 logmath_get_table_shape(logmath_t *lmath, uint32 *out_size,
                              uint32 *out_width, uint32 *out_shift);

/**
 * Get the log base.
 */
SPHINXBASE_EXPORT
float64 logmath_get_base(logmath_t *lmath);

/**
 * Get the smallest possible value represented in this base.
 */
SPHINXBASE_EXPORT
int logmath_get_zero(logmath_t *lmath);

/**
 * Get the width of the values in a log table.
 */
SPHINXBASE_EXPORT
int logmath_get_width(logmath_t *lmath);

/**
 * Get the shift of the values in a log table.
 */
SPHINXBASE_EXPORT
int logmath_get_shift(logmath_t *lmath);

/**
 * Retain ownership of a log table.
 *
 * @return pointer to retained log table.
 */
SPHINXBASE_EXPORT
logmath_t *logmath_retain(logmath_t *lmath);

/**
 * Free a log table.
 *
 * @return new reference count (0 if freed completely)
 */
SPHINXBASE_EXPORT
int logmath_free(logmath_t *lmath);

/**
 * Add two values in log space exactly and slowly (without using add table).
 */
SPHINXBASE_EXPORT
int logmath_add_exact(logmath_t *lmath, int logb_p, int logb_q);

/**
 * Add two values in log space (i.e. return log(exp(p)+exp(q)))
 */
SPHINXBASE_EXPORT
int logmath_add(logmath_t *lmath, int logb_p, int logb_q);

/**
 * Convert linear floating point number to integer log in base B.
 */
SPHINXBASE_EXPORT
int logmath_log(logmath_t *lmath, float64 p);

/**
 * Convert integer log in base B to linear floating point.
 */
SPHINXBASE_EXPORT
float64 logmath_exp(logmath_t *lmath, int logb_p);

/**
 * Convert natural log (in floating point) to integer log in base B.
 */
SPHINXBASE_EXPORT
int logmath_ln_to_log(logmath_t *lmath, float64 log_p);

/**
 * Convert integer log in base B to natural log (in floating point).
 */
SPHINXBASE_EXPORT
float64 logmath_log_to_ln(logmath_t *lmath, int logb_p);

/**
 * Convert base 10 log (in floating point) to integer log in base B.
 */
SPHINXBASE_EXPORT
int logmath_log10_to_log(logmath_t *lmath, float64 log_p);

/**
 * Convert integer log in base B to base 10 log (in floating point).
 */
SPHINXBASE_EXPORT
float64 logmath_log_to_log10(logmath_t *lmath, int logb_p);

#ifdef __cplusplus
}
#endif


#endif /*  __LOGMATH_H__ */