------------------------------------------------------------------------ -- The Agda standard library -- -- Logarithm base 2 core definitions and properties ------------------------------------------------------------------------ {-# OPTIONS --cubical-compatible --safe #-} module Data.Nat.Logarithm.Core where open import Data.Nat.Base using (ℕ; _<_; zero; suc; _+_; ⌊_/2⌋; ⌈_/2⌉ ; _≤_; z≤n; s≤s; _∸_; NonZero; _*_; _^_) open import Data.Nat.Properties open import Data.Nat.Induction using (<-wellFounded) open import Induction.WellFounded using (Acc; acc) open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; cong; sym) open import Relation.Binary.PropositionalEquality.Properties using (module ≡-Reasoning) ------------------------------------------------------------------------ -- Logarithm base 2 -- Floor version ⌊log2⌋ : ∀ n → Acc _<_ n → ℕ ⌊log2⌋ 0 _ = 0 ⌊log2⌋ 1 _ = 0 ⌊log2⌋ (suc n′@(suc n)) (acc rs) = 1 + ⌊log2⌋ (suc ⌊ n /2⌋) (rs (⌊n/2⌋