File: nsieve-bits.pir

package info (click to toggle)
parrot 2.0.0-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 23,156 kB
  • ctags: 14,972
  • sloc: perl: 96,188; ansic: 88,708; yacc: 4,923; lex: 4,254; lisp: 1,163; cpp: 746; python: 541; ruby: 351; makefile: 184; sh: 146; cs: 49; asm: 30
file content (74 lines) | stat: -rw-r--r-- 1,328 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!./parrot
# Copyright (C) 2005-2009, Parrot Foundation.
# $Id$
#
# ./parrot -R jit nsieve-bits.pir N  (N = 9 for shootout)
# by Leopold Toetsch
# modified by Joshua Isom
# modified by Karl Forner to accept shootout default value of N=2


# set bits - this might be cheating see nsieve-bits-2 for resetting bits

.sub primes_in_range
    .param int M
    .param pmc flags
    .local int i, count
    i = 2
    count = 0
lp1:
     $I0 = flags[i]
     if $I0 goto not_p
     .local int j
     j = i + i
     if j >= M goto done
lp2:
     flags[j] = 1
     j += i
     if j < M goto lp2
done:
     inc count
not_p:
     inc i
     if i < M goto lp1
    .return (count)
.end
.sub main :main
    .param pmc argv
    .local int argc, i, j, N, M, count
	.local pmc flags

	argc = argv
	N = 2
	if argc == 1 goto default
    $S0 = argv[1]
	N = $S0
default:
    flags = new 'FixedBooleanArray'
	M = 1 << N
	M *= 10000
	flags = M
	null i
    null j
loop:
    $I0 = N - j
    inc j
    $I1 = 1 << $I0
    M = $I1 * 10000
    count = primes_in_range(M, flags)
    $P0 = new 'FixedIntegerArray'
    $P0 = 2
    $P0[0] = M
    $P0[1] = count
    $S0 = sprintf "Primes up to %8u %8u\n", $P0
    print $S0
    inc i
    if i < 3 goto loop
.end


# Local Variables:
#   mode: pir
#   fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: