Page 1 of 1

intrinsic functions

Posted: Mon May 24, 2021 8:13 am
by newosdeveloper2021
any way to use intrinsic functions like in windows kernel? like __writecr0 and __cpuidex?
In my kernel?

EDIT: also can I use microsoft-style inline assembler?

Re: intrinsic functions

Posted: Mon May 24, 2021 9:18 am
by Octocontrabass
newosdeveloper2021 wrote:any way to use intrinsic functions like in windows kernel? like __writecr0 and __cpuidex?
In my kernel?
Yes: write them yourself using inline assembler.
newosdeveloper2021 wrote:EDIT: also can I use microsoft-style inline assembler?
Only if you use Microsoft's compiler.

Re: intrinsic functions

Posted: Mon May 24, 2021 10:23 am
by newosdeveloper2021
Octocontrabass wrote:
newosdeveloper2021 wrote:any way to use intrinsic functions like in windows kernel? like __writecr0 and __cpuidex?
In my kernel?
Yes: write them yourself using inline assembler.

Can I use immintrin.h or any compiler-specific intrinsic? or would there be some random issue even though it's bare-metal?

Re: intrinsic functions

Posted: Mon May 24, 2021 10:35 am
by nullplan
newosdeveloper2021 wrote:Yes: write them yourself using inline assembler.

Can I use immintrin.h or any compiler-specific intrinsic? or would there be some random issue even though it's bare-metal?
You can use compiler-internal headers (like immintrin.h) in freestanding mode. And I would highly recommend using those over trying to write the inline assembler yourself. Inline assembler for control instructions is non-trivial, and I would recommend if you must write those yourself, then please use a normal external function for those. The problem is that it is not always easy, telling the compiler about all the things that your snippet can do. An external function call has well-defined characteristics you can rely on. Inline assembler is hard to get right and impossible to test: What works today may not work tomorrow.

That said, I appear to be in the minority with this opinion, so take it for what it is worth.