Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exercises(collatz-conjecture): example: refactor #330

Merged

Conversation

ee7
Copy link
Member

@ee7 ee7 commented Sep 21, 2023

For this unsigned integer let's use:

  • / instead of @divTrunc
  • % instead of @mod

The result is the same. From the Zig Language Reference here:

@divTrunc(numerator: T, denominator: T) T

Truncated division. Rounds toward zero.
For unsigned integers it is the same as numerator / denominator.

and here:

@mod(numerator: T, denominator: T) T

Modulus division. For unsigned integers this is the same as numerator % denominator.

That is, this commit does not change the instructions for the loop body, which are (with Zig 0.12.0-dev.415+5af5d87ad and -O ReleaseFast -mcpu raptorlake):

.LBB0_1:
        mov     rcx, rdi
        shr     rcx
        test    dil, 1
        lea     rdi, [rdi + 2*rdi + 1]
        cmove   rdi, rcx
        inc     rax
        cmp     rdi, 1
        ja      .LBB0_1

Refs: #313

For this unsigned integer let's use `/` instead of `@divTrunc`,
and `%` instead of `@mod`.

The result is the same. From the Zig Language Reference [1]:

    @divTrunc(numerator: T, denominator: T) T

    Truncated division. Rounds toward zero.
    For unsigned integers it is the same as `numerator / denominator`.

and [2]:

    @mod(numerator: T, denominator: T) T

    Modulus division. For unsigned integers this is the same as `numerator % denominator`.

That is, this commit does not change the instructions for the loop body,
which are [3]:

    .LBB0_1:
            mov     rcx, rdi
            shr     rcx
            test    dil, 1
            lea     rdi, [rdi + 2*rdi + 1]
            cmove   rdi, rcx
            inc     rax
            cmp     rdi, 1
            ja      .LBB0_1

[1] https://ziglang.org/documentation/0.11.0/#divTrunc
[2] https://ziglang.org/documentation/0.11.0/#mod
[3] with Zig 0.12.0-dev.415+5af5d87ad and `-O ReleaseFast -mcpu raptorlake`
@ee7 ee7 requested a review from booniepepper September 21, 2023 10:46
@ee7 ee7 merged commit 963305b into exercism:main Oct 4, 2023
@ee7 ee7 deleted the exercises-collatz-conjecture-example-refactor branch October 4, 2023 20:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants