Is there a way for me to use the CountLeadingZero (CLZ) assembly instruction in a sketch? A C version is really slow. I guess my real question is how to access assembly and where it might be documented.
I don't have an exact answer about CLZ but there are many examples of assembler code in this project e.g. https://github.com/esp8266/Arduino/blob/4897e0006b5b0123a2fa31f67b14a3fff65ce561/cores/esp8266/Arduino.h#L118
So "accessing" assembler is basically simply a case of declaring a code construct with asm or asm volatile (according to the gcc docs, the volatile keyword indicates that the instruction has important side-effects).
If the next question was "how do I construct the correct asm CLZ instruction for an ESP board?", this would be a very hardware specific question and since ESP8266 devices are based on the LX106 processor along with the Xtensa instruction set architecture, to answer that kind of question we'd need detailed reference documentation for that: https://www.google.com/search?q=Tensilica+Xtensa+Instruction+Set+Architecture&ie=utf-8&oe=utf-8
I'm sorry I don't have an exact CLZ answer, someone else might. But in the absence of anything else, this is a starting point.
You may consider using GCC's __builtin_clz() intrinsic. It is implemented using the correct instructions on each architecture. For Xtensa, it uses nsau instruction (on configurations which include nsau; and a software fallback for other configurations).
Thanks to both of you. I have plenty of info to dig into this. Sounds like fun.
Most helpful comment
You may consider using GCC's
__builtin_clz()intrinsic. It is implemented using the correct instructions on each architecture. For Xtensa, it usesnsauinstruction (on configurations which include nsau; and a software fallback for other configurations).