Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Firefox/third_party/rust/jxl/src/util/   (Firefox Browser Version 153.0.1©)  Datei vom 27.6.2026 mit Größe 2 kB image not shown  

Quelle  log2.rs

  Sprache: Rust
 

// 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.

pub trait FloorLog2 {
    fn floor_log2(&self) -> Self;
}

pub trait CeilLog2 {
    fn ceil_log2(&self) -> Self;
}

impl FloorLog2 for u32 {
    fn floor_log2(&self) -> Self {
        debug_assert_ne!(*self0);
        0u32.leading_zeros() - self.leading_zeros() - 1
    }
}

impl FloorLog2 for u64 {
    fn floor_log2(&self) -> Self {
        debug_assert_ne!(*self0);
        (0u64.leading_zeros() - self.leading_zeros() - 1as u64
    }
}

impl FloorLog2 for usize {
    fn floor_log2(&self) -> Self {
        debug_assert_ne!(*self0);
        (0usize.leading_zeros() - self.leading_zeros() - 1as usize
    }
}

impl<T> CeilLog2 for T
where
    T: FloorLog2,
    T: std::ops::Add<Output = Self>,
    T: std::ops::Sub<Output = Self>,
    T: std::ops::BitAnd<Output = Self>,
    T: std::cmp::PartialEq,
    T: From<u8>,
    T: Copy,
{
    fn ceil_log2(&self) -> Self {
        if (*self & (*self - 1.into())) != 0.into() {
            self.floor_log2() + 1.into()
        } else {
            self.floor_log2()
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn test_floor() {
        assert_eq!(01u32.floor_log2());
        assert_eq!(12u32.floor_log2());
        assert_eq!(13u32.floor_log2());
        assert_eq!(24u32.floor_log2());
    }
    #[test]
    fn test_ceil() {
        assert_eq!(01u32.ceil_log2());
        assert_eq!(12u32.ceil_log2());
        assert_eq!(23u32.ceil_log2());
        assert_eq!(24u32.ceil_log2());
    }
    #[test]
    fn test_floor_us() {
        assert_eq!(01usize.floor_log2());
        assert_eq!(12usize.floor_log2());
        assert_eq!(13usize.floor_log2());
        assert_eq!(24usize.floor_log2());
    }
    #[test]
    fn test_ceil_us() {
        assert_eq!(01usize.ceil_log2());
        assert_eq!(12usize.ceil_log2());
        assert_eq!(23usize.ceil_log2());
        assert_eq!(24usize.ceil_log2());
    }
}

Messung V0.5 in Prozent
C=87 H=94 G=90

¤ Dauer der Verarbeitung: 0.11 Sekunden  (vorverarbeitet am  2026-08-25) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.