File: mandelbrot.pir

package info (click to toggle)
parrot 6.6.0-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 25,164 kB
  • ctags: 16,050
  • sloc: ansic: 110,715; perl: 94,382; yacc: 1,911; lex: 1,529; lisp: 1,163; cpp: 782; python: 646; ruby: 335; sh: 140; makefile: 129; cs: 49; asm: 30
file content (130 lines) | stat: -rw-r--r-- 2,500 bytes parent folder | download | duplicates (2)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!./parrot
# Copyright (C) 2005-2009, Parrot Foundation.

=head1 NAME

examples/shootout/mandelbrot.pir - Print the Mandelbrot set

=head1 SYNOPSIS

    % ./parrot examples/shootout/mandelbrot.pir -R jit 600 > out.pbm

=head1 DESCRIPTION

This outputs a pbm file of the Mandelbrot set. Defaults to 200x200.

Translated from C code by Greg Buchholz into PIR
by Peter Baylies <pbaylies@gmail.com>.

The C code is at:
   The Great Computer Language Shootout
   http://shootout.alioth.debian.org/

=cut

.sub 'main' :main
    .param pmc argv
#    int w, h, x, y, bit_num = 0;
#    char byte_acc = 0;
#    int i, iter = 50;
#    double limit = 2.0;
#    double Zr, Zi, Cr, Ci, Tr, Ti;
    .local int    w, h, x, y, bit_num, byte_acc, i, iter
    .local num    limit, Zr, Zi, Cr, Ci, Tr, Ti
    .local int argc
    bit_num = 0
    byte_acc = 0
    iter = 50
    limit = 2.0
#   slight optimization here -- nothing a decent C compiler wouldn't already do :)
    limit = limit * limit
    argc = argv
    w = 200
    if argc <= 1 goto noarg
#   w = atoi(argv[1]);
    $S0 = argv[1]
    w = $S0
#   h = w
noarg:  h = w
#   printf("P4\n%d %d\n",w,h);
    print "P4\n"
    print w
    print " "
    print h
    print "\n"
    y = 0
YREDO:
        x = 0
XREDO:
#       Zr = 0.0; Zi = 0.0;
    Zr = 0.0
    Zi = 0.0
#       Cr = (2*(double)x/w - 1.5);
    Cr = x
    Cr /= w
    Cr *= 2
    Cr -= 1.5
#   Ci=(2*(double)y/h - 1);
    Ci = y
    Ci /= h
    Ci *= 2
    Ci -= 1

    i = 0
IREDO:
#   Tr = Zr*Zr - Zi*Zi + Cr;
        $N1 = Zr * Zr
        $N2 = Zi * Zi
        Tr = $N1 - $N2
        Tr += Cr
#       Ti = 2*Zr*Zi + Ci;
        Ti = 2
    Ti *= Zr
        Ti *= Zi
        Ti += Ci
#   Zr = Tr; Zi = Ti;
    Zr = Tr
    Zi = Ti
#   if (Zr*Zr+Zi*Zi > limit*limit) break;
    $N1 = Zr * Zr
    $N2 = Zi * Zi
        $N1 += $N2
        if $N1 > limit goto IBRK
    inc i
        if i < iter goto IREDO
IBRK:
    byte_acc <<= 1
        if $N1 <= limit goto SLA
    byte_acc |= 0
    goto SLE
SLA:    byte_acc |= 1
SLE:    inc bit_num
    if bit_num != 8 goto NTST1
PRINT:  chr $S1, byte_acc
    print $S1
    byte_acc = 0
    bit_num = 0
    goto NTSTE
NTST1:  $I1 = w
    dec $I1
    goto NTSTE
    if x != $I1 goto NTSTE
    $I1 = w
    $I1 %= 8
    $I1 = 8 - $I1
    byte_acc <<= $I1
    goto PRINT
NTSTE:
    inc x
        if x < w goto XREDO

        inc y
        if y < h goto YREDO
    end
.end

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