Skip to content

Comptime evaluation of bit-manipulation methods (§4.2.4) #422

Closed

Description

@ehartford

Comptime evaluation of bit-manipulation methods (§4.2.4)

Summary

The integer bit-manipulation methods required by §4.2.4 work in runtime LLVM codegen, but they are not comptime-evaluable. Constants using rotate_left, rotate_right, swap_bytes, popcount, clz, ctz, or bitreverse either fail or avoid the method path instead of producing the specified constant result.

Spec

docs/with-specification.md:1031-1145 defines bit methods and explicitly requires compile-time evaluation when operands are known at comptime. popcount, clz, and ctz return i32; clz(0) and ctz(0) return the integer width; swap_bytes is identity for 8-bit integers; bitreverse and rotates operate at the integer width.

Current state

  • src/MirLower.w:5852-5894 classifies integer methods including rotate_left, rotate_right, swap_bytes, popcount, clz, ctz, bitreverse, min, max, and abs.
  • src/CodegenDispatch.w:7070-7190 lowers the runtime LLVM forms: rotates from shifts/or, swap_bytes via llvm.bswap or identity for <=8 bits, popcount via llvm.ctpop, clz/ctz via LLVM count intrinsics with zero-defined behavior, and bitreverse via llvm.bitreverse.
  • src/ComptimeEval.w has no corresponding evaluator entries for these method names. A scan for rotate_left, rotate_right, swap_bytes, popcount, clz, ctz, and bitreverse found the methods in MIR/codegen, not in comptime evaluation.
  • Existing behavior tests cover some runtime cases, such as test/behavior/behav_rotate.w and test/behavior/behav_int_swap_bytes.w, but do not prove const/comptime evaluation.

Implementation plan

  1. Add a comptime integer-method dispatch path in src/ComptimeEval.w parallel to the existing runtime intrinsic classification. Keep the method list centralized or mirrored deliberately so runtime and comptime support cannot drift silently.
  2. Implement width-aware exact-integer operations for:
    • rotate_left / rotate_right, masking shift counts by width,
    • swap_bytes, with 8-bit identity,
    • popcount, clz, ctz, including zero returning width for clz/ctz,
    • bitreverse.
  3. Return i32 comptime values for popcount, clz, and ctz; return the receiver type for rotates, swap_bytes, and bitreverse.
  4. Reject non-integer receivers through the existing Sema path; the comptime evaluator should not accept a method that Sema would reject.
  5. Verify signed integer handling uses the bit pattern at the declared width, not host signed arithmetic semantics.

Test plan

  • Add test/behavior/behav_comptime_bit_methods.w with const or comptime values for each method and representative widths (u8, u16, u32, u64, and one signed type).
  • Include zero cases for clz and ctz.
  • Include rotate counts equal to or larger than the bit width.
  • Include swap_bytes on u8 and a multi-byte integer.
  • Include bitreverse on a small width where the expected value is obvious.
  • Run with build, with build :fixpoint, and with build :test.

Acceptance criteria

  • All §4.2.4 integer bit methods are comptime-evaluable.
  • Comptime results match runtime codegen results for the same inputs.
  • popcount, clz, and ctz produce i32 values.
  • Zero handling for clz/ctz follows the spec.
  • Signed receivers are evaluated by fixed-width bit pattern, not host overflow behavior.

Dependencies

Overlaps #483, which is broader behavior-test coverage for bit methods. This issue should add the comptime-specific tests needed for the implementation.

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 4.2.4.28.

Activity

  1. ehartford commented on Jun 12, 2026

    @ehartford
    ContributorAuthor

    Implemented in 298d951. Integer bit methods now evaluate in comptime for rotate_left, rotate_right, swap_bytes, popcount, clz, ctz, and bitreverse, with fixed-width bit-pattern semantics and signed receiver coverage. Added positive behavior coverage and a negative arity test. Verification passed: build, fixpoint, full test suite, and test-green. Follow-up #565 tracks the separate comptime pipeline syntax dispatch bug found during testing.

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