"collatz problem func"
This commit is contained in:
commit
0a1d4fbb2e
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
-test
|
||||||
|
test
|
||||||
|
main
|
||||||
35
collatz.S
Normal file
35
collatz.S
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
# bedir t karaabalı
|
||||||
|
# 2025
|
||||||
|
# collatz problem
|
||||||
|
|
||||||
|
.text
|
||||||
|
# void collatz_len_and_peek(uint64_t x, uint64_t *steps, uint64_t *peek)
|
||||||
|
.global collatz_len_and_peek
|
||||||
|
collatz_len_and_peek:
|
||||||
|
# Collatz problem implementation
|
||||||
|
# x -> rdi, steps -> rsi, peak -> rdx
|
||||||
|
|
||||||
|
movq $0, (%rsi) # *steps=0
|
||||||
|
movq %rdi, (%rdx) # *peak = x
|
||||||
|
movq %rdi, %r8 # r8(n) (caller-saved) = x
|
||||||
|
|
||||||
|
.loop:
|
||||||
|
cmpq $1, %r8 # while (n != 1)
|
||||||
|
je .done
|
||||||
|
testq $1, %r8 # 1 and n
|
||||||
|
jz .even
|
||||||
|
# odd case
|
||||||
|
leaq (%r8, %r8, 2), %r8 # n *= 3 -> lea base + index * scale
|
||||||
|
addq $1,%r8 # n += 1
|
||||||
|
jmp .next_step
|
||||||
|
.even:
|
||||||
|
shrq $1, %r8 # n /= 2
|
||||||
|
.next_step:
|
||||||
|
cmpq (%rdx), %r8 # if (n > *peek) n - *peek
|
||||||
|
jle .next
|
||||||
|
movq %r8, (%rdx) # *peek = n
|
||||||
|
.next:
|
||||||
|
addq $1, (%rsi) # (*steps)++
|
||||||
|
jmp .loop
|
||||||
|
.done:
|
||||||
|
ret
|
||||||
Loading…
Reference in New Issue
Block a user