Skip to content

Commit

Permalink
Use logarithms to calculate number of digits
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Jul 17, 2023
1 parent 7774e2c commit d52798e
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 74 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
strategy:
matrix:
rust:
- { version: "1.65.0", name: MSRV }
- { version: "1.67.0", name: MSRV }
- { version: stable, name: stable }
kind:
- name: no_std
Expand Down Expand Up @@ -153,7 +153,7 @@ jobs:
strategy:
matrix:
rust:
- { version: "1.65.0", name: MSRV }
- { version: "1.67.0", name: MSRV }
- { version: stable, name: stable }
os:
- { name: Ubuntu, value: ubuntu-20.04 }
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/powerset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
strategy:
matrix:
rust:
- { version: "1.65.0", name: MSRV }
- { version: "1.67.0", name: MSRV }
- { version: stable, name: stable }
kind:
- name: no_std
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# time

[![minimum rustc: 1.65](https://img.shields.io/badge/minimum%20rustc-1.65-yellowgreen?logo=rust&style=flat-square)](https://www.whatrustisit.com)
[![minimum rustc: 1.67](https://img.shields.io/badge/minimum%20rustc-1.67-yellowgreen?logo=rust&style=flat-square)](https://www.whatrustisit.com)
[![version](https://img.shields.io/crates/v/time?color=blue&logo=rust&style=flat-square)](https://crates.io/crates/time)
[![build status](https://img.shields.io/github/actions/workflow/status/time-rs/time/build.yaml?branch=main&style=flat-square)](https://github.com/time-rs/time/actions)
[![codecov](https://codecov.io/gh/time-rs/time/branch/main/graph/badge.svg?token=yt4XSmQNKQ)](https://codecov.io/gh/time-rs/time)
Expand Down
2 changes: 1 addition & 1 deletion time-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "time-core"
version = "0.1.1"
authors = ["Jacob Pratt <[email protected]>", "Time contributors"]
edition = "2021"
rust-version = "1.65.0"
rust-version = "1.67.0"
repository = "https://github.com/time-rs/time"
keywords = ["date", "time", "calendar", "duration"]
categories = ["date-and-time"]
Expand Down
2 changes: 1 addition & 1 deletion time-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "time-macros"
version = "0.2.10"
authors = ["Jacob Pratt <[email protected]>", "Time contributors"]
edition = "2021"
rust-version = "1.65.0"
rust-version = "1.67.0"

This comment has been minimized.

Copy link
@nuttycom

nuttycom Aug 1, 2023

This MSRV bump should result in a minor version bump of time-macros; releasing the patch version 0.2.11 with the MSRV changes breaks the build for downstream users of this crate. Is there any chance of 0.2.11 being yanked and replaced with 0.3.0?

This comment has been minimized.

Copy link
@jhpratt

jhpratt Aug 2, 2023

Author Member

No. For one, time-macros is an implementation detail and should never be relied upon directly. Setting that aside, the change is in accordance with the MSRV policy of time.

repository = "https://github.com/time-rs/time"
keywords = ["date", "time", "calendar", "duration"]
categories = ["date-and-time"]
Expand Down
2 changes: 1 addition & 1 deletion time/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "time"
version = "0.3.23"
authors = ["Jacob Pratt <[email protected]>", "Time contributors"]
edition = "2021"
rust-version = "1.65.0"
rust-version = "1.67.0"
repository = "https://github.com/time-rs/time"
homepage = "https://time-rs.github.io"
keywords = ["date", "time", "calendar", "duration"]
Expand Down
81 changes: 14 additions & 67 deletions time/src/formatting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,75 +47,22 @@ pub(crate) trait DigitCount {
/// The number of digits in the stringified value.
fn num_digits(self) -> u8;
}
impl DigitCount for u8 {
fn num_digits(self) -> u8 {
// Using a lookup table as with u32 is *not* faster in standalone benchmarks.
if self < 10 {
1
} else if self < 100 {
2
} else {
3
}
}
}
impl DigitCount for u16 {
fn num_digits(self) -> u8 {
// Using a lookup table as with u32 is *not* faster in standalone benchmarks.
if self < 10 {
1
} else if self < 100 {
2
} else if self < 1_000 {
3
} else if self < 10_000 {
4
} else {
5
}
}
}

impl DigitCount for u32 {
fn num_digits(self) -> u8 {
/// Lookup table
const TABLE: &[u64] = &[
0x0001_0000_0000,
0x0001_0000_0000,
0x0001_0000_0000,
0x0001_FFFF_FFF6,
0x0002_0000_0000,
0x0002_0000_0000,
0x0002_FFFF_FF9C,
0x0003_0000_0000,
0x0003_0000_0000,
0x0003_FFFF_FC18,
0x0004_0000_0000,
0x0004_0000_0000,
0x0004_0000_0000,
0x0004_FFFF_D8F0,
0x0005_0000_0000,
0x0005_0000_0000,
0x0005_FFFE_7960,
0x0006_0000_0000,
0x0006_0000_0000,
0x0006_FFF0_BDC0,
0x0007_0000_0000,
0x0007_0000_0000,
0x0007_0000_0000,
0x0007_FF67_6980,
0x0008_0000_0000,
0x0008_0000_0000,
0x0008_FA0A_1F00,
0x0009_0000_0000,
0x0009_0000_0000,
0x0009_C465_3600,
0x000A_0000_0000,
0x000A_0000_0000,
];
((self as u64 + TABLE[31_u32.saturating_sub(self.leading_zeros()) as usize]) >> 32) as _
}
/// A macro to generate implementations of `DigitCount` for unsigned integers.
macro_rules! impl_digit_count {
($($t:ty),* $(,)?) => {
$(impl DigitCount for $t {
fn num_digits(self) -> u8 {
match self.checked_ilog10() {
Some(n) => (n as u8) + 1,
None => 1,
}
}
})*
};
}

impl_digit_count!(u8, u16, u32);
// endregion extension trait

/// Write all bytes to the output, returning the number of bytes written.
Expand Down

0 comments on commit d52798e

Please sign in to comment.