From 0a1d4fbb2e538f895434e3bd51503f09915954de Mon Sep 17 00:00:00 2001 From: bdrtr Date: Tue, 26 Aug 2025 06:11:33 +0300 Subject: [PATCH] "collatz problem func" --- .gitignore | 3 +++ README.md | 0 collatz.S | 35 +++++++++++++++++++++++++++++++++++ collatz.o | Bin 0 -> 840 bytes main.c | 11 +++++++++++ 5 files changed, 49 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 collatz.S create mode 100644 collatz.o create mode 100644 main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9594209 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +-test +test +main diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/collatz.S b/collatz.S new file mode 100644 index 0000000..6f3f76d --- /dev/null +++ b/collatz.S @@ -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 diff --git a/collatz.o b/collatz.o new file mode 100644 index 0000000000000000000000000000000000000000..ed55a23164b7196a617a25cb07e843e67e5bbc29 GIT binary patch literal 840 zcmb<-^>JfjWMqH=Mg}_u1P><4z|est=l~XWU|?rpWAHf62I6^iT6uQ<@NE9USfb+j z{QyXD373B_i-TwL0mj$No)=&ESTofz`*bpSG_x_jzIGUF1Qr5f4I=|DnjAZr&&Xho zCJs@@$e@NME(}$#i6$-w6*oi^R{`<`u=!jsCqKV{K`$jgFO@+rwJbG{K`$@0q9ne! zBo!zOVlpJ>=j0@oRK@3{=EW!GrNkGcre-rR=oMGymLw(t>5?J{odIK&fOP4lB$gyH z=p_{wqdA2vt~5{=DD1$ofn JMktN09{|}#J;MM1 literal 0 HcmV?d00001 diff --git a/main.c b/main.c new file mode 100644 index 0000000..e2ecc2d --- /dev/null +++ b/main.c @@ -0,0 +1,11 @@ +#include +#include + +void collatz_len_and_peek(uint64_t x, uint64_t *steps, uint64_t *peek); + +int main() { + uint64_t steps, peak; + collatz_len_and_peek(13, &steps, &peak); + printf("steps=%lu, peak=%lu\n", steps, peak); + return 0; +} \ No newline at end of file