korken89 / fugit

`fugit` provides a comprehensive library of `Duration` and `Instant` for the handling of time in embedded systems, doing all it can at compile time.
Apache License 2.0
56 stars 16 forks source link

Support picoseconds #21

Open jordens opened 2 years ago

jordens commented 2 years ago

As a general purpose time library, it would be nice to also support picosecond timebases. These arise frequently in time-to-digital conversion (TDC, whiterabbit, ToF, OTDR) or other high accuracy measurement (time/frequency metrology). The stuff in helpers.rs would need a u64/u128 versions.

korken89 commented 2 years ago

Hmm, I think it should be possible as long as the GCD conversion constants being made are within u32::MAX in the end. It should be possible to check this with const panic now to also give more sane errors.

adamgreig commented 2 years ago

I think the main problem will be right now the NOM and DENOM are u32 const generics, but you can't fit 1e12 in a u32, so DENOM would be out of range. There's no way to even create an Instant or Duration that ticks in picoseconds; the smallest possible tick with u32 NOM/DENOM is 233ps.

I'm not sure if you end up incurring any runtime cost in moving those constants to be u64 for everyone; if so it might be a steep price to pay. Maybe there could be a specialised PicosInstant/PicosDuration that isn't awn alias of Duration but can interwork with them, with some carefully checked conversions to avoid overflow.