Re: [PATCH RFC v3 6/7] gpu: nova-core: add basic timer device
From: Alexandre Courbot
Date: Thu Mar 20 2025 - 23:10:22 EST
On Fri Mar 21, 2025 at 12:54 AM JST, Daniel Brooks wrote:
> Alexandre Courbot <acourbot@xxxxxxxxxx> writes:
>
>> +impl Add<Duration> for Timestamp {
>> + type Output = Self;
>> +
>> + fn add(mut self, rhs: Duration) -> Self::Output {
>> + let mut nanos = rhs.as_nanos();
>> + while nanos > u64::MAX as u128 {
>> + self.0 = self.0.wrapping_add(nanos as u64);
>> + nanos -= u64::MAX as u128;
>> + }
>> +
>> + Timestamp(self.0.wrapping_add(nanos as u64))
>> + }
>> +}
>
> Perhaps I missed something, but couldn’t you simplify this method like
> this:
>
> fn add(mut self, rhs: Duration) -> Self::Output {
> let stamp = self.0 as u128;
> Timestamp(stamp.wrapping_add(rhs.as_nanos()) as u64)
> }
You are absolutely right, this loop will just wrap self.0 to the same value
after the initial pass.
... or at least it would if there weren't two bugs in it. >_<
- It adds the lower part of nanos while substracting u64::MAX from rhs,
which is completely wrong;
- Substracting u64::MAX is also wrong, it should be u64::MAX as u128 +
1.
So thanks a lot for pointing this out!