ethnum/intrinsics/native/
shr.rs

1//! Module containing logical right shift intrinsic.
2
3use crate::{int::I256, uint::U256};
4use core::mem::MaybeUninit;
5
6#[inline]
7pub fn sar2(r: &mut I256, a: u32) {
8    debug_assert!(a < 256, "shr intrinsic called with overflowing shift");
9
10    let (hi, lo) = if a == 0 {
11        return;
12    } else if a < 128 {
13        (
14            r.high() >> a,
15            ((*r.low() as u128) >> a | ((*r.high() as u128) << (128 - a))) as i128,
16        )
17    } else {
18        (r.high() >> 127, (r.high() >> (a & 0x7f)))
19    };
20
21    *r = I256::from_words(hi, lo);
22}
23
24#[inline]
25pub fn sar3(r: &mut MaybeUninit<I256>, a: &I256, b: u32) {
26    debug_assert!(b < 256, "shr intrinsic called with overflowing shift");
27
28    let (hi, lo) = if b == 0 {
29        (*a.high(), *a.low())
30    } else if b < 128 {
31        (
32            a.high() >> b,
33            ((*a.low() as u128) >> b | ((*a.high() as u128) << (128 - b))) as i128,
34        )
35    } else {
36        (a.high() >> 127, a.high() >> (b & 0x7f))
37    };
38
39    r.write(I256::from_words(hi, lo));
40}
41
42#[inline]
43pub fn shr2(r: &mut U256, a: u32) {
44    debug_assert!(a < 256, "shr intrinsic called with overflowing shift");
45
46    let (hi, lo) = if a == 0 {
47        return;
48    } else if a < 128 {
49        (r.high() >> a, r.low() >> a | (r.high() << (128 - a)))
50    } else {
51        (0, r.high() >> (a & 0x7f))
52    };
53
54    *r = U256::from_words(hi, lo);
55}
56
57#[inline]
58pub fn shr3(r: &mut MaybeUninit<U256>, a: &U256, b: u32) {
59    debug_assert!(b < 256, "shr intrinsic called with overflowing shift");
60
61    let (hi, lo) = if b == 0 {
62        (*a.high(), *a.low())
63    } else if b < 128 {
64        (a.high() >> b, a.low() >> b | (a.high() << (128 - b)))
65    } else {
66        (0, a.high() >> (b & 0x7f))
67    };
68
69    r.write(U256::from_words(hi, lo));
70}