Skip to content

BitOps: branchless Log2; release the TrailingZeroCount table #1170

Closed

Description

BitOps.Log2(uint) is a five-compare chain. On varied inputs it mispredicts, and it costs about 10 times a branchless de Bruijn lookup. TrailingZeroCount is fixed on main, but not released.

Measured per call, .NET 11 Release, 4M inputs, 15 rotated rounds. Every variant gives the same answer on every input.

variant varied inputs inputs 1..40
Log2 3.7.1 (compare chain) 9.04 ns 3.54 ns
Log2 de Bruijn, ReadOnlySpan<byte> table 0.90 ns 0.91 ns
Log2 BitOperations (hardware, .NET only) 0.66 ns
TrailingZeroCount 3.7.1 (PopCount) 1.56 ns
TrailingZeroCount main (byte[], zero branch) 1.22 ns
TrailingZeroCount de Bruijn, ReadOnlySpan<byte> table 1.11 ns
TrailingZeroCount BitOperations (hardware) 0.99 ns

The de Bruijn Log2: smear the bits right (v |= v >> 1; ... v |= v >> 16), then table[(v * 0x07C4ACDDu) >> 27]. DoxReloaded shipped it until #1172 swapped in BitOps.Log2. Its depth-first search calls it once per node, on counts of 1 to 40.

Ask:

  • Log2(uint) and Log2(ulong) as branchless de Bruijn lookups.
  • Release main's TrailingZeroCount table.
  • Hold both tables as ReadOnlySpan<byte> properties over constant data. That skips the static field load and the bounds check, which a static readonly byte[] pays.

Why: Unity's .NET Standard 2.1 has no BitOperations, so this software path is the one Mono and IL2CPP run.

From Ambiguous-Interactive/DoxReloaded#1172.

DISCLOSURE: LLM-GENERATED TEXT

Activity

  1. wallstop commented on Nov 12, 2026

    @wallstop
    CollaboratorAuthor

    DISCLOSURE: LLM-GENERATED TEXT

    PR #1188 implements de Bruijn Log2 lookups for both unsigned widths and constant read-only span tables. Zero and signed bit-pattern behavior are preserved.

    Native Mono measurements rejected both direct span construction and explicit getter inlining. The retained implementation uses fixed array caches under ENABLE_MONO, initialized once from the constant spans. Other backends use the spans. The cache holds 160 table bytes plus array overhead; warm lookups create no arrays.

    Three alternating fresh Unity 6000.6.7f1 process pairs each pass all 619 cases with no skips. Each pair improves both Log2 widths; trailing-zero controls stay within about 3%. The varied 64-bit baseline ranges from 2.306 to 9.335 ns, so the guide shows the variation and all paired ranges. The replay includes complete NUnit XML, raw rounds, source/module identities, native process receipts and IL proving the actual Mono cache path.

    These are Editor Mono observations, not IL2CPP or game-workload guarantees. CI/review is pending. Keep this issue open for its release requirement; #1172 still tracks publishing a version after 3.7.1. No package version or release tag changed.

  2. wallstop commented on Nov 12, 2026

    @wallstop
    CollaboratorAuthor

    DISCLOSURE: LLM-GENERATED TEXT

    Additional qualification for PR #1188 head 0103f81: a native Windows x64 Unity 6000.6.7f1 IL2CPP player passes 639/639 selected cases, zero skipped/inconclusive/failed. These comprise 613 BitOps functional cases and 26 corpus-control cases. The four frozen corpus size witnesses all pass, retaining all 32 workload identities. Fourteen input hashes are unchanged before/after; configuration and build processes exit zero, reach both output-stream ends and have no drain stop. The driver exits zero. NUnit XML SHA-256: 69ebae8a6ef8c57050bab00beb414b473ced1c5538d4d768f9884e172355e0d7.

    The real player build's generated C++ contains all three ordinary and forced-inline table getters. Each constructs a local ReadOnlySpan over a static RVAStorage address and length 32/64. The six getter bodies have no managed array/object allocation calls. This confirms that the C# constant-array span syntax does not allocate an array per getter call.

    This player uses Debug C++ configuration. The first local Release attempt hit MSVC 14.51.36231 C1001 in generated test-assembly C++ (__35.cpp:5969), the optimizer failure class documented in #374; it produced no player result and is excluded from passing evidence. The mechanism-changing Debug build succeeded. These are functional/code-generation results, not IL2CPP throughput measurements. The existing Mono timing archive and remaining release acceptance stay unchanged.

  3. wallstop commented on Nov 20, 2026

    @wallstop
    CollaboratorAuthor

    DISCLOSURE: LLM-GENERATED TEXT

    Version 3.7.2 includes #1188 on GitHub and npm. The published BitOps.cs matches the release tag and the previously qualified implementation. Both unsigned Log2 overloads use de Bruijn indexing. TrailingZeroCount also uses its released table. All three tables are defined as constant read-only spans. Mono uses the once-initialized array caches selected by the earlier paired native runs.

    The earlier Unity Mono timing evidence and 639 passing IL2CPP player cases remain the qualification evidence. The release tarball downloaded from npm matches the GitHub asset at SHA-256 c3eeef4740838c10912b0dcdcf159b1deb8a60129bc9c2e37e5342fc4f9d0b4e.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions