Re: [PATCH v1] rust: time: Avoid 64-bit integer division
From: FUJITA Tomonori
Date: Thu May 01 2025 - 09:38:34 EST
On Thu, 1 May 2025 06:25:04 -0700
Boqun Feng <boqun.feng@xxxxxxxxx> wrote:
> On Thu, May 01, 2025 at 03:22:00PM +0200, Miguel Ojeda wrote:
>> On Thu, May 1, 2025 at 3:12 PM Boqun Feng <boqun.feng@xxxxxxxxx> wrote:
>> >
>> > #[cfg(CONFIG_ARM)]
>> > fn ns_to_ms(ns: i64) -> i64 {
>> >
>> > #[cfg(not(CONFIG_ARM))]
>> > fn ns_to_ms(ns: i64) -> i64 {
>>
>> I think `cfg`s may be better inside, i.e. as local as reasonably
>> possible, so that we share e.g. signature as well as any attributes
>> and docs.
>>
>
> Fair enough.
I'll go with the following.
#[inline]
pub fn as_millis(self) -> i64 {
#[cfg(CONFIG_ARM)]
// SAFETY: It is always safe to call `ktime_to_ms()` with any value.
unsafe {
bindings::ktime_to_ms(self.as_nanos())
}
#[cfg(not(CONFIG_ARM))]
{
self.as_nanos() / NSEC_PER_MSEC
}
}