Mozzi  alpha 0.01.1t
sound synthesis library for Arduino
 All Classes Functions Typedefs
fixedMath.cpp
00001 #include "fixedMath.h"
00002 
00003 //Snipped from http://code.google.com/p/ht1632c/wiki/Optimizations
00004 //TB2012 changed names to not interfere with arduino compilation
00005 //Fast integer math
00006 //
00007 //If you need to include arithmetic operations in you code but you don't need
00008 //floating point operations, you could use boolean operations instead of arithmetic
00009 //operations, or use smaller data types and custom functions instead of stdlib functions
00010 //or C operators (expecially / and %).
00011 //Look at IntegerCodeSnippets, http://code.google.com/p/ht1632c/wiki/IntegerCodeSnippets
00012 //
00013 //Here is some ready to use fast integer 1 byte wide math functions (from ht1632c library).
00014 
00015 /* fast integer (1 byte) modulus */
00016 byte byteMod(byte n, byte d)
00017 {
00018                 while(n >= d)
00019                                 n -= d;
00020                 return n;
00021 }
00022 
00023 /* fast integer (1 byte) division */
00024 byte byteDiv(byte n, byte d)
00025 {
00026                 byte q = 0;
00027                 while(n >= d)
00028                 {
00029                                 n -= d;
00030                                 q++;
00031                 }
00032                 return q;
00033 }
00034 
00035 /* fast integer (1 byte) PRNG */
00036 byte byteRnd(byte min, byte max)
00037 {
00038                 static byte seed;
00039                 seed = (21 * seed + 21);
00040                 return min + byteMod(seed, --max);
00041 }
00042 //WARNING: don't use this byteRnd() function for cryptography!
00043 
00044 //end of snip from http://code.google.com/p/ht1632c/wiki/Optimizations
00045 
00046 
00047 // Exponentiation by squaring.
00048 // from http://stackoverflow.com/questions/101439/the-most-efficient-way-to-implement-an-integer-based-power-function-powint-int
00049 
00050 int ipow(int base, int exp)
00051 {
00052                 int result = 1;
00053                 while (exp)
00054                 {
00055                                 if (exp & 1)
00056                                                 result *= base;
00057                                 exp >>= 1;
00058                                 base *= base;
00059                 }
00060 
00061                 return result;
00062 }
00063 
00064 
00065 /*
00066 from: http://objectmix.com/vhdl/189970-2-powerof-x-where-x-fixed-point-value-2.html
00067  
00068 to do 2^(x.y) first find
00069 2^x and 2^(x+1) through bit shifting 1 to the left by x and (x + 1) places
00070  
00071 now you do linear interpolation by drawing a line through these two points 2^x,
00072 2^(x+1), then use f = m*x+b. the slope, m = rise over run
00073 = (2^(x+1) - 2^x)/((x+1) - (x))
00074 = 2^(x) * (2 - 1) / 1
00075 = 2^(x)
00076 b = 2^x, so to linearly interpolate do....(edited out typo)..
00077 f = 2^(x) * (y) + 2^x
00078 = 2^x * (y + 1)
00079 where x is integer part, y is fractional part
00080 */
00081 
00089 Q16n16 Q16n16_pow2(Q8n8 exponent)
00090 {
00091                 // to do 2^(x.y) first find
00092                 //2^x and 2^(x+1) through bit shifting 1 to the left by x and (x + 1) places
00093                 unsigned char Q = (unsigned char)((Q8n8)exponent>>8); // integer part
00094                 unsigned char n = (unsigned char) exponent; // fractional part
00095                 // f = 2^x * (y + 1)
00096                 return (((Q16n16)Q8n8_FIX1 << Q) * (Q8n8_FIX1 + n));
00097 }