// Copyright (c) the JPEG XL Project Authors. All rights reserved. // // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. use std::ops::{Add, Shl, Shr, Sub}; pub trait ShiftRightCeil: Copy { fn shrc(self, rhs: T) -> Self where Self: Shr + Shl; } impl + Sub + From> ShiftRightCeil for S { fn shrc(self, rhs: T) -> Self where Self: Shr + Shl, { (self + (Self::from(1u8) << rhs) - Self::from(1u8)) >> rhs } } #[cfg(test)] mod test { use crate::util::ShiftRightCeil; #[test] fn test_shrc() { assert_eq!(1u8, 1u8.shrc(1u8)); assert_eq!(1u8, 2u8.shrc(1u8)); assert_eq!(2u8, 9u8.shrc(3u8)); assert_eq!(1u32, 1u32.shrc(1u32)); assert_eq!(1u32, 2u32.shrc(1u32)); assert_eq!(2u32, 9u32.shrc(3u32)); assert_eq!(1u32, 1u32.shrc(1u8)); assert_eq!(1u32, 2u32.shrc(1u8)); assert_eq!(2u32, 9u32.shrc(3u8)); } }