Is RDTSC pure?
IchorDev
zxinsworld at gmail.com
Fri Sep 12 09:48:49 UTC 2025
Recently I wanted to write a pure function that returns an
unpredictable number, so I decided to use RDTSC (and any
equivalent instruction for other CPU architectures) to do this,
since the compiler allows RDTSC to be marked as `pure`.
However, in the end I discarded this idea because I figured that
a `pure` function should never return a different value with the
same input; and doing so would surely break any applicable
memoisation. Inline assembly isn't checked by the compiler, so I
was essentially doing the same thing as misusing `@trusted`…
Or so I thought. Today I remembered that `pureMalloc` exists,
which surely doesn't follow these rules and would definitely not
work when memoised. So how come it's still allowed to be `pure`
just by resetting ERRNO? It can return a different value with the
same input, so does that mean that using RDTSC is also `pure`?
More information about the Digitalmars-d-learn
mailing list