\doxysection{logmath.\+c}
\label{logmath_8c_source}\index{src/libsphinxbase/util/logmath.c@{src/libsphinxbase/util/logmath.c}}

\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ \textcolor{comment}{/*\ -\/*-\/\ c-\/basic-\/offset:\ 4;\ indent-\/tabs-\/mode:\ nil\ -\/*-\/\ */}}
\DoxyCodeLine{00002\ \textcolor{comment}{/*\ ====================================================================}}
\DoxyCodeLine{00003\ \textcolor{comment}{\ *\ Copyright\ (c)\ 1999-\/2007\ Carnegie\ Mellon\ University.\ \ All\ rights}}
\DoxyCodeLine{00004\ \textcolor{comment}{\ *\ reserved.}}
\DoxyCodeLine{00005\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00006\ \textcolor{comment}{\ *\ Redistribution\ and\ use\ in\ source\ and\ binary\ forms,\ with\ or\ without}}
\DoxyCodeLine{00007\ \textcolor{comment}{\ *\ modification,\ are\ permitted\ provided\ that\ the\ following\ conditions}}
\DoxyCodeLine{00008\ \textcolor{comment}{\ *\ are\ met:}}
\DoxyCodeLine{00009\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00010\ \textcolor{comment}{\ *\ 1.\ Redistributions\ of\ source\ code\ must\ retain\ the\ above\ copyright}}
\DoxyCodeLine{00011\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer.\ }}
\DoxyCodeLine{00012\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00013\ \textcolor{comment}{\ *\ 2.\ Redistributions\ in\ binary\ form\ must\ reproduce\ the\ above\ copyright}}
\DoxyCodeLine{00014\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer\ in}}
\DoxyCodeLine{00015\ \textcolor{comment}{\ *\ \ \ \ the\ documentation\ and/or\ other\ materials\ provided\ with\ the}}
\DoxyCodeLine{00016\ \textcolor{comment}{\ *\ \ \ \ distribution.}}
\DoxyCodeLine{00017\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00018\ \textcolor{comment}{\ *\ This\ work\ was\ supported\ in\ part\ by\ funding\ from\ the\ Defense\ Advanced\ }}
\DoxyCodeLine{00019\ \textcolor{comment}{\ *\ Research\ Projects\ Agency\ and\ the\ National\ Science\ Foundation\ of\ the\ }}
\DoxyCodeLine{00020\ \textcolor{comment}{\ *\ United\ States\ of\ America,\ and\ the\ CMU\ Sphinx\ Speech\ Consortium.}}
\DoxyCodeLine{00021\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00022\ \textcolor{comment}{\ *\ THIS\ SOFTWARE\ IS\ PROVIDED\ BY\ CARNEGIE\ MELLON\ UNIVERSITY\ \`{}\`{}AS\ IS''\ AND\ }}
\DoxyCodeLine{00023\ \textcolor{comment}{\ *\ ANY\ EXPRESSED\ OR\ IMPLIED\ WARRANTIES,\ INCLUDING,\ BUT\ NOT\ LIMITED\ TO,\ }}
\DoxyCodeLine{00024\ \textcolor{comment}{\ *\ THE\ IMPLIED\ WARRANTIES\ OF\ MERCHANTABILITY\ AND\ FITNESS\ FOR\ A\ PARTICULAR}}
\DoxyCodeLine{00025\ \textcolor{comment}{\ *\ PURPOSE\ ARE\ DISCLAIMED.\ \ IN\ NO\ EVENT\ SHALL\ CARNEGIE\ MELLON\ UNIVERSITY}}
\DoxyCodeLine{00026\ \textcolor{comment}{\ *\ NOR\ ITS\ EMPLOYEES\ BE\ LIABLE\ FOR\ ANY\ DIRECT,\ INDIRECT,\ INCIDENTAL,}}
\DoxyCodeLine{00027\ \textcolor{comment}{\ *\ SPECIAL,\ EXEMPLARY,\ OR\ CONSEQUENTIAL\ DAMAGES\ (INCLUDING,\ BUT\ NOT\ }}
\DoxyCodeLine{00028\ \textcolor{comment}{\ *\ LIMITED\ TO,\ PROCUREMENT\ OF\ SUBSTITUTE\ GOODS\ OR\ SERVICES;\ LOSS\ OF\ USE,\ }}
\DoxyCodeLine{00029\ \textcolor{comment}{\ *\ DATA,\ OR\ PROFITS;\ OR\ BUSINESS\ INTERRUPTION)\ HOWEVER\ CAUSED\ AND\ ON\ ANY\ }}
\DoxyCodeLine{00030\ \textcolor{comment}{\ *\ THEORY\ OF\ LIABILITY,\ WHETHER\ IN\ CONTRACT,\ STRICT\ LIABILITY,\ OR\ TORT\ }}
\DoxyCodeLine{00031\ \textcolor{comment}{\ *\ (INCLUDING\ NEGLIGENCE\ OR\ OTHERWISE)\ ARISING\ IN\ ANY\ WAY\ OUT\ OF\ THE\ USE\ }}
\DoxyCodeLine{00032\ \textcolor{comment}{\ *\ OF\ THIS\ SOFTWARE,\ EVEN\ IF\ ADVISED\ OF\ THE\ POSSIBILITY\ OF\ SUCH\ DAMAGE.}}
\DoxyCodeLine{00033\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00034\ \textcolor{comment}{\ *\ ====================================================================}}
\DoxyCodeLine{00035\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00036\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00037\ }
\DoxyCodeLine{00038\ \textcolor{preprocessor}{\#include\ <math.h>}}
\DoxyCodeLine{00039\ \textcolor{preprocessor}{\#include\ <string.h>}}
\DoxyCodeLine{00040\ \textcolor{preprocessor}{\#include\ <assert.h>}}
\DoxyCodeLine{00041\ }
\DoxyCodeLine{00042\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/logmath.h"{}}}
\DoxyCodeLine{00043\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/err.h"{}}}
\DoxyCodeLine{00044\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/ckd\_alloc.h"{}}}
\DoxyCodeLine{00045\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/mmio.h"{}}}
\DoxyCodeLine{00046\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/bio.h"{}}}
\DoxyCodeLine{00047\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/strfuncs.h"{}}}
\DoxyCodeLine{00048\ }
\DoxyCodeLine{00049\ \textcolor{keyword}{struct\ }logmath\_s\ \{}
\DoxyCodeLine{00050\ \ \ \ \ logadd\_t\ t;}
\DoxyCodeLine{00051\ \ \ \ \ \textcolor{keywordtype}{int}\ refcount;}
\DoxyCodeLine{00052\ \ \ \ \ mmio\_file\_t\ *filemap;}
\DoxyCodeLine{00053\ \ \ \ \ float64\ base;}
\DoxyCodeLine{00054\ \ \ \ \ float64\ log\_of\_base;}
\DoxyCodeLine{00055\ \ \ \ \ float64\ log10\_of\_base;}
\DoxyCodeLine{00056\ \ \ \ \ float64\ inv\_log\_of\_base;}
\DoxyCodeLine{00057\ \ \ \ \ float64\ inv\_log10\_of\_base;}
\DoxyCodeLine{00058\ \ \ \ \ int32\ zero;}
\DoxyCodeLine{00059\ \};}
\DoxyCodeLine{00060\ }
\DoxyCodeLine{00061\ logmath\_t\ *}
\DoxyCodeLine{00062\ logmath\_init(float64\ base,\ \textcolor{keywordtype}{int}\ shift,\ \textcolor{keywordtype}{int}\ use\_table)}
\DoxyCodeLine{00063\ \{}
\DoxyCodeLine{00064\ \ \ \ \ logmath\_t\ *lmath;}
\DoxyCodeLine{00065\ \ \ \ \ uint32\ maxyx,\ i;}
\DoxyCodeLine{00066\ \ \ \ \ float64\ byx;}
\DoxyCodeLine{00067\ \ \ \ \ \textcolor{keywordtype}{int}\ width;}
\DoxyCodeLine{00068\ }
\DoxyCodeLine{00069\ \ \ \ \ \textcolor{comment}{/*\ Check\ that\ the\ base\ is\ correct.\ */}}
\DoxyCodeLine{00070\ \ \ \ \ \textcolor{keywordflow}{if}\ (base\ <=\ 1.0)\ \{}
\DoxyCodeLine{00071\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Base\ must\ be\ greater\ than\ 1.0\(\backslash\)n"{}});}
\DoxyCodeLine{00072\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00073\ \ \ \ \ \}}
\DoxyCodeLine{00074\ \ \ \ \ }
\DoxyCodeLine{00075\ \ \ \ \ \textcolor{comment}{/*\ Set\ up\ various\ necessary\ constants.\ */}}
\DoxyCodeLine{00076\ \ \ \ \ lmath\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*lmath));}
\DoxyCodeLine{00077\ \ \ \ \ lmath-\/>refcount\ =\ 1;}
\DoxyCodeLine{00078\ \ \ \ \ lmath-\/>base\ =\ base;}
\DoxyCodeLine{00079\ \ \ \ \ lmath-\/>log\_of\_base\ =\ log(base);}
\DoxyCodeLine{00080\ \ \ \ \ lmath-\/>log10\_of\_base\ =\ log10(base);}
\DoxyCodeLine{00081\ \ \ \ \ lmath-\/>inv\_log\_of\_base\ =\ 1.0/lmath-\/>log\_of\_base;}
\DoxyCodeLine{00082\ \ \ \ \ lmath-\/>inv\_log10\_of\_base\ =\ 1.0/lmath-\/>log10\_of\_base;}
\DoxyCodeLine{00083\ \ \ \ \ lmath-\/>t.shift\ =\ shift;}
\DoxyCodeLine{00084\ \ \ \ \ \textcolor{comment}{/*\ Shift\ this\ sufficiently\ that\ overflows\ can\ be\ avoided.\ */}}
\DoxyCodeLine{00085\ \ \ \ \ lmath-\/>zero\ =\ MAX\_NEG\_INT32\ >>\ (shift\ +\ 2);}
\DoxyCodeLine{00086\ }
\DoxyCodeLine{00087\ \ \ \ \ \textcolor{keywordflow}{if}\ (!use\_table)}
\DoxyCodeLine{00088\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ lmath;}
\DoxyCodeLine{00089\ }
\DoxyCodeLine{00090\ \ \ \ \ \textcolor{comment}{/*\ Create\ a\ logadd\ table\ with\ the\ appropriate\ width\ */}}
\DoxyCodeLine{00091\ \ \ \ \ maxyx\ =\ (uint32)\ (log(2.0)\ /\ log(base)\ +\ 0.5)\ >>\ shift;}
\DoxyCodeLine{00092\ \ \ \ \ \textcolor{comment}{/*\ Poor\ man's\ log2\ */}}
\DoxyCodeLine{00093\ \ \ \ \ \textcolor{keywordflow}{if}\ (maxyx\ <\ 256)\ width\ =\ 1;}
\DoxyCodeLine{00094\ \ \ \ \ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}\ (maxyx\ <\ 65536)\ width\ =\ 2;}
\DoxyCodeLine{00095\ \ \ \ \ \textcolor{keywordflow}{else}\ width\ =\ 4;}
\DoxyCodeLine{00096\ }
\DoxyCodeLine{00097\ \ \ \ \ lmath-\/>t.width\ =\ width;}
\DoxyCodeLine{00098\ \ \ \ \ \textcolor{comment}{/*\ Figure\ out\ size\ of\ add\ table\ required.\ */}}
\DoxyCodeLine{00099\ \ \ \ \ byx\ =\ 1.0;\ \textcolor{comment}{/*\ Maximum\ possible\ base\string^\{y-\/x\}\ value\ -\/\ note\ that\ this\ implies\ that\ y-\/x\ ==\ 0\ */}}
\DoxyCodeLine{00100\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;;\ ++i)\ \{}
\DoxyCodeLine{00101\ \ \ \ \ \ \ \ \ float64\ lobyx\ =\ log(1.0\ +\ byx)\ *\ lmath-\/>inv\_log\_of\_base;\ \textcolor{comment}{/*\ log\_\{base\}(1\ +\ base\string^\{y-\/x\});\ */}}
\DoxyCodeLine{00102\ \ \ \ \ \ \ \ \ int32\ k\ =\ (int32)\ (lobyx\ +\ 0.5\ *\ (1<<shift))\ >>\ shift;\ \textcolor{comment}{/*\ Round\ to\ shift\ */}}
\DoxyCodeLine{00103\ }
\DoxyCodeLine{00104\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ base\string^\{y-\/x\}\ has\ reached\ the\ smallest\ representable\ value.\ */}}
\DoxyCodeLine{00105\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (k\ <=\ 0)}
\DoxyCodeLine{00106\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00107\ }
\DoxyCodeLine{00108\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ This\ table\ is\ indexed\ by\ -\/(y-\/x),\ so\ we\ multiply\ byx\ by}}
\DoxyCodeLine{00109\ \textcolor{comment}{\ \ \ \ \ \ \ \ \ *\ base\string^\{-\/1\}\ here\ which\ is\ equivalent\ to\ subtracting\ one\ from}}
\DoxyCodeLine{00110\ \textcolor{comment}{\ \ \ \ \ \ \ \ \ *\ (y-\/x).\ */}}
\DoxyCodeLine{00111\ \ \ \ \ \ \ \ \ byx\ /=\ base;}
\DoxyCodeLine{00112\ \ \ \ \ \}}
\DoxyCodeLine{00113\ \ \ \ \ i\ >>=\ shift;}
\DoxyCodeLine{00114\ }
\DoxyCodeLine{00115\ \ \ \ \ \textcolor{comment}{/*\ Never\ produce\ a\ table\ smaller\ than\ 256\ entries.\ */}}
\DoxyCodeLine{00116\ \ \ \ \ \textcolor{keywordflow}{if}\ (i\ <\ 255)\ i\ =\ 255;}
\DoxyCodeLine{00117\ }
\DoxyCodeLine{00118\ \ \ \ \ lmath-\/>t.table\ =\ ckd\_calloc(i+1,\ width);}
\DoxyCodeLine{00119\ \ \ \ \ lmath-\/>t.table\_size\ =\ i\ +\ 1;}
\DoxyCodeLine{00120\ \ \ \ \ \textcolor{comment}{/*\ Create\ the\ add\ table\ (see\ above).\ */}}
\DoxyCodeLine{00121\ \ \ \ \ byx\ =\ 1.0;}
\DoxyCodeLine{00122\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;;\ ++i)\ \{}
\DoxyCodeLine{00123\ \ \ \ \ \ \ \ \ float64\ lobyx\ =\ log(1.0\ +\ byx)\ *\ lmath-\/>inv\_log\_of\_base;}
\DoxyCodeLine{00124\ \ \ \ \ \ \ \ \ int32\ k\ =\ (int32)\ (lobyx\ +\ 0.5\ *\ (1<<shift))\ >>\ shift;\ \textcolor{comment}{/*\ Round\ to\ shift\ */}}
\DoxyCodeLine{00125\ \ \ \ \ \ \ \ \ uint32\ prev\ =\ 0;}
\DoxyCodeLine{00126\ }
\DoxyCodeLine{00127\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Check\ any\ previous\ value\ -\/\ if\ there\ is\ a\ shift,\ we\ want\ to}}
\DoxyCodeLine{00128\ \textcolor{comment}{\ \ \ \ \ \ \ \ \ *\ only\ store\ the\ highest\ one.\ */}}
\DoxyCodeLine{00129\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{switch}\ (width)\ \{}
\DoxyCodeLine{00130\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{case}\ 1:}
\DoxyCodeLine{00131\ \ \ \ \ \ \ \ \ \ \ \ \ prev\ =\ ((uint8\ *)lmath-\/>t.table)[i\ >>\ shift];}
\DoxyCodeLine{00132\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00133\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{case}\ 2:}
\DoxyCodeLine{00134\ \ \ \ \ \ \ \ \ \ \ \ \ prev\ =\ ((uint16\ *)lmath-\/>t.table)[i\ >>\ shift];}
\DoxyCodeLine{00135\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00136\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{case}\ 4:}
\DoxyCodeLine{00137\ \ \ \ \ \ \ \ \ \ \ \ \ prev\ =\ ((uint32\ *)lmath-\/>t.table)[i\ >>\ shift];}
\DoxyCodeLine{00138\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00139\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00140\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (prev\ ==\ 0)\ \{}
\DoxyCodeLine{00141\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{switch}\ (width)\ \{}
\DoxyCodeLine{00142\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{case}\ 1:}
\DoxyCodeLine{00143\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ((uint8\ *)lmath-\/>t.table)[i\ >>\ shift]\ =\ (uint8)\ k;}
\DoxyCodeLine{00144\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00145\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{case}\ 2:}
\DoxyCodeLine{00146\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ((uint16\ *)lmath-\/>t.table)[i\ >>\ shift]\ =\ (uint16)\ k;}
\DoxyCodeLine{00147\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00148\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{case}\ 4:}
\DoxyCodeLine{00149\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ((uint32\ *)lmath-\/>t.table)[i\ >>\ shift]\ =\ (uint32)\ k;}
\DoxyCodeLine{00150\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00151\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00152\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00153\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (k\ <=\ 0)}
\DoxyCodeLine{00154\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00155\ }
\DoxyCodeLine{00156\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Decay\ base\string^\{y-\/x\}\ exponentially\ according\ to\ base.\ */}}
\DoxyCodeLine{00157\ \ \ \ \ \ \ \ \ byx\ /=\ base;}
\DoxyCodeLine{00158\ \ \ \ \ \}}
\DoxyCodeLine{00159\ }
\DoxyCodeLine{00160\ \ \ \ \ \textcolor{keywordflow}{return}\ lmath;}
\DoxyCodeLine{00161\ \}}
\DoxyCodeLine{00162\ }
\DoxyCodeLine{00163\ logmath\_t\ *}
\DoxyCodeLine{00164\ logmath\_read(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *file\_name)}
\DoxyCodeLine{00165\ \{}
\DoxyCodeLine{00166\ \ \ \ \ logmath\_t\ *lmath;}
\DoxyCodeLine{00167\ \ \ \ \ \textcolor{keywordtype}{char}\ **argname,\ **argval;}
\DoxyCodeLine{00168\ \ \ \ \ int32\ byteswap,\ i;}
\DoxyCodeLine{00169\ \ \ \ \ \textcolor{keywordtype}{int}\ chksum\_present,\ do\_mmap;}
\DoxyCodeLine{00170\ \ \ \ \ uint32\ chksum;}
\DoxyCodeLine{00171\ \ \ \ \ \textcolor{keywordtype}{long}\ pos;}
\DoxyCodeLine{00172\ \ \ \ \ FILE\ *fp;}
\DoxyCodeLine{00173\ }
\DoxyCodeLine{00174\ \ \ \ \ E\_INFO(\textcolor{stringliteral}{"{}Reading\ log\ table\ file\ '\%s'\(\backslash\)n"{}},\ file\_name);}
\DoxyCodeLine{00175\ \ \ \ \ \textcolor{keywordflow}{if}\ ((fp\ =\ fopen(file\_name,\ \textcolor{stringliteral}{"{}rb"{}}))\ ==\ NULL)\ \{}
\DoxyCodeLine{00176\ \ \ \ \ \ \ \ \ E\_ERROR\_SYSTEM(\textcolor{stringliteral}{"{}Failed\ to\ open\ log\ table\ file\ '\%s'\ for\ reading"{}},\ file\_name);}
\DoxyCodeLine{00177\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00178\ \ \ \ \ \}}
\DoxyCodeLine{00179\ }
\DoxyCodeLine{00180\ \ \ \ \ \textcolor{comment}{/*\ Read\ header,\ including\ argument-\/value\ info\ and\ 32-\/bit\ byteorder\ magic\ */}}
\DoxyCodeLine{00181\ \ \ \ \ \textcolor{keywordflow}{if}\ (bio\_readhdr(fp,\ \&argname,\ \&argval,\ \&byteswap)\ <\ 0)\ \{}
\DoxyCodeLine{00182\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Failed\ to\ read\ the\ header\ from\ the\ file\ '\%s'\(\backslash\)n"{}},\ file\_name);}
\DoxyCodeLine{00183\ \ \ \ \ \ \ \ \ fclose(fp);}
\DoxyCodeLine{00184\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00185\ \ \ \ \ \}}
\DoxyCodeLine{00186\ }
\DoxyCodeLine{00187\ \ \ \ \ lmath\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*lmath));}
\DoxyCodeLine{00188\ \ \ \ \ \textcolor{comment}{/*\ Default\ values.\ */}}
\DoxyCodeLine{00189\ \ \ \ \ lmath-\/>t.shift\ =\ 0;}
\DoxyCodeLine{00190\ \ \ \ \ lmath-\/>t.width\ =\ 2;}
\DoxyCodeLine{00191\ \ \ \ \ lmath-\/>base\ =\ 1.0001;}
\DoxyCodeLine{00192\ }
\DoxyCodeLine{00193\ \ \ \ \ \textcolor{comment}{/*\ Parse\ argument-\/value\ list\ */}}
\DoxyCodeLine{00194\ \ \ \ \ chksum\_present\ =\ 0;}
\DoxyCodeLine{00195\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ argname[i];\ i++)\ \{}
\DoxyCodeLine{00196\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (strcmp(argname[i],\ \textcolor{stringliteral}{"{}version"{}})\ ==\ 0)\ \{}
\DoxyCodeLine{00197\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00198\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}\ (strcmp(argname[i],\ \textcolor{stringliteral}{"{}chksum0"{}})\ ==\ 0)\ \{}
\DoxyCodeLine{00199\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (strcmp(argval[i],\ \textcolor{stringliteral}{"{}yes"{}})\ ==\ 0)}
\DoxyCodeLine{00200\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ chksum\_present\ =\ 1;}
\DoxyCodeLine{00201\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00202\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}\ (strcmp(argname[i],\ \textcolor{stringliteral}{"{}width"{}})\ ==\ 0)\ \{}
\DoxyCodeLine{00203\ \ \ \ \ \ \ \ \ \ \ \ \ lmath-\/>t.width\ =\ atoi(argval[i]);}
\DoxyCodeLine{00204\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00205\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}\ (strcmp(argname[i],\ \textcolor{stringliteral}{"{}shift"{}})\ ==\ 0)\ \{}
\DoxyCodeLine{00206\ \ \ \ \ \ \ \ \ \ \ \ \ lmath-\/>t.shift\ =\ atoi(argval[i]);}
\DoxyCodeLine{00207\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00208\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}\ (strcmp(argname[i],\ \textcolor{stringliteral}{"{}logbase"{}})\ ==\ 0)\ \{}
\DoxyCodeLine{00209\ \ \ \ \ \ \ \ \ \ \ \ \ lmath-\/>base\ =\ atof\_c(argval[i]);}
\DoxyCodeLine{00210\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00211\ \ \ \ \ \}}
\DoxyCodeLine{00212\ \ \ \ \ bio\_hdrarg\_free(argname,\ argval);}
\DoxyCodeLine{00213\ \ \ \ \ chksum\ =\ 0;}
\DoxyCodeLine{00214\ }
\DoxyCodeLine{00215\ \ \ \ \ \textcolor{comment}{/*\ Set\ up\ various\ necessary\ constants.\ */}}
\DoxyCodeLine{00216\ \ \ \ \ lmath-\/>log\_of\_base\ =\ log(lmath-\/>base);}
\DoxyCodeLine{00217\ \ \ \ \ lmath-\/>log10\_of\_base\ =\ log10(lmath-\/>base);}
\DoxyCodeLine{00218\ \ \ \ \ lmath-\/>inv\_log\_of\_base\ =\ 1.0/lmath-\/>log\_of\_base;}
\DoxyCodeLine{00219\ \ \ \ \ lmath-\/>inv\_log10\_of\_base\ =\ 1.0/lmath-\/>log10\_of\_base;}
\DoxyCodeLine{00220\ \ \ \ \ \textcolor{comment}{/*\ Shift\ this\ sufficiently\ that\ overflows\ can\ be\ avoided.\ */}}
\DoxyCodeLine{00221\ \ \ \ \ lmath-\/>zero\ =\ MAX\_NEG\_INT32\ >>\ (lmath-\/>t.shift\ +\ 2);}
\DoxyCodeLine{00222\ }
\DoxyCodeLine{00223\ \ \ \ \ \textcolor{comment}{/*\ \#Values\ to\ follow\ */}}
\DoxyCodeLine{00224\ \ \ \ \ \textcolor{keywordflow}{if}\ (bio\_fread(\&lmath-\/>t.table\_size,\ \textcolor{keyword}{sizeof}(int32),\ 1,\ fp,\ byteswap,\ \&chksum)\ !=\ 1)\ \{}
\DoxyCodeLine{00225\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Failed\ to\ read\ values\ from\ the\ file\ '\%s'"{}},\ file\_name);}
\DoxyCodeLine{00226\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ error\_out;}
\DoxyCodeLine{00227\ \ \ \ \ \}}
\DoxyCodeLine{00228\ }
\DoxyCodeLine{00229\ \ \ \ \ \textcolor{comment}{/*\ Check\ alignment\ constraints\ for\ memory\ mapping\ */}}
\DoxyCodeLine{00230\ \ \ \ \ do\_mmap\ =\ 1;}
\DoxyCodeLine{00231\ \ \ \ \ pos\ =\ ftell(fp);}
\DoxyCodeLine{00232\ \ \ \ \ \textcolor{keywordflow}{if}\ (pos\ \&\ ((\textcolor{keywordtype}{long})lmath-\/>t.width\ -\/\ 1))\ \{}
\DoxyCodeLine{00233\ \ \ \ \ \ \ \ \ E\_WARN(\textcolor{stringliteral}{"{}\%s:\ Data\ start\ \%ld\ is\ not\ aligned\ on\ \%d-\/byte\ boundary,\ will\ not\ memory\ map\(\backslash\)n"{}},}
\DoxyCodeLine{00234\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ file\_name,\ pos,\ lmath-\/>t.width);}
\DoxyCodeLine{00235\ \ \ \ \ \ \ \ \ do\_mmap\ =\ 0;}
\DoxyCodeLine{00236\ \ \ \ \ \}}
\DoxyCodeLine{00237\ \ \ \ \ \textcolor{comment}{/*\ Check\ byte\ order\ for\ memory\ mapping\ */}}
\DoxyCodeLine{00238\ \ \ \ \ \textcolor{keywordflow}{if}\ (byteswap)\ \{}
\DoxyCodeLine{00239\ \ \ \ \ \ \ \ \ E\_WARN(\textcolor{stringliteral}{"{}\%s:\ Data\ is\ wrong-\/endian,\ will\ not\ memory\ map\(\backslash\)n"{}},\ file\_name);}
\DoxyCodeLine{00240\ \ \ \ \ \ \ \ \ do\_mmap\ =\ 0;}
\DoxyCodeLine{00241\ \ \ \ \ \}}
\DoxyCodeLine{00242\ }
\DoxyCodeLine{00243\ \ \ \ \ \textcolor{keywordflow}{if}\ (do\_mmap)\ \{}
\DoxyCodeLine{00244\ \ \ \ \ \ \ \ \ lmath-\/>filemap\ =\ mmio\_file\_read(file\_name);}
\DoxyCodeLine{00245\ \ \ \ \ \ \ \ \ lmath-\/>t.table\ =\ (\textcolor{keywordtype}{char}\ *)mmio\_file\_ptr(lmath-\/>filemap)\ +\ pos;}
\DoxyCodeLine{00246\ \ \ \ \ \}}
\DoxyCodeLine{00247\ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00248\ \ \ \ \ \ \ \ \ lmath-\/>t.table\ =\ ckd\_calloc(lmath-\/>t.table\_size,\ lmath-\/>t.width);}
\DoxyCodeLine{00249\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (bio\_fread(lmath-\/>t.table,\ lmath-\/>t.width,\ lmath-\/>t.table\_size,}
\DoxyCodeLine{00250\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ fp,\ byteswap,\ \&chksum)\ !=\ lmath-\/>t.table\_size)\ \{}
\DoxyCodeLine{00251\ \ \ \ \ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Failed\ to\ read\ data\ (\%d\ x\ \%d\ bytes)\ from\ the\ file\ '\%s'\ failed"{}},}
\DoxyCodeLine{00252\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ lmath-\/>t.table\_size,\ lmath-\/>t.width,\ file\_name);}
\DoxyCodeLine{00253\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ error\_out;}
\DoxyCodeLine{00254\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00255\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (chksum\_present)}
\DoxyCodeLine{00256\ \ \ \ \ \ \ \ \ \ \ \ \ bio\_verify\_chksum(fp,\ byteswap,\ chksum);}
\DoxyCodeLine{00257\ }
\DoxyCodeLine{00258\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (fread(\&i,\ 1,\ 1,\ fp)\ ==\ 1)\ \{}
\DoxyCodeLine{00259\ \ \ \ \ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}\%s:\ More\ data\ than\ expected\(\backslash\)n"{}},\ file\_name);}
\DoxyCodeLine{00260\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ error\_out;}
\DoxyCodeLine{00261\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00262\ \ \ \ \ \}}
\DoxyCodeLine{00263\ \ \ \ \ fclose(fp);}
\DoxyCodeLine{00264\ }
\DoxyCodeLine{00265\ \ \ \ \ \textcolor{keywordflow}{return}\ lmath;}
\DoxyCodeLine{00266\ error\_out:}
\DoxyCodeLine{00267\ \ \ \ \ logmath\_free(lmath);}
\DoxyCodeLine{00268\ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00269\ \}}
\DoxyCodeLine{00270\ }
\DoxyCodeLine{00271\ int32}
\DoxyCodeLine{00272\ logmath\_write(logmath\_t\ *lmath,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *file\_name)}
\DoxyCodeLine{00273\ \{}
\DoxyCodeLine{00274\ \ \ \ \ FILE\ *fp;}
\DoxyCodeLine{00275\ \ \ \ \ \textcolor{keywordtype}{long}\ pos;}
\DoxyCodeLine{00276\ \ \ \ \ uint32\ chksum;}
\DoxyCodeLine{00277\ }
\DoxyCodeLine{00278\ \ \ \ \ \textcolor{keywordflow}{if}\ (lmath-\/>t.table\ ==\ NULL)\ \{}
\DoxyCodeLine{00279\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}No\ log\ table\ to\ write!\(\backslash\)n"{}});}
\DoxyCodeLine{00280\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00281\ \ \ \ \ \}}
\DoxyCodeLine{00282\ }
\DoxyCodeLine{00283\ \ \ \ \ E\_INFO(\textcolor{stringliteral}{"{}Writing\ log\ table\ file\ '\%s'\(\backslash\)n"{}},\ file\_name);}
\DoxyCodeLine{00284\ \ \ \ \ \textcolor{keywordflow}{if}\ ((fp\ =\ fopen(file\_name,\ \textcolor{stringliteral}{"{}wb"{}}))\ ==\ NULL)\ \{}
\DoxyCodeLine{00285\ \ \ \ \ \ \ \ \ E\_ERROR\_SYSTEM(\textcolor{stringliteral}{"{}Failed\ to\ open\ logtable\ file\ '\%s'\ for\ writing"{}},\ file\_name);}
\DoxyCodeLine{00286\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00287\ \ \ \ \ \}}
\DoxyCodeLine{00288\ }
\DoxyCodeLine{00289\ \ \ \ \ \textcolor{comment}{/*\ For\ whatever\ reason,\ we\ have\ to\ do\ this\ manually\ at\ the}}
\DoxyCodeLine{00290\ \textcolor{comment}{\ \ \ \ \ *\ moment.\ */}}
\DoxyCodeLine{00291\ \ \ \ \ fprintf(fp,\ \textcolor{stringliteral}{"{}s3\(\backslash\)nversion\ 1.0\(\backslash\)nchksum0\ yes\(\backslash\)n"{}});}
\DoxyCodeLine{00292\ \ \ \ \ fprintf(fp,\ \textcolor{stringliteral}{"{}width\ \%d\(\backslash\)n"{}},\ lmath-\/>t.width);}
\DoxyCodeLine{00293\ \ \ \ \ fprintf(fp,\ \textcolor{stringliteral}{"{}shift\ \%d\(\backslash\)n"{}},\ lmath-\/>t.shift);}
\DoxyCodeLine{00294\ \ \ \ \ fprintf(fp,\ \textcolor{stringliteral}{"{}logbase\ \%f\(\backslash\)n"{}},\ lmath-\/>base);}
\DoxyCodeLine{00295\ \ \ \ \ \textcolor{comment}{/*\ Pad\ it\ out\ to\ ensure\ alignment.\ */}}
\DoxyCodeLine{00296\ \ \ \ \ pos\ =\ ftell(fp)\ +\ strlen(\textcolor{stringliteral}{"{}endhdr\(\backslash\)n"{}});}
\DoxyCodeLine{00297\ \ \ \ \ \textcolor{keywordflow}{if}\ (pos\ \&\ ((\textcolor{keywordtype}{long})lmath-\/>t.width\ -\/\ 1))\ \{}
\DoxyCodeLine{00298\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{size\_t}\ align\ =\ lmath-\/>t.width\ -\/\ (pos\ \&\ ((long)lmath-\/>t.width\ -\/\ 1));}
\DoxyCodeLine{00299\ \ \ \ \ \ \ \ \ assert(lmath-\/>t.width\ <=\ 8);}
\DoxyCodeLine{00300\ \ \ \ \ \ \ \ \ fwrite(\textcolor{stringliteral}{"{}\ \ \ \ \ \ \ \ "{}}\ \textcolor{comment}{/*\ 8\ spaces\ */},\ 1,\ align,\ fp);}
\DoxyCodeLine{00301\ \ \ \ \ \}}
\DoxyCodeLine{00302\ \ \ \ \ fprintf(fp,\ \textcolor{stringliteral}{"{}endhdr\(\backslash\)n"{}});}
\DoxyCodeLine{00303\ }
\DoxyCodeLine{00304\ \ \ \ \ \textcolor{comment}{/*\ Now\ write\ the\ binary\ data.\ */}}
\DoxyCodeLine{00305\ \ \ \ \ chksum\ =\ (uint32)BYTE\_ORDER\_MAGIC;}
\DoxyCodeLine{00306\ \ \ \ \ fwrite(\&chksum,\ \textcolor{keyword}{sizeof}(uint32),\ 1,\ fp);}
\DoxyCodeLine{00307\ \ \ \ \ chksum\ =\ 0;}
\DoxyCodeLine{00308\ \ \ \ \ \textcolor{comment}{/*\ \#Values\ to\ follow\ */}}
\DoxyCodeLine{00309\ \ \ \ \ \textcolor{keywordflow}{if}\ (bio\_fwrite(\&lmath-\/>t.table\_size,\ \textcolor{keyword}{sizeof}(uint32),}
\DoxyCodeLine{00310\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 1,\ fp,\ 0,\ \&chksum)\ !=\ 1)\ \{}
\DoxyCodeLine{00311\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Failed\ to\ write\ data\ to\ a\ file\ '\%s'"{}},\ file\_name);}
\DoxyCodeLine{00312\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ error\_out;}
\DoxyCodeLine{00313\ \ \ \ \ \}}
\DoxyCodeLine{00314\ }
\DoxyCodeLine{00315\ \ \ \ \ \textcolor{keywordflow}{if}\ (bio\_fwrite(lmath-\/>t.table,\ lmath-\/>t.width,\ lmath-\/>t.table\_size,}
\DoxyCodeLine{00316\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ fp,\ 0,\ \&chksum)\ !=\ lmath-\/>t.table\_size)\ \{}
\DoxyCodeLine{00317\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Failed\ to\ write\ data\ (\%d\ x\ \%d\ bytes)\ to\ the\ file\ '\%s'"{}},}
\DoxyCodeLine{00318\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ lmath-\/>t.table\_size,\ lmath-\/>t.width,\ file\_name);}
\DoxyCodeLine{00319\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ error\_out;}
\DoxyCodeLine{00320\ \ \ \ \ \}}
\DoxyCodeLine{00321\ \ \ \ \ \textcolor{keywordflow}{if}\ (bio\_fwrite(\&chksum,\ \textcolor{keyword}{sizeof}(uint32),\ 1,\ fp,\ 0,\ NULL)\ !=\ 1)\ \{}
\DoxyCodeLine{00322\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Failed\ to\ write\ checksum\ to\ the\ file\ '\%s'"{}},\ file\_name);}
\DoxyCodeLine{00323\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ error\_out;}
\DoxyCodeLine{00324\ \ \ \ \ \}}
\DoxyCodeLine{00325\ }
\DoxyCodeLine{00326\ \ \ \ \ fclose(fp);}
\DoxyCodeLine{00327\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00328\ }
\DoxyCodeLine{00329\ error\_out:}
\DoxyCodeLine{00330\ \ \ \ \ fclose(fp);}
\DoxyCodeLine{00331\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00332\ \}}
\DoxyCodeLine{00333\ }
\DoxyCodeLine{00334\ logmath\_t\ *}
\DoxyCodeLine{00335\ logmath\_retain(logmath\_t\ *lmath)}
\DoxyCodeLine{00336\ \{}
\DoxyCodeLine{00337\ \ \ \ \ ++lmath-\/>refcount;}
\DoxyCodeLine{00338\ \ \ \ \ \textcolor{keywordflow}{return}\ lmath;}
\DoxyCodeLine{00339\ \}}
\DoxyCodeLine{00340\ }
\DoxyCodeLine{00341\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00342\ logmath\_free(logmath\_t\ *lmath)}
\DoxyCodeLine{00343\ \{}
\DoxyCodeLine{00344\ \ \ \ \ \textcolor{keywordflow}{if}\ (lmath\ ==\ NULL)}
\DoxyCodeLine{00345\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00346\ \ \ \ \ \textcolor{keywordflow}{if}\ (-\/-\/lmath-\/>refcount\ >\ 0)}
\DoxyCodeLine{00347\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ lmath-\/>refcount;}
\DoxyCodeLine{00348\ \ \ \ \ \textcolor{keywordflow}{if}\ (lmath-\/>filemap)}
\DoxyCodeLine{00349\ \ \ \ \ \ \ \ \ mmio\_file\_unmap(lmath-\/>filemap);}
\DoxyCodeLine{00350\ \ \ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{00351\ \ \ \ \ \ \ \ \ ckd\_free(lmath-\/>t.table);}
\DoxyCodeLine{00352\ \ \ \ \ ckd\_free(lmath);}
\DoxyCodeLine{00353\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00354\ \}}
\DoxyCodeLine{00355\ }
\DoxyCodeLine{00356\ int32}
\DoxyCodeLine{00357\ logmath\_get\_table\_shape(logmath\_t\ *lmath,\ uint32\ *out\_size,}
\DoxyCodeLine{00358\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ uint32\ *out\_width,\ uint32\ *out\_shift)}
\DoxyCodeLine{00359\ \{}
\DoxyCodeLine{00360\ \ \ \ \ \textcolor{keywordflow}{if}\ (out\_size)\ *out\_size\ =\ lmath-\/>t.table\_size;}
\DoxyCodeLine{00361\ \ \ \ \ \textcolor{keywordflow}{if}\ (out\_width)\ *out\_width\ =\ lmath-\/>t.width;}
\DoxyCodeLine{00362\ \ \ \ \ \textcolor{keywordflow}{if}\ (out\_shift)\ *out\_shift\ =\ lmath-\/>t.shift;}
\DoxyCodeLine{00363\ }
\DoxyCodeLine{00364\ \ \ \ \ \textcolor{keywordflow}{return}\ lmath-\/>t.table\_size\ *\ lmath-\/>t.width;}
\DoxyCodeLine{00365\ \}}
\DoxyCodeLine{00366\ }
\DoxyCodeLine{00367\ float64}
\DoxyCodeLine{00368\ logmath\_get\_base(logmath\_t\ *lmath)}
\DoxyCodeLine{00369\ \{}
\DoxyCodeLine{00370\ \ \ \ \ \textcolor{keywordflow}{return}\ lmath-\/>base;}
\DoxyCodeLine{00371\ \}}
\DoxyCodeLine{00372\ }
\DoxyCodeLine{00373\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00374\ logmath\_get\_zero(logmath\_t\ *lmath)}
\DoxyCodeLine{00375\ \{}
\DoxyCodeLine{00376\ \ \ \ \ \textcolor{keywordflow}{return}\ lmath-\/>zero;}
\DoxyCodeLine{00377\ \}}
\DoxyCodeLine{00378\ }
\DoxyCodeLine{00379\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00380\ logmath\_get\_width(logmath\_t\ *lmath)}
\DoxyCodeLine{00381\ \{}
\DoxyCodeLine{00382\ \ \ \ \ \textcolor{keywordflow}{return}\ lmath-\/>t.width;}
\DoxyCodeLine{00383\ \}}
\DoxyCodeLine{00384\ }
\DoxyCodeLine{00385\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00386\ logmath\_get\_shift(logmath\_t\ *lmath)}
\DoxyCodeLine{00387\ \{}
\DoxyCodeLine{00388\ \ \ \ \ \textcolor{keywordflow}{return}\ lmath-\/>t.shift;}
\DoxyCodeLine{00389\ \}}
\DoxyCodeLine{00390\ }
\DoxyCodeLine{00391\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00392\ logmath\_add(logmath\_t\ *lmath,\ \textcolor{keywordtype}{int}\ logb\_x,\ \textcolor{keywordtype}{int}\ logb\_y)}
\DoxyCodeLine{00393\ \{}
\DoxyCodeLine{00394\ \ \ \ \ logadd\_t\ *t\ =\ LOGMATH\_TABLE(lmath);}
\DoxyCodeLine{00395\ \ \ \ \ \textcolor{keywordtype}{int}\ d,\ r;}
\DoxyCodeLine{00396\ }
\DoxyCodeLine{00397\ \ \ \ \ \textcolor{comment}{/*\ handle\ 0\ +\ x\ =\ x\ case.\ */}}
\DoxyCodeLine{00398\ \ \ \ \ \textcolor{keywordflow}{if}\ (logb\_x\ <=\ lmath-\/>zero)}
\DoxyCodeLine{00399\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ logb\_y;}
\DoxyCodeLine{00400\ \ \ \ \ \textcolor{keywordflow}{if}\ (logb\_y\ <=\ lmath-\/>zero)}
\DoxyCodeLine{00401\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ logb\_x;}
\DoxyCodeLine{00402\ }
\DoxyCodeLine{00403\ \ \ \ \ \textcolor{keywordflow}{if}\ (t-\/>table\ ==\ NULL)}
\DoxyCodeLine{00404\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ logmath\_add\_exact(lmath,\ logb\_x,\ logb\_y);}
\DoxyCodeLine{00405\ }
\DoxyCodeLine{00406\ \ \ \ \ \textcolor{comment}{/*\ d\ must\ be\ positive,\ obviously.\ */}}
\DoxyCodeLine{00407\ \ \ \ \ \textcolor{keywordflow}{if}\ (logb\_x\ >\ logb\_y)\ \{}
\DoxyCodeLine{00408\ \ \ \ \ \ \ \ \ d\ =\ (logb\_x\ -\/\ logb\_y);}
\DoxyCodeLine{00409\ \ \ \ \ \ \ \ \ r\ =\ logb\_x;}
\DoxyCodeLine{00410\ \ \ \ \ \}}
\DoxyCodeLine{00411\ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00412\ \ \ \ \ \ \ \ \ d\ =\ (logb\_y\ -\/\ logb\_x);}
\DoxyCodeLine{00413\ \ \ \ \ \ \ \ \ r\ =\ logb\_y;}
\DoxyCodeLine{00414\ \ \ \ \ \}}
\DoxyCodeLine{00415\ }
\DoxyCodeLine{00416\ \ \ \ \ \textcolor{keywordflow}{if}\ (d\ <\ 0)\ \{}
\DoxyCodeLine{00417\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Some\ kind\ of\ overflow\ has\ occurred,\ fail\ gracefully.\ */}}
\DoxyCodeLine{00418\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ r;}
\DoxyCodeLine{00419\ \ \ \ \ \}}
\DoxyCodeLine{00420\ \ \ \ \ \textcolor{keywordflow}{if}\ ((\textcolor{keywordtype}{size\_t})d\ >=\ t-\/>table\_size)\ \{}
\DoxyCodeLine{00421\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ If\ this\ happens,\ it's\ not\ actually\ an\ error,\ because\ the}}
\DoxyCodeLine{00422\ \textcolor{comment}{\ \ \ \ \ \ \ \ \ *\ last\ entry\ in\ the\ logadd\ table\ is\ guaranteed\ to\ be\ zero.}}
\DoxyCodeLine{00423\ \textcolor{comment}{\ \ \ \ \ \ \ \ \ *\ Therefore\ we\ just\ return\ the\ larger\ of\ the\ two\ values.\ */}}
\DoxyCodeLine{00424\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ r;}
\DoxyCodeLine{00425\ \ \ \ \ \}}
\DoxyCodeLine{00426\ }
\DoxyCodeLine{00427\ \ \ \ \ \textcolor{keywordflow}{switch}\ (t-\/>width)\ \{}
\DoxyCodeLine{00428\ \ \ \ \ \textcolor{keywordflow}{case}\ 1:}
\DoxyCodeLine{00429\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ r\ +\ (((uint8\ *)t-\/>table)[d]);}
\DoxyCodeLine{00430\ \ \ \ \ \textcolor{keywordflow}{case}\ 2:}
\DoxyCodeLine{00431\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ r\ +\ (((uint16\ *)t-\/>table)[d]);}
\DoxyCodeLine{00432\ \ \ \ \ \textcolor{keywordflow}{case}\ 4:}
\DoxyCodeLine{00433\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ r\ +\ (((uint32\ *)t-\/>table)[d]);}
\DoxyCodeLine{00434\ \ \ \ \ \}}
\DoxyCodeLine{00435\ \ \ \ \ \textcolor{keywordflow}{return}\ r;}
\DoxyCodeLine{00436\ \}}
\DoxyCodeLine{00437\ }
\DoxyCodeLine{00438\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00439\ logmath\_add\_exact(logmath\_t\ *lmath,\ \textcolor{keywordtype}{int}\ logb\_p,\ \textcolor{keywordtype}{int}\ logb\_q)}
\DoxyCodeLine{00440\ \{}
\DoxyCodeLine{00441\ \ \ \ \ \textcolor{keywordflow}{return}\ logmath\_log(lmath,}
\DoxyCodeLine{00442\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ logmath\_exp(lmath,\ logb\_p)}
\DoxyCodeLine{00443\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ +\ logmath\_exp(lmath,\ logb\_q));}
\DoxyCodeLine{00444\ \}}
\DoxyCodeLine{00445\ }
\DoxyCodeLine{00446\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00447\ logmath\_log(logmath\_t\ *lmath,\ float64\ p)}
\DoxyCodeLine{00448\ \{}
\DoxyCodeLine{00449\ \ \ \ \ \textcolor{keywordflow}{if}\ (p\ <=\ 0)\ \{}
\DoxyCodeLine{00450\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ lmath-\/>zero;}
\DoxyCodeLine{00451\ \ \ \ \ \}}
\DoxyCodeLine{00452\ \ \ \ \ \textcolor{keywordflow}{return}\ (\textcolor{keywordtype}{int})(log(p)\ *\ lmath-\/>inv\_log\_of\_base)\ >>\ lmath-\/>t.shift;}
\DoxyCodeLine{00453\ \}}
\DoxyCodeLine{00454\ }
\DoxyCodeLine{00455\ float64}
\DoxyCodeLine{00456\ logmath\_exp(logmath\_t\ *lmath,\ \textcolor{keywordtype}{int}\ logb\_p)}
\DoxyCodeLine{00457\ \{}
\DoxyCodeLine{00458\ \ \ \ \ \textcolor{keywordflow}{return}\ pow(lmath-\/>base,\ (float64)(logb\_p\ <<\ lmath-\/>t.shift));}
\DoxyCodeLine{00459\ \}}
\DoxyCodeLine{00460\ }
\DoxyCodeLine{00461\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00462\ logmath\_ln\_to\_log(logmath\_t\ *lmath,\ float64\ log\_p)}
\DoxyCodeLine{00463\ \{}
\DoxyCodeLine{00464\ \ \ \ \ \textcolor{keywordflow}{return}\ (\textcolor{keywordtype}{int})(log\_p\ *\ lmath-\/>inv\_log\_of\_base)\ >>\ lmath-\/>t.shift;}
\DoxyCodeLine{00465\ \}}
\DoxyCodeLine{00466\ }
\DoxyCodeLine{00467\ float64}
\DoxyCodeLine{00468\ logmath\_log\_to\_ln(logmath\_t\ *lmath,\ \textcolor{keywordtype}{int}\ logb\_p)}
\DoxyCodeLine{00469\ \{}
\DoxyCodeLine{00470\ \ \ \ \ \textcolor{keywordflow}{return}\ (float64)(logb\_p\ <<\ lmath-\/>t.shift)\ *\ lmath-\/>log\_of\_base;}
\DoxyCodeLine{00471\ \}}
\DoxyCodeLine{00472\ }
\DoxyCodeLine{00473\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00474\ logmath\_log10\_to\_log(logmath\_t\ *lmath,\ float64\ log\_p)}
\DoxyCodeLine{00475\ \{}
\DoxyCodeLine{00476\ \ \ \ \ \textcolor{keywordflow}{return}\ (\textcolor{keywordtype}{int})(log\_p\ *\ lmath-\/>inv\_log10\_of\_base)\ >>\ lmath-\/>t.shift;}
\DoxyCodeLine{00477\ \}}
\DoxyCodeLine{00478\ }
\DoxyCodeLine{00479\ float64}
\DoxyCodeLine{00480\ logmath\_log\_to\_log10(logmath\_t\ *lmath,\ \textcolor{keywordtype}{int}\ logb\_p)}
\DoxyCodeLine{00481\ \{}
\DoxyCodeLine{00482\ \ \ \ \ \textcolor{keywordflow}{return}\ (float64)(logb\_p\ <<\ lmath-\/>t.shift)\ *\ lmath-\/>log10\_of\_base;}
\DoxyCodeLine{00483\ \}}

\end{DoxyCode}
