text
stringlengths 1
2.05k
|
---|
fn test_sin() {
let a = FixedTrait::new(HALF_PI, false);
assert_precise(a.sin(), ONE.into(), 'invalid half pi', Option::None(()));
} |
fn test_tan() {
let a = FixedTrait::<FP16x16W>::new(HALF_PI / 2, false);
assert(a.tan().mag == 65536, 'invalid quarter pi');
} |
fn test_sign() {
let a = FixedTrait::<FP16x16W>::new(0, false);
assert(a.sign().mag == 0 && !a.sign().sign, 'invalid sign (0, true)');
let a = FixedTrait::<FP16x16W>::new(HALF, true);
assert(a.sign().mag == ONE && a.sign().sign, 'invalid sign (HALF, true)');
let a = FixedTrait::<FP16x16W>::new(HALF, false);
assert(a.sign().mag == ONE && !a.sign().sign, 'invalid sign (HALF, false)');
let a = FixedTrait::<FP16x16W>::new(ONE, true);
assert(a.sign().mag == ONE && a.sign().sign, 'invalid sign (ONE, true)');
let a = FixedTrait::<FP16x16W>::new(ONE, false);
assert(a.sign().mag == ONE && !a.sign().sign, 'invalid sign (ONE, false)');
} |
fn test_sign_fail() {
let a = FixedTrait::<FP16x16W>::new(HALF, true);
assert(a.sign().mag != ONE && !a.sign().sign, 'invalid sign (HALF, true)');
}
} |
use orion::numbers::fixed_point::implementations::fp16x16wide::core::{ONE, FP16x16W, FixedTrait};
use orion::numbers::fixed_point::implementations::fp16x16wide::math::lut::erf_lut;
const ERF_COMPUTATIONAL_ACCURACY: u64 = 100;
const ROUND_CHECK_NUMBER: u64 = 10;
// Values > MAX_ERF_NUMBER return 1
const MAX_ERF_NUMBER: u64 = 229376;
// Values <= ERF_TRUNCATION_NUMBER -> two decimal places, and values > ERF_TRUNCATION_NUMBER -> one decimal place
const ERF_TRUNCATION_NUMBER: u64 = 131072;
fn erf(x: FP16x16W) -> FP16x16W {
// Lookup
// 1. if x.mag < 3.5 { lookup table }
// 2. else{ return 1}
let mut erf_value: u64 = 0;
if x.mag < MAX_ERF_NUMBER {
erf_value = erf_lut(x.mag);
} else {
erf_value = ONE;
}
FP16x16W { mag: erf_value, sign: x.sign }
}
|
use orion::numbers::fixed_point::implementations::fp16x16wide::core::{
HALF, ONE, TWO, FP16x16W, FP16x16WImpl, FP16x16WAdd, FP16x16WAddEq, FP16x16WSub, FP16x16WMul,
FP16x16WMulEq, FP16x16WTryIntoU128, FP16x16WPartialEq, FP16x16WPartialOrd, FP16x16WSubEq,
FP16x16WNeg, FP16x16WDiv, FP16x16WIntoFelt252, FixedTrait
};
fn cosh(a: FP16x16W) -> FP16x16W {
let ea = a.exp();
(ea + (FixedTrait::ONE() / ea)) / FixedTrait::new(TWO, false)
}
fn sinh(a: FP16x16W) -> FP16x16W {
let ea = a.exp();
(ea - (FixedTrait::ONE() / ea)) / FixedTrait::new(TWO, false)
}
fn tanh(a: FP16x16W) -> FP16x16W {
let ea = a.exp();
let ea_i = FixedTrait::ONE() / ea;
(ea - ea_i) / (ea + ea_i)
}
fn acosh(a: FP16x16W) -> FP16x16W {
let root = (a * a - FixedTrait::ONE()).sqrt();
(a + root).ln()
}
fn asinh(a: FP16x16W) -> FP16x16W {
let root = (a * a + FixedTrait::ONE()).sqrt();
(a + root).ln()
}
fn atanh(a: FP16x16W) -> FP16x16W {
let one = FixedTrait::ONE();
let ln_arg = (one + a) / (one - a);
ln_arg.ln() / FixedTrait::new(TWO, false)
}
mod tests {
use orion::numbers::fixed_point::implementations::fp16x16wide::helpers::assert_precise;
use super::{FixedTrait, TWO, cosh, ONE, sinh, tanh, acosh, asinh, atanh, HALF}; |
fn test_cosh() {
let a = FixedTrait::new(TWO, false);
assert_precise(cosh(a), 246550, 'invalid two', Option::None(()));
let a = FixedTrait::ONE();
assert_precise(cosh(a), 101127, 'invalid one', Option::None(()));
let a = FixedTrait::ZERO();
assert_precise(cosh(a), ONE.into(), 'invalid zero', Option::None(()));
let a = FixedTrait::ONE();
assert_precise(cosh(a), 101127, 'invalid neg one', Option::None(()));
let a = FixedTrait::new(TWO, true);
assert_precise(cosh(a), 246568, 'invalid neg two', Option::None(()));
} |
fn test_sinh() {
let a = FixedTrait::new(TWO, false);
assert_precise(sinh(a), 237681, 'invalid two', Option::None(()));
let a = FixedTrait::ONE();
assert_precise(sinh(a), 77018, 'invalid one', Option::None(()));
let a = FixedTrait::ZERO();
assert(sinh(a).into() == 0, 'invalid zero');
let a = FixedTrait::new(ONE, true);
assert_precise(sinh(a), -77018, 'invalid neg one', Option::None(()));
let a = FixedTrait::new(TWO, true);
assert_precise(sinh(a), -237699, 'invalid neg two', Option::None(()));
} |
fn test_tanh() {
let a = FixedTrait::new(TWO, false);
assert_precise(tanh(a), 63179, 'invalid two', Option::None(()));
let a = FixedTrait::ONE();
assert_precise(tanh(a), 49912, 'invalid one', Option::None(()));
let a = FixedTrait::ZERO();
assert(tanh(a).into() == 0, 'invalid zero');
let a = FixedTrait::new(ONE, true);
assert_precise(tanh(a), -49912, 'invalid neg one', Option::None(()));
let a = FixedTrait::new(TWO, true);
assert_precise(tanh(a), -63179, 'invalid neg two', Option::None(()));
} |
fn test_acosh() {
let a = FixedTrait::new(246559, false);
assert_precise(acosh(a), 131072, 'invalid two', Option::None(()));
let a = FixedTrait::new(101127, false);
assert_precise(acosh(a), ONE.into(), 'invalid one', Option::None(()));
let a = FixedTrait::ONE();
assert(acosh(a).into() == 0, 'invalid zero');
} |
fn test_asinh() {
let a = FixedTrait::new(237690, false);
assert_precise(asinh(a), 131072, 'invalid two', Option::None(()));
let a = FixedTrait::new(77018, false);
assert_precise(asinh(a), ONE.into(), 'invalid one', Option::None(()));
let a = FixedTrait::ZERO();
assert(asinh(a).into() == 0, 'invalid zero');
let a = FixedTrait::new(77018, true);
assert_precise(asinh(a), -ONE.into(), 'invalid neg one', Option::None(()));
let a = FixedTrait::new(237690, true);
assert_precise(asinh(a), -131017, 'invalid neg two', Option::None(()));
} |
fn test_atanh() {
let a = FixedTrait::new(58982, false);
assert_precise(atanh(a), 96483, 'invalid 0.9', Option::None(()));
let a = FixedTrait::new(HALF, false);
assert_precise(atanh(a), 35999, 'invalid half', Option::None(()));
let a = FixedTrait::ZERO();
assert(atanh(a).into() == 0, 'invalid zero');
let a = FixedTrait::new(HALF, true);
assert_precise(atanh(a), -35999, 'invalid neg half', Option::None(()));
let a = FixedTrait::new(58982, true);
assert_precise(atanh(a), -96483, 'invalid -0.9', Option::None(()));
}
} |
use orion::numbers::fixed_point::implementations::fp8x23wide::core::ONE;
fn msb(whole: u64) -> (u64, u64) {
if whole < 256 {
if whole < 2 {
return (0, 1);
}
if whole < 4 {
return (1, 2);
}
if whole < 8 {
return (2, 4);
}
if whole < 16 {
return (3, 8);
}
if whole < 32 {
return (4, 16);
}
if whole < 64 {
return (5, 32);
}
if whole < 128 {
return (6, 64);
}
if whole < 256 {
return (7, 128);
}
} else if whole < 65536 {
if whole < 512 {
return (8, 256);
}
if whole < 1024 {
return (9, 512);
}
if whole < 2048 {
return (10, 1024);
}
if whole < 4096 {
return (11, 2048);
}
if whole < 8192 {
return (12, 4096);
}
if whole < 16384 {
return (13, 8192);
}
if whole < 32768 {
return (14, 16384);
}
if whole < 65536 {
return (15, 32768);
}
}
(16, 65536)
}
fn exp2(exp: u64) -> u64 {
if exp <= 16 {
if exp == 0 {
return 1;
}
if exp == 1 {
return 2;
}
if exp == 2 {
return 4;
}
if exp == 3 {
return 8;
}
if exp == 4 {
return 16;
}
if exp == 5 {
return 32;
}
if exp == 6 {
return 64;
}
if exp == 7 {
return 128;
}
if exp == 8 {
return 256;
}
if exp == 9 {
return 512;
}
if exp == 10 {
return 1024;
}
if exp == 11 {
return 2048;
}
if exp == 12 {
return 4096;
}
if exp == 13 {
return 8192;
} |
if exp == 14 {
return 16384;
}
if exp == 15 {
return 32768;
}
if exp == 16 {
return 65536;
}
}
65536
}
fn sin(a: u64) -> (u64, u64, u64) {
let slot = a / 402;
if slot < 128 {
if slot < 64 {
if slot < 32 {
if slot < 16 {
if slot == 0 {
return (0, 0, 402);
}
if slot == 1 {
return (402, 402, 804);
}
if slot == 2 {
return (804, 804, 1206);
}
if slot == 3 {
return (1206, 1206, 1608);
}
if slot == 4 {
return (1608, 1608, 2010);
}
if slot == 5 {
return (2011, 2010, 2412);
}
if slot == 6 {
return (2413, 2412, 2814);
}
if slot == 7 {
return (2815, 2814, 3216);
}
if slot == 8 {
return (3217, 3216, 3617);
}
if slot == 9 {
return (3619, 3617, 4019);
}
if slot == 10 {
return (4023, 4019, 4420);
}
if slot == 11 {
return (4423, 4420, 4821);
}
if slot == 12 {
return (4825, 4821, 5222);
}
if slot == 13 {
return (5228, 5222, 5623);
}
if slot == 14 {
return (5630, 5623, 6023);
}
if slot == 15 {
return (6032, 6023, 64 |
24);
}
} else {
if slot == 16 {
return (6434, 6424, 6824);
}
if slot == 17 {
return (6836, 6824, 7224);
}
if slot == 18 {
return (7238, 7224, 7623);
}
if slot == 19 {
return (7640, 7623, 8022);
}
if slot == 20 {
return (8042, 8022, 8421);
}
if slot == 21 {
return (8445, 8421, 8820);
}
if slot == 22 {
return (8847, 8820, 9218);
}
if slot == 23 {
return (9249, 9218, 9616);
}
if slot == 24 {
return (9651, 9616, 10014);
}
if slot == 25 {
return (10053, 10014, 10411);
}
if slot == 26 {
return (10455, 10411, 10808);
}
if slot == 27 {
return (10857, 10808, 11204);
}
if slot == 28 {
return (11259, 11204, 11600);
}
if slot == 29 {
return (11662, 11600, 11996);
}
if slot == 30 {
return (12064, 11996, 12391);
}
if slot == 31 {
return (12466, 12391, 12785);
}
}
} else {
if slot < 48 {
if slot == 32 {
return (12868, 12785, 13180);
}
if slot == 33 { |
return (13270, 13180, 13573);
}
if slot == 34 {
return (13672, 13573, 13966);
}
if slot == 35 {
return (14074, 13966, 14359);
}
if slot == 36 {
return (14476, 14359, 14751);
}
if slot == 37 {
return (14879, 14751, 15143);
}
if slot == 38 {
return (15281, 15143, 15534);
}
if slot == 39 {
return (15683, 15534, 15924);
}
if slot == 40 {
return (16081, 15924, 16314);
}
if slot == 41 {
return (16487, 16314, 16703);
}
if slot == 42 {
return (16889, 16703, 17091);
}
if slot == 43 {
return (17291, 17091, 17479);
}
if slot == 44 {
return (17693, 17479, 17867);
}
if slot == 45 {
return (18096, 17867, 18253);
}
if slot == 46 {
return (18498, 18253, 18639);
}
if slot == 47 {
return (18900, 18639, 19024);
}
} else {
if slot == 48 {
return (19302, 19024, 19409);
}
if slot == 49 {
return (19704, 19409, 19792);
}
if slot == 50 {
return (20113, 19792, 20175);
}
if slot == 51 { |
return (20508, 20175, 20557);
}
if slot == 52 {
return (20910, 20557, 20939);
}
if slot == 53 {
return (21313, 20939, 21320);
}
if slot == 54 {
return (21715, 21320, 21699);
}
if slot == 55 {
return (22117, 21699, 22078);
}
if slot == 56 {
return (22519, 22078, 22457);
}
if slot == 57 {
return (22921, 22457, 22834);
}
if slot == 58 {
return (23323, 22834, 23210);
}
if slot == 59 {
return (23725, 23210, 23586);
}
if slot == 60 {
return (24127, 23586, 23961);
}
if slot == 61 {
return (24530, 23961, 24335);
}
if slot == 62 {
return (24932, 24335, 24708);
}
if slot == 63 {
return (25334, 24708, 25080);
}
}
}
} else {
if slot < 96 {
if slot < 80 {
if slot == 64 {
return (25736, 25080, 25451);
}
if slot == 65 {
return (26138, 25451, 25821);
}
if slot == 66 {
return (26540, 25821, 26190);
}
if slot == 67 {
return (26942, 26190, 26558);
}
if slot == 68 {
return (27344, 26558, 26925); |
}
if slot == 69 {
return (27747, 26925, 27291);
}
if slot == 70 {
return (28149, 27291, 27656);
}
if slot == 71 {
return (28551, 27656, 28020);
}
if slot == 72 {
return (28953, 28020, 28383);
}
if slot == 73 {
return (29355, 28383, 28745);
}
if slot == 74 {
return (29757, 28745, 29106);
}
if slot == 75 {
return (30159, 29106, 29466);
}
if slot == 76 {
return (30561, 29466, 29824);
}
if slot == 77 {
return (30964, 29824, 30182);
}
if slot == 78 {
return (31366, 30182, 30538);
}
if slot == 79 {
return (31768, 30538, 30893);
}
} else {
if slot == 80 {
return (32171, 30893, 31248);
}
if slot == 81 {
return (32572, 31248, 31600);
}
if slot == 82 {
return (32974, 31600, 31952);
}
if slot == 83 {
return (33376, 31952, 32303);
}
if slot == 84 {
return (33778, 32303, 32652);
}
if slot == 85 {
return (34181, 32652, 33000);
}
if slot == 86 {
return (34583, 33000, 33347); |
}
if slot == 87 {
return (34985, 33347, 33692);
}
if slot == 88 {
return (35387, 33692, 34037);
}
if slot == 89 {
return (35789, 34037, 34380);
}
if slot == 90 {
return (36194, 34380, 34721);
}
if slot == 91 {
return (36593, 34721, 35062);
}
if slot == 92 {
return (36995, 35062, 35401);
}
if slot == 93 {
return (37398, 35401, 35738);
}
if slot == 94 {
return (37800, 35738, 36075);
}
if slot == 95 {
return (38202, 36075, 36410);
}
}
} else {
if slot < 112 {
if slot == 96 {
return (38604, 36410, 36744);
}
if slot == 97 {
return (39006, 36744, 37076);
}
if slot == 98 {
return (39408, 37076, 37407);
}
if slot == 99 {
return (39810, 37407, 37736);
}
if slot == 100 {
return (40227, 37736, 38064);
}
if slot == 101 {
return (40615, 38064, 38391);
}
if slot == 102 {
return (41017, 38391, 38716);
}
if slot == 103 {
return (41419, 38716, 39040);
}
if slot == 104 { |
return (41821, 39040, 39362);
}
if slot == 105 {
return (42223, 39362, 39683);
}
if slot == 106 {
return (42625, 39683, 40002);
}
if slot == 107 {
return (43027, 40002, 40320);
}
if slot == 108 {
return (43429, 40320, 40636);
}
if slot == 109 {
return (43832, 40636, 40951);
}
if slot == 110 {
return (44234, 40951, 41264);
}
if slot == 111 {
return (44636, 41264, 41576);
}
} else {
if slot == 112 {
return (45038, 41576, 41886);
}
if slot == 113 {
return (45440, 41886, 42194);
}
if slot == 114 {
return (45842, 42194, 42501);
}
if slot == 115 {
return (46244, 42501, 42806);
}
if slot == 116 {
return (46646, 42806, 43110);
}
if slot == 117 {
return (47048, 43110, 43412);
}
if slot == 118 {
return (47451, 43412, 43713);
}
if slot == 119 {
return (47853, 43713, 44011);
}
if slot == 120 {
return (48252, 44011, 44308);
}
if slot == 121 {
return (48657, 44308, 44604);
}
if slot == 122 { |
return (49059, 44604, 44898);
}
if slot == 123 {
return (49461, 44898, 45190);
}
if slot == 124 {
return (49863, 45190, 45480);
}
if slot == 125 {
return (50265, 45480, 45769);
}
if slot == 126 {
return (50668, 45769, 46056);
}
if slot == 127 {
return (51070, 46056, 46341);
}
}
}
}
} else {
if slot < 192 {
if slot < 160 {
if slot < 144 {
if slot == 128 {
return (51472, 46341, 46624);
}
if slot == 129 {
return (51874, 46624, 46906);
}
if slot == 130 {
return (52285, 46906, 47186);
}
if slot == 131 {
return (52678, 47186, 47464);
}
if slot == 132 {
return (53080, 47464, 47741);
}
if slot == 133 {
return (53482, 47741, 48015);
}
if slot == 134 {
return (53885, 48015, 48288);
}
if slot == 135 {
return (54287, 48288, 48559);
}
if slot == 136 {
return (54689, 48559, 48828);
}
if slot == 137 {
return (55091, 48828, 49095);
}
if slot == 138 {
return (55493, 49095, 49361);
}
if slot == 13 |
9 {
return (55895, 49361, 49624);
}
if slot == 140 {
return (56297, 49624, 49886);
}
if slot == 141 {
return (56699, 49886, 50146);
}
if slot == 142 {
return (57102, 50146, 50404);
}
if slot == 143 {
return (57504, 50404, 50660);
}
} else {
if slot == 144 {
return (57906, 50660, 50914);
}
if slot == 145 {
return (58308, 50914, 51166);
}
if slot == 146 {
return (58710, 51166, 51417);
}
if slot == 147 {
return (59112, 51417, 51665);
}
if slot == 148 {
return (59514, 51665, 51911);
}
if slot == 149 {
return (59916, 51911, 52156);
}
if slot == 150 {
return (60320, 52156, 52398);
}
if slot == 151 {
return (60721, 52398, 52639);
}
if slot == 152 {
return (61123, 52639, 52878);
}
if slot == 153 {
return (61525, 52878, 53114);
}
if slot == 154 {
return (61927, 53114, 53349);
}
if slot == 155 {
return (62329, 53349, 53581);
}
if slot == 156 {
return (62731, 53581, 53812);
}
if |
slot == 157 {
return (63133, 53812, 54040);
}
if slot == 158 {
return (63536, 54040, 54267);
}
if slot == 159 {
return (63938, 54267, 54491);
}
if slot == 160 {
return (64343, 54491, 54714);
}
}
} else {
if slot < 176 {
if slot == 161 {
return (64742, 54714, 54934);
}
if slot == 162 {
return (65144, 54934, 55152);
}
if slot == 163 {
return (65546, 55152, 55368);
}
if slot == 164 {
return (65948, 55368, 55582);
}
if slot == 165 {
return (66350, 55582, 55794);
}
if slot == 166 {
return (66753, 55794, 56004);
}
if slot == 167 {
return (67155, 56004, 56212);
}
if slot == 168 {
return (67557, 56212, 56418);
}
if slot == 169 {
return (67959, 56418, 56621);
}
if slot == 170 {
return (68361, 56621, 56823);
}
if slot == 171 {
return (68763, 56823, 57022);
}
if slot == 172 {
return (69165, 57022, 57219);
}
if slot == 173 {
return (69567, 57219, 57414);
}
if slot == 174 {
return (69970, 57 |
414, 57607);
}
if slot == 175 {
return (70372, 57607, 57798);
}
} else {
if slot == 176 {
return (70774, 57798, 57986);
}
if slot == 177 {
return (71176, 57986, 58172);
}
if slot == 178 {
return (71578, 58172, 58356);
}
if slot == 179 {
return (71980, 58356, 58538);
}
if slot == 180 {
return (72382, 58538, 58718);
}
if slot == 181 {
return (72784, 58718, 58896);
}
if slot == 182 {
return (73187, 58896, 59071);
}
if slot == 183 {
return (73589, 59071, 59244);
}
if slot == 184 {
return (73991, 59244, 59415);
}
if slot == 185 {
return (74393, 59415, 59583);
}
if slot == 186 {
return (74795, 59583, 59750);
}
if slot == 187 {
return (75197, 59750, 59914);
}
if slot == 188 {
return (75599, 59914, 60075);
}
if slot == 189 {
return (76001, 60075, 60235);
}
if slot == 190 {
return (76401, 60235, 60392);
}
if slot == 191 {
return (76806, 60392, 60547);
}
}
}
} else {
if slo |
t < 224 {
if slot < 208 {
if slot == 192 {
return (77208, 60547, 60700);
}
if slot == 193 {
return (77610, 60700, 60851);
}
if slot == 194 {
return (78012, 60851, 60999);
}
if slot == 195 {
return (78414, 60999, 61145);
}
if slot == 196 {
return (78816, 61145, 61288);
}
if slot == 197 {
return (79218, 61288, 61429);
}
if slot == 198 {
return (79621, 61429, 61568);
}
if slot == 199 {
return (80023, 61568, 61705);
}
if slot == 200 {
return (80423, 61705, 61839);
}
if slot == 201 {
return (80827, 61839, 61971);
}
if slot == 202 {
return (81229, 61971, 62101);
}
if slot == 203 {
return (81631, 62101, 62228);
}
if slot == 204 {
return (82033, 62228, 62353);
}
if slot == 205 {
return (82435, 62353, 62476);
}
if slot == 206 {
return (82838, 62476, 62596);
}
if slot == 207 {
return (83240, 62596, 62714);
}
} else {
if slot == 208 {
return (83642, 62714, 62830);
}
if slot == 209 { |
return (84044, 62830, 62943);
}
if slot == 210 {
return (84446, 62943, 63054);
}
if slot == 211 {
return (84848, 63054, 63162);
}
if slot == 212 {
return (85250, 63162, 63268);
}
if slot == 213 {
return (85652, 63268, 63372);
}
if slot == 214 {
return (86055, 63372, 63473);
}
if slot == 215 {
return (86457, 63473, 63572);
}
if slot == 216 {
return (86859, 63572, 63668);
}
if slot == 217 {
return (87261, 63668, 63763);
}
if slot == 218 {
return (87663, 63763, 63854);
}
if slot == 219 {
return (88065, 63854, 63944);
}
if slot == 220 {
return (88467, 63944, 64031);
}
if slot == 221 {
return (88869, 64031, 64115);
}
if slot == 222 {
return (89271, 64115, 64197);
}
if slot == 223 {
return (89674, 64197, 64277);
}
}
} else {
if slot < 240 {
if slot == 224 {
return (90076, 64277, 64354);
}
if slot == 225 {
return (90478, 64354, 64429);
}
if slot == 226 {
return (90880, 64429, 64501);
} |
if slot == 227 {
return (91282, 64501, 64571);
}
if slot == 228 {
return (91684, 64571, 64639);
}
if slot == 229 {
return (92086, 64639, 64704);
}
if slot == 230 {
return (92491, 64704, 64766);
}
if slot == 231 {
return (92891, 64766, 64827);
}
if slot == 232 {
return (93293, 64827, 64884);
}
if slot == 233 {
return (93695, 64884, 64940);
}
if slot == 234 {
return (94097, 64940, 64993);
}
if slot == 235 {
return (94499, 64993, 65043);
}
if slot == 236 {
return (94901, 65043, 65091);
}
if slot == 237 {
return (95303, 65091, 65137);
}
if slot == 238 {
return (95705, 65137, 65180);
}
if slot == 239 {
return (96108, 65180, 65220);
}
} else {
if slot == 240 {
return (96514, 65220, 65259);
}
if slot == 241 {
return (96912, 65259, 65294);
}
if slot == 242 {
return (97314, 65294, 65328);
}
if slot == 243 {
return (97716, 65328, 65358);
}
if slot == 244 {
return (98118, 65358, 65387); |
}
if slot == 245 {
return (98520, 65387, 65413);
}
if slot == 246 {
return (98922, 65413, 65436);
}
if slot == 247 {
return (99325, 65436, 65457);
}
if slot == 248 {
return (99727, 65457, 65476);
}
if slot == 249 {
return (100129, 65476, 65492);
}
if slot == 250 {
return (100531, 65492, 65505);
}
if slot == 251 {
return (100933, 65505, 65516);
}
if slot == 252 {
return (101335, 65516, 65525);
}
if slot == 253 {
return (101737, 65525, 65531);
}
if slot == 254 {
return (102139, 65531, 65535);
}
}
}
}
}
(102542, 65535, 65536)
}
fn atan(a: u64) -> (u64, u64, u64) {
let slot = a / 459;
if slot == 0 {
return (0, 0, 459);
}
if slot == 1 {
return (459, 459, 917);
}
if slot == 2 {
return (918, 917, 1376);
}
if slot == 3 {
return (1376, 1376, 1835);
}
if slot == 4 {
return (1835, 1835, 2293);
}
if slot == 5 {
return (2294, 2293, 2751);
}
if slot == 6 {
return (2753, 2751, 3209);
}
if slot == 7 {
return (3211, 3209, 3666);
}
if slot == 8 {
return (3670, 3666, 4123);
}
if slot == 9 {
return (4129, 4123, 4580);
}
if slot == 10 {
return (4591, 4580, 5036);
}
if slot == 11 {
return (5046, 5036, 5492);
}
if slot == 12 {
return (5505, 5492, 5947) |
;
}
if slot == 13 {
return (5964, 5947, 6402);
}
if slot == 14 {
return (6423, 6402, 6856);
}
if slot == 15 {
return (6881, 6856, 7310);
}
if slot == 16 {
return (7340, 7310, 7762);
}
if slot == 17 {
return (7799, 7762, 8214);
}
if slot == 18 {
return (8258, 8214, 8665);
}
if slot == 19 {
return (8716, 8665, 9116);
}
if slot == 20 {
return (9181, 9116, 9565);
}
if slot == 21 {
return (9634, 9565, 10014);
}
if slot == 22 {
return (10093, 10014, 10462);
}
if slot == 23 {
return (10551, 10462, 10908);
}
if slot == 24 {
return (11010, 10908, 11354);
}
if slot == 25 {
return (11469, 11354, 11798);
}
if slot == 26 {
return (11928, 11798, 12242);
}
if slot == 27 {
return (12386, 12242, 12684);
}
if slot == 28 {
return (12845, 12684, 13125);
}
if slot == 29 {
return (13304, 13125, 13565);
}
if slot == 30 {
return (13762, 13565, 14004);
}
if slot == 31 {
return (14221, 14004, 14442);
}
if slot == 32 {
return (14680, 14442, 14878);
}
if slot == 33 {
return (15139, 14878, 15313);
}
if slot == 34 {
return (15598, 15313, 15746);
}
if slot == 35 {
return (16056, 15746, 16178);
}
if slot == 36 {
return (16515, 16178, 16609);
}
if slot == 37 {
return (16974, 16609, 17038);
}
if slot == 38 {
return (17433, 17038, 17466);
}
if slot == 39 {
return (17891, 17466, 17892);
}
if slot == 40 {
return (18353, 17892, 18317);
}
if slot == 41 {
return (18809, 18317, 18740);
}
if slot == 42 {
return (19268, 18740, 19161);
}
if slot == 43 {
return (19726, 19161, 19581);
}
if slot == 44 {
return (20185, 19581, 19999);
}
if slot == 45 |
{
return (20644, 19999, 20416);
}
if slot == 46 {
return (21103, 20416, 20830);
}
if slot == 47 {
return (21561, 20830, 21243);
}
if slot == 48 {
return (22020, 21243, 21655);
}
if slot == 49 {
return (22479, 21655, 22064);
}
if slot == 50 {
return (22944, 22064, 22472);
}
if slot == 51 {
return (23396, 22472, 22878);
}
if slot == 52 {
return (23855, 22878, 23282);
}
if slot == 53 {
return (24314, 23282, 23685);
}
if slot == 54 {
return (24773, 23685, 24085);
}
if slot == 55 {
return (25231, 24085, 24484);
}
if slot == 56 {
return (25690, 24484, 24880);
}
if slot == 57 {
return (26149, 24880, 25275);
}
if slot == 58 {
return (26608, 25275, 25668);
}
if slot == 59 {
return (27066, 25668, 26059);
}
if slot == 60 {
return (27534, 26059, 26448);
}
if slot == 61 {
return (27984, 26448, 26835);
}
if slot == 62 {
return (28443, 26835, 27220);
}
if slot == 63 {
return (28901, 27220, 27603);
}
if slot == 64 {
return (29360, 27603, 27984);
}
if slot == 65 {
return (29819, 27984, 28363);
}
if slot == 66 {
return (30278, 28363, 28740);
}
if slot == 67 {
return (30736, 28740, 29115);
}
if slot == 68 {
return (31195, 29115, 29488);
}
if slot == 69 {
return (31654, 29488, 29859);
}
if slot == 70 {
return (32113, 29859, 30228);
}
if slot == 71 {
return (32571, 30228, 30595);
}
if slot == 72 {
return (33030, 30595, 30960);
}
if slot == 73 {
return (33489, 30960, 31323);
}
if slot == 74 {
return (33948, 31323, 31683);
}
if slot == 75 {
return (34406, 31683, 32042);
}
if slot == 76 {
return (34865, 32042, 32398);
}
if slot == 77 |
{
return (35324, 32398, 32753);
}
if slot == 78 {
return (35783, 32753, 33105);
}
if slot == 79 {
return (36241, 33105, 33455);
}
if slot == 80 {
return (36700, 33455, 33804);
}
if slot == 81 {
return (37159, 33804, 34150);
}
if slot == 82 {
return (37618, 34150, 34494);
}
if slot == 83 {
return (38076, 34494, 34836);
}
if slot == 84 {
return (38535, 34836, 35175);
}
if slot == 85 {
return (38994, 35175, 35513);
}
if slot == 86 {
return (39453, 35513, 35849);
}
if slot == 87 {
return (39911, 35849, 36183);
}
if slot == 88 {
return (40370, 36183, 36514);
}
if slot == 89 {
return (40829, 36514, 36843);
}
if slot == 90 {
return (41288, 36843, 37171);
}
if slot == 91 {
return (41746, 37171, 37496);
}
if slot == 92 {
return (42205, 37496, 37819);
}
if slot == 93 {
return (42664, 37819, 38141);
}
if slot == 94 {
return (43123, 38141, 38460);
}
if slot == 95 {
return (43581, 38460, 38777);
}
if slot == 96 {
return (44040, 38777, 39092);
}
if slot == 97 {
return (44499, 39092, 39405);
}
if slot == 98 {
return (44958, 39405, 39716);
}
(45416, 39716, 40025)
}
fn erf_lut(x: u64) -> u64 {
if x <= 5898 {
if x <= 0 {
return 0;
}
if x <= 655 {
return 739;
}
if x <= 1310 {
return 1478;
}
if x <= 1966 {
return 2217;
}
if x <= 2621 {
return 2956;
}
if x <= 3276 {
return 3694;
}
if x <= 3932 {
return 4431;
}
if x <= 4587 {
return 5168;
}
if x <= 5242 {
return 5903;
}
if x <= 5898 {
return 6637;
} |
}
if x <= 12451 {
if x <= 6553 {
return 7370;
}
if x <= 7208 {
return 8101;
}
if x <= 7864 {
return 8831;
}
if x <= 8519 {
return 9559;
}
if x <= 9175 {
return 10285;
}
if x <= 9830 {
return 11009;
}
if x <= 10485 {
return 11731;
}
if x <= 11141 {
return 12451;
}
if x <= 11796 {
return 13168;
}
if x <= 12451 {
return 13883;
}
}
if x <= 19005 {
if x <= 13107 {
return 14595;
}
if x <= 13762 {
return 15304;
}
if x <= 14417 {
return 16010;
}
if x <= 15073 {
return 16713;
}
if x <= 15728 {
return 17412;
}
if x <= 16384 {
return 18109;
}
if x <= 17039 {
return 18802;
}
if x <= 17694 {
return 19491;
}
if x <= 18350 {
return 20177;
}
if x <= 19005 {
return 20859;
}
}
if x <= 25559 {
if x <= 19660 {
return 21536;
}
if x <= 20316 {
return 22210;
}
if x <= 20971 {
return 22880;
}
if x <= 21626 {
return 23545;
}
if x <= 22282 {
return 24206;
}
if x <= 22937 {
return 24863;
}
if x <= 23592 {
return 25515;
}
if x <= 24248 {
return 26162;
}
if x <= 24903 {
return 26804;
}
if x <= 25559 {
return 27442;
}
}
if x <= 32112 {
if x <= 26214 {
return 28075;
}
if x <= 26869 {
return 28702;
}
if x <= 27525 {
re |
turn 29325;
}
if x <= 28180 {
return 29942;
}
if x <= 28835 {
return 30554;
}
if x <= 29491 {
return 31161;
}
if x <= 30146 {
return 31762;
}
if x <= 30801 {
return 32358;
}
if x <= 31457 {
return 32948;
}
if x <= 32112 {
return 33532;
}
}
if x <= 38666 {
if x <= 32768 {
return 34111;
}
if x <= 33423 {
return 34684;
}
if x <= 34078 {
return 35251;
}
if x <= 34734 {
return 35813;
}
if x <= 35389 {
return 36368;
}
if x <= 36044 {
return 36917;
}
if x <= 36700 {
return 37461;
}
if x <= 37355 {
return 37998;
}
if x <= 38010 {
return 38530;
}
if x <= 38666 {
return 39055;
}
}
if x <= 45219 {
if x <= 39321 {
return 39574;
}
if x <= 39976 {
return 40087;
}
if x <= 40632 {
return 40593;
}
if x <= 41287 {
return 41094;
}
if x <= 41943 {
return 41588;
}
if x <= 42598 {
return 42076;
}
if x <= 43253 {
return 42557;
}
if x <= 43909 {
return 43032;
}
if x <= 44564 {
return 43501;
}
if x <= 45219 {
return 43964;
}
}
if x <= 51773 {
if x <= 45875 {
return 44420;
}
if x <= 46530 {
return 44870;
}
if x <= 47185 {
return 45313;
}
if x <= 47841 {
return 45750;
}
if x <= 48496 {
return 46181;
}
if x <= 49152 { |
return 46606;
}
if x <= 49807 {
return 47024;
}
if x <= 50462 {
return 47436;
}
if x <= 51118 {
return 47841;
}
if x <= 51773 {
return 48241;
}
}
if x <= 58327 {
if x <= 52428 {
return 48634;
}
if x <= 53084 {
return 49021;
}
if x <= 53739 {
return 49401;
}
if x <= 54394 {
return 49776;
}
if x <= 55050 {
return 50144;
}
if x <= 55705 {
return 50506;
}
if x <= 56360 {
return 50862;
}
if x <= 57016 {
return 51212;
}
if x <= 57671 {
return 51556;
}
if x <= 58327 {
return 51894;
}
}
if x <= 64880 {
if x <= 58982 {
return 52226;
}
if x <= 59637 {
return 52552;
}
if x <= 60293 {
return 52872;
}
if x <= 60948 {
return 53186;
}
if x <= 61603 {
return 53495;
}
if x <= 62259 {
return 53797;
}
if x <= 62914 {
return 54094;
}
if x <= 63569 {
return 54386;
}
if x <= 64225 {
return 54672;
}
if x <= 64880 {
return 54952;
}
}
if x <= 71434 {
if x <= 65536 {
return 55227;
}
if x <= 66191 {
return 55496;
}
if x <= 66846 {
return 55760;
}
if x <= 67502 {
return 56019;
}
if x <= 68157 {
return 56272;
}
if x <= 68812 {
return 56520;
}
if x <= 69468 {
return 56763;
}
if x <= 70123 {
return 57001;
}
if x <= 70 |
778 {
return 57234;
}
if x <= 71434 {
return 57462;
}
}
if x <= 77987 {
if x <= 72089 {
return 57685;
}
if x <= 72744 {
return 57903;
}
if x <= 73400 {
return 58116;
}
if x <= 74055 {
return 58325;
}
if x <= 74711 {
return 58529;
}
if x <= 75366 {
return 58728;
}
if x <= 76021 {
return 58923;
}
if x <= 76677 {
return 59113;
}
if x <= 77332 {
return 59299;
}
if x <= 77987 {
return 59481;
}
}
if x <= 84541 {
if x <= 78643 {
return 59658;
}
if x <= 79298 {
return 59831;
}
if x <= 79953 {
return 60000;
}
if x <= 80609 {
return 60165;
}
if x <= 81264 {
return 60326;
}
if x <= 81920 {
return 60483;
}
if x <= 82575 {
return 60636;
}
if x <= 83230 {
return 60785;
}
if x <= 83886 {
return 60931;
}
if x <= 84541 {
return 61072;
}
}
if x <= 91095 {
if x <= 85196 {
return 61211;
}
if x <= 85852 {
return 61345;
}
if x <= 86507 {
return 61477;
}
if x <= 87162 {
return 61604;
}
if x <= 87818 {
return 61729;
}
if x <= 88473 {
return 61850;
}
if x <= 89128 {
return 61968;
}
if x <= 89784 {
return 62083;
}
if x <= 90439 {
return 62194;
}
if x <= 91095 {
return 62303;
}
}
if x <= 97648 {
if x <= 91750 {
return |
62408;
}
if x <= 92405 {
return 62511;
}
if x <= 93061 {
return 62611;
}
if x <= 93716 {
return 62708;
}
if x <= 94371 {
return 62802;
}
if x <= 95027 {
return 62894;
}
if x <= 95682 {
return 62983;
}
if x <= 96337 {
return 63070;
}
if x <= 96993 {
return 63154;
}
if x <= 97648 {
return 63235;
}
}
if x <= 104202 {
if x <= 98304 {
return 63314;
}
if x <= 98959 {
return 63391;
}
if x <= 99614 {
return 63465;
}
if x <= 100270 {
return 63538;
}
if x <= 100925 {
return 63608;
}
if x <= 101580 {
return 63676;
}
if x <= 102236 {
return 63742;
}
if x <= 102891 {
return 63806;
}
if x <= 103546 {
return 63867;
}
if x <= 104202 {
return 63927;
}
}
if x <= 110755 {
if x <= 104857 {
return 63985;
}
if x <= 105512 {
return 64042;
}
if x <= 106168 {
return 64096;
}
if x <= 106823 {
return 64149;
}
if x <= 107479 {
return 64200;
}
if x <= 108134 {
return 64249;
}
if x <= 108789 {
return 64297;
}
if x <= 109445 {
return 64343;
}
if x <= 110100 {
return 64388;
}
if x <= 110755 {
return 64431;
}
}
if x <= 117309 {
if x <= 111411 {
return 64473;
}
if x <= 112066 {
return 64514;
}
if x <= 112721 {
return 64553;
}
i |
f x <= 113377 {
return 64590;
}
if x <= 114032 {
return 64627;
}
if x <= 114688 {
return 64662;
}
if x <= 115343 {
return 64696;
}
if x <= 115998 {
return 64729;
}
if x <= 116654 {
return 64760;
}
if x <= 117309 {
return 64791;
}
}
if x <= 123863 {
if x <= 117964 {
return 64821;
}
if x <= 118620 {
return 64849;
}
if x <= 119275 {
return 64876;
}
if x <= 119930 {
return 64903;
}
if x <= 120586 {
return 64928;
}
if x <= 121241 {
return 64953;
}
if x <= 121896 {
return 64977;
}
if x <= 122552 {
return 64999;
}
if x <= 123207 {
return 65021;
}
if x <= 123863 {
return 65043;
}
}
if x <= 130416 {
if x <= 124518 {
return 65063;
}
if x <= 125173 {
return 65083;
}
if x <= 125829 {
return 65102;
}
if x <= 126484 {
return 65120;
}
if x <= 127139 {
return 65137;
}
if x <= 127795 {
return 65154;
}
if x <= 128450 {
return 65170;
}
if x <= 129105 {
return 65186;
}
if x <= 129761 {
return 65201;
}
if x <= 130416 {
return 65215;
}
}
if x <= 222822 {
if x <= 131072 {
return 65229;
}
if x <= 137625 {
return 65340;
}
if x <= 144179 {
return 65413;
}
if x <= 150732 {
return 65461;
}
if x <= 157286 {
return 65490;
}
if x <= 163840 { |
return 65509;
}
if x <= 170393 {
return 65520;
}
if x <= 176947 {
return 65527;
}
if x <= 183500 {
return 65531;
}
if x <= 190054 {
return 65533;
}
if x <= 196608 {
return 65534;
}
if x <= 203161 {
return 65535;
}
if x <= 209715 {
return 65535;
}
if x <= 216268 {
return 65535;
}
if x <= 222822 {
return 65535;
}
}
ONE
} |
use core::integer;
use orion::numbers::fixed_point::implementations::fp16x16wide::math::lut;
use orion::numbers::fixed_point::implementations::fp16x16wide::core::{
HALF, ONE, TWO, FP16x16W, FP16x16WImpl, FP16x16WAdd, FP16x16WSub, FP16x16WMul, FP16x16WDiv,
FP16x16WIntoFelt252, FixedTrait
};
const TWO_PI: u64 = 411775;
const PI: u64 = 205887;
const HALF_PI: u64 = 102944;
fn acos(a: FP16x16W) -> FP16x16W {
let asin_arg = (FixedTrait::ONE() - a * a).sqrt();
let asin_res = asin(asin_arg);
if a.sign {
FixedTrait::new(PI, false) - asin_res
} else {
asin_res
}
}
fn acos_fast(a: FP16x16W) -> FP16x16W {
let asin_arg = (FixedTrait::ONE() - a * a).sqrt();
let asin_res = asin_fast(asin_arg);
if a.sign {
FixedTrait::new(PI, false) - asin_res
} else {
asin_res
}
}
fn asin(a: FP16x16W) -> FP16x16W {
if (a.mag == ONE) {
return FixedTrait::new(HALF_PI, a.sign);
}
let div = (FixedTrait::ONE() - a * a).sqrt();
atan(a / div)
}
fn asin_fast(a: FP16x16W) -> FP16x16W {
if (a.mag == ONE) {
return FixedTrait::new(HALF_PI, a.sign);
}
let div = (FixedTrait::ONE() - a * a).sqrt();
atan_fast(a / div)
}
fn atan(a: FP16x16W) -> FP16x16W {
let mut at = a.abs();
let mut shift = false;
let mut invert = false;
if (at.mag > ONE) {
at = FixedTrait::ONE() / at;
invert = true;
}
if (at.mag > 45875) {
let sqrt3_3 = FixedTrait::new(37837, false);
at = (at - sqrt3_3) / (FixedTrait::ONE() + at * sqrt3_3);
shift = true;
}
let r10 = FixedTrait::new(120, true) * at;
let r9 = (r10 + FixedTrait::new(3066, true)) * at;
let r8 = (r9 + FixedTrait::new(12727, false)) * at;
let r7 = (r8 + FixedTrait::new(17170, true)) * at;
let r6 = (r7 + FixedTrait::new(2865, false)) * at;
let r5 = (r6 + FixedTrait::new(12456, false)) * at;
let r4 = (r5 + FixedTrait::new(90, false)) * at;
let r3 = (r4 + FixedTrait::new |
(21852, true)) * at;
let r2 = r3 * at;
let mut res = (r2 + FixedTrait::new(65536, false)) * at;
if (shift) {
res = res + FixedTrait::new(34315, false);
}
if (invert) {
res = res - FixedTrait::new(HALF_PI, false);
}
FixedTrait::new(res.mag, a.sign)
}
fn atan_fast(a: FP16x16W) -> FP16x16W {
let mut at = a.abs();
let mut shift = false;
let mut invert = false;
if (at.mag > ONE) {
at = FixedTrait::ONE() / at;
invert = true;
}
if (at.mag > 45875) {
let sqrt3_3 = FixedTrait::new(37837, false);
at = (at - sqrt3_3) / (FixedTrait::ONE() + at * sqrt3_3);
shift = true;
}
let (start, low, high) = lut::atan(at.mag);
let partial_step = FixedTrait::new(at.mag - start, false) / FixedTrait::new(459, false);
let mut res = partial_step * FixedTrait::new(high - low, false) + FixedTrait::new(low, false);
if (shift) {
res = res + FixedTrait::new(34315, false);
}
if (invert) {
res = res - FixedTrait::<FP16x16W>::new(HALF_PI, false);
}
FixedTrait::new(res.mag, a.sign)
}
fn cos(a: FP16x16W) -> FP16x16W {
sin(FixedTrait::new(HALF_PI, false) - a)
}
fn cos_fast(a: FP16x16W) -> FP16x16W {
sin_fast(FixedTrait::new(HALF_PI, false) - a)
}
fn sin(a: FP16x16W) -> FP16x16W {
let a1 = a.mag % TWO_PI;
let (whole_rem, partial_rem) = integer::u64_safe_divmod(a1, integer::u64_as_non_zero(PI));
let a2 = FixedTrait::new(partial_rem, false);
let partial_sign = whole_rem == 1;
let loop_res = a2 * _sin_loop(a2, 7, FixedTrait::ONE());
FixedTrait::new(loop_res.mag, a.sign ^ partial_sign && loop_res.mag != 0)
}
fn sin_fast(a: FP16x16W) -> FP16x16W {
let a1 = a.mag % TWO_PI;
let (whole_rem, mut partial_rem) = integer::u64_safe_divmod(a1, integer::u64_as_non_zero(PI));
let partial_sign = whole_rem == 1;
if partial_rem >= HALF_PI {
partial_rem = PI - partial_rem;
}
let (start, low, high) = lut::sin(part |
ial_rem);
let partial_step = FixedTrait::new(partial_rem - start, false) / FixedTrait::new(402, false);
let res = partial_step * (FixedTrait::new(high, false) - FixedTrait::new(low, false))
+ FixedTrait::<FP16x16W>::new(low, false);
FixedTrait::new(res.mag, a.sign ^ partial_sign && res.mag != 0)
}
fn tan(a: FP16x16W) -> FP16x16W {
let sinx = sin(a);
let cosx = cos(a);
assert(cosx.mag != 0, 'tan undefined');
sinx / cosx
}
fn tan_fast(a: FP16x16W) -> FP16x16W {
let sinx = sin_fast(a);
let cosx = cos_fast(a);
assert(cosx.mag != 0, 'tan undefined');
sinx / cosx
}
fn _sin_loop(a: FP16x16W, i: u64, acc: FP16x16W) -> FP16x16W {
let div = (2 * i + 2) * (2 * i + 3);
let term = a * a * acc / FixedTrait::new_unscaled(div, false);
let new_acc = FixedTrait::ONE() - term;
if (i == 0) {
return new_acc;
}
_sin_loop(a, i - 1, new_acc)
}
mod tests {
use orion::numbers::fixed_point::implementations::fp16x16wide::helpers::{
assert_precise, assert_relative
};
use orion::numbers::fixed_point::implementations::fp16x16wide::core::{
FP16x16WPartialEq, FP16x16WPrint
};
use super::{
FixedTrait, acos, HALF_PI, ONE, acos_fast, PI, atan_fast, atan, asin, cos, cos_fast, sin,
sin_fast, tan
}; |
fn test_acos() {
let error = Option::Some(84);
let a = FixedTrait::ONE();
assert(acos(a).into() == 0, 'invalid one');
let a = FixedTrait::new(ONE / 2, false);
assert_relative(acos(a), 68629, 'invalid half', error);
let a = FixedTrait::ZERO();
assert_relative(acos(a), HALF_PI.into(), 'invalid zero', Option::None(()));
let a = FixedTrait::new(ONE / 2, true);
assert_relative(acos(a), 137258, 'invalid neg half', error);
let a = FixedTrait::new(ONE, true);
assert_relative(acos(a), PI.into(), 'invalid neg one', Option::None(()));
} |
fn test_acos_fast() {
let error = Option::Some(84);
let a = FixedTrait::ONE();
assert(acos_fast(a).into() == 0, 'invalid one');
let a = FixedTrait::new(ONE / 2, false);
assert_relative(acos_fast(a), 68629, 'invalid half', error);
let a = FixedTrait::ZERO();
assert_relative(acos_fast(a), HALF_PI.into(), 'invalid zero', Option::None(()));
let a = FixedTrait::new(ONE / 2, true);
assert_relative(acos_fast(a), 137258, 'invalid neg half', error);
let a = FixedTrait::new(ONE, true);
assert_relative(acos_fast(a), PI.into(), 'invalid neg one', Option::None(()));
} |
fn test_acos_fail() {
let a = FixedTrait::new(2 * ONE, true);
acos(a);
} |
fn test_atan_fast() {
let error = Option::Some(84);
let a = FixedTrait::new(2 * ONE, false);
assert_relative(atan_fast(a), 72558, 'invalid two', error);
let a = FixedTrait::ONE();
assert_relative(atan_fast(a), 51472, 'invalid one', error);
let a = FixedTrait::new(ONE / 2, false);
assert_relative(atan_fast(a), 30386, 'invalid half', error);
let a = FixedTrait::ZERO();
assert(atan_fast(a).into() == 0, 'invalid zero');
let a = FixedTrait::new(ONE / 2, true);
assert_relative(atan_fast(a), -30386, 'invalid neg half', error);
let a = FixedTrait::new(ONE, true);
assert_relative(atan_fast(a), -51472, 'invalid neg one', error);
let a = FixedTrait::new(2 * ONE, true);
assert_relative(atan_fast(a), -72558, 'invalid neg two', error);
} |
fn test_atan() {
let a = FixedTrait::new(2 * ONE, false);
assert_relative(atan(a), 72558, 'invalid two', Option::None(()));
let a = FixedTrait::ONE();
assert_relative(atan(a), 51472, 'invalid one', Option::None(()));
let a = FixedTrait::new(ONE / 2, false);
assert_relative(atan(a), 30386, 'invalid half', Option::None(()));
let a = FixedTrait::ZERO();
assert(atan(a).into() == 0, 'invalid zero');
let a = FixedTrait::new(ONE / 2, true);
assert_relative(atan(a), -30386, 'invalid neg half', Option::None(()));
let a = FixedTrait::new(ONE, true);
assert_relative(atan(a), -51472, 'invalid neg one', Option::None(()));
let a = FixedTrait::new(2 * ONE, true);
assert_relative(atan(a), -72558, 'invalid neg two', Option::None(()));
} |
fn test_asin() {
let error = Option::Some(84);
let a = FixedTrait::ONE();
assert_relative(asin(a), HALF_PI.into(), 'invalid one', Option::None(()));
let a = FixedTrait::new(ONE / 2, false);
assert_relative(asin(a), 34315, 'invalid half', error);
let a = FixedTrait::ZERO();
assert_precise(asin(a), 0, 'invalid zero', Option::None(()));
let a = FixedTrait::new(ONE / 2, true);
assert_relative(asin(a), -34315, 'invalid neg half', error);
let a = FixedTrait::new(ONE, true);
assert_relative(asin(a), -HALF_PI.into(), 'invalid neg one', Option::None(()));
} |
fn test_asin_fail() {
let a = FixedTrait::new(2 * ONE, false);
asin(a);
} |
fn test_cos() {
let a = FixedTrait::new(HALF_PI, false);
assert(cos(a).into() == 0, 'invalid half pi');
let a = FixedTrait::new(HALF_PI / 2, false);
assert_relative(cos(a), 46341, 'invalid quarter pi', Option::None(()));
let a = FixedTrait::new(PI, false);
assert_relative(cos(a), -1 * ONE.into(), 'invalid pi', Option::None(()));
let a = FixedTrait::new(HALF_PI, true);
assert_precise(cos(a), 0, 'invalid neg half pi', Option::None(()));
let a = FixedTrait::new_unscaled(17, false);
assert_relative(cos(a), -18033, 'invalid 17', Option::None(()));
let a = FixedTrait::new_unscaled(17, true);
assert_relative(cos(a), -18033, 'invalid -17', Option::None(()));
} |
fn test_cos_fast() {
let error = Option::Some(84);
let a = FixedTrait::new(HALF_PI, false);
assert(cos_fast(a).into() == 0, 'invalid half pi');
let a = FixedTrait::new(HALF_PI / 2, false);
assert_precise(cos_fast(a), 46341, 'invalid quarter pi', error);
let a = FixedTrait::new(PI, false);
assert_precise(cos_fast(a), -1 * ONE.into(), 'invalid pi', error);
let a = FixedTrait::new(HALF_PI, true);
assert_precise(cos(a), 0, 'invalid neg half pi', Option::None(()));
let a = FixedTrait::new_unscaled(17, false);
assert_precise(cos_fast(a), -18033, 'invalid 17', error);
} |
fn test_sin() {
let a = FixedTrait::new(HALF_PI, false);
assert_precise(sin(a), ONE.into(), 'invalid half pi', Option::None(()));
let a = FixedTrait::new(HALF_PI / 2, false);
assert_precise(sin(a), 46341, 'invalid quarter pi', Option::None(()));
let a = FixedTrait::new(PI, false);
assert(sin(a).into() == 0, 'invalid pi');
let a = FixedTrait::new(HALF_PI, true);
assert_precise(
sin(a), -ONE.into(), 'invalid neg half pi', Option::None(())
);
let a = FixedTrait::new_unscaled(17, false);
assert_precise(sin(a), -63006, 'invalid 17', Option::None(()));
let a = FixedTrait::new_unscaled(17, true);
assert_precise(sin(a), 63006, 'invalid -17', Option::None(()));
} |
fn test_sin_fast() {
let error = Option::Some(84);
let a = FixedTrait::new(HALF_PI, false);
assert_precise(sin_fast(a), ONE.into(), 'invalid half pi', error);
let a = FixedTrait::new(HALF_PI / 2, false);
assert_precise(sin_fast(a), 46341, 'invalid quarter pi', error);
let a = FixedTrait::new(PI, false);
assert(sin_fast(a).into() == 0, 'invalid pi');
let a = FixedTrait::new(HALF_PI, true);
assert_precise(sin_fast(a), -ONE.into(), 'invalid neg half pi', error);
let a = FixedTrait::new_unscaled(17, false);
assert_precise(sin_fast(a), -63006, 'invalid 17', error);
let a = FixedTrait::new_unscaled(17, true);
assert_precise(sin_fast(a), 63006, 'invalid -17', error);
} |
fn test_tan() {
let a = FixedTrait::new(HALF_PI / 2, false);
assert_precise(tan(a), ONE.into(), 'invalid quarter pi', Option::None(()));
let a = FixedTrait::new(PI, false);
assert_precise(tan(a), 0, 'invalid pi', Option::None(()));
let a = FixedTrait::new_unscaled(17, false);
assert_precise(tan(a), 228990, 'invalid 17', Option::None(()));
let a = FixedTrait::new_unscaled(17, true);
assert_precise(tan(a), -228952, 'invalid -17', Option::None(()));
}
} |
mod core;
mod comp;
mod erf;
mod lut;
|
use orion::numbers::{FP32x32, FP32x32Impl, FixedTrait};
fn xor(a: FP32x32, b: FP32x32) -> bool {
if (a == FixedTrait::new(0, false) || b == FixedTrait::new(0, false)) && (a != b) {
true
} else {
false
}
}
fn or(a: FP32x32, b: FP32x32) -> bool {
let zero = FixedTrait::new(0, false);
if a == zero && b == zero {
false
} else {
true
}
}
fn and(a: FP32x32, b: FP32x32) -> bool {
let zero = FixedTrait::new(0, false);
if a == zero || b == zero {
false
} else {
true
}
}
fn where(a: FP32x32, b: FP32x32, c: FP32x32) -> FP32x32 {
if a == FixedTrait::new(0, false) {
c
} else {
b
}
}
fn bitwise_and(a: FP32x32, b: FP32x32) -> FP32x32 {
FixedTrait::new(a.mag & b.mag, a.sign & b.sign)
}
fn bitwise_xor(a: FP32x32, b: FP32x32) -> FP32x32 {
FixedTrait::new(a.mag ^ b.mag, a.sign ^ b.sign)
}
fn bitwise_or(a: FP32x32, b: FP32x32) -> FP32x32 {
FixedTrait::new(a.mag | b.mag, a.sign | b.sign)
}
|
use core::debug::PrintTrait;
use cubit::f64 as fp32x32;
use cubit::f64::Fixed as FP32x32;
use cubit::f64::{ONE, HALF};
use cubit::f64::types::fixed;
use orion::numbers::fixed_point::core::{FixedTrait};
use orion::numbers::fixed_point::implementations::fp32x32::erf;
use orion::numbers::fixed_point::utils;
const MAX: u64 = 9223372036854775808;
impl FP32x32Impl of FixedTrait<FP32x32, u64> {
fn ZERO() -> FP32x32 {
FP32x32 { mag: 0, sign: false }
}
fn HALF() -> FP32x32 {
FP32x32 { mag: HALF, sign: false }
}
fn ONE() -> FP32x32 {
FP32x32 { mag: ONE, sign: false }
}
fn MAX() -> FP32x32 {
FP32x32 { mag: MAX, sign: false }
}
fn new(mag: u64, sign: bool) -> FP32x32 {
FP32x32 { mag: mag, sign: sign }
}
fn new_unscaled(mag: u64, sign: bool) -> FP32x32 {
FP32x32 { mag: mag * ONE, sign: sign }
}
fn from_felt(val: felt252) -> FP32x32 {
let mag = core::integer::u64_try_from_felt252(utils::felt_abs(val)).unwrap();
FixedTrait::new(mag, utils::felt_sign(val))
}
fn abs(self: FP32x32) -> FP32x32 {
fp32x32::ops::abs(self)
}
fn acos(self: FP32x32) -> FP32x32 {
fp32x32::trig::acos_fast(self)
}
fn acos_fast(self: FP32x32) -> FP32x32 {
fp32x32::trig::acos_fast(self)
}
fn acosh(self: FP32x32) -> FP32x32 {
fp32x32::hyp::acosh(self)
}
fn asin(self: FP32x32) -> FP32x32 {
fp32x32::trig::asin_fast(self)
}
fn asin_fast(self: FP32x32) -> FP32x32 {
fp32x32::trig::asin_fast(self)
}
fn asinh(self: FP32x32) -> FP32x32 {
fp32x32::hyp::asinh(self)
}
fn atan(self: FP32x32) -> FP32x32 {
fp32x32::trig::atan_fast(self)
}
fn atan_fast(self: FP32x32) -> FP32x32 {
fp32x32::trig::atan_fast(self)
}
fn atanh(self: FP32x32) -> FP32x32 {
fp32x32::hyp::atanh(self)
}
fn ceil(self: FP32x32) -> FP32x32 {
fp32x32::ops::ceil(self)
}
fn cos(self: FP32x32 |
) -> FP32x32 {
fp32x32::trig::cos_fast(self)
}
fn cos_fast(self: FP32x32) -> FP32x32 {
fp32x32::trig::cos_fast(self)
}
fn cosh(self: FP32x32) -> FP32x32 {
fp32x32::hyp::cosh(self)
}
fn floor(self: FP32x32) -> FP32x32 {
fp32x32::ops::floor(self)
}
fn exp(self: FP32x32) -> FP32x32 {
fp32x32::ops::exp(self)
}
fn exp2(self: FP32x32) -> FP32x32 {
fp32x32::ops::exp2(self)
}
fn ln(self: FP32x32) -> FP32x32 {
fp32x32::ops::ln(self)
}
fn log2(self: FP32x32) -> FP32x32 {
fp32x32::ops::log2(self)
}
fn log10(self: FP32x32) -> FP32x32 {
fp32x32::ops::log10(self)
}
fn pow(self: FP32x32, b: FP32x32) -> FP32x32 {
fp32x32::ops::pow(self, b)
}
fn round(self: FP32x32) -> FP32x32 {
fp32x32::ops::round(self)
}
fn sin(self: FP32x32) -> FP32x32 {
fp32x32::trig::sin_fast(self)
}
fn sin_fast(self: FP32x32) -> FP32x32 {
fp32x32::trig::sin_fast(self)
}
fn sinh(self: FP32x32) -> FP32x32 {
fp32x32::hyp::sinh(self)
}
fn sqrt(self: FP32x32) -> FP32x32 {
fp32x32::ops::sqrt(self)
}
fn tan(self: FP32x32) -> FP32x32 {
fp32x32::trig::tan_fast(self)
}
fn tan_fast(self: FP32x32) -> FP32x32 {
fp32x32::trig::tan_fast(self)
}
fn tanh(self: FP32x32) -> FP32x32 {
fp32x32::hyp::tanh(self)
}
fn sign(self: FP32x32) -> FP32x32 {
panic(array!['not supported!'])
}
fn NaN() -> FP32x32 {
FP32x32 { mag: 0, sign: true }
}
fn is_nan(self: FP32x32) -> bool {
self == FP32x32 { mag: 0, sign: true }
}
fn INF() -> FP32x32 {
FP32x32 { mag: 4294967295, sign: false }
}
fn POS_INF() -> FP32x32 {
FP32x32 { mag: 4294967295, sign: false }
}
fn NEG_INF() -> FP32x32 {
FP32x32 { mag: 4294967295, sign: true }
}
fn is_ |
inf(self: FP32x32) -> bool {
self.mag == 4294967295
}
fn is_pos_inf(self: FP32x32) -> bool {
self.is_inf() && !self.sign
}
fn is_neg_inf(self: FP32x32) -> bool {
self.is_inf() && self.sign
}
fn erf(self: FP32x32) -> FP32x32 {
erf::erf(self)
}
}
impl FP32x32Print of PrintTrait<FP32x32> { |
fn print(self: FP32x32) {
self.sign.print();
self.mag.print();
}
}
impl FP32x32IntoFelt252 of Into<FP32x32, felt252> {
fn into(self: FP32x32) -> felt252 {
let mag_felt = self.mag.into();
if self.sign {
mag_felt * -1
} else {
mag_felt * 1
}
}
}
impl FP32x32TryIntoU64 of TryInto<FP32x32, u64> {
fn try_into(self: FP32x32) -> Option<u64> {
if self.sign {
Option::None(())
} else {
Option::Some((self.mag / ONE).into())
}
}
}
impl FP32x32TryIntoU16 of TryInto<FP32x32, u16> {
fn try_into(self: FP32x32) -> Option<u16> {
if self.sign {
Option::None(())
} else {
(self.mag / ONE).try_into()
}
}
}
impl FP32x32TryIntoU32 of TryInto<FP32x32, u32> {
fn try_into(self: FP32x32) -> Option<u32> {
if self.sign {
Option::None(())
} else {
(self.mag / ONE).try_into()
}
}
}
impl FP32x32TryIntoU8 of TryInto<FP32x32, u8> {
fn try_into(self: FP32x32) -> Option<u8> {
if self.sign {
Option::None(())
} else {
(self.mag / ONE).try_into()
}
}
}
impl FP32x32TryIntoI8 of TryInto<FP32x32, i8> {
fn try_into(self: FP32x32) -> Option<i8> {
_i8_try_from_fp(self)
}
}
impl FP32x32Add of Add<FP32x32> {
fn add(lhs: FP32x32, rhs: FP32x32) -> FP32x32 {
fp32x32::ops::add(lhs, rhs)
}
}
impl FP32x32AddEq of AddEq<FP32x32> { |
fn add_eq(ref self: FP32x32, other: FP32x32) {
self = fp32x32::ops::add(self, other);
}
}
impl FP32x32Sub of Sub<FP32x32> {
fn sub(lhs: FP32x32, rhs: FP32x32) -> FP32x32 {
fp32x32::ops::sub(lhs, rhs)
}
}
impl FP32x32SubEq of SubEq<FP32x32> { |
fn sub_eq(ref self: FP32x32, other: FP32x32) {
self = fp32x32::ops::sub(self, other);
}
}
impl FP32x32Mul of Mul<FP32x32> {
fn mul(lhs: FP32x32, rhs: FP32x32) -> FP32x32 {
fp32x32::ops::mul(lhs, rhs)
}
}
impl FP32x32MulEq of MulEq<FP32x32> { |
fn mul_eq(ref self: FP32x32, other: FP32x32) {
self = fp32x32::ops::mul(self, other);
}
}
impl FP32x32Div of Div<FP32x32> {
fn div(lhs: FP32x32, rhs: FP32x32) -> FP32x32 {
fp32x32::ops::div(lhs, rhs)
}
}
impl FP32x32DivEq of DivEq<FP32x32> { |
fn div_eq(ref self: FP32x32, other: FP32x32) {
self = fp32x32::ops::div(self, other);
}
}
impl FP32x32PartialOrd of PartialOrd<FP32x32> {
fn ge(lhs: FP32x32, rhs: FP32x32) -> bool {
fp32x32::ops::ge(lhs, rhs)
}
fn gt(lhs: FP32x32, rhs: FP32x32) -> bool {
fp32x32::ops::gt(lhs, rhs)
}
fn le(lhs: FP32x32, rhs: FP32x32) -> bool {
fp32x32::ops::le(lhs, rhs)
}
fn lt(lhs: FP32x32, rhs: FP32x32) -> bool {
fp32x32::ops::lt(lhs, rhs)
}
}
impl FP32x32Neg of Neg<FP32x32> {
fn neg(a: FP32x32) -> FP32x32 {
fp32x32::ops::neg(a)
}
}
impl FP32x32Rem of Rem<FP32x32> {
fn rem(lhs: FP32x32, rhs: FP32x32) -> FP32x32 {
fp32x32::ops::rem(lhs, rhs)
}
}
fn eq(a: @FP32x32, b: @FP32x32) -> bool {
(*a.mag == *b.mag) && (*a.sign == *b.sign)
}
fn _i8_try_from_fp(x: FP32x32) -> Option<i8> {
let unscaled_mag: Option<u8> = (x.mag / ONE).try_into();
match unscaled_mag {
Option::Some => {
let number_felt: felt252 = unscaled_mag.unwrap().into();
let mut number_i8: i8 = number_felt.try_into().unwrap();
if x.sign {
return Option::Some(number_i8 * -1_i8);
}
Option::Some(number_i8)
},
Option::None => Option::None(())
}
} |
use cubit::f64::ONE;
use orion::numbers::{FP32x32, FixedTrait};
use orion::numbers::fixed_point::implementations::fp32x32::lut::erf_lut;
const ERF_COMPUTATIONAL_ACCURACY: u64 = 100;
const ROUND_CHECK_NUMBER: u64 = 10;
// Values > MAX_ERF_NUMBER return 1
const MAX_ERF_NUMBER: u64 = 15032385536;
// Values <= ERF_TRUNCATION_NUMBER -> two decimal places, and values > ERF_TRUNCATION_NUMBER -> one decimal place
const ERF_TRUNCATION_NUMBER: u64 = 8589934592;
fn erf(x: FP32x32) -> FP32x32 {
// Lookup
// 1. if x.mag < 3.5 { lookup table }
// 2. else{ return 1}
let mut erf_value: u64 = 0_u64;
if x.mag < MAX_ERF_NUMBER {
erf_value = erf_lut(x.mag);
} else {
erf_value = ONE;
}
FP32x32 { mag: erf_value, sign: x.sign }
}
|
use orion::numbers::fixed_point::implementations::fp32x32::core::ONE;
fn erf_lut(x: u64) -> u64 {
if x <= 386547056 {
if x <= 0 {
return 0;
}
if x <= 42949672 {
return 48461900;
}
if x <= 85899345 {
return 96914110;
}
if x <= 128849018 {
return 145346943;
}
if x <= 171798691 {
return 193750725;
}
if x <= 214748364 {
return 242115801;
}
if x <= 257698037 {
return 290432536;
}
if x <= 300647710 {
return 338691327;
}
if x <= 343597383 {
return 386882604;
}
if x <= 386547056 {
return 434996838;
}
}
if x <= 816043786 {
if x <= 429496729 {
return 483024546;
}
if x <= 472446402 {
return 530956296;
}
if x <= 515396075 {
return 578782713;
}
if x <= 558345748 {
return 626494487;
}
if x <= 601295421 {
return 674082374;
}
if x <= 644245094 {
return 721537203;
}
if x <= 687194767 {
return 768849883;
}
if x <= 730144440 {
return 816011407;
}
if x <= 773094113 {
return 863012857;
}
if x <= 816043786 {
return 909845408;
}
}
if x <= 1245540515 {
if x <= 858993459 {
return 956500337;
}
if x <= 901943132 {
return 1002969022;
}
if x <= 944892805 {
return 1049242950;
}
if x <= 987842478 {
return 1095313724;
}
if x <= 1030792151 {
return 1141173063;
}
if x <= 1073741824 {
return 1186812808;
}
if x <= 1116691496 {
return 1232224928;
}
if x <= 1159641169 { |
return 1277401521;
}
if x <= 1202590842 {
return 1322334823;
}
if x <= 1245540515 {
return 1367017205;
}
}
if x <= 1675037245 {
if x <= 1288490188 {
return 1411441184;
}
if x <= 1331439861 {
return 1455599421;
}
if x <= 1374389534 {
return 1499484729;
}
if x <= 1417339207 {
return 1543090073;
}
if x <= 1460288880 {
return 1586408573;
}
if x <= 1503238553 {
return 1629433512;
}
if x <= 1546188226 {
return 1672158333;
}
if x <= 1589137899 {
return 1714576645;
}
if x <= 1632087572 {
return 1756682226;
}
if x <= 1675037245 {
return 1798469022;
}
}
if x <= 2104533975 {
if x <= 1717986918 {
return 1839931154;
}
if x <= 1760936591 {
return 1881062918;
}
if x <= 1803886264 {
return 1921858787;
}
if x <= 1846835937 {
return 1962313411;
}
if x <= 1889785610 {
return 2002421622;
}
if x <= 1932735283 {
return 2042178436;
}
if x <= 1975684956 {
return 2081579049;
}
if x <= 2018634629 {
return 2120618846;
}
if x <= 2061584302 {
return 2159293393;
}
if x <= 2104533975 {
return 2197598448;
}
}
if x <= 2534030704 {
if x <= 2147483648 {
return 2235529952;
}
if x <= 2190433320 {
return 2273084038;
}
if x <= 2233382993 {
return 2310257026;
}
if x <= 2276332666 {
return 2347045424;
}
if x <= 2319282339 {
return 2383445931;
}
if x <= 2362232012 { |
return 2419455435;
}
if x <= 2405181685 {
return 2455071011;
}
if x <= 2448131358 {
return 2490289925;
}
if x <= 2491081031 {
return 2525109629;
}
if x <= 2534030704 {
return 2559527765;
}
}
if x <= 2963527434 {
if x <= 2576980377 {
return 2593542161;
}
if x <= 2619930050 {
return 2627150830;
}
if x <= 2662879723 {
return 2660351971;
}
if x <= 2705829396 {
return 2693143967;
}
if x <= 2748779069 {
return 2725525382;
}
if x <= 2791728742 {
return 2757494964;
}
if x <= 2834678415 {
return 2789051637;
}
if x <= 2877628088 {
return 2820194507;
}
if x <= 2920577761 {
return 2850922852;
}
if x <= 2963527434 {
return 2881236128;
}
}
if x <= 3393024163 {
if x <= 3006477107 {
return 2911133960;
}
if x <= 3049426780 {
return 2940616146;
}
if x <= 3092376453 {
return 2969682651;
}
if x <= 3135326126 {
return 2998333604;
}
if x <= 3178275799 {
return 3026569298;
}
if x <= 3221225472 {
return 3054390188;
}
if x <= 3264175144 {
return 3081796886;
}
if x <= 3307124817 {
return 3108790160;
}
if x <= 3350074490 {
return 3135370928;
}
if x <= 3393024163 {
return 3161540260;
}
}
if x <= 3822520893 {
if x <= 3435973836 {
return 3187299373;
}
if x <= 3478923509 {
return 3212649627;
}
if x <= 3521873182 {
return 3237592522;
}
if x <= 35648228 |
55 {
return 3262129696;
}
if x <= 3607772528 {
return 3286262922;
}
if x <= 3650722201 {
return 3309994103;
}
if x <= 3693671874 {
return 3333325270;
}
if x <= 3736621547 {
return 3356258580;
}
if x <= 3779571220 {
return 3378796308;
}
if x <= 3822520893 {
return 3400940848;
}
}
if x <= 4252017623 {
if x <= 3865470566 {
return 3422694710;
}
if x <= 3908420239 {
return 3444060511;
}
if x <= 3951369912 {
return 3465040979;
}
if x <= 3994319585 {
return 3485638942;
}
if x <= 4037269258 {
return 3505857331;
}
if x <= 4080218931 {
return 3525699170;
}
if x <= 4123168604 {
return 3545167580;
}
if x <= 4166118277 {
return 3564265768;
}
if x <= 4209067950 {
return 3582997028;
}
if x <= 4252017623 {
return 3601364736;
}
}
if x <= 4681514352 {
if x <= 4294967296 {
return 3619372346;
}
if x <= 4337916968 {
return 3637023387;
}
if x <= 4380866641 {
return 3654321460;
}
if x <= 4423816314 {
return 3671270233;
}
if x <= 4466765987 {
return 3687873439;
}
if x <= 4509715660 {
return 3704134870;
}
if x <= 4552665333 {
return 3720058378;
}
if x <= 4595615006 {
return 3735647866;
}
if x <= 4638564679 {
return 3750907289;
}
if x <= 4681514352 {
return 3765840647;
}
}
if x <= 5111011082 {
if x <= 4724464025 {
return 3780451987;
}
if x <= 476 |
7413698 {
return 3794745393;
}
if x <= 4810363371 {
return 3808724986;
}
if x <= 4853313044 {
return 3822394923;
}
if x <= 4896262717 {
return 3835759389;
}
if x <= 4939212390 {
return 3848822598;
}
if x <= 4982162063 {
return 3861588787;
}
if x <= 5025111736 {
return 3874062214;
}
if x <= 5068061409 {
return 3886247156;
}
if x <= 5111011082 {
return 3898147905;
}
}
if x <= 5540507811 {
if x <= 5153960755 {
return 3909768765;
}
if x <= 5196910428 {
return 3921114049;
}
if x <= 5239860101 {
return 3932188077;
}
if x <= 5282809774 {
return 3942995173;
}
if x <= 5325759447 {
return 3953539662;
}
if x <= 5368709120 {
return 3963825868;
}
if x <= 5411658792 {
return 3973858111;
}
if x <= 5454608465 {
return 3983640704;
}
if x <= 5497558138 {
return 3993177952;
}
if x <= 5540507811 {
return 4002474150;
}
}
if x <= 5970004541 {
if x <= 5583457484 {
return 4011533577;
}
if x <= 5626407157 {
return 4020360499;
}
if x <= 5669356830 {
return 4028959162;
}
if x <= 5712306503 {
return 4037333795;
}
if x <= 5755256176 {
return 4045488602;
}
if x <= 5798205849 {
return 4053427767;
}
if x <= 5841155522 {
return 4061155446;
}
if x <= 5884105195 {
return 4068675768;
}
if x <= 5927054868 {
return 4075992834;
}
if x <= 5970004541 {
retu |
rn 4083110714;
}
}
if x <= 6399501271 {
if x <= 6012954214 {
return 4090033445;
}
if x <= 6055903887 {
return 4096765032;
}
if x <= 6098853560 {
return 4103309442;
}
if x <= 6141803233 {
return 4109670609;
}
if x <= 6184752906 {
return 4115852426;
}
if x <= 6227702579 {
return 4121858749;
}
if x <= 6270652252 {
return 4127693393;
}
if x <= 6313601925 {
return 4133360131;
}
if x <= 6356551598 {
return 4138862695;
}
if x <= 6399501271 {
return 4144204773;
}
}
if x <= 6828998000 {
if x <= 6442450944 {
return 4149390008;
}
if x <= 6485400616 {
return 4154421999;
}
if x <= 6528350289 {
return 4159304298;
}
if x <= 6571299962 {
return 4164040410;
}
if x <= 6614249635 {
return 4168633795;
}
if x <= 6657199308 {
return 4173087863;
}
if x <= 6700148981 {
return 4177405975;
}
if x <= 6743098654 {
return 4181591444;
}
if x <= 6786048327 {
return 4185647533;
}
if x <= 6828998000 {
return 4189577456;
}
}
if x <= 7258494730 {
if x <= 6871947673 {
return 4193384375;
}
if x <= 6914897346 {
return 4197071404;
}
if x <= 6957847019 {
return 4200641603;
}
if x <= 7000796692 {
return 4204097984;
}
if x <= 7043746365 {
return 4207443505;
}
if x <= 7086696038 {
return 4210681075;
}
if x <= 7129645711 {
return 4213813550;
}
if x <= 7172595384 { |
return 4216843737;
}
if x <= 7215545057 {
return 4219774388;
}
if x <= 7258494730 {
return 4222608207;
}
}
if x <= 7687991459 {
if x <= 7301444403 {
return 4225347845;
}
if x <= 7344394076 {
return 4227995903;
}
if x <= 7387343749 {
return 4230554929;
}
if x <= 7430293422 {
return 4233027424;
}
if x <= 7473243095 {
return 4235415834;
}
if x <= 7516192768 {
return 4237722559;
}
if x <= 7559142440 {
return 4239949947;
}
if x <= 7602092113 {
return 4242100295;
}
if x <= 7645041786 {
return 4244175854;
}
if x <= 7687991459 {
return 4246178824;
}
}
if x <= 8117488189 {
if x <= 7730941132 {
return 4248111357;
}
if x <= 7773890805 {
return 4249975557;
}
if x <= 7816840478 {
return 4251773482;
}
if x <= 7859790151 {
return 4253507139;
}
if x <= 7902739824 {
return 4255178493;
}
if x <= 7945689497 {
return 4256789460;
}
if x <= 7988639170 {
return 4258341912;
}
if x <= 8031588843 {
return 4259837674;
}
if x <= 8074538516 {
return 4261278529;
}
if x <= 8117488189 {
return 4262666214;
}
}
if x <= 8546984919 {
if x <= 8160437862 {
return 4264002425;
}
if x <= 8203387535 {
return 4265288813;
}
if x <= 8246337208 {
return 4266526989;
}
if x <= 8289286881 {
return 4267718520;
}
if x <= 8332236554 {
return 4268864936;
}
if x <= 8375186227 { |
return 4269967724;
}
if x <= 8418135900 {
return 4271028331;
}
if x <= 8461085573 {
return 4272048167;
}
if x <= 8504035246 {
return 4273028604;
}
if x <= 8546984919 {
return 4273970975;
}
}
if x <= 14602888806 {
if x <= 8589934592 {
return 4274876577;
}
if x <= 9019431321 {
return 4282170584;
}
if x <= 9448928051 {
return 4286966432;
}
if x <= 9878424780 {
return 4290057389;
}
if x <= 10307921510 {
return 4292010151;
}
if x <= 10737418240 {
return 4293219450;
}
if x <= 11166914969 {
return 4293953535;
}
if x <= 11596411699 {
return 4294390341;
}
if x <= 12025908428 {
return 4294645116;
}
if x <= 12455405158 {
return 4294790781;
}
if x <= 12884901888 {
return 4294872418;
}
if x <= 13314398617 {
return 4294917265;
}
if x <= 13743895347 {
return 4294941415;
}
if x <= 14173392076 {
return 4294954163;
}
if x <= 14602888806 {
return 4294960759;
}
}
ONE
} |
mod core;
mod comp;
mod erf;
mod lut;
|
use orion::numbers::{FP64x64, FixedTrait};
use orion::numbers::FP64x64Impl;
fn xor(a: FP64x64, b: FP64x64) -> bool {
if (a == FixedTrait::new(0, false) || b == FixedTrait::new(0, false)) && (a != b) {
true
} else {
false
}
}
fn or(a: FP64x64, b: FP64x64) -> bool {
let zero = FixedTrait::new(0, false);
if a == zero && b == zero {
false
} else {
true
}
}
fn and(a: FP64x64, b: FP64x64) -> bool {
let zero = FixedTrait::new(0, false);
if a == zero || b == zero {
false
} else {
true
}
}
fn where(a: FP64x64, b: FP64x64, c: FP64x64) -> FP64x64 {
if a == FixedTrait::new(0, false) {
c
} else {
b
}
}
fn bitwise_and(a: FP64x64, b: FP64x64) -> FP64x64 {
FixedTrait::new(a.mag & b.mag, a.sign & b.sign)
}
fn bitwise_xor(a: FP64x64, b: FP64x64) -> FP64x64 {
FixedTrait::new(a.mag ^ b.mag, a.sign ^ b.sign)
}
fn bitwise_or(a: FP64x64, b: FP64x64) -> FP64x64 {
FixedTrait::new(a.mag | b.mag, a.sign | b.sign)
}
|
use core::debug::PrintTrait;
use cubit::f128 as fp64x64;
use cubit::f128::types::Fixed as FP64x64;
use cubit::f128::ONE_u128 as ONE;
use cubit::f128::ops::MAX_u128 as MAX;
use orion::numbers::fixed_point::implementations::fp64x64::erf;
use orion::numbers::fixed_point::core::{FixedTrait};
use orion::numbers::fixed_point::utils;
const HALF: u128 = 9223372036854775808_u128;
impl FP64x64Impl of FixedTrait<FP64x64, u128> {
fn ZERO() -> FP64x64 {
FP64x64 { mag: 0, sign: false }
}
fn HALF() -> FP64x64 {
FP64x64 { mag: HALF, sign: false }
}
fn ONE() -> FP64x64 {
FP64x64 { mag: ONE, sign: false }
}
fn MAX() -> FP64x64 {
FP64x64 { mag: MAX, sign: false }
}
fn new(mag: u128, sign: bool) -> FP64x64 {
FP64x64 { mag: mag, sign: sign }
}
fn new_unscaled(mag: u128, sign: bool) -> FP64x64 {
FP64x64 { mag: mag * ONE, sign: sign }
}
fn from_felt(val: felt252) -> FP64x64 {
let mag = core::integer::u128_try_from_felt252(utils::felt_abs(val)).unwrap();
FixedTrait::new(mag, utils::felt_sign(val))
}
fn abs(self: FP64x64) -> FP64x64 {
fp64x64::ops::abs(self)
}
fn acos(self: FP64x64) -> FP64x64 {
fp64x64::trig::acos_fast(self)
}
fn acos_fast(self: FP64x64) -> FP64x64 {
fp64x64::trig::acos_fast(self)
}
fn acosh(self: FP64x64) -> FP64x64 {
fp64x64::hyp::acosh(self)
}
fn asin(self: FP64x64) -> FP64x64 {
fp64x64::trig::asin_fast(self)
}
fn asin_fast(self: FP64x64) -> FP64x64 {
fp64x64::trig::asin_fast(self)
}
fn asinh(self: FP64x64) -> FP64x64 {
fp64x64::hyp::asinh(self)
}
fn atan(self: FP64x64) -> FP64x64 {
fp64x64::trig::atan_fast(self)
}
fn atan_fast(self: FP64x64) -> FP64x64 {
fp64x64::trig::atan_fast(self)
}
fn atanh(self: FP64x64) -> FP64x64 {
fp64x64::hyp::atanh(self)
}
fn ceil(self: FP64x64) -> FP64x64 {
fp64x64::ops::ceil(se |
lf)
}
fn cos(self: FP64x64) -> FP64x64 {
fp64x64::trig::cos_fast(self)
}
fn cos_fast(self: FP64x64) -> FP64x64 {
fp64x64::trig::cos_fast(self)
}
fn cosh(self: FP64x64) -> FP64x64 {
fp64x64::hyp::cosh(self)
}
fn floor(self: FP64x64) -> FP64x64 {
fp64x64::ops::floor(self)
}
fn exp(self: FP64x64) -> FP64x64 {
fp64x64::ops::exp(self)
}
fn exp2(self: FP64x64) -> FP64x64 {
fp64x64::ops::exp2(self)
}
fn ln(self: FP64x64) -> FP64x64 {
fp64x64::ops::ln(self)
}
fn log2(self: FP64x64) -> FP64x64 {
fp64x64::ops::log2(self)
}
fn log10(self: FP64x64) -> FP64x64 {
fp64x64::ops::log10(self)
}
fn pow(self: FP64x64, b: FP64x64) -> FP64x64 {
fp64x64::ops::pow(self, b)
}
fn round(self: FP64x64) -> FP64x64 {
fp64x64::ops::round(self)
}
fn sin(self: FP64x64) -> FP64x64 {
fp64x64::trig::sin_fast(self)
}
fn sin_fast(self: FP64x64) -> FP64x64 {
fp64x64::trig::sin_fast(self)
}
fn sinh(self: FP64x64) -> FP64x64 {
fp64x64::hyp::sinh(self)
}
fn sqrt(self: FP64x64) -> FP64x64 {
fp64x64::ops::sqrt(self)
}
fn tan(self: FP64x64) -> FP64x64 {
fp64x64::trig::tan_fast(self)
}
fn tan_fast(self: FP64x64) -> FP64x64 {
fp64x64::trig::tan_fast(self)
}
fn tanh(self: FP64x64) -> FP64x64 {
fp64x64::hyp::tanh(self)
}
fn sign(self: FP64x64) -> FP64x64 {
panic(array!['not supported!'])
}
fn NaN() -> FP64x64 {
FP64x64 { mag: 0, sign: true }
}
fn is_nan(self: FP64x64) -> bool {
self == FP64x64 { mag: 0, sign: true }
}
fn INF() -> FP64x64 {
FP64x64 { mag: 4294967295, sign: false }
}
fn POS_INF() -> FP64x64 {
FP64x64 { mag: 4294967295, sign: false }
}
fn NEG_INF() -> FP64x64 {
FP64x64 { mag: 4294967 |
295, sign: true }
}
fn is_inf(self: FP64x64) -> bool {
self.mag == 4294967295
}
fn is_pos_inf(self: FP64x64) -> bool {
self.is_inf() && !self.sign
}
fn is_neg_inf(self: FP64x64) -> bool {
self.is_inf() && self.sign
}
fn erf(self: FP64x64) -> FP64x64 {
erf::erf(self)
}
}
impl FP64x64Print of PrintTrait<FP64x64> { |
fn print(self: FP64x64) {
self.sign.print();
self.mag.print();
}
}
impl FP64x64IntoFelt252 of Into<FP64x64, felt252> {
fn into(self: FP64x64) -> felt252 {
let mag_felt = self.mag.into();
if self.sign {
mag_felt * -1
} else {
mag_felt * 1
}
}
}
impl FP64x64TryIntoU128 of TryInto<FP64x64, u128> {
fn try_into(self: FP64x64) -> Option<u128> {
if self.sign {
Option::None(())
} else {
Option::Some((self.mag / ONE).into())
}
}
}
impl FP64x64TryIntoU16 of TryInto<FP64x64, u16> {
fn try_into(self: FP64x64) -> Option<u16> {
if self.sign {
Option::None(())
} else {
(self.mag / ONE).try_into()
}
}
}
impl FP64x64TryIntoU32 of TryInto<FP64x64, u32> {
fn try_into(self: FP64x64) -> Option<u32> {
if self.sign {
Option::None(())
} else {
(self.mag / ONE).try_into()
}
}
}
impl FP64x64TryIntoU8 of TryInto<FP64x64, u8> {
fn try_into(self: FP64x64) -> Option<u8> {
if self.sign {
Option::None(())
} else {
(self.mag / ONE).try_into()
}
}
}
impl FP64x64TryIntoI8 of TryInto<FP64x64, i8> {
fn try_into(self: FP64x64) -> Option<i8> {
_i8_try_from_fp(self)
}
}
impl FP64x64Add of Add<FP64x64> {
fn add(lhs: FP64x64, rhs: FP64x64) -> FP64x64 {
fp64x64::ops::add(lhs, rhs)
}
}
impl FP64x64AddEq of AddEq<FP64x64> { |
fn add_eq(ref self: FP64x64, other: FP64x64) {
self = fp64x64::ops::add(self, other);
}
}
impl FP64x64Sub of Sub<FP64x64> {
fn sub(lhs: FP64x64, rhs: FP64x64) -> FP64x64 {
fp64x64::ops::sub(lhs, rhs)
}
}
impl FP64x64SubEq of SubEq<FP64x64> { |
fn sub_eq(ref self: FP64x64, other: FP64x64) {
self = fp64x64::ops::sub(self, other);
}
}
impl FP64x64Mul of Mul<FP64x64> {
fn mul(lhs: FP64x64, rhs: FP64x64) -> FP64x64 {
fp64x64::ops::mul(lhs, rhs)
}
}
impl FP64x64MulEq of MulEq<FP64x64> { |
fn mul_eq(ref self: FP64x64, other: FP64x64) {
self = fp64x64::ops::mul(self, other);
}
}
impl FP64x64Div of Div<FP64x64> {
fn div(lhs: FP64x64, rhs: FP64x64) -> FP64x64 {
fp64x64::ops::div(lhs, rhs)
}
}
impl FP64x64DivEq of DivEq<FP64x64> { |
fn div_eq(ref self: FP64x64, other: FP64x64) {
self = fp64x64::ops::div(self, other);
}
}
impl FP64x64PartialOrd of PartialOrd<FP64x64> {
fn ge(lhs: FP64x64, rhs: FP64x64) -> bool {
fp64x64::ops::ge(lhs, rhs)
}
fn gt(lhs: FP64x64, rhs: FP64x64) -> bool {
fp64x64::ops::gt(lhs, rhs)
}
fn le(lhs: FP64x64, rhs: FP64x64) -> bool {
fp64x64::ops::le(lhs, rhs)
}
fn lt(lhs: FP64x64, rhs: FP64x64) -> bool {
fp64x64::ops::lt(lhs, rhs)
}
}
impl FP64x64Neg of Neg<FP64x64> {
fn neg(a: FP64x64) -> FP64x64 {
fp64x64::ops::neg(a)
}
}
impl FP64x64Rem of Rem<FP64x64> {
fn rem(lhs: FP64x64, rhs: FP64x64) -> FP64x64 {
fp64x64::ops::rem(lhs, rhs)
}
}
fn eq(a: @FP64x64, b: @FP64x64) -> bool {
(*a.mag == *b.mag) && (*a.sign == *b.sign)
}
fn _i8_try_from_fp(x: FP64x64) -> Option<i8> {
let unscaled_mag: Option<u8> = (x.mag / ONE).try_into();
match unscaled_mag {
Option::Some => {
let number_felt: felt252 = unscaled_mag.unwrap().into();
let mut number_i8: i8 = number_felt.try_into().unwrap();
if x.sign {
return Option::Some(number_i8 * -1_i8);
}
Option::Some(number_i8)
},
Option::None => Option::None(())
}
} |
use cubit::f128::ONE_u128 as ONE;
use orion::numbers::{FP64x64, FixedTrait};
use orion::numbers::fixed_point::implementations::fp64x64::lut::erf_lut;
const ERF_COMPUTATIONAL_ACCURACY: u128 = 100_u128;
const ROUND_CHECK_NUMBER: u128 = 10_u128;
// Values > MAX_ERF_NUMBER return 1
const MAX_ERF_NUMBER: u128 = 64563604257983430656_u128;
// Values <= ERF_TRUNCATION_NUMBER -> two decimal places, and values > ERF_TRUNCATION_NUMBER -> one decimal place
const ERF_TRUNCATION_NUMBER: u128 = 36893488147419103232_u128;
fn erf(x: FP64x64) -> FP64x64 {
// Lookup
// 1. if x.mag < 3.5 { lookup table }
// 2. else{ return 1}
let mut erf_value: u128 = 0_u128;
if x.mag <= MAX_ERF_NUMBER {
erf_value = erf_lut(x.mag);
} else {
erf_value = ONE;
}
FP64x64 { mag: erf_value, sign: x.sign }
}
|
use orion::numbers::fixed_point::implementations::fp64x64::core::ONE;
fn erf_lut(x: u128) -> u128 {
if x <= 1660206966633859584 {
if x <= 0 {
return 0;
}
if x <= 184467440737095520 {
return 208142279036071072;
}
if x <= 368934881474191040 {
return 416242934472567232;
}
if x <= 553402322211286528 {
return 624260367679495296;
}
if x <= 737869762948382080 {
return 832153029941062528;
}
if x <= 922337203685477632 {
return 1039879447350402944;
}
if x <= 1106804644422573056 {
return 1247398245629553408;
}
if x <= 1291272085159668736 {
return 1454668174849927424;
}
if x <= 1475739525896764160 {
return 1661648134028665088;
}
if x <= 1660206966633859584 {
return 1868297195576427008;
}
}
if x <= 3504881374004814848 {
if x <= 1844674407370955264 {
return 2074574629572391936;
}
if x <= 2029141848108050688 {
return 2280439927842463744;
}
if x <= 2213609288845146112 {
return 2485852827816977408;
}
if x <= 2398076729582241792 {
return 2690773336144481280;
}
if x <= 2582544170319337472 {
return 2895161752038532608;
}
if x <= 2767011611056432640 {
return 3098978690334796800;
}
if x <= 2951479051793528320 {
return 3302185104236156928;
}
if x <= 3135946492530624000 {
return 3504742307723958272;
}
if x <= 3320413933267719168 {
return 3706611997613982720;
}
if x <= 3504881374004814848 {
return 3907756275236240384;
}
}
if x <= 5349555781375769600 {
if x <= 3689348814741910528 {
return 4108137667718166528;
}
if x <= 387381625 |
5479005696 {
return 4307719148851377152;
}
if x <= 4058283696216101376 {
return 4506464159522699776;
}
if x <= 4242751136953197056 {
return 4704336627690769408;
}
if x <= 4427218577690292224 {
return 4901300987890141184;
}
if x <= 4611686018427387904 {
return 5097322200245477376;
}
if x <= 4796153459164483584 {
return 5292365768979031040;
}
if x <= 4980620899901579264 {
return 5486397760395360256;
}
if x <= 5165088340638674944 {
return 5679384820327877632;
}
if x <= 5349555781375769600 {
return 5871294191032579072;
}
}
if x <= 7194230188746725376 {
if x <= 5534023222112865280 {
return 6062093727515032576;
}
if x <= 5718490662849960960 {
return 6251751913277435904;
}
if x <= 5902958103587056640 {
return 6440237875473368064;
}
if x <= 6087425544324152320 {
return 6627521399458594816;
}
if x <= 6271892985061248000 {
return 6813572942727099392;
}
if x <= 6456360425798342656 {
return 6998363648222307328;
}
if x <= 6640827866535438336 {
return 7181865357014296576;
}
if x <= 6825295307272534016 {
return 7364050620334585856;
}
if x <= 7009762748009629696 {
return 7544892710960923648;
}
if x <= 7194230188746725376 {
return 7724365633945352192;
}
}
if x <= 9038904596117680128 {
if x <= 7378697629483821056 {
return 7902444136679609344;
}
if x <= 7563165070220915712 {
return 8079103718292817920;
}
if x <= 7747632510958011392 {
return 8254320638377208832;
}
if x <= 7932099951695107072 {
return |
8428071925038478336;
}
if x <= 8116567392432202752 {
return 8600335382268215296;
}
if x <= 8301034833169298432 {
return 8771089596636659712;
}
if x <= 8485502273906394112 {
return 8940313943304876032;
}
if x <= 8669969714643488768 {
return 9107988591356256256;
}
if x <= 8854437155380584448 {
return 9274094508448081920;
}
if x <= 9038904596117680128 {
return 9438613464784658432;
}
}
if x <= 10883579003488634880 {
if x <= 9223372036854775808 {
return 9601528036414361600;
}
if x <= 9407839477591871488 {
return 9762821607853701120;
}
if x <= 9592306918328967168 {
return 9922478374042292224;
}
if x <= 9776774359066062848 {
return 10080483341633368064;
}
if x <= 9961241799803158528 {
return 10236822329625237504;
}
if x <= 10145709240540254208 {
return 10391481969339820032;
}
if x <= 10330176681277349888 {
return 10544449703755059200;
}
if x <= 10514644122014443520 {
return 10695713786198818816;
}
if x <= 10699111562751539200 {
return 10845263278412423168;
}
if x <= 10883579003488634880 {
return 10993088047992748032;
}
}
if x <= 12728253410859589632 {
if x <= 11068046444225730560 {
return 11139178765222393856;
}
if x <= 11252513884962826240 {
return 11283526899298078720;
}
if x <= 11436981325699921920 {
return 11426124713968005120;
}
if x <= 11621448766437017600 {
return 11566965262589513728;
}
if x <= 11805916207174113280 {
return 11706042382618923008;
}
if x <= 11990383647911208960 {
return 1184335 |
0689545969664;
}
if x <= 12174851088648304640 {
return 11978885570285762560;
}
if x <= 12359318529385400320 {
return 12112643176041672704;
}
if x <= 12543785970122496000 {
return 12244620414653018112;
}
if x <= 12728253410859589632 {
return 12374814942441867264;
}
}
if x <= 14572927818230546432 {
if x <= 12912720851596685312 {
return 12503225155573657600;
}
if x <= 13097188292333780992 {
return 12629850180946728960;
}
if x <= 13281655733070876672 {
return 12754689866626244608;
}
if x <= 13466123173807972352 {
return 12877744771838261248;
}
if x <= 13650590614545068032 {
return 12999016156540069888;
}
if x <= 13835058055282163712 {
return 13118505970583140352;
}
if x <= 14019525496019259392 {
return 13236216842485327872;
}
if x <= 14203992936756355072 {
return 13352152067829151744;
}
if x <= 14388460377493450752 {
return 13466315597303212032;
}
if x <= 14572927818230546432 {
return 13578712024403965952;
}
}
if x <= 16417602225601501184 {
if x <= 14757395258967642112 {
return 13689346572815177728;
}
if x <= 14941862699704737792 {
return 13798225083482576896;
}
if x <= 15126330140441831424 {
return 13905354001401262080;
}
if x <= 15310797581178927104 {
return 14010740362133477376;
}
if x <= 15495265021916022784 {
return 14114391778074478592;
}
if x <= 15679732462653118464 {
return 14216316424484128768;
}
if x <= 15864199903390214144 {
return 14316523025301962752;
}
if x <= 16048667344127309824 { |
return 14415020838763323392;
}
if x <= 16233134784864405504 {
return 14511819642834194432;
}
if x <= 16417602225601501184 {
return 14606929720482222080;
}
}
if x <= 18262276632972455936 {
if x <= 16602069666338596864 {
return 14700361844801351680;
}
if x <= 16786537107075692544 {
return 14792127264007346176;
}
if x <= 16971004547812788224 {
return 14882237686321330176;
}
if x <= 17155471988549883904 {
return 14970705264758325248;
}
if x <= 17339939429286977536 {
return 15057542581837537280;
}
if x <= 17524406870024073216 {
return 15142762634230988800;
}
if x <= 17708874310761168896 {
return 15226378817366812672;
}
if x <= 17893341751498264576 {
return 15308404910003300352;
}
if x <= 18077809192235360256 {
return 15388855058789533696;
}
if x <= 18262276632972455936 {
return 15467743762828154880;
}
}
if x <= 20106951040343412736 {
if x <= 18446744073709551616 {
return 15545085858255493120;
}
if x <= 18631211514446647296 {
return 15620896502854008832;
}
if x <= 18815678955183742976 {
return 15695191160711634944;
}
if x <= 19000146395920838656 {
return 15767985586942304256;
}
if x <= 19184613836657934336 {
return 15839295812481531904;
}
if x <= 19369081277395030016 {
return 15909138128970633216;
}
if x <= 19553548718132125696 {
return 15977529073742716928;
}
if x <= 19738016158869221376 {
return 16044485414923208704;
}
if x <= 19922483599606317056 {
return 16110024136657332224;
}
if x <= 20106951040343412736 |
{
return 16174162424476436480;
}
}
if x <= 21951625447714365440 {
if x <= 20291418481080508416 {
return 16236917650814795776;
}
if x <= 20475885921817604096 {
return 16298307360687947776;
}
if x <= 20660353362554699776 {
return 16358349257543309312;
}
if x <= 20844820803291791360 {
return 16417061189293291520;
}
if x <= 21029288244028887040 {
return 16474461134540791808;
}
if x <= 21213755684765982720 {
return 16530567189006364672;
}
if x <= 21398223125503078400 {
return 16585397552166088704;
}
if x <= 21582690566240174080 {
return 16638970514108524544;
}
if x <= 21767158006977269760 {
return 16691304442618875904;
}
if x <= 21951625447714365440 {
return 16742417770497863680;
}
}
if x <= 23796299855085322240 {
if x <= 22136092888451461120 {
return 16792328983122491392;
}
if x <= 22320560329188556800 {
return 16841056606255333376;
}
if x <= 22505027769925652480 {
return 16888619194108602368;
}
if x <= 22689495210662748160 {
return 16935035317668771840;
}
if x <= 22873962651399843840 {
return 16980323553287045120;
}
if x <= 23058430092136939520 {
return 17024502471540604928;
}
if x <= 23242897532874035200 {
return 17067590626369081344;
}
if x <= 23427364973611130880 {
return 17109606544490213376;
}
if x <= 23611832414348226560 {
return 17150568715098355712;
}
if x <= 23796299855085322240 {
return 17190495579848931328;
}
}
if x <= 25640974262456274944 {
if x <= 23980767295822417920 {
return 17229405523131 |
617280;
}
if x <= 24165234736559513600 {
return 17267316862634573824;
}
if x <= 24349702177296609280 {
return 17304247840201650176;
}
if x <= 24534169618033704960 {
return 17340216612984107008;
}
if x <= 24718637058770800640 {
return 17375241244887996416;
}
if x <= 24903104499507896320 {
return 17409339698317971456;
}
if x <= 25087571940244992000 {
return 17442529826217906176;
}
if x <= 25272039380982087680 {
return 17474829364408369152;
}
if x <= 25456506821719179264 {
return 17506255924220641280;
}
if x <= 25640974262456274944 {
return 17536826985426591744;
}
}
if x <= 27485648669827231744 {
if x <= 25825441703193370624 {
return 17566559889463431168;
}
if x <= 26009909143930466304 {
return 17595471832952045568;
}
if x <= 26194376584667561984 {
return 17623579861507229696;
}
if x <= 26378844025404657664 {
return 17650900863837954048;
}
if x <= 26563311466141753344 {
return 17677451566135410688;
}
if x <= 26747778906878849024 {
return 17703248526746337280;
}
if x <= 26932246347615944704 {
return 17728308131128877056;
}
if x <= 27116713788353040384 {
return 17752646587087935488;
}
if x <= 27301181229090136064 {
return 17776279920286781440;
}
if x <= 27485648669827231744 {
return 17799223970031376384;
}
}
if x <= 29330323077198188544 {
if x <= 27670116110564327424 {
return 17821494385323737088;
}
if x <= 27854583551301423104 {
return 17843106621180358656;
}
if x <= 28039050992038518784 {
return |
17864075935211624448;
}
if x <= 28223518432775614464 {
return 17884417384457840640;
}
if x <= 28407985873512710144 {
return 17904145822477408256;
}
if x <= 28592453314249805824 {
return 17923275896682506240;
}
if x <= 28776920754986901504 {
return 17941822045917437952;
}
if x <= 28961388195723997184 {
return 17959798498274711552;
}
if x <= 29145855636461092864 {
return 17977219269143760896;
}
if x <= 29330323077198188544 {
return 17994098159487121408;
}
}
if x <= 31174997484569141248 {
if x <= 29514790517935284224 {
return 18010448754338713600;
}
if x <= 29699257958672379904 {
return 18026284421518878720;
}
if x <= 29883725399409475584 {
return 18041618310560610304;
}
if x <= 30068192840146567168 {
return 18056463351841458176;
}
if x <= 30252660280883662848 {
return 18070832255915431936;
}
if x <= 30437127721620758528 {
return 18084737513039206400;
}
if x <= 30621595162357854208 {
return 18098191392886906880;
}
if x <= 30806062603094949888 {
return 18111205944447655936;
}
if x <= 30990530043832045568 {
return 18123792996100098048;
}
if x <= 31174997484569141248 {
return 18135964155858038784;
}
}
if x <= 33019671891940098048 {
if x <= 31359464925306236928 {
return 18147730811781371904;
}
if x <= 31543932366043332608 {
return 18159104132546453504;
}
if x <= 31728399806780428288 {
return 18170095068170047488;
}
if x <= 31912867247517523968 {
return 18180714350881038336;
}
if x <= 32097334688254619648 { |
return 18190972496134107136;
}
if x <= 32281802128991715328 {
return 18200879803759552512;
}
if x <= 32466269569728811008 {
return 18210446359243550720;
}
if x <= 32650737010465906688 {
return 18219682035133120512;
}
if x <= 32835204451203002368 {
return 18228596492560154624;
}
if x <= 33019671891940098048 {
return 18237199182878894080;
}
}
if x <= 34864346299311050752 {
if x <= 33204139332677193728 {
return 18245499349411323904;
}
if x <= 33388606773414289408 {
return 18253506029294995456;
}
if x <= 33573074214151385088 {
return 18261228055427880960;
}
if x <= 33757541654888480768 {
return 18268674058504921088;
}
if x <= 33942009095625576448 {
return 18275852469141008384;
}
if x <= 34126476536362672128 {
return 18282771520075268096;
}
if x <= 34310943977099767808 {
return 18289439248451522560;
}
if x <= 34495411417836863488 {
return 18295863498169980928;
}
if x <= 34679878858573955072 {
return 18302051922305267712;
}
if x <= 34864346299311050752 {
return 18308011985585967104;
}
}
if x <= 36709020706682007552 {
if x <= 35048813740048146432 {
return 18313750966931048448;
}
if x <= 35233281180785242112 {
return 18319275962038544384;
}
if x <= 35417748621522337792 {
return 18324593886022047744;
}
if x <= 35602216062259433472 {
return 18329711476090615808;
}
if x <= 35786683502996529152 {
return 18334635294267887616;
}
if x <= 35971150943733624832 {
return 18339371730146226176;
}
if x <= 36155618384470 |
720512 {
return 18343927003671875584;
}
if x <= 36340085825207816192 {
return 18348307167957243904;
}
if x <= 36524553265944911872 {
return 18352518112116494336;
}
if x <= 36709020706682007552 {
return 18356565564120772608;
}
}
if x <= 62718929850612473856 {
if x <= 36893488147419103232 {
return 18360455093669533696;
}
if x <= 38738162554790060032 {
return 18391782614824026112;
}
if x <= 40582836962161016832 {
return 18412380624802023424;
}
if x <= 42427511369531965440 {
return 18425656187587059712;
}
if x <= 44272185776902922240 {
return 18434043234066948096;
}
if x <= 46116860184273879040 {
return 18439237133993463808;
}
if x <= 47961534591644835840 {
return 18442390007235248128;
}
if x <= 49806208999015792640 {
return 18444266072035147776;
}
if x <= 51650883406386741248 {
return 18445360324505407488;
}
if x <= 53495557813757698048 {
return 18445985951670278144;
}
if x <= 55340232221128654848 {
return 18446336575964956672;
}
if x <= 57184906628499611648 {
return 18446529193908295680;
}
if x <= 59029581035870568448 {
return 18446632918035736576;
}
if x <= 60874255443241517056 {
return 18446687668919484416;
}
if x <= 62718929850612473856 {
return 18446715997887504384;
}
}
ONE
} |
mod core;
mod math;
mod helpers;
|
use core::debug::PrintTrait;
use orion::numbers::fixed_point::core::{FixedTrait};
use orion::numbers::fixed_point::implementations::fp8x23::math::{core as core_math, trig, hyp, erf};
use orion::numbers::fixed_point::utils; |
struct FP8x23 {
mag: u32,
sign: bool
}
const TWO: u32 = 16777216;
const ONE: u32 = 8388608;
const HALF: u32 = 4194304;
const MAX: u32 = 2147483648;
impl FP8x23Impl of FixedTrait<FP8x23, u32> {
fn ZERO() -> FP8x23 {
FP8x23 { mag: 0, sign: false }
}
fn HALF() -> FP8x23 {
FP8x23 { mag: HALF, sign: false }
}
fn ONE() -> FP8x23 {
FP8x23 { mag: ONE, sign: false }
}
fn MAX() -> FP8x23 {
FP8x23 { mag: MAX, sign: false }
}
fn new(mag: u32, sign: bool) -> FP8x23 {
FP8x23 { mag: mag, sign: sign }
}
fn new_unscaled(mag: u32, sign: bool) -> FP8x23 {
FP8x23 { mag: mag * ONE, sign: sign }
}
fn from_felt(val: felt252) -> FP8x23 {
let mag = core::integer::u32_try_from_felt252(utils::felt_abs(val)).unwrap();
FixedTrait::new(mag, utils::felt_sign(val))
}
fn abs(self: FP8x23) -> FP8x23 {
core_math::abs(self)
}
fn acos(self: FP8x23) -> FP8x23 {
trig::acos_fast(self)
}
fn acos_fast(self: FP8x23) -> FP8x23 {
trig::acos_fast(self)
}
fn acosh(self: FP8x23) -> FP8x23 {
hyp::acosh(self)
}
fn asin(self: FP8x23) -> FP8x23 {
trig::asin_fast(self)
}
fn asin_fast(self: FP8x23) -> FP8x23 {
trig::asin_fast(self)
}
fn asinh(self: FP8x23) -> FP8x23 {
hyp::asinh(self)
}
fn atan(self: FP8x23) -> FP8x23 {
trig::atan_fast(self)
}
fn atan_fast(self: FP8x23) -> FP8x23 {
trig::atan_fast(self)
}
fn atanh(self: FP8x23) -> FP8x23 {
hyp::atanh(self)
}
fn ceil(self: FP8x23) -> FP8x23 {
core_math::ceil(self)
}
fn cos(self: FP8x23) -> FP8x23 {
trig::cos_fast(self)
}
fn cos_fast(self: FP8x23) -> FP8x23 {
trig::cos_fast(self)
}
fn cosh(self: FP8x23) -> FP8x23 {
hyp::cosh(self)
}
fn floor(self: FP8x23) -> FP8x23 {
core_math::floor(self)
}
fn exp(self: FP8x23) -> FP |
8x23 {
core_math::exp(self)
}
fn exp2(self: FP8x23) -> FP8x23 {
core_math::exp2(self)
}
fn ln(self: FP8x23) -> FP8x23 {
core_math::ln(self)
}
fn log2(self: FP8x23) -> FP8x23 {
core_math::log2(self)
}
fn log10(self: FP8x23) -> FP8x23 {
core_math::log10(self)
}
fn pow(self: FP8x23, b: FP8x23) -> FP8x23 {
core_math::pow(self, b)
}
fn round(self: FP8x23) -> FP8x23 {
core_math::round(self)
}
fn sin(self: FP8x23) -> FP8x23 {
trig::sin_fast(self)
}
fn sin_fast(self: FP8x23) -> FP8x23 {
trig::sin_fast(self)
}
fn sinh(self: FP8x23) -> FP8x23 {
hyp::sinh(self)
}
fn sqrt(self: FP8x23) -> FP8x23 {
core_math::sqrt(self)
}
fn tan(self: FP8x23) -> FP8x23 {
trig::tan_fast(self)
}
fn tan_fast(self: FP8x23) -> FP8x23 {
trig::tan_fast(self)
}
fn tanh(self: FP8x23) -> FP8x23 {
hyp::tanh(self)
}
fn sign(self: FP8x23) -> FP8x23 {
core_math::sign(self)
}
fn NaN() -> FP8x23 {
FP8x23 { mag: 0, sign: true }
}
fn is_nan(self: FP8x23) -> bool {
self == FP8x23 { mag: 0, sign: true }
}
fn INF() -> FP8x23 {
FP8x23 { mag: 4294967295, sign: false }
}
fn POS_INF() -> FP8x23 {
FP8x23 { mag: 4294967295, sign: false }
}
fn NEG_INF() -> FP8x23 {
FP8x23 { mag: 4294967295, sign: true }
}
fn is_inf(self: FP8x23) -> bool {
self.mag == 4294967295
}
fn is_pos_inf(self: FP8x23) -> bool {
self.is_inf() && !self.sign
}
fn is_neg_inf(self: FP8x23) -> bool {
self.is_inf() && self.sign
}
fn erf(self: FP8x23) -> FP8x23 {
erf::erf(self)
}
}
impl FP8x23Print of PrintTrait<FP8x23> { |
fn print(self: FP8x23) {
self.sign.print();
self.mag.print();
}
}
impl FP8x23IntoFelt252 of Into<FP8x23, felt252> {
fn into(self: FP8x23) -> felt252 {
let mag_felt = self.mag.into();
if self.sign {
mag_felt * -1
} else {
mag_felt * 1
}
}
}
impl FP8x23TryIntoU128 of TryInto<FP8x23, u128> {
fn try_into(self: FP8x23) -> Option<u128> {
if self.sign {
Option::None(())
} else {
Option::Some((self.mag / ONE).into())
}
}
}
impl FP8x23TryIntoU64 of TryInto<FP8x23, u64> {
fn try_into(self: FP8x23) -> Option<u64> {
if self.sign {
Option::None(())
} else {
Option::Some((self.mag / ONE).into())
}
}
}
impl FP8x23TryIntoU32 of TryInto<FP8x23, u32> {
fn try_into(self: FP8x23) -> Option<u32> {
if self.sign {
Option::None(())
} else {
Option::Some(self.mag / ONE)
}
}
}
impl FP8x23TryIntoU16 of TryInto<FP8x23, u16> {
fn try_into(self: FP8x23) -> Option<u16> {
if self.sign {
Option::None(())
} else {
(self.mag / ONE).try_into()
}
}
}
impl FP8x23TryIntoU8 of TryInto<FP8x23, u8> {
fn try_into(self: FP8x23) -> Option<u8> {
if self.sign {
Option::None(())
} else {
(self.mag / ONE).try_into()
}
}
}
impl FP8x23IntoI32 of Into<FP8x23, i32> {
fn into(self: FP8x23) -> i32 {
_i32_into_fp(self)
}
}
impl FP8x23TryIntoI8 of TryInto<FP8x23, i8> {
fn try_into(self: FP8x23) -> Option<i8> {
_i8_try_from_fp(self)
}
}
impl FP8x23PartialEq of PartialEq<FP8x23> {
fn eq(lhs: @FP8x23, rhs: @FP8x23) -> bool {
core_math::eq(lhs, rhs)
}
fn ne(lhs: @FP8x23, rhs: @FP8x23) -> bool {
core_math::ne(lhs, rhs)
}
}
impl FP8x23Add of Add<FP8x23> {
fn add(lh |
s: FP8x23, rhs: FP8x23) -> FP8x23 {
core_math::add(lhs, rhs)
}
}
impl FP8x23AddEq of AddEq<FP8x23> { |
fn add_eq(ref self: FP8x23, other: FP8x23) {
self = Add::add(self, other);
}
}
impl FP8x23Sub of Sub<FP8x23> {
fn sub(lhs: FP8x23, rhs: FP8x23) -> FP8x23 {
core_math::sub(lhs, rhs)
}
}
impl FP8x23SubEq of SubEq<FP8x23> { |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.