quick_noise/noise/generators/value/
batch_2d.rs1use simply_simd::{Arch, Simd};
2
3use crate::api::batch::interface::BatchGenerator;
4use crate::noise::generators::Value;
5use crate::noise::util::constants::{BYTE_SHUFFLE, VALUE_EXP_MASK, HASH_MASK, HASH_PRIME};
6
7impl BatchGenerator<2> for Value {
8 #[inline(always)]
9 fn sample_batch<A: Arch>(
10 seed: u32,
11 input: [Simd<f32, A>; 2],
12 freq: [Simd<f32, A>; 2],
13 ) -> Simd<f32, A> {
14 let neg_two: Simd<f32, A> = Simd::splat(-2.0);
16 let three: Simd<f32, A> = Simd::splat(3.0);
17
18 let hash_mask: Simd<u32, A> = Simd::splat(HASH_MASK);
19 let exp_bits: Simd<u32, A> = Simd::splat(VALUE_EXP_MASK);
20
21 let shuffle_indices = Simd::<u8, A>::from_slice(&BYTE_SHUFFLE[..]);
22 let channel_seed = Simd::splat(seed);
23 let prime = Simd::splat(HASH_PRIME);
24
25 let x_scaled = input[0] * freq[0];
27 let y_scaled = input[1] * freq[1];
28
29 let x_scaled_floored = x_scaled.floor();
31 let y_scaled_floored = y_scaled.floor();
32
33 let x_grid_lo = x_scaled_floored.cast_int_trunc();
34 let y_grid_lo = y_scaled_floored.cast_int_trunc();
35
36 let x_dist_lo = x_scaled - x_scaled_floored;
37 let y_dist_lo = y_scaled - y_scaled_floored;
38
39 let t = x_dist_lo;
41 let s = y_dist_lo;
42 let x_lerp = t * t * t.mul_add(neg_two, three);
43 let y_lerp = s * s * s.mul_add(neg_two, three);
44
45 let x1: Simd<u32, A> = x_grid_lo.raw_cast() * channel_seed;
47 let y1: Simd<u32, A> = y_grid_lo.raw_cast() * channel_seed;
48 let x2 = x1 + channel_seed;
49 let y2 = y1 + channel_seed;
50
51 let x1_shuf = x1.permute_8(shuffle_indices) ^ prime;
52 let y1_shuf = y1.permute_8(shuffle_indices) ^ prime;
53 let x2_shuf = x2.permute_8(shuffle_indices) ^ prime;
54 let y2_shuf = y2.permute_8(shuffle_indices) ^ prime;
55
56 let hash_tl = x1_shuf * y1_shuf * y1_shuf;
57 let hash_tr = x1_shuf * y2_shuf * y2_shuf;
58 let hash_bl = x2_shuf * y1_shuf * y1_shuf;
59 let hash_br = x2_shuf * y2_shuf * y2_shuf;
60
61 let val_tl = ((hash_tl & hash_mask) | exp_bits).raw_cast::<f32>() - three;
63 let val_tr = ((hash_tr & hash_mask) | exp_bits).raw_cast::<f32>() - three;
64 let val_bl = ((hash_bl & hash_mask) | exp_bits).raw_cast::<f32>() - three;
65 let val_br = ((hash_br & hash_mask) | exp_bits).raw_cast::<f32>() - three;
66
67 let top_lerp = y_lerp.mul_add(val_tr - val_tl, val_tl);
69 let bottom_lerp = y_lerp.mul_add(val_br - val_bl, val_bl);
70
71 x_lerp.mul_add(bottom_lerp - top_lerp, top_lerp)
72 }
73}