So sieve.fs works slightly better on VFX64. Let's see if I can do better:
DECIMAL
CREATE FLAGS 8190 ALLOT
variable eflag
: PRIMES ( -- n ) FLAGS 8190 1 FILL 0 3 EFLAG @ FLAGS
DO I C@
IF DUP I + DUP EFLAG @ <
IF EFLAG @ SWAP
begin ( prime limit index )
0 over c! 2 pick + 2dup u<=
until
2drop
ELSE DROP THEN SWAP 1+ SWAP
THEN 2 +
LOOP DROP ;
The part about EFLAGS being a variable is the main change between
sieve.fs and siev.fs and has nothing to do with the inner loop. The
inner loop is now decompiled as:
MOV Byte 0 [RBX], # 00
ADD RBX, [RBP+08]
CMP RBX, [RBP]
JB/NAE 0050C3C4
That's smaller by one instruction, but that probably does not help on
most modern CPUs which can do 5 instructions/cycle, but only one taken
branch per cycle.
Invocations:
perf stat vfx64 "include gilbreath-works.4th : bench 10000 0 do do-prime drop loop ; bench bye"
perf stat vfx64 "include /nfs/nfstmp/anton/gforth-amd64/siev.fs flags 8190 + eflag ! : bench 10000 0 do primes drop loop ; bench bye"
perf stat vfx64 "include sieve-ae.fs flags 8190 + eflag ! : bench 10000 0 do primes drop loop ; bench bye"
[..]That's smaller by one instruction, but that probably does not help on
most modern CPUs which can do 5 instructions/cycle, but only one taken >>branch per cycle.
By unrolling the loop by a factor of 2 we can convert every second
branch in the inner loop into a not-taken branch:
On Tue, 9 Jul 2024 10:12:40 +0000, Anton Ertl wrote:
[..]
[..]That's smaller by one instruction, but that probably does not help on >>>most modern CPUs which can do 5 instructions/cycle, but only one taken >>>branch per cycle.
By unrolling the loop by a factor of 2 we can convert every second
branch in the inner loop into a not-taken branch:
Interesting. Shouldn't unrolling the BEGIN ... UNTIL loop help even
more then? Maybe make the FLAGS array (much) bigger and don't check
for out of bounds?
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 475 |
Nodes: | 16 (2 / 14) |
Uptime: | 19:50:59 |
Calls: | 9,487 |
Calls today: | 6 |
Files: | 13,617 |
Messages: | 6,121,093 |