| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | pub const MAX_16: i16 = 0x7fff; |
| 7 | pub const MIN_16: i16 = -0x8000; |
| 8 | pub const MAX_32: i32 = 0x7fff_ffff; |
| 9 | pub const MIN_32: i32 = i32::MIN; |
| 10 | |
| 11 | use super::tables::{INV_SQRT_TBL, LOG2_TBL, POW2_TBL, SQRT_L_TBL}; |
| 12 | |
| 13 | |
| 14 | |
| 15 | #[inline] |
| 16 | pub fn add(a: i16, b: i16) -> i16 { |
| 17 | (a as i32 + b as i32).clamp(-32768, 32767) as i16 |
| 18 | } |
| 19 | #[inline] |
| 20 | pub fn sub(a: i16, b: i16) -> i16 { |
| 21 | (a as i32 - b as i32).clamp(-32768, 32767) as i16 |
| 22 | } |
| 23 | |
| 24 | #[inline] |
| 25 | pub fn abs_s(a: i16) -> i16 { |
| 26 | if a == MIN_16 { |
| 27 | MAX_16 |
| 28 | } else { |
| 29 | a.abs() |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | #[inline] |
| 34 | pub fn negate(a: i16) -> i16 { |
| 35 | if a == MIN_16 { |
| 36 | MAX_16 |
| 37 | } else { |
| 38 | -a |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | #[inline] |
| 43 | pub fn mult(a: i16, b: i16) -> i16 { |
| 44 | let p = (a as i32 * b as i32) >> 15; |
| 45 | if p > 0x7fff { |
| 46 | MAX_16 |
| 47 | } else { |
| 48 | p as i16 |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | |
| 53 | #[inline] |
| 54 | pub fn shl_o(var1: i16, var2: i16, ovf: &mut bool) -> i16 { |
| 55 | if var2 < 0 { |
| 56 | let n = -(var2 as i32); |
| 57 | if n < 15 { |
| 58 | var1 >> n |
| 59 | } else { |
| 60 | 0 |
| 61 | } |
| 62 | } else { |
| 63 | let n = var2 as u32; |
| 64 | let out: i16 = if n >= 32 { 0 } else { ((var1 as i32) << n) as i16 }; |
| 65 | let back = if n >= 32 { 0 } else { (out as i32) >> n }; |
| 66 | |
| 67 | let chk = if n >= 16 { if out < 0 { -1 } else { 0 } } else { back }; |
| 68 | if chk != var1 as i32 { |
| 69 | *ovf = true; |
| 70 | (var1 >> 15) ^ MAX_16 |
| 71 | } else { |
| 72 | out |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | #[inline] |
| 77 | pub fn shl(var1: i16, var2: i16) -> i16 { |
| 78 | shl_o(var1, var2, &mut false) |
| 79 | } |
| 80 | |
| 81 | #[inline] |
| 82 | pub fn shr_o(var1: i16, var2: i16, ovf: &mut bool) -> i16 { |
| 83 | if var2 == 0 { |
| 84 | var1 |
| 85 | } else if var2 > 0 { |
| 86 | var1 >> (var2.min(15) as u32) |
| 87 | } else { |
| 88 | let n = ((-(var2 as i32)).min(15)) as u32; |
| 89 | let result = ((var1 as i32) << n) as i16; |
| 90 | if ((result as i32) >> n) != var1 as i32 { |
| 91 | *ovf = true; |
| 92 | if var1 > 0 { MAX_16 } else { MIN_16 } |
| 93 | } else { |
| 94 | result |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | #[inline] |
| 99 | pub fn shr(var1: i16, var2: i16) -> i16 { |
| 100 | shr_o(var1, var2, &mut false) |
| 101 | } |
| 102 | |
| 103 | #[inline] |
| 104 | pub fn shr_r(var1: i16, var2: i16) -> i16 { |
| 105 | if var2 > 15 { |
| 106 | 0 |
| 107 | } else { |
| 108 | let mut out = shr(var1, var2); |
| 109 | if var2 > 0 && (var1 as i32 & (1 << (var2 - 1))) != 0 { |
| 110 | out = out.wrapping_add(1); |
| 111 | } |
| 112 | out |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | |
| 117 | |
| 118 | #[inline] |
| 119 | pub fn l_add_o(a: i32, b: i32, ovf: &mut bool) -> i32 { |
| 120 | match a.checked_add(b) { |
| 121 | Some(v) => v, |
| 122 | None => { |
| 123 | *ovf = true; |
| 124 | if a < 0 { MIN_32 } else { MAX_32 } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | #[inline] |
| 129 | pub fn l_add(a: i32, b: i32) -> i32 { |
| 130 | a.saturating_add(b) |
| 131 | } |
| 132 | #[inline] |
| 133 | pub fn l_sub(a: i32, b: i32) -> i32 { |
| 134 | a.saturating_sub(b) |
| 135 | } |
| 136 | |
| 137 | #[inline] |
| 138 | pub fn l_mult(a: i16, b: i16) -> i32 { |
| 139 | let p = a as i32 * b as i32; |
| 140 | if p != 0x4000_0000 { |
| 141 | p << 1 |
| 142 | } else { |
| 143 | MAX_32 |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | #[inline] |
| 148 | pub fn l_mac_o(acc: i32, a: i16, b: i16, ovf: &mut bool) -> i32 { |
| 149 | let p = a as i32 * b as i32; |
| 150 | if p != 0x4000_0000 { |
| 151 | l_add_o(acc, p << 1, ovf) |
| 152 | } else { |
| 153 | *ovf = true; |
| 154 | MAX_32 |
| 155 | } |
| 156 | } |
| 157 | #[inline] |
| 158 | pub fn l_mac(acc: i32, a: i16, b: i16) -> i32 { |
| 159 | l_mac_o(acc, a, b, &mut false) |
| 160 | } |
| 161 | #[inline] |
| 162 | pub fn l_msu(acc: i32, a: i16, b: i16) -> i32 { |
| 163 | l_sub(acc, l_mult(a, b)) |
| 164 | } |
| 165 | |
| 166 | #[inline] |
| 167 | pub fn l_shl_o(x: i32, n: i16, ovf: &mut bool) -> i32 { |
| 168 | if n > 0 { |
| 169 | let n = n as u32; |
| 170 | let ok = if n >= 32 { x == 0 } else { ((x << n) >> n) == x }; |
| 171 | if ok { |
| 172 | if n >= 32 { 0 } else { x << n } |
| 173 | } else { |
| 174 | *ovf = true; |
| 175 | (x >> 31) ^ MAX_32 |
| 176 | } |
| 177 | } else { |
| 178 | let m = -(n as i32); |
| 179 | if m < 31 { x >> m } else { 0 } |
| 180 | } |
| 181 | } |
| 182 | #[inline] |
| 183 | pub fn l_shl(x: i32, n: i16) -> i32 { |
| 184 | l_shl_o(x, n, &mut false) |
| 185 | } |
| 186 | |
| 187 | #[inline] |
| 188 | pub fn l_shr_o(x: i32, n: i16, ovf: &mut bool) -> i32 { |
| 189 | if n > 0 { |
| 190 | if n < 31 { x >> n } else { 0 } |
| 191 | } else { |
| 192 | let m = -(n as i32); |
| 193 | let ok = if m >= 32 { x == 0 } else { ((x << m) >> m) == x }; |
| 194 | if ok { |
| 195 | if m >= 32 { 0 } else { x << m } |
| 196 | } else { |
| 197 | *ovf = true; |
| 198 | (x >> 31) ^ MAX_32 |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | #[inline] |
| 203 | pub fn l_shr(x: i32, n: i16) -> i32 { |
| 204 | l_shr_o(x, n, &mut false) |
| 205 | } |
| 206 | |
| 207 | #[inline] |
| 208 | pub fn l_shr_r(x: i32, n: i16) -> i32 { |
| 209 | if n > 31 { |
| 210 | 0 |
| 211 | } else { |
| 212 | let mut r = l_shr(x, n); |
| 213 | if n > 0 && (x & (1i32 << (n - 1))) != 0 { |
| 214 | r = r.wrapping_add(1); |
| 215 | } |
| 216 | r |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | #[inline] |
| 221 | pub fn round_(x: i32) -> i16 { |
| 222 | (l_add(x, 0x8000) >> 16) as i16 |
| 223 | } |
| 224 | |
| 225 | |
| 226 | #[inline] |
| 227 | pub fn norm_s(x: i16) -> i16 { |
| 228 | if x == 0 { |
| 229 | 0 |
| 230 | } else { |
| 231 | |
| 232 | let y = x.wrapping_sub((x < 0) as i16); |
| 233 | let v = y ^ (y >> 15); |
| 234 | (v.leading_zeros() as i16) - 1 |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | #[inline] |
| 239 | pub fn norm_l(x: i32) -> i16 { |
| 240 | if x == 0 { |
| 241 | 0 |
| 242 | } else { |
| 243 | let y = x.wrapping_sub((x < 0) as i32); |
| 244 | let v = y ^ (y >> 31); |
| 245 | (v.leading_zeros() as i16) - 1 |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | |
| 250 | #[inline] |
| 251 | pub fn div_s(var1: i16, var2: i16) -> i16 { |
| 252 | if var1 > var2 || var1 < 0 { |
| 253 | return 0; |
| 254 | } |
| 255 | if var1 == 0 { |
| 256 | return 0; |
| 257 | } |
| 258 | if var1 == var2 { |
| 259 | return MAX_16; |
| 260 | } |
| 261 | let mut out: i16 = 0; |
| 262 | let mut num = var1 as i32; |
| 263 | let den = var2 as i32; |
| 264 | for _ in 0..5 { |
| 265 | out <<= 3; |
| 266 | num <<= 3; |
| 267 | if num >= den << 2 { |
| 268 | num -= den << 2; |
| 269 | out |= 4; |
| 270 | } |
| 271 | if num >= den << 1 { |
| 272 | num -= den << 1; |
| 273 | out |= 2; |
| 274 | } |
| 275 | if num >= den { |
| 276 | num -= den; |
| 277 | out |= 1; |
| 278 | } |
| 279 | } |
| 280 | out |
| 281 | } |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | #[inline] |
| 287 | pub fn mpy_32_16(hi: i16, lo: i16, v: i16) -> i32 { |
| 288 | let mut p = hi as i32 * v as i32; |
| 289 | p = if p != 0x4000_0000 { p << 1 } else { MAX_32 }; |
| 290 | let r = (lo as i32 * v as i32) >> 15; |
| 291 | let s = p.wrapping_add(r << 1); |
| 292 | if (p ^ r) > 0 && ((s ^ p) >> 31) != 0 { |
| 293 | return if (p >> 31) != 0 { MIN_32 } else { MAX_32 }; |
| 294 | } |
| 295 | s |
| 296 | } |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | #[inline] |
| 302 | pub fn pow2(exponent: i16, fraction: i16) -> i32 { |
| 303 | let lx = l_mult(fraction, 32); |
| 304 | let i = (((lx >> 16) as i16) & 31) as usize; |
| 305 | let a = ((lx >> 1) & 0x7fff) as i16; |
| 306 | let mut x = (POW2_TBL[i] as i32) << 16; |
| 307 | let tmp = POW2_TBL[i].wrapping_sub(POW2_TBL[i + 1]); |
| 308 | x = l_msu(x, tmp, a); |
| 309 | let exp = 30i16.wrapping_sub(exponent); |
| 310 | l_shr_r(x, exp) |
| 311 | } |
| 312 | |
| 313 | #[inline] |
| 314 | pub fn log2_norm(x: i32, exp: i16, exponent: &mut i16, fraction: &mut i16) { |
| 315 | if x <= 0 { |
| 316 | *exponent = 0; |
| 317 | *fraction = 0; |
| 318 | } else { |
| 319 | *exponent = 30i16.wrapping_sub(exp); |
| 320 | let x = x >> 10; |
| 321 | let i = ((x >> 15) as i16).wrapping_sub(32) as usize; |
| 322 | let a = x & 0x7fff; |
| 323 | let mut y = (LOG2_TBL[i] as i32) << 16; |
| 324 | let tmp = LOG2_TBL[i].wrapping_sub(LOG2_TBL[i + 1]) as i32; |
| 325 | y = y.wrapping_sub((tmp * a) << 1); |
| 326 | *fraction = (y >> 16) as i16; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | #[inline] |
| 331 | pub fn log2(l_x: i32, exponent: &mut i16, fraction: &mut i16) { |
| 332 | let exp = norm_l(l_x); |
| 333 | let r = l_x.wrapping_shl(exp as u32); |
| 334 | log2_norm(r, exp, exponent, fraction); |
| 335 | } |
| 336 | |
| 337 | #[inline] |
| 338 | pub fn inv_sqrt(l_x: i32) -> i32 { |
| 339 | if l_x <= 0 { |
| 340 | return 0x3fff_ffff; |
| 341 | } |
| 342 | let mut exp = norm_l(l_x); |
| 343 | let mut x = l_x << exp; |
| 344 | exp = 30 - exp; |
| 345 | if (exp & 1) == 0 { |
| 346 | x >>= 1; |
| 347 | } |
| 348 | exp >>= 1; |
| 349 | exp += 1; |
| 350 | x >>= 9; |
| 351 | let i = ((x >> 16) as i16 - 16) as usize; |
| 352 | let a = ((x >> 1) as i16 & 0x7fff) as i32; |
| 353 | let mut y = (INV_SQRT_TBL[i] as i32) << 16; |
| 354 | let tmp = (INV_SQRT_TBL[i] - INV_SQRT_TBL[i + 1]) as i32; |
| 355 | y = y.wrapping_sub((tmp * a) << 1); |
| 356 | y >> exp |
| 357 | } |
| 358 | |
| 359 | #[inline] |
| 360 | pub fn sqrt_l_exp(l_x: i32, exp: &mut i16) -> i32 { |
| 361 | if l_x <= 0 { |
| 362 | *exp = 0; |
| 363 | return 0; |
| 364 | } |
| 365 | let e = norm_l(l_x) & !1; |
| 366 | let x = l_shl(l_x, e); |
| 367 | *exp = e; |
| 368 | let x = x >> 10; |
| 369 | let mut i = ((x >> 15) & 63) as usize; |
| 370 | let a = x as i16 & 0x7fff; |
| 371 | if i > 15 { |
| 372 | i -= 16; |
| 373 | } |
| 374 | let y = (SQRT_L_TBL[i] as i32) << 16; |
| 375 | let tmp = SQRT_L_TBL[i].wrapping_sub(SQRT_L_TBL[i + 1]); |
| 376 | l_msu(y, tmp, a) |
| 377 | } |
| 378 | |
| 379 | #[inline] |
| 380 | pub fn gmed_n(ind: &[i16], n: usize) -> i16 { |
| 381 | let mut tmp2 = [0i16; 9]; |
| 382 | let mut tmp = [0usize; 9]; |
| 383 | tmp2[..n].copy_from_slice(&ind[..n]); |
| 384 | let mut ix = 0usize; |
| 385 | for i in 0..n { |
| 386 | let mut max = -32767i16; |
| 387 | for j in 0..n { |
| 388 | if tmp2[j] >= max { |
| 389 | max = tmp2[j]; |
| 390 | ix = j; |
| 391 | } |
| 392 | } |
| 393 | tmp2[ix] = -32768; |
| 394 | tmp[i] = ix; |
| 395 | } |
| 396 | ind[tmp[n >> 1]] |
| 397 | } |
| 398 | |
| 399 | #[cfg(test)] |
| 400 | mod tests { |
| 401 | use super::*; |
| 402 | |
| 403 | #[test] |
| 404 | fn saturation() { |
| 405 | assert_eq!(add(32767, 1), 32767); |
| 406 | assert_eq!(sub(-32768, 1), -32768); |
| 407 | assert_eq!(abs_s(-32768), 32767); |
| 408 | assert_eq!(negate(-32768), 32767); |
| 409 | assert_eq!(l_add(i32::MAX, 1), i32::MAX); |
| 410 | assert_eq!(l_sub(i32::MIN, 1), i32::MIN); |
| 411 | assert_eq!(l_mult(-32768, -32768), i32::MAX); |
| 412 | assert_eq!(l_mac(5, -32768, -32768), i32::MAX); |
| 413 | assert_eq!(mult(-32768, -32768), 32767); |
| 414 | assert_eq!(shl(0x4000, 1), 32767); |
| 415 | assert_eq!(shl(-0x4001, 1), -32768); |
| 416 | assert_eq!(shl(3, 2), 12); |
| 417 | assert_eq!(shl(-8, -2), -2); |
| 418 | assert_eq!(shr(1, -15), 32767); |
| 419 | assert_eq!(shr(-5, 1), -3); |
| 420 | assert_eq!(shr_r(5, 1), 3); |
| 421 | assert_eq!(l_shl(0x4000_0000, 1), i32::MAX); |
| 422 | assert_eq!(l_shl(-0x4000_0001, 1), i32::MIN); |
| 423 | assert_eq!(l_shr_r(5, 1), 3); |
| 424 | assert_eq!(l_shr(1, -31), i32::MAX); |
| 425 | } |
| 426 | |
| 427 | #[test] |
| 428 | fn norms_and_math() { |
| 429 | assert_eq!(norm_s(0), 0); |
| 430 | assert_eq!(norm_s(1), 14); |
| 431 | assert_eq!(norm_s(-1), 14); |
| 432 | assert_eq!(norm_s(-32768), 0); |
| 433 | assert_eq!(norm_l(1), 30); |
| 434 | assert_eq!(norm_l(-1), 30); |
| 435 | assert_eq!(norm_l(i32::MIN), 0); |
| 436 | assert_eq!(div_s(1, 2), 16384); |
| 437 | assert_eq!(div_s(5, 5), 32767); |
| 438 | assert_eq!(round_(0x7fff_ffff), 32767); |
| 439 | assert_eq!(pow2(14, 0), 16384); |
| 440 | assert_eq!(inv_sqrt(0), 0x3fff_ffff); |
| 441 | let (mut e, mut f) = (0, 0); |
| 442 | log2(1 << 20, &mut e, &mut f); |
| 443 | assert_eq!((e, f), (20, 0)); |
| 444 | assert_eq!(gmed_n(&[5, 1, 9, 3, 7], 5), 5); |
| 445 | } |
| 446 | } |