Thread overview
Is RDTSC pure?
20 hours ago
IchorDev
15 hours ago
Kagamin
14 hours ago
IchorDev
20 hours ago

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?

15 hours ago

malloc is weakly pure, because it returns mutable pointer. The difference is when you call it from strongly pure function, then it doesn't matter, how many times malloc was called.

14 hours ago

On Friday, 12 September 2025 at 14:48:43 UTC, Kagamin wrote:

>

malloc is weakly pure, because it returns mutable pointer. The difference is when you call it from strongly pure function, then it doesn't matter, how many times malloc was called.

And if the compiler tries to memoise it?