Zajímavé hraní :-) paradoxně mi tu RV64 vychází jako nejkratší. Krátká aritmetická funkce:
fn is_prime(n: u32) bool {
var i: u32 = 2;
while (i <= n / 2) {
if (n % i == 0) {
return false;
}
i += 1;
}
return true;
}
Chvíli jsem musel přemýšlet, protože v jednoduchém programu, kdy jen funkci zavolám z mainu s nějakým parametrem, mi Zig během kompilace dokázal, že jsou všechny argumenty známé a tak funkci vůbec nezkompiloval, rovnou dosadil výsledek :-)
RV32 mi chybí, ve standardní knihovně není implementace jednoho syscallu.
zig build-exe -O ReleaseSmall main.zig:
0000000000208c36 <is_prime>:
208c36: 89 f9 mov ecx, edi
208c38: d1 e9 shr ecx
208c3a: 6a 02 push 2
208c3c: 58 pop rax
208c3d: 89 c6 mov esi, eax
208c3f: 39 c8 cmp eax, ecx
208c41: 77 0d ja 0x208c50 <is_prime+0x1a>
208c43: 89 f8 mov eax, edi
208c45: 31 d2 xor edx, edx
208c47: f7 f6 div esi
208c49: 8d 46 01 lea eax, [rsi + 1]
208c4c: 85 d2 test edx, edx
208c4e: 75 ed jne 0x208c3d <is_prime+0x7>
208c50: 39 ce cmp esi, ecx
208c52: 0f 97 c0 seta al
208c55: c3 ret
zig build-exe -O ReleaseSmall main.zig -target i386-linux:
004087b1 <is_prime>:
4087b1: 57 push edi
4087b2: 56 push esi
4087b3: 89 ce mov esi, ecx
4087b5: d1 ee shr esi
4087b7: 6a 02 push 2
4087b9: 58 pop eax
4087ba: 89 c7 mov edi, eax
4087bc: 39 f0 cmp eax, esi
4087be: 77 0d ja 0x4087cd <is_prime+0x1c>
4087c0: 89 c8 mov eax, ecx
4087c2: 31 d2 xor edx, edx
4087c4: f7 f7 div edi
4087c6: 8d 47 01 lea eax, [edi + 1]
4087c9: 85 d2 test edx, edx
4087cb: 75 ed jne 0x4087ba <is_prime+0x9>
4087cd: 39 f7 cmp edi, esi
4087cf: 0f 97 c0 seta al
4087d2: 5e pop esi
4087d3: 5f pop edi
4087d4: c3 ret
zig build-exe -O ReleaseSmall main.zig -target arm-linux:
00028100 <is_prime>:
28100: 70 40 2d e9 push {r4, r5, r6, lr}
28104: 00 40 a0 e1 mov r4, r0
28108: a0 60 a0 e1 lsr r6, r0, #1
2810c: 02 10 a0 e3 mov r1, #2
28110: 01 50 a0 e1 mov r5, r1
28114: 06 00 51 e1 cmp r1, r6
28118: 05 00 00 8a bhi 0x28134 <is_prime+0x34> @ imm = #20
2811c: 04 00 a0 e1 mov r0, r4
28120: 05 10 a0 e1 mov r1, r5
28124: eb 02 00 eb bl 0x28cd8 <__umodsi3> @ imm = #2988
28128: 01 10 85 e2 add r1, r5, #1
2812c: 00 00 50 e3 cmp r0, #0
28130: f6 ff ff 1a bne 0x28110 <is_prime+0x10> @ imm = #-40
28134: 00 00 a0 e3 mov r0, #0
28138: 06 00 55 e1 cmp r5, r6
2813c: 01 00 00 83 movwhi r0, #1
28140: 70 80 bd e8 pop {r4, r5, r6, pc}
zig build-exe -O ReleaseSmall main.zig -target aarch64-linux:
0000000000216a94 <is_prime>:
216a94: 08 7c 01 53 lsr w8, w0, #1
216a98: 4a 00 80 52 mov w10, #2
216a9c: e9 03 0a 2a mov w9, w10
216aa0: 5f 01 08 6b cmp w10, w8
216aa4: a8 00 00 54 b.hi 0x216ab8 <is_prime+0x24>
216aa8: 0a 08 c9 1a udiv w10, w0, w9
216aac: 4b 81 09 1b msub w11, w10, w9, w0
216ab0: 2a 05 00 11 add w10, w9, #1 // =1
216ab4: 4b ff ff 35 cbnz w11, 0x216a9c <is_prime+0x8>
216ab8: 3f 01 08 6b cmp w9, w8
216abc: e0 97 9f 1a cset w0, hi
216ac0: c0 03 5f d6 ret
zig build-exe -O ReleaseSmall main.zig -target riscv64-linux:
000000000001778c <is_prime>:
1778c: 9b 55 15 00 srliw a1, a0, 1
17790: 09 46 addi a2, zero, 2
17792: 9b 06 06 00 sext.w a3, a2
17796: 63 e6 d5 00 bltu a1, a3, 0x177a2 <is_prime+0x16>
1779a: 3b 77 c5 02 remuw a4, a0, a2
1779e: 05 06 addi a2, a2, 1
177a0: 6d fb bnez a4, 0x17792 <is_prime+0x6>
177a2: 33 b5 d5 00 sltu a0, a1, a3
177a6: 82 80 ret
zig build-exe -O ReleaseSmall main.zig -target powerpc-linux:
10018e9c <is_prime>:
10018e9c: 54 64 f8 7e srwi 4, 3, 1
10018ea0: 38 c4 00 01 addi 6, 4, 1
10018ea4: 38 a0 00 02 li 5, 2
10018ea8: 28 06 00 02 cmplwi 6, 2
10018eac: 41 81 00 0c bt 1, 0x10018eb8
10018eb0: 60 a6 00 00 ori 6, 5, 0
10018eb4: 48 00 00 04 b 0x10018eb8
10018eb8: 38 c6 ff ff addi 6, 6, -1
10018ebc: 7c c9 03 a6 mtctr 6
10018ec0: 7c a6 2b 78 mr 6, 5
10018ec4: 42 40 00 18 bdz 0x10018edc
10018ec8: 7c a3 33 96 divwu 5, 3, 6
10018ecc: 7c a5 31 d6 mullw 5, 5, 6
10018ed0: 7c a5 18 51 sub. 5, 3, 5
10018ed4: 38 a6 00 01 addi 5, 6, 1
10018ed8: 40 82 ff e8 bf 2, 0x10018ec0
10018edc: 7c 06 20 40 cmplw 6, 4
10018ee0: 38 60 00 00 li 3, 0
10018ee4: 38 80 00 01 li 4, 1
10018ee8: 41 81 00 08 bt 1, 0x10018ef0
10018eec: 4e 80 00 20 blr
10018ef0: 38 64 00 00 addi 3, 4, 0
10018ef4: 4e 80 00 20 blr
zig build-exe -O ReleaseSmall main.zig -target mips-linux:
00029adc <is_prime>:
29adc: 00 04 10 42 srl $2, $4, 1 <main+0x1>
29ae0: 24 03 00 02 addiu $3, $zero, 2 <main+0x2>
29ae4: 24 06 00 02 addiu $6, $zero, 2 <main+0x2>
29ae8: 24 41 00 01 addiu $1, $2, 1 <main+0x1>
29aec: 2c 25 00 03 sltiu $5, $1, 3 <main+0x3>
29af0: 00 25 18 0a movz $3, $1, $5
29af4: 10 66 00 07 beq $3, $6, 32 <is_prime+0x38>
29af8: 00 c0 28 25 move $5, $6
29afc: 00 85 00 1b divu $zero, $4, $5
29b00: 24 a6 00 01 addiu $6, $5, 1 <main+0x1>
29b04: 00 a0 01 f4 teq $5, $zero, 7 <main+0x7>
29b08: 00 00 08 10 mfhi $1
29b0c: 14 20 ff f9 bnez $1, -24 <is_prime+0x18>
29b10: 00 00 00 00 nop <main>
29b14: 03 e0 00 08 jr $ra
29b18: 00 45 10 2b sltu $2, $2, $5