Skip to content

Safe reading of CPUID#1277

Open
AntoinePrv wants to merge 9 commits intoxtensor-stack:masterfrom
AntoinePrv:cpuid-safe
Open

Safe reading of CPUID#1277
AntoinePrv wants to merge 9 commits intoxtensor-stack:masterfrom
AntoinePrv:cpuid-safe

Conversation

@AntoinePrv
Copy link
Contributor

@AntoinePrv AntoinePrv commented Mar 23, 2026

Not implemented previously in xsimd, CPUID requires to first check which leaves are available before calling it, otherwise the value returned may be invalid.

  • Before reading leaf < 0x80000000, check leaf 0 EAX
  • Before reading leaf > 0x80000000, check leaf 0x80000000 EAX
  • Before reading leaf 7 subleaf 1, check leaf 7 EAX

@AntoinePrv AntoinePrv force-pushed the cpuid-safe branch 3 times, most recently from ba6d557 to 25a1cf8 Compare March 24, 2026 12:17
@AntoinePrv
Copy link
Contributor Author

@serge-sans-paille this one is ready too

}

template <status status_id, typename L>
inline auto const& safe_get_leaf(L& leaf) const
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm puzzled by the fact that this functions takes a non-const ref but returns a const-ref on the same storage. leaf has an integral type, so maybe pass everything by value?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And rename leaf has default_value or something

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm puzzled by the fact that this functions takes a non-const ref but returns a const-ref on the same storage.

Well that where the caching happens. This function is a utility to factor code internally.
The input must be a non-cons reference. It is passed the class member, that may or may not be initialized and returns something that should not modify the object itself.
It need to be given the class member leaf because the point it to factor the code between different leaves, so we need to know which one and its type.

leaf has an integral type, so maybe pass everything by value?

Leaf is 4 32 bit integers. Not sure if better to return by value or const ref, but probably does not make a difference after the compiler has done its pass.

And rename leaf has default_value or something

It's not really the default value though, it's the "storage" that we write to if it was uninitialized (in the C++ sens everything is initialized, just not with the value we care about).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants