On Monday, 21 December 2015 at 17:28:51 UTC, Andrei Alexandrescu wrote:
https://www.reddit.com/r/programming/comments/3xq2ul/codedive_2015_talk_three_cool_things_about_d/


By the way, even though I wholeheartedly share your sentiment regarding those "functional" examples, I found it to be an interesting example for the power of modern optimizers that LDC manages to compile this

---
ulong factorial(this n) {
        return n <= 1 ? 1 : n * factorial(n - 1);
}
---

to this:

---
__D4test9factorialFkZm:
    mov eax, 1
    cmp edi, 2
    jb  LBB0_7
    mov ecx, edi
    lea edx, [rdi + 7]
    add edi, -2
    mov eax, 1
    test    dl, 7
    je  LBB0_4
    and edx, 7
    neg edx
    mov eax, 1
    .align  4, 0x90
LBB0_3:
    imul    rax, rcx
    dec rcx
    inc edx
    jne LBB0_3
LBB0_4:
    cmp edi, 7
    jb  LBB0_7
    add rcx, -3
    .align  4, 0x90
LBB0_6:
    lea rdx, [rcx + 3]
    imul    rdx, rax
    lea rax, [rcx + 2]
    lea rsi, [rcx + 1]
    imul    rax, rsi
    imul    rax, rdx
    lea rdx, [rcx - 1]
    imul    rdx, rcx
    lea rsi, [rcx - 2]
    imul    rsi, rdx
    imul    rsi, rax
    lea rax, [rcx - 3]
    lea rdx, [rcx - 4]
    imul    rax, rdx
    imul    rax, rsi
    add rcx, -8
    lea edx, [rcx + 3]
    cmp edx, 1
    ja  LBB0_6
LBB0_7:
    ret
---

Not sure about how it arrives at the crazily unrolled loop, but no recursion in sight anymore.

 — David

Reply via email to