BLOG | DOCUMENTATION | TRAC

Home --> Documentations --> PJLIB Reference

math.h
Go to the documentation of this file.
1 /* $Id: math.h 3553 2011-05-05 06:14:19Z nanang $ */
2 /*
3  * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4  * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 #ifndef __PJ_MATH_H__
22 #define __PJ_MATH_H__
23 
29 #include <pj/string.h>
30 #include <pj/compat/high_precision.h>
31 
33 
48 #define PJ_PI 3.14159265358979323846 /* pi */
49 #define PJ_1_PI 0.318309886183790671538 /* 1/pi */
50 
54 #define PJ_ABS(x) ((x) > 0 ? (x) : -(x))
55 #define PJ_MAX(x, y) ((x) > (y)? (x) : (y))
56 #define PJ_MIN(x, y) ((x) < (y)? (x) : (y))
57 
61 typedef struct pj_math_stat
62 {
63  int n; /* number of samples */
64  int max; /* maximum value */
65  int min; /* minimum value */
66  int last; /* last value */
67  int mean; /* mean */
68 
69  /* Private members */
70 #if PJ_HAS_FLOATING_POINT
71  float fmean_; /* mean(floating point) */
72 #else
73  int mean_res_; /* mean residu */
74 #endif
75  pj_highprec_t m2_; /* variance * n */
76 } pj_math_stat;
77 
85 PJ_INLINE(unsigned) pj_isqrt(unsigned i)
86 {
87  unsigned res = 1, prev;
88 
89  /* Rough guess, calculate half bit of input */
90  prev = i >> 2;
91  while (prev) {
92  prev >>= 2;
93  res <<= 1;
94  }
95 
96  /* Babilonian method */
97  do {
98  prev = res;
99  res = (prev + i/prev) >> 1;
100  } while ((prev+res)>>1 != res);
101 
102  return res;
103 }
104 
111 {
112  pj_bzero(stat, sizeof(pj_math_stat));
113 }
114 
122 {
123 #if PJ_HAS_FLOATING_POINT
124  float delta;
125 #else
126  int delta;
127 #endif
128 
129  stat->last = val;
130 
131  if (stat->n++) {
132  if (stat->min > val)
133  stat->min = val;
134  if (stat->max < val)
135  stat->max = val;
136  } else {
137  stat->min = stat->max = val;
138  }
139 
140 #if PJ_HAS_FLOATING_POINT
141  delta = val - stat->fmean_;
142  stat->fmean_ += delta/stat->n;
143 
144  /* Return mean value with 'rounding' */
145  stat->mean = (int) (stat->fmean_ + 0.5);
146 
147  stat->m2_ += (int)(delta * (val-stat->fmean_));
148 #else
149  delta = val - stat->mean;
150  stat->mean += delta/stat->n;
151  stat->mean_res_ += delta % stat->n;
152  if (stat->mean_res_ >= stat->n) {
153  ++stat->mean;
154  stat->mean_res_ -= stat->n;
155  } else if (stat->mean_res_ <= -stat->n) {
156  --stat->mean;
157  stat->mean_res_ += stat->n;
158  }
159 
160  stat->m2_ += delta * (val-stat->mean);
161 #endif
162 }
163 
172 {
173  if (stat->n == 0) return 0;
174  return (pj_isqrt((unsigned)(stat->m2_/stat->n)));
175 }
176 
187 {
188  if (stat->n == 0)
189  stat->n = 1;
190  stat->m2_ = dev*dev*stat->n;
191 }
192 
196 
197 #endif /* __PJ_MATH_H__ */
Definition: math.h:61
unsigned pj_math_stat_get_stddev(const pj_math_stat *stat)
Definition: math.h:171
PJLIB String Operations.
void pj_math_stat_update(pj_math_stat *stat, int val)
Definition: math.h:121
#define PJ_END_DECL
Definition: config.h:1067
void pj_math_stat_init(pj_math_stat *stat)
Definition: math.h:110
unsigned pj_isqrt(unsigned i)
Definition: math.h:85
#define PJ_BEGIN_DECL
Definition: config.h:1066
#define PJ_INLINE(type)
Definition: config.h:960
void pj_bzero(void *dst, pj_size_t size)
Definition: string.h:619
void pj_math_stat_set_stddev(pj_math_stat *stat, unsigned dev)
Definition: math.h:186
typedef int(PJ_THREAD_FUNC pj_thread_proc)(void *)

 


PJLIB Open Source, high performance, small footprint, and very very portable framework
Copyright (C) 2006-2009 Teluu Inc.