The nnue_index function itself, consisting of bitwise operations, shifts, and a few additions, is likely very fast, taking only a handful of CPU cycles. A reasonable estimate would be in the range of 5-20 cycles on a modern CPU architecture. The exact number depends on the specific instructions generated by the compiler, the CPU's microarchitecture, and the availability of relevant data in the cache.
Here's a breakdown of the potential cycle costs:
- Bitwise and Shift Operations: These are typically single-cycle instructions on most CPUs.
- Integer Additions: These are also usually single-cycle instructions.
- Array Indexing Overhead: Accessing the
Mirror array involves an additional memory access. If the array is in the cache, this could take a few cycles. If it's not in the cache, it could take significantly longer (tens or even hundreds of cycles). However, given the small size of the Mirror array (8 elements), it's likely to reside in the cache after the first few accesses.
Why so few cycles?
The operations in nnue_index are fundamental and highly optimized in modern CPUs. Bitwise operations and integer arithmetic are extremely fast. The compiler can also perform optimizations like constant folding or instruction reordering to further reduce the number of cycles.
Important Note: As mentioned in the previous response, if profiling attributes a significant amount of time to nnue_index, the bottleneck is not within the function itself. The measured time likely includes the overhead of the functions that call nnue_index and the memory accesses triggered by the computed index. The small cycle count of nnue_index reinforces this point – it's too fast to be a major bottleneck on its own. The profiler is likely including related operations in its measurement for nnue_index which makes it appear slower.