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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
|
/*
* fastmath2.c
* Test for new math-routines in another way
* uses lookup tables
*
* Created on: 24.12.2011
* Author: holger
*/
// gcc -o fastmath2 fastmath2.c -lm -ffast-math
// if this is better in speed as with -ffast-math , then use it
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include <time.h>
#include <stdint.h>
/// fast exp implementation
static union{
double d;
struct{
int j,i;
} n;
} d2i;
#ifndef M_LN2
#define M_LN2 0.69314718055994530942
#endif
#define EXP_A (1048576/M_LN2)
#define EXP_C 60801
//#define fastexp(y) (d2i.n.i = EXP_A*(y)+(1072693248-EXP_C),d2i.d)
inline double fastexp(const double y){
return (d2i.n.i = EXP_A*(y)+(1072693248-EXP_C),d2i.d);
}
// fast fabs routine
//inline float fastfabs(const float f) {
// int i=((*(int*)&f)&0x7fffffff);
// return (*(float*)&i);
//}
inline float fastfabs(const float n){
if(n >= 0.0f)return n; //if positive, return without any change
else return 0.0f - n; //if negative, return a positive version
}
inline float fastneg(const float f) {
int i=((*(int*)&f)^0x80000000);
return (*(float*)&i);
}
inline int fastsgn(const float f) {
return 1+(((*(int*)&f)>>31)<<1);
}
// fast sqrt with table lookup (from Nvidia)
//typedef unsigned char BYTE; // better: uint8_t out <stdint.h>
//typedef unsigned short WORD; // better: uint16_t out <stdint.h>
//typedef unsigned long DWORD; // better: uint32_t out <stdint.h>
//typedef unsigned long QWORD; // better: uint64_t out <stdint.h>
//#define FP_BITS(fp) (*(DWORD *)&(fp))
#define FP_BITS(fp) (*(uint32_t *)&(fp))
#define FP_ABS_BITS(fp) (FP_BITS(fp)&0x7FFFFFFF)
#define FP_SIGN_BIT(fp) (FP_BITS(fp)&0x80000000)
#define FP_ONE_BITS 0x3F800000
static unsigned int fast_sqrt_table[0x10000]; // declare table of square roots
typedef union FastSqrtUnion
{
float f;
unsigned int i;
} FastSqrtUnion;
void build_sqrt_table()
{
unsigned int i;
FastSqrtUnion s;
for (i = 0; i <= 0x7FFF; i++)
{
// Build a float with the bit pattern i as mantissa
// and an exponent of 0, stored as 127
s.i = (i << 8) | (0x7F << 23);
s.f = (float)sqrt(s.f);
// Take the square root then strip the first 7 bits of
// the mantissa into the table
fast_sqrt_table[i + 0x8000] = (s.i & 0x7FFFFF);
// Repeat the process, this time with an exponent of 1,
// stored as 128
s.i = (i << 8) | (0x80 << 23);
s.f = (float)sqrt(s.f);
fast_sqrt_table[i] = (s.i & 0x7FFFFF);
}
}
inline float fastsqrt(float n)
{
if (FP_BITS(n) == 0)
return 0.0; // check for square root of 0
FP_BITS(n) = fast_sqrt_table[(FP_BITS(n) >> 8) & 0xFFFF] | ((((FP_BITS(n) - 0x3F800000) >> 1) + 0x3F800000) & 0x7F800000);
return n;
}
//fast pow only with double (float won't work)
double fastpow(double a, double b) {
int tmp = (*(1 + (int *)&a));
int tmp2 = (int)(b * (tmp - 1072632447) + 1072632447);
double p = 0.0;
//*(1 + (int * )&p) = tmp2;
//return p;
union { double d; int x[2]; } u = { p }; u.x[1] = tmp2;
return u.d;
}
// fast atan only with float
float fastatan(float x)
{
return M_PI_4*x - x*(fabs(x) - 1)*(0.2447 + 0.0663*fabs(x));
}
// fast atan2 only with float
float fastatan2(float y, float x) {
float coeff_1 = M_PI / 4.0f;
float coeff_2 = 3.0f * coeff_1;
float abs_y = abs(y);
float angle;
if (x > 0.0f) {
float r = (x - abs_y) / (x + abs_y);
angle = coeff_1 - coeff_1 * r;
} else {
float r = (x + abs_y) / (abs_y - x);
angle = coeff_2 - coeff_1 * r;
}
return y < 0.0f ? -angle : angle;
}
#define MAX_CIRCLE_ANGLE 8192
#define HALF_MAX_CIRCLE_ANGLE (MAX_CIRCLE_ANGLE/2)
#define QUARTER_MAX_CIRCLE_ANGLE (MAX_CIRCLE_ANGLE/4)
#define MASK_MAX_CIRCLE_ANGLE (MAX_CIRCLE_ANGLE - 1)
float fast_cossin_table[MAX_CIRCLE_ANGLE]; // Declare table of fast cosinus and sinus
inline float fastcos(float n) {
float f = n * HALF_MAX_CIRCLE_ANGLE / M_PI;
int i;
i = (int)f;
if (i < 0) {
return fast_cossin_table[((-i) + QUARTER_MAX_CIRCLE_ANGLE)&MASK_MAX_CIRCLE_ANGLE];
} else {
return fast_cossin_table[(i + QUARTER_MAX_CIRCLE_ANGLE)&MASK_MAX_CIRCLE_ANGLE];
}
assert(0);
}
inline float fastsin(float n) {
float f = n * HALF_MAX_CIRCLE_ANGLE / M_PI;
int i;
i = (int)f;
if (i < 0) {
return fast_cossin_table[(-((-i)&MASK_MAX_CIRCLE_ANGLE)) + MAX_CIRCLE_ANGLE];
} else {
return fast_cossin_table[i&MASK_MAX_CIRCLE_ANGLE];
}
assert(0);
}
const long iMaxTests = 10000000;
int main(int argc,char *argv[])
{
long i;
float s, c;
//float e[iMaxTests];
float sc, scr = 0;
unsigned long dwTickStart, dwTickEnd, dwDuration;
// Build cossin table
for (i = 0 ; i < MAX_CIRCLE_ANGLE ; i++)
{
fast_cossin_table[i] = (float)sin((double)i * M_PI / HALF_MAX_CIRCLE_ANGLE);
}
double d;
dwTickStart = clock();
for (i = - (iMaxTests/2) ; i < iMaxTests/2 ; i++)
{
d = (double)i;
s = (float)sin(d);
c = (float)cos(d);
// This exist only to force optimiser to not delete code
sc = s * c;
if (sc > scr)
{
scr = sc;
}
}
dwTickEnd = clock();
dwDuration = dwTickEnd - dwTickStart;
printf("%d sin and cos computed in %d ticks with standard math funcs\n", iMaxTests, dwDuration);
float f;
dwTickStart = clock();
for (i = - (iMaxTests/2) ; i < iMaxTests/2 ; i++)
{
f = (float)i;
s = fastsin(f);
c = fastcos(f);
// This exist only to force optimiser to not delete code
sc = s * c;
if (sc > scr)
{
scr = sc;
}
}
dwTickEnd = clock();
dwDuration = dwTickEnd - dwTickStart;
printf("%d sin and cos computed in %d ticks with fast[cos/sin] lookup-table\n", iMaxTests, dwDuration);
float h;
dwTickStart = clock();
for (i = - (iMaxTests/2) ; i < iMaxTests/2 ; i++)
{
f = (float)i;
h = f;
s = atan2(f,h);
// This exist only to force optimiser to not delete code
sc = s * c;
if (sc > scr)
{
scr = sc;
}
}
dwTickEnd = clock();
dwDuration = dwTickEnd - dwTickStart;
printf("%d atan2 computed in %d ticks with normal[atan2]\n", iMaxTests, dwDuration);
dwTickStart = clock();
for (i = - (iMaxTests/2) ; i < iMaxTests/2 ; i++)
{
f = (float)i;
h = f;
s = fastatan2(f,h);
// This exist only to force optimiser to not delete code
sc = s * c;
if (sc > scr)
{
scr = sc;
}
}
dwTickEnd = clock();
dwDuration = dwTickEnd - dwTickStart;
printf("%d atan2 computed in %d ticks with fast[atan2]\n", iMaxTests, dwDuration);
dwTickStart = clock();
for (i = - (iMaxTests/2) ; i < iMaxTests/2 ; i++)
{
f = (float)i;
s = fastatan(f);
// This exist only to force optimiser to not delete code
sc = s * c;
if (sc > scr)
{
scr = sc;
}
}
dwTickEnd = clock();
dwDuration = dwTickEnd - dwTickStart;
printf("%d atan computed in %d ticks with fast[atan]\n", iMaxTests, dwDuration);
dwTickStart = clock();
for (i = - (iMaxTests/2) ; i < iMaxTests/2 ; i++)
{
f = (float)i;
s = atan(f);
// This exist only to force optimiser to not delete code
sc = s * c;
if (sc > scr)
{
scr = sc;
}
}
dwTickEnd = clock();
dwDuration = dwTickEnd - dwTickStart;
printf("%d atan computed in %d ticks with normal[atan]\n", iMaxTests, dwDuration);
dwTickStart = clock();
for (i = - (iMaxTests/2) ; i < iMaxTests/2 ; i++)
{
f = (float)i;
s = pow(f, f);
// This exist only to force optimiser to not delete code
sc = s * c;
if (sc > scr)
{
scr = sc;
}
}
dwTickEnd = clock();
dwDuration = dwTickEnd - dwTickStart;
printf("%d pow computed in %d ticks with normal[pow]\n", iMaxTests, dwDuration);
dwTickStart = clock();
for (i = - (iMaxTests/2) ; i < iMaxTests/2 ; i++)
{
f = (float)i;
s = fastpow(f, f);
// This exist only to force optimiser to not delete code
sc = s * c;
if (sc > scr)
{
scr = sc;
}
}
dwTickEnd = clock();
dwDuration = dwTickEnd - dwTickStart;
printf("%d pow computed in %d ticks with fast[pow]\n", iMaxTests, dwDuration);
build_sqrt_table();
dwTickStart = clock();
for (i = - (iMaxTests/2) ; i < iMaxTests/2 ; i++)
{
f = (float)i;
s = sqrt(f);
//e[i] = s;
// This exist only to force optimiser to not delete code
sc = s * c;
if (sc > scr)
{
scr = sc;
}
}
dwTickEnd = clock();
dwDuration = dwTickEnd - dwTickStart;
printf("%d sqrt computed in %d ticks with normal[sqrt]\n", iMaxTests, dwDuration);
dwTickStart = clock();
for (i = - (iMaxTests/2) ; i < iMaxTests/2 ; i++)
{
f = (float)i;
s = fastsqrt(f);
//fprintf(stderr,"%f %f\n",s,e[i]);
// This exist only to force optimiser to not delete code
sc = s * c;
if (sc > scr)
{
scr = sc;
}
}
dwTickEnd = clock();
dwDuration = dwTickEnd - dwTickStart;
printf("%d sqrt computed in %d ticks with fast[sqrt]\n", iMaxTests, dwDuration);
dwTickStart = clock();
for (i = - (iMaxTests/2) ; i < iMaxTests/2 ; i++)
{
f = (float)i;
s = fabs(f);
// This exist only to force optimiser to not delete code
sc = s * c;
if (sc > scr)
{
scr = sc;
}
}
dwTickEnd = clock();
dwDuration = dwTickEnd - dwTickStart;
printf("%d fabs computed in %d ticks with normal[fabs]\n", iMaxTests, dwDuration);
dwTickStart = clock();
for (i = - (iMaxTests/2) ; i < iMaxTests/2 ; i++)
{
f = (float)i;
s = fastfabs(f);
// This exist only to force optimiser to not delete code
sc = s * c;
if (sc > scr)
{
scr = sc;
}
}
dwTickEnd = clock();
dwDuration = dwTickEnd - dwTickStart;
printf("%d fabs computed in %d ticks with fast[fabs]\n", iMaxTests, dwDuration);
printf("Too slow, not implemented.... think a little bit about a new fabs......\n");
dwTickStart = clock();
for (i = - (iMaxTests/2) ; i < iMaxTests/2 ; i++)
{
f = (float)i;
s = exp(f);
//e[i] = s;
// This exist only to force optimiser to not delete code
sc = s * c;
if (sc > scr)
{
scr = sc;
}
}
dwTickEnd = clock();
dwDuration = dwTickEnd - dwTickStart;
printf("%d exp computed in %d ticks with normal[exp]\n", iMaxTests, dwDuration);
dwTickStart = clock();
for (i = - (iMaxTests/2) ; i < iMaxTests/2 ; i++)
{
f = (float)i;
s = fastexp(f);
//fprintf(stderr,"%f %f\n",s,e[i]);
// This exist only to force optimiser to not delete code
sc = s * c;
if (sc > scr)
{
scr = sc;
}
}
dwTickEnd = clock();
dwDuration = dwTickEnd - dwTickStart;
printf("%d exp computed in %d ticks with fast[exp]\n", iMaxTests, dwDuration);
}
|