Commit 184ea804 authored by Luca Barbato's avatar Luca Barbato Committed by Luca Barbato
Browse files

Add a generic ilog

parent d6ed0308
......@@ -20,6 +20,7 @@ rustyline = { version = "1", optional = true }
y4m = "0.3"
enum-iterator-derive = "0.1.1"
backtrace = "0.3"
num-traits = "0.2"
[build-dependencies]
cmake = "0.1.32"
......
......@@ -14,6 +14,7 @@
#![cfg_attr(feature = "cargo-clippy", allow(needless_range_loop))]
use bitstream_io::{BitWriter, BE};
use util::ILog;
use std;
pub const OD_BITRES: u8 = 3;
......@@ -147,7 +148,7 @@ impl WriterEncoder {
impl StorageBackend for WriterBase<WriterCounter> {
fn store(&mut self, fl: u16, fh: u16, nms: u16) {
let (_l, r) = self.lr_compute(fl, fh, nms);
let d = 16 - WriterBase::<Self>::ilog_nz(r);
let d = 16 - r.ilog();
let mut c = self.cnt;
let mut s = c + (d as i16);
......@@ -188,7 +189,7 @@ impl StorageBackend for WriterBase<WriterCounter> {
impl StorageBackend for WriterBase<WriterRecorder> {
fn store(&mut self, fl: u16, fh: u16, nms: u16) {
let (_l, r) = self.lr_compute(fl, fh, nms);
let d = 16 - WriterBase::<Self>::ilog_nz(r);
let d = 16 - r.ilog();
let mut c = self.cnt;
let mut s = c + (d as i16);
......@@ -233,7 +234,7 @@ impl StorageBackend for WriterBase<WriterEncoder> {
let (l, r) = self.lr_compute(fl, fh, nms);
let mut low = l + self.s.low;
let mut c = self.cnt;
let d = 16 - WriterBase::<Self>::ilog_nz(r);
let d = 16 - r.ilog();
let mut s = c + (d as i16);
if s >= 0 {
......@@ -337,10 +338,6 @@ impl<S> WriterBase<S>{
}
nbits - l
}
/// Simple calculation of position of leading 1 bit.
fn ilog_nz(x: u16) -> u16 {
16 - (x.leading_zeros() as u16)
}
/// Function to update the CDF for Writer calls that do so.
fn update_cdf(cdf: &mut [u16], val: u32) {
let nsymbs = cdf.len() - 1;
......
......@@ -21,6 +21,8 @@ extern crate y4m;
#[macro_use]
extern crate enum_iterator_derive;
extern crate num_traits;
use std::fs::File;
use std::io::prelude::*;
use bitstream_io::{BE, LE, BitWriter};
......
......@@ -89,3 +89,14 @@ pub fn clamp<T: PartialOrd>(input: T, min: T, max: T) -> T {
return input;
}
}
use num_traits::PrimInt;
use std::mem::size_of;
pub trait ILog : PrimInt {
fn ilog(self) -> Self {
Self::from(size_of::<Self>() * 8 - self.leading_zeros() as usize).unwrap()
}
}
impl<T> ILog for T where T: PrimInt {}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment