Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
2af18761bThis file is pinned to the selected release. Download the original to open it in your design tool.
#[allow(unused_imports)]
use micromath::F32Ext as _;
// Generated using Python:
//
// ```
// import math
//
// for i in range(256):
// print("{},".format(math.sin((i / 256) * 2 * math.pi)))
// ```
const COS: [f32; 256] = [
0.0,
0.024_541_229,
0.049_067_676,
0.073_564_57,
0.098_017_14,
0.122_410_68,
0.146_730_47,
0.170_961_89,
0.195_090_32,
0.219_101_24,
0.242_980_18,
0.266_712_75,
0.290_284_66,
0.313_681_75,
0.336_889_86,
0.359_895_05,
0.382_683_43,
0.405_241_3,
0.427_555_08,
0.449_611_34,
0.471_396_74,
0.492_898_2,
0.514_102_76,
0.534_997_64,
0.555_570_24,
0.575_808_17,
0.595_699_3,
0.615_231_6,
0.634_393_3,
0.653_172_85,
0.671_559,
0.689_540_57,
0.707_106_77,
0.724_247_1,
0.740_951_1,
0.757_208_8,
0.773_010_43,
0.788_346_4,
0.803_207_5,
0.817_584_8,
0.831_469_6,
0.844_853_6,
0.857_728_6,
0.870_086_97,
0.881_921_3,
0.893_224_3,
0.903_989_3,
0.914_209_8,
0.923_879_5,
0.932_992_8,
0.941_544_06,
0.949_528_16,
0.956_940_35,
0.963_776_05,
0.970_031_26,
0.975_702_1,
0.980_785_25,
0.985_277_65,
0.989_176_5,
0.992_479_56,
0.995_184_7,
0.997_290_43,
0.998_795_45,
0.999_698_8,
1.0,
0.999_698_8,
0.998_795_45,
0.997_290_43,
0.995_184_7,
0.992_479_56,
0.989_176_5,
0.985_277_65,
0.980_785_25,
0.975_702_1,
0.970_031_26,
0.963_776_05,
0.956_940_35,
0.949_528_16,
0.941_544_06,
0.932_992_8,
0.923_879_5,
0.914_209_8,
0.903_989_3,
0.893_224_3,
0.881_921_3,
0.870_086_97,
0.857_728_6,
0.844_853_6,
0.831_469_6,
0.817_584_8,
0.803_207_5,
0.788_346_4,
0.773_010_43,
0.757_208_8,
0.740_951_1,
0.724_247_1,
0.707_106_77,
0.689_540_57,
0.671_559,
0.653_172_85,
0.634_393_3,
0.615_231_6,
0.595_699_3,
0.575_808_17,
0.555_570_24,
0.534_997_64,
0.514_102_76,
0.492_898_2,
0.471_396_74,
0.449_611_34,
0.427_555_08,
0.405_241_3,
0.382_683_43,
0.359_895_05,
0.336_889_86,
0.313_681_75,
0.290_284_66,
0.266_712_75,
0.242_980_18,
0.219_101_24,
0.195_090_32,
0.170_961_89,
0.146_730_47,
0.122_410_68,
0.098_017_14,
0.073_564_57,
0.049_067_676,
0.024_541_229,
1.224_646_9e-16,
-0.024_541_229,
-0.049_067_676,
-0.073_564_57,
-0.098_017_14,
-0.122_410_68,
-0.146_730_47,
-0.170_961_89,
-0.195_090_32,
-0.219_101_24,
-0.242_980_18,
-0.266_712_75,
-0.290_284_66,
-0.313_681_75,
-0.336_889_86,
-0.359_895_05,
-0.382_683_43,
-0.405_241_3,
-0.427_555_08,
-0.449_611_34,
-0.471_396_74,
-0.492_898_2,
-0.514_102_76,
-0.534_997_64,
-0.555_570_24,
-0.575_808_17,
-0.595_699_3,
-0.615_231_6,
-0.634_393_3,
-0.653_172_85,
-0.671_559,
-0.689_540_57,
-0.707_106_77,
-0.724_247_1,
-0.740_951_1,
-0.757_208_8,
-0.773_010_43,
-0.788_346_4,
-0.803_207_5,
-0.817_584_8,
-0.831_469_6,
-0.844_853_6,
-0.857_728_6,
-0.870_086_97,
-0.881_921_3,
-0.893_224_3,
-0.903_989_3,
-0.914_209_8,
-0.923_879_5,
-0.932_992_8,
-0.941_544_06,
-0.949_528_16,
-0.956_940_35,
-0.963_776_05,
-0.970_031_26,
-0.975_702_1,
-0.980_785_25,
-0.985_277_65,
-0.989_176_5,
-0.992_479_56,
-0.995_184_7,
-0.997_290_43,
-0.998_795_45,
-0.999_698_8,
-1.0,
-0.999_698_8,
-0.998_795_45,
-0.997_290_43,
-0.995_184_7,
-0.992_479_56,
-0.989_176_5,
-0.985_277_65,
-0.980_785_25,
-0.975_702_1,
-0.970_031_26,
-0.963_776_05,
-0.956_940_35,
-0.949_528_16,
-0.941_544_06,
-0.932_992_8,
-0.923_879_5,
-0.914_209_8,
-0.903_989_3,
-0.893_224_3,
-0.881_921_3,
-0.870_086_97,
-0.857_728_6,
-0.844_853_6,
-0.831_469_6,
-0.817_584_8,
-0.803_207_5,
-0.788_346_4,
-0.773_010_43,
-0.757_208_8,
-0.740_951_1,
-0.724_247_1,
-0.707_106_77,
-0.689_540_57,
-0.671_559,
-0.653_172_85,
-0.634_393_3,
-0.615_231_6,
-0.595_699_3,
-0.575_808_17,
-0.555_570_24,
-0.534_997_64,
-0.514_102_76,
-0.492_898_2,
-0.471_396_74,
-0.449_611_34,
-0.427_555_08,
-0.405_241_3,
-0.382_683_43,
-0.359_895_05,
-0.336_889_86,
-0.313_681_75,
-0.290_284_66,
-0.266_712_75,
-0.242_980_18,
-0.219_101_24,
-0.195_090_32,
-0.170_961_89,
-0.146_730_47,
-0.122_410_68,
-0.098_017_14,
-0.073_564_57,
-0.049_067_676,
-0.024_541_229,
];
pub fn cos(mut phase: f32) -> f32 {
phase += 0.25;
phase *= 256.0;
let a = COS[(phase as usize) % 256];
let b = COS[(phase as usize + 1) % 256];
a + (b - a) * phase.fract()
}
#[cfg(test)]
mod tests {
use super::*;
use core::f32::consts::PI;
#[test]
fn cos_lookup_matches() {
for i in 0..1024 {
let phase = i as f32 / 1024_f32;
assert_relative_eq!(cos(phase), libm::cosf(phase * 2.0 * PI), epsilon = 0.0001);
}
}
}