|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from typing import List |
| 4 | +import os |
| 5 | + |
| 6 | +noise_types = { |
| 7 | + 'cellular': ['2', '3'], |
| 8 | + 'cellular2': ['2', '3'], |
| 9 | + 'ridge': ['1', '2', '3', '4'], |
| 10 | + 'fbm': ['1', '2', '3', '4'], |
| 11 | + 'turbulence': ['1', '2', '3', '4'], |
| 12 | + 'gradient': ['1', '2', '3', '4'] |
| 13 | + } |
| 14 | +float_types = [ |
| 15 | + '32', |
| 16 | + '64' |
| 17 | + ] |
| 18 | +intrinsics = [ |
| 19 | + 'avx2', |
| 20 | + 'scalar', |
| 21 | + 'sse2', |
| 22 | + 'sse41' |
| 23 | + ] |
| 24 | + |
| 25 | +def generate_intrinsic_tests()-> List[str]: |
| 26 | + codes = [ |
| 27 | +""" |
| 28 | +use core::arch::x86_64::__m256; |
| 29 | +use simdnoise::intrinsics::{avx2, scalar, sse2, sse41}; |
| 30 | +use simdnoise::{NoiseType, TurbulenceSettings, RidgeSettings, FbmSettings, CellularSettings, Cellular2Settings, GradientSettings, NoiseDimensions, CellDistanceFunction, CellReturnType, Cell2ReturnType}; |
| 31 | +
|
| 32 | +mod helpers; |
| 33 | +use helpers::{BIN_PATH, read_from_file_f32, save_to_file_f32, read_from_file_f64, save_to_file_f64}; |
| 34 | +""" |
| 35 | + ] |
| 36 | + dim_lookup = { |
| 37 | + '1': 'width: 64,', |
| 38 | + '2': 'width: 64, height: 32,', |
| 39 | + '3': 'width: 64, height: 32, depth: 16,', |
| 40 | + '4': 'width: 64, height: 32, depth: 16, time: 8,', |
| 41 | + } |
| 42 | + cell_options = { |
| 43 | + 'cellular': ( "", ["CellValue", "Distance"]), |
| 44 | + 'cellular2': ("2", ["Distance2", "Distance2Add", "Distance2Sub", "Distance2Mul", "Distance2Div"]), |
| 45 | + } |
| 46 | + |
| 47 | + for noise_type, dimensions in noise_types.items(): |
| 48 | + options = {"normal": ""} |
| 49 | + if noise_type in ['fbm', 'turbulence', 'ridge']: |
| 50 | + options = {"normal": f""" |
| 51 | + .with_lacunarity(0.5) |
| 52 | + .with_gain(2.0) |
| 53 | + .with_octaves(5) |
| 54 | + """} |
| 55 | + elif noise_type in cell_options: |
| 56 | + (count, dist_ret) = cell_options[noise_type] |
| 57 | + for dist in ["Euclidean", "Manhattan", "Natural"]: |
| 58 | + for ret in dist_ret: |
| 59 | + title = f"{dist.lower()}_{ret.lower()}" |
| 60 | + option = f""" |
| 61 | + .with_distance_function(CellDistanceFunction::{dist}) |
| 62 | + .with_return_type(Cell{count}ReturnType::{ret}) |
| 63 | + """ |
| 64 | + options[title] = option |
| 65 | + for (postfix, option) in options.items(): |
| 66 | + for dimension in dimensions: |
| 67 | + dims = dim_lookup[dimension] |
| 68 | + for intrinsic in intrinsics: |
| 69 | + for float_type in float_types: |
| 70 | + if float_type == '64' and noise_type in ['cellular', 'cellular2']: |
| 71 | + # we skip these due to overflow errors |
| 72 | + continue |
| 73 | + variant = ["", f"_{float_type}"][float_type!="32"] |
| 74 | + fn_name = f"intrinsic_{noise_type}_{dimension}_{intrinsic}_{float_type}_{postfix}" |
| 75 | + enabled = "" |
| 76 | + if intrinsic == "sse41": |
| 77 | + enabled = "#[target_feature(enable = \"sse4.1\")]" |
| 78 | + elif intrinsic != "scalar": |
| 79 | + enabled = f"#[target_feature(enable = \"{intrinsic}\")]" |
| 80 | + block = f""" |
| 81 | +{enabled} |
| 82 | +unsafe fn do_{fn_name}() -> Vec<f{float_type}>{{ |
| 83 | + let dims = NoiseDimensions {{ |
| 84 | + {dims} |
| 85 | + ..NoiseDimensions::default({dimension}) |
| 86 | + }}; |
| 87 | +
|
| 88 | + let noise_type = {noise_type.capitalize()}Settings::default(dims) |
| 89 | + .with_seed(1337) |
| 90 | + {option} |
| 91 | + .wrap(); |
| 92 | + let (noise, _min, _max) = {intrinsic}::get_{dimension}d_noise{variant}(&noise_type); |
| 93 | + noise |
| 94 | +}} |
| 95 | +
|
| 96 | +#[test] |
| 97 | +fn test_{fn_name} () {{ |
| 98 | + let file_name = format!( |
| 99 | + "{{}}/{{}}_{{}}_{{}}_{{}}_{{}}_{{}}.bin", |
| 100 | + BIN_PATH, "intrinsics", "{noise_type}", "{float_type}", "{dimension}d", "{intrinsic}", "{postfix}" |
| 101 | + ); |
| 102 | + unsafe {{ |
| 103 | + let noise = do_{fn_name}(); |
| 104 | + //save_to_file_f{float_type}(&file_name, noise.as_slice()).unwrap(); |
| 105 | + let expected = read_from_file_f{float_type}(&file_name).unwrap(); |
| 106 | + assert_eq!(expected, noise); |
| 107 | + }} |
| 108 | +}} |
| 109 | +""" |
| 110 | + codes.append(block) |
| 111 | + return codes |
| 112 | + |
| 113 | +def main() : |
| 114 | + codes = generate_intrinsic_tests() |
| 115 | + file_name = "tests/intrinsics.rs" |
| 116 | + with open(file_name, "w") as file_h: |
| 117 | + source = "\n".join(codes) |
| 118 | + file_h.write(source) |
| 119 | + os.system(f"rustfmt {file_name}"); |
| 120 | + |
| 121 | +if __name__ == '__main__': |
| 122 | + main() |
0 commit comments