Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimizing the code: up to +19% performance on core i7 2700k with -O2 option #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 16 additions & 25 deletions Source/Noise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,49 +49,40 @@ static inline float lerp (float a0, float a1, float w)
return (1.0f - w) * a0 + w * a1;
}

float Noise::gen1 (float z, float torsion)
inline float gen1 (float *gradients, float z, float torsion)
{
float g1, g2, d2;
float result = 0;
const int z1 = static_cast<int>(z);
const float dz1 = z - z1;
const float dz2 = dz1 - 1.0f;

int z1 = (int) z;
int z2 = (z1 + 1);
float g1 = gradients[z1] + torsion;
float g2 = gradients[z1 + 1] + torsion;

float dz1 = z - z1;
float dz2 = dz1 - 1.0f;
float d1 = 1.0f - (dz1 * dz1);
d1 = d1 * d1 * d1 * d1;

g1 = gradients[z1];
g2 = gradients[z2];
float d2 = 1.0f - (dz2 * dz2);
d2 = d2 * d2 * d2 * d2;

g1 = g1 + torsion;
if (g1 > 1.0f) { g1 = 2.0f - g1; }
if (g1 < -1.0f) { g1 = -2.0f - g1; }
else if (g1 < -1.0f) { g1 = -2.0f - g1; }

g2 = g2 + torsion;
if (g2 > 1.0f) { g2 = 2.0f - g2; }
if (g2 < -1.0f) { g2 = -2.0f - g2; }

d2 = 1.0 - (dz1 * dz1);
d2 = d2 * d2 * d2 * d2;
result += d2 * (g1 * dz1);

d2 = 1.0 - (dz2 * dz2);
d2 = d2 * d2 * d2 * d2;
result += d2 * (g2 * dz2);
else if (g2 < -1.0f) { g2 = -2.0f - g2; }

return result * 3.33; // temp normalization value
return (d1 * (g1 * dz1) + d2 * (g2 * dz2)) * 3.33f; // temp normalization value
}

float Noise::gen (float z, int octaves, float persistence, float torsion, float warping)
{
float result = 0;
float multiplier = 1.0f;
float offset = gen1 (z, torsion) * warping;
float offset = gen1 (gradients, z, torsion) * warping;

for (int octave = 0; octave < octaves; ++octave)
{
float t = fmod(torsion, 4);
float value = gen1 (z * (1 << octave) + offset, t) * multiplier;
float t = fmodf(torsion, 4);
float value = gen1 (gradients, z * (1 << octave) + offset, t) * multiplier;
offset = value * warping;
result += value;
multiplier *= persistence;
Expand Down
2 changes: 0 additions & 2 deletions Source/Noise.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,4 @@ class Noise
float gradients[65536];
std::mt19937 random;
uint32_t seed_;

float gen1 (float z, float torsion);
};