1 /*
2 	dsp/util.h
3 
4 	Copyright 2002-12 Tim Goetze <tim@quitte.de>
5 
6 	http://quitte.de/dsp/
7 
8 	Common math utility functions.
9 
10 */
11 /*
12 	This program is free software; you can redistribute it and/or
13 	modify it under the terms of the GNU General Public License
14 	as published by the Free Software Foundation; either version 3
15 	of the License, or (at your option) any later version.
16 
17 	This program is distributed in the hope that it will be useful,
18 	but WITHOUT ANY WARRANTY; without even the implied warranty of
19 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 	GNU General Public License for more details.
21 
22 	You should have received a copy of the GNU General Public License
23 	along with this program; if not, write to the Free Software
24 	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 	02111-1307, USA or point your web browser to http://www.gnu.org.
26 */
27 
28 #ifndef DSP_UTIL_H
29 #define DSP_UTIL_H
30 
31 namespace DSP {
32 
pow2(float x)33 inline float pow2 (float x) { return x * x; }
pow3(float x)34 inline float pow3 (float x) { return x * pow2(x); }
pow4(float x)35 inline float pow4 (float x) { return pow2 (pow2(x)); }
pow5(float x)36 inline float pow5 (float x) { return x * pow4(x); }
pow6(float x)37 inline float pow6 (float x) { return pow3 (pow2(x)); }
pow7(float x)38 inline float pow7 (float x) { return x * (pow6 (x)); }
pow8(float x)39 inline float pow8 (float x) { return pow2 (pow4 (x)); }
40 
41 inline float
sgn(float x)42 sgn (float x)
43 {
44 	union { float f; uint32 i; } u;
45 	u.f = x;
46 	u.i &= 0x80000000;
47 	u.i |= 0x3F800000;
48 	return u.f;
49 }
50 
51 inline bool
isprime(int v)52 isprime (int v)
53 {
54 	if (v <= 3)
55 		return true;
56 
57 	if (!(v & 1))
58 		return false;
59 
60 	for (int i = 3; i < (int) sqrt (v) + 1; i += 2)
61 		if ((v % i) == 0)
62 			return false;
63 
64 	return true;
65 }
66 
67 } /* namespace DSP */
68 
69 #endif /* DSP_UTIL_H */
70