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
//! Fast, SIMD accelerated noise generation functions
//! with optional runtime feature detection.
//!
//! [Github Link](https://github.com/jackmott/rust-simd-noise)
//!
//! ## Features
//!
//! * SSE2, SSE41, and AVX2 instruction sets, along with non SIMD fallback
//! * AVX2 version also leverages FMA3
//! * Runtime detection picks the best available instruction set
//! * Simplex noise, fractal brownian motion, and turbulence
//! * 2d and 3d
//!
//! ## Benchmarks
//! *Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz*
//! *Single Threaded*
//!
//! ### 2D 1000x1000 FBM Noise, 3 Octaves
//!
//! * scalar_2d ... bench:  74,207,703 ns/iter (+/- 2,184,952)
//! * sse2_2d   ... bench:  23,863,725 ns/iter (+/- 746,331)
//! * sse41_2d  ... bench:  22,440,765 ns/iter (+/- 995,336)
//! * avx2_2d   ... bench:  12,022,253 ns/iter (+/- 508,793)
//!
//! ### 3D 100x100x100 FBM Noise, 3 Octaves
//!
//! * scalar_3d ... bench: 102,543,499 ns/iter (+/- 3,310,472)
//! * sse2_3d   ... bench:  39,991,825 ns/iter (+/- 1,043,332)
//! * sse41_3d  ... bench:  38,852,436 ns/iter (+/- 1,350,831)
//! * avx2_3d   ... bench:  23,231,237 ns/iter (+/- 777,420)
//!
//! ## Get a block of noise with runtime SIMD detection
//!
//! The library will, at runtime, pick the fastest available options between SSE2, SSE41, and AVX2
//!
//! ```rust
//! use simdnoise::*;
//!
//! // A struct to set up noise parameters
//! let fractal_settings = simdnoise::FractalSettings {
//!       freq: 0.04,
//!       lacunarity: 0.5,
//!       gain: 2.0,
//!       octaves: 3,
//!       noise_type: simdnoise::NoiseType::FBM,
//! };
//!
//! // Get a block of 2d 800x600 noise, with no scaling of resulting values
//! // min and max values are returned so you can apply your own scaling
//! let (an_f32_vec,min,max) = simdnoise::get_2d_noise(0.0, 800, 0.0, 600, fractal_settings);
//!
//! // Get a block of 200x200x200 3d noise
//! let (an_f32_vec,min,max) = simdnoise::get_3d_noise(0.0, 200, 0.0, 200,0.0, 200, fractal_settings);
//!
//! // Get a block of noise scaled between -1 and 1
//! let an_f32_vec = simdnoise::get_2d_scaled_noise(0.0, 800, 0.0, 600, fractal_settings,-1.0,1.0);
//! ```
//!
//! ## Call noise functions directly
//! Sometimes you need something other than a block, like the points on the surface of a sphere.
//! Sometimes you may want to use SSE41 even with AVX2 is available
//!
//! ```rust
//!
//! // get a block of 100x100 sse41 noise, skip runtime detection
//! let (noise,min,max) = simdnoise::sse41::get_2d_noise(0.0,100,0.0,100,fractal_settings);
//!
//! // send your own SIMD x,y values to the noise functions directly
//! unsafe {
//!   // sse2 simplex noise
//!   let x = _mm_set1_ps(5.0);
//!   let y = _mm_set1_ps(10.0);
//!   let f : __m128 = simdnoise::sse2::simplex_2d(x,y);
//!   
//!   // avx2 turbulence
//!   let x = _mm256_set1_ps(5.0);
//!   let y = _mm256_set1_ps(10.0);
//!   let freq = _mm_256_set1_ps(1.0);
//!   let lacunarity = _mm256_set1_ps(0.5);
//!   let gain = _mm256_set1_ps(2.0);
//!   let octaves = 3;
//!   let f_turbulence : __m256 = simdnoise::avx2::turbulence_2d(x,y,freq,lacunarity,gain,octaves);
//!     
//! }
//! ```
#![feature(test)]
extern crate test;

pub mod avx2;
pub mod scalar;
mod shared;
mod shared_sse;
pub mod sse2;
pub mod sse41;

/// Specifies what type of noise to generate.
#[derive(Copy, Clone)]
pub enum NoiseType {
    /// Fractal brownian motion on top of simplex noise
    FBM,
    /// Turbulence on top of simplex noise
    Turbulence,
    /// Simplex Noise
    Normal,
}

/// Contains parameters for noise functions. When using
/// `Normal` noise, only frequency is used.
#[derive(Copy, Clone)]
pub struct FractalSettings {
    /// Higher frequency will appear to 'zoom' out, lower will appear to 'zoom' in. A good
    /// starting value for experimentation is around 0.05
    pub freq: f32,
    /// Lacunarity affects how the octaves are layered together. A good starting value to
    /// experiment with is 0.5, change from there in 0.25 increments to see what it looks like.
    pub lacunarity: f32,
    /// Gain affects how the octaves are layered together. A good starting value is 2.0
    pub gain: f32,
    /// Specifies how many layers of nose to combine. More octaves can yeild more detail
    /// and will increase runtime linearlly.
    pub octaves: u8,
    /// The type of noise to generate.  
    pub noise_type: NoiseType,
}

/// Gets a width X height sized block of 2d noise, unscaled,
/// using runtime CPU feature detection to pick the fastest method
/// between scalar, SSE2, SSE41, and AVX2
/// `start_x` and `start_y` can be used to provide an offset in the
/// coordinates. Results are unscaled, 'min' and 'max' noise values
/// are returned so you can scale and transform the noise as you see fit
/// in a single pass.
pub fn get_2d_noise(
    start_x: f32,
    width: usize,
    start_y: f32,
    height: usize,
    fractal_settings: FractalSettings,
) -> (Vec<f32>, f32, f32) {
    if is_x86_feature_detected!("avx2") {
        unsafe { avx2::get_2d_noise(start_x, width, start_y, height, fractal_settings) }
    } else if is_x86_feature_detected!("sse4.1") {
        unsafe { sse41::get_2d_noise(start_x, width, start_y, height, fractal_settings) }
    } else if is_x86_feature_detected!("sse2") {
        unsafe { sse2::get_2d_noise(start_x, width, start_y, height, fractal_settings) }
    } else {
        scalar::get_2d_noise(start_x, width, start_y, height, fractal_settings)
    }
}

/// Gets a width X height sized block of scaled 2d noise
/// using runtime CPU feature detection to pick the fastest method
/// between scalar, SSE2, SSE41, and AVX2
/// `start_x` and `start_y` can be used to provide an offset in the
/// coordinates.
/// `scaled_min` and `scaled_max` specify the range you want the noise scaled to.
pub fn get_2d_scaled_noise(
    start_x: f32,
    width: usize,
    start_y: f32,
    height: usize,
    fractal_settings: FractalSettings,
    scaled_min: f32,
    scaled_max: f32,
) -> Vec<f32> {
    if is_x86_feature_detected!("avx2") {
        unsafe {
            avx2::get_2d_scaled_noise(
                start_x,
                width,
                start_y,
                height,
                fractal_settings,
                scaled_min,
                scaled_max,
            )
        }
    } else if is_x86_feature_detected!("sse4.1") {
        unsafe {
            sse41::get_2d_scaled_noise(
                start_x,
                width,
                start_y,
                height,
                fractal_settings,
                scaled_min,
                scaled_max,
            )
        }
    } else if is_x86_feature_detected!("sse2") {
        unsafe {
            sse2::get_2d_scaled_noise(
                start_x,
                width,
                start_y,
                height,
                fractal_settings,
                scaled_min,
                scaled_max,
            )
        }
    } else {
        scalar::get_2d_scaled_noise(
            start_x,
            width,
            start_y,
            height,
            fractal_settings,
            scaled_min,
            scaled_max,
        )
    }
}

/// Gets a width X height X depth sized block of 3d noise, unscaled,
/// using runtime CPU feature detection to pick the fastest method
/// between scalar, SSE2, SSE41, and AVX2
/// `start_x`,`start_y` and `start_z` can be used to provide an offset in the
/// coordinates. Results are unscaled, 'min' and 'max' noise values
/// are returned so you can scale and transform the noise as you see fit
/// in a single pass.
pub fn get_3d_noise(
    start_x: f32,
    width: usize,
    start_y: f32,
    height: usize,
    start_z: f32,
    depth: usize,
    fractal_settings: FractalSettings,
) -> (Vec<f32>, f32, f32) {
    if is_x86_feature_detected!("avx2") {
        unsafe {
            avx2::get_3d_noise(
                start_x,
                width,
                start_y,
                height,
                start_z,
                depth,
                fractal_settings,
            )
        }
    } else if is_x86_feature_detected!("sse4.1") {
        unsafe {
            sse41::get_3d_noise(
                start_x,
                width,
                start_y,
                height,
                start_z,
                depth,
                fractal_settings,
            )
        }
    } else if is_x86_feature_detected!("sse2") {
        unsafe {
            sse2::get_3d_noise(
                start_x,
                width,
                start_y,
                height,
                start_z,
                depth,
                fractal_settings,
            )
        }
    } else {
        scalar::get_3d_noise(
            start_x,
            width,
            start_y,
            height,
            start_z,
            depth,
            fractal_settings,
        )
    }
}

/// Gets a width X height X depth sized block of scaled 3d noise
/// using runtime CPU feature detection to pick the fastest method
/// between scalar, SSE2, SSE41, and AVX2
/// `start_x`, `start_y` and `start_z` can be used to provide an offset in the
/// coordinates.
/// `scaled_min` and `scaled_max` specify the range you want the noise scaled to.
pub fn get_3d_scaled_noise(
    start_x: f32,
    width: usize,
    start_y: f32,
    height: usize,
    start_z: f32,
    depth: usize,
    fractal_settings: FractalSettings,
    scaled_min: f32,
    scaled_max: f32,
) -> Vec<f32> {
    if is_x86_feature_detected!("avx2") {
        unsafe {
            avx2::get_3d_scaled_noise(
                start_x,
                width,
                start_y,
                height,
                start_z,
                depth,
                fractal_settings,
                scaled_min,
                scaled_max,
            )
        }
    } else if is_x86_feature_detected!("sse4.1") {
        unsafe {
            sse41::get_3d_scaled_noise(
                start_x,
                width,
                start_y,
                height,
                start_z,
                depth,
                fractal_settings,
                scaled_min,
                scaled_max,
            )
        }
    } else if is_x86_feature_detected!("sse2") {
        unsafe {
            sse2::get_3d_scaled_noise(
                start_x,
                width,
                start_y,
                height,
                start_z,
                depth,
                fractal_settings,
                scaled_min,
                scaled_max,
            )
        }
    } else {
        scalar::get_3d_scaled_noise(
            start_x,
            width,
            start_y,
            height,
            start_z,
            depth,
            fractal_settings,
            scaled_min,
            scaled_max,
        )
    }
}

#[cfg(test)]
mod benchmarks {

    use super::*;
    use test::{black_box, Bencher};

    const FRACTAL_SETTINGS: FractalSettings = FractalSettings {
        freq: 0.04,
        lacunarity: 0.5,
        gain: 2.0,
        octaves: 3,
        noise_type: NoiseType::FBM,
    };

    #[bench]
    fn b2d_1_scalar(b: &mut Bencher) {
        b.iter(|| black_box(scalar::get_2d_noise(0.0, 1000, 0.0, 1000, FRACTAL_SETTINGS)));
    }
    #[bench]
    fn b2d_2_sse2(b: &mut Bencher) {
        unsafe {
            b.iter(|| black_box(sse2::get_2d_noise(0.0, 1000, 0.0, 1000, FRACTAL_SETTINGS)));
        }
    }
    #[bench]
    fn b2d_3_sse41(b: &mut Bencher) {
        unsafe {
            b.iter(|| black_box(sse41::get_2d_noise(0.0, 1000, 0.0, 1000, FRACTAL_SETTINGS)));
        }
    }
    #[bench]
    fn b2d_4_avx2(b: &mut Bencher) {
        unsafe {
            b.iter(|| black_box(avx2::get_2d_noise(0.0, 1000, 0.0, 1000, FRACTAL_SETTINGS)));
        }
    }
    #[bench]
    fn b3d_1_scalar(b: &mut Bencher) {
        b.iter(|| {
            black_box(scalar::get_3d_noise(
                0.0,
                100,
                0.0,
                100,
                0.0,
                100,
                FRACTAL_SETTINGS,
            ))
        });
    }
    #[bench]
    fn b3d_2_sse2(b: &mut Bencher) {
        unsafe {
            b.iter(|| {
                black_box(sse2::get_3d_noise(
                    0.0,
                    100,
                    0.0,
                    100,
                    0.0,
                    100,
                    FRACTAL_SETTINGS,
                ))
            });
        }
    }
    #[bench]
    fn b3d_3_sse41(b: &mut Bencher) {
        unsafe {
            b.iter(|| {
                black_box(sse41::get_3d_noise(
                    0.0,
                    100,
                    0.0,
                    100,
                    0.0,
                    100,
                    FRACTAL_SETTINGS,
                ))
            });
        }
    }
    #[bench]
    fn b3d_4_avx2(b: &mut Bencher) {
        unsafe {
            b.iter(|| {
                black_box(avx2::get_3d_noise(
                    0.0,
                    100,
                    0.0,
                    100,
                    0.0,
                    100,
                    FRACTAL_SETTINGS,
                ))
            });
        }
    }
}