Skip to content

Commit

Permalink
Merge pull request #136 from Snowiiii/revert-135-flops
Browse files Browse the repository at this point in the history
Revert "Optimize floating point operations"
  • Loading branch information
Snowiiii authored Oct 16, 2024
2 parents fe7f708 + 310d5d4 commit 5ed7cae
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pumpkin-core/src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn wrap_degrees(var: f32) -> f32 {
}

pub fn squared_magnitude(a: f64, b: f64, c: f64) -> f64 {
c.mul_add(c, a.mul_add(a, b * b))
a * a + b * b + c * c
}

pub fn magnitude(a: f64, b: f64, c: f64) -> f64 {
Expand Down
6 changes: 3 additions & 3 deletions pumpkin-core/src/random/gaussian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ pub trait GaussianGenerator: RandomImpl {
gaussian
} else {
loop {
let d = 2f64.mul_add(self.next_f64(), -1f64);
let e = 2f64.mul_add(self.next_f64(), -1f64);
let f = d.mul_add(d, e * e);
let d = 2f64 * self.next_f64() - 1f64;
let e = 2f64 * self.next_f64() - 1f64;
let f = d * d + e * e;

if f < 1f64 && f != 0f64 {
let g = (-2f64 * f.ln() / f).sqrt();
Expand Down
4 changes: 2 additions & 2 deletions pumpkin-core/src/random/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl RandomGenerator {

#[inline]
pub fn next_triangular(&mut self, mode: f64, deviation: f64) -> f64 {
deviation.mul_add(self.next_f64() - self.next_f64(), mode)
mode + deviation * (self.next_f64() - self.next_f64())
}

#[inline]
Expand Down Expand Up @@ -184,7 +184,7 @@ pub trait RandomImpl {
fn next_gaussian(&mut self) -> f64;

fn next_triangular(&mut self, mode: f64, deviation: f64) -> f64 {
deviation.mul_add(self.next_f64() - self.next_f64(), mode)
mode + deviation * (self.next_f64() - self.next_f64())
}

fn skip(&mut self, count: i32) {
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-world/src/world_gen/generic_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl<B: BiomeGenerator, T: PerlinTerrainGenerator> WorldGenerator for GenericGen

let base_height = 64.0;
let height_variation = 16.0;
let chunk_height = noise_value.mul_add(height_variation, base_height) as i32;
let chunk_height = (noise_value * height_variation + base_height) as i32;

for x in 0..16u8 {
for z in 0..16u8 {
Expand Down
7 changes: 3 additions & 4 deletions pumpkin-world/src/world_gen/noise/density/end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl EndIslandFunction {
let k = x % 2;
let l = z % 2;

let f = ((x * x + z * z) as f32).sqrt().mul_add(-8f32, 100f32);
let f = 100f32 - ((x * x + z * z) as f32).sqrt() * 8f32;
let mut f = f.clamp(-100f32, 80f32);

for m in -12..=12 {
Expand All @@ -40,11 +40,10 @@ impl EndIslandFunction {
if (o * o + p * p) > 4096i64
&& sampler.sample_2d(o as f64, p as f64) < -0.9f32 as f64
{
let g =
(o as f32).abs().mul_add(3439f32, (p as f32).abs() * 147f32) % 13f32 + 9f32;
let g = ((o as f32).abs() * 3439f32 + (p as f32).abs() * 147f32) % 13f32 + 9f32;
let h = (k - m * 2) as f32;
let q = (l - n * 2) as f32;
let r = h.hypot(q).mul_add(-g, 100f32);
let r = 100f32 - (h * h + q * q).sqrt() * g;
let s = r.clamp(-100f32, 80f32);

f = f.max(s);
Expand Down
6 changes: 3 additions & 3 deletions pumpkin-world/src/world_gen/noise/density/noise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ impl<'a> ShiftedNoiseFunction<'a> {

impl<'a> DensityFunctionImpl<'a> for ShiftedNoiseFunction<'a> {
fn sample(&self, pos: &NoisePos) -> f64 {
let d = (pos.x() as f64).mul_add(self.xz_scale, self.shift_x.sample(pos));
let e = (pos.y() as f64).mul_add(self.y_scale, self.shift_y.sample(pos));
let f = (pos.z() as f64).mul_add(self.xz_scale, self.shift_z.sample(pos));
let d = pos.x() as f64 * self.xz_scale + self.shift_x.sample(pos);
let e = pos.y() as f64 * self.y_scale + self.shift_y.sample(pos);
let f = pos.z() as f64 * self.xz_scale + self.shift_z.sample(pos);

self.noise.sample(d, e, f)
}
Expand Down
15 changes: 6 additions & 9 deletions pumpkin-world/src/world_gen/noise/density/spline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl<'a> Spline<'a> {
if f == 0f32 {
value
} else {
f.mul_add(point - points[i].location, value)
value + f * (point - points[i].location)
}
}

Expand Down Expand Up @@ -154,8 +154,8 @@ impl<'a> Spline<'a> {
let ad = z.min(ab);
let ae = aa.max(ac);

f = f.min(0.25f32.mul_add(ad, x));
g = g.max(0.25f32.mul_add(ae, y));
f = f.min(x + 0.25f32 * ad);
g = g.max(y + 0.25f32 * ae);
}
}

Expand Down Expand Up @@ -189,12 +189,9 @@ impl<'a> Spline<'a> {
let n = point_1.value.apply(pos);
let o = point_2.value.apply(pos);

let p = point_1
.derivative
.mul_add(point_2.location - point_1.location, -(o - n));
let q =
(-point_2.derivative).mul_add(point_2.location - point_1.location, o - n);
(k * (1f32 - k)).mul_add(lerp(k, p, q), lerp(k, n, o))
let p = point_1.derivative * (point_2.location - point_1.location) - (o - n);
let q = -point_2.derivative * (point_2.location - point_1.location) + (o - n);
lerp(k, n, o) + k * (1f32 - k) * lerp(k, p, q)
}
}
Range::Below => {
Expand Down
6 changes: 3 additions & 3 deletions pumpkin-world/src/world_gen/noise/density/terrain_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use crate::world_gen::noise::lerp;

#[inline]
fn get_offset_value(f: f32, g: f32, h: f32) -> f32 {
let k = (1f32 - g).mul_add(-0.5f32, 1f32);
let k = 1f32 - (1f32 - g) * 0.5f32;
let l = 0.5f32 * (1f32 - g);

let m = (f + 1.17f32) * 0.46082947f32;
let n = m.mul_add(k, -l);
let n = m * k - l;

if f < h {
n.max(-0.2222f32)
Expand All @@ -25,7 +25,7 @@ fn get_offset_value(f: f32, g: f32, h: f32) -> f32 {

#[inline]
fn skew_map(f: f32) -> f32 {
let k = (1f32 - f).mul_add(-0.5f32, 1f32);
let k = 1f32 - (1f32 - f) * 0.5f32;
let l = 0.5f32 * (1f32 - f);

l / (0.46082947f32 * k) - 1.17f32
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-world/src/world_gen/noise/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,5 +265,5 @@ const GRADIENTS: [Gradient; 16] = [
];

fn dot(gradient: &Gradient, x: f64, y: f64, z: f64) -> f64 {
(gradient.z as f64).mul_add(z, (gradient.x as f64).mul_add(x, gradient.y as f64 * y))
gradient.x as f64 * x + gradient.y as f64 * y + gradient.z as f64 * z
}
6 changes: 2 additions & 4 deletions pumpkin-world/src/world_gen/noise/perlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl PerlinNoiseSampler {
}

fn perlin_fade(value: f64) -> f64 {
value * value * value * value.mul_add(value.mul_add(6f64, -15f64), 10f64)
value * value * value * (value * (value * 6f64 - 15f64) + 10f64)
}

fn map(&self, input: i32) -> i32 {
Expand Down Expand Up @@ -183,9 +183,7 @@ impl OctavePerlinNoiseSampler {
}

pub fn maintain_precision(value: f64) -> f64 {
(value / 3.3554432E7f64 + 0.5f64)
.floor()
.mul_add(-3.3554432E7f64, value)
value - (value / 3.3554432E7f64 + 0.5f64).floor() * 3.3554432E7f64
}

pub fn calculate_amplitudes(octaves: &[i32]) -> (i32, Vec<f64>) {
Expand Down
10 changes: 5 additions & 5 deletions pumpkin-world/src/world_gen/noise/simplex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl SimplexNoiseSampler {
}

fn grad(gradient_index: usize, x: f64, y: f64, z: f64, distance: f64) -> f64 {
let d = z.mul_add(-z, y.mul_add(-y, x.mul_add(-x, distance)));
let d = distance - x * x - y * y - z * z;
if d < 0f64 {
0f64
} else {
Expand All @@ -71,8 +71,8 @@ impl SimplexNoiseSampler {

let n = h - l as f64 + Self::UNSKEW_FACTOR_2D;
let o = k - m as f64 + Self::UNSKEW_FACTOR_2D;
let p = 2f64.mul_add(Self::UNSKEW_FACTOR_2D, h - 1f64);
let q = 2f64.mul_add(Self::UNSKEW_FACTOR_2D, k - 1f64);
let p = h - 1f64 + 2f64 * Self::UNSKEW_FACTOR_2D;
let q = k - 1f64 + 2f64 * Self::UNSKEW_FACTOR_2D;

let r = i & 0xFF;
let s = j & 0xFF;
Expand Down Expand Up @@ -236,8 +236,8 @@ impl OctaveSimplexNoiseSampler {
for sampler in self.octave_samplers.iter() {
if let Some(sampler) = sampler {
d += sampler.sample_2d(
x.mul_add(e, if use_origin { sampler.x_origin } else { 0f64 }),
y.mul_add(e, if use_origin { sampler.y_origin } else { 0f64 }),
x * e + if use_origin { sampler.x_origin } else { 0f64 },
y * e + if use_origin { sampler.y_origin } else { 0f64 },
) * f;
}

Expand Down

0 comments on commit 5ed7cae

Please sign in to comment.