therocode/featherkit — pull request #50 by Mischa-Alff (2014-10-31 02:23:59+00:00)
Title: Optimize C++ implementation of Simplex Noise

This PR optimizes the simplex noise implementations of `fea-util` by:
- Replacing `std::pow()` with expanded exponents
- Replacing `std::floor()` with a faster implementation that works using type casts

This method is better than the ASM optimization because:
- It's faster
- It's platform independent
- Sorry @FredYeye

The speed increase is around 32% on my Intel i7 4770:
```c++
/*
Built on ArchLinux x86_64 with GCC 4.9.1
Featherkit-util linked as static library
CPU: Intel Core i7 4770 @ 3.4 GHz
Tests were performed with TurboBoost on, and the clock speed was 3.75GHz

Featherkit with `-O3`:
- C++ Noise:
- With std::pow():
- Times (min, max, avg): 18500990us, 19314295us, 18761359us
- With expanded exponent:
- Times (min, max, avg): 13590441us, 13778924us, 13641680us
- With expanded exponent and fast_floor:
- Times (min, max, avg): 12741825us, 12785273us, 12761292us
- ASM Noise:
- Times (min, max, avg): 17619293us, 18048159us, 17756850us

Featherkit with `-O3 -march=native -mfpmath=sse`:
- C++ Noise:
- With std::pow():
- Times (min, max, avg): 16363849us, 16540378us, 16425663us
- With expanded exponent:
- Times (min, max, avg): 12010800us, 12160890us, 12042282us
- With expanded exponent and fast_floor:
- Times (min, max, avg): 11155214us, 11217809us, 11175114us
- ASM Noise:
- Times (min, max, avg): 17765922us, 17830874us, 17787408us

If I compile the example with `-O3 -march=native -mfpmath=sse`, I get:
- C++ Noise:
- With expanded exponent:
- Times (min, max, avg): 11598168us, 11839490us, 11654674us
- With expanded exponent and fast_floor:
- Times (min, max, avg): 10935452us, 10988102us, 10964837us

*/
```

The test program I used can be found here: https://gist.github.com/Mischa-Alff/e6407a9a9b5894a317a1