[Shootout-list] Mandelbrot

Greg Buchholz sleepingsquirrel@member.fsf.org
Mon, 20 Dec 2004 11:03:54 -0800 (PST)


--0-23165646-1103569434=:84653
Content-Type: text/plain; charset=us-ascii
Content-Id: 
Content-Disposition: inline

--- Isaac Gouy <igouy2@yahoo.com> wrote:

> It's a monochrome image - let's try writing pbm byte-by-byte
> 

    Attached is a C program which does this.

Greg Buchholz





		
__________________________________ 
Do you Yahoo!? 
The all-new My Yahoo! - What will yours do?
http://my.yahoo.com 
--0-23165646-1103569434=:84653
Content-Type: text/x-c; name="fractal.c"
Content-Description: fractal.c
Content-Disposition: inline; filename="fractal.c"

//Mandelbrot benchmark
//compile:  gcc -O2 -o fractal fractal.c
//run:  fractal 600 >mandel.pbm
//by Greg Buchholz

#include<stdio.h>

int main (int argc, char **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;
    
    w = atoi(argv[1]);
    h = w;

    printf("P4\n%d %d\n",w,h);

    for(y=0;y<h;y++) 
    {
        for(x=0;x<w;x++)
        {
            Zr = 0.0; Zi = 0.0;
            Cr = (2*(double)x/w - 1.5); Ci=(2*(double)y/h - 1);
        
            for (i=0;i<iter;i++)
            {
                Tr = Zr*Zr - Zi*Zi + Cr;
                Ti = 2*Zr*Zi + Ci;
                Zr = Tr; Zi = Ti;
                if (Zr*Zr+Zi*Zi > limit*limit)
                    break;
            }
        
            if(Zr*Zr+Zi*Zi > limit*limit) 
                byte_acc = (byte_acc << 1) | 0x00;
            else
                byte_acc = (byte_acc << 1) | 0x01;
                
            bit_num++; 
            if(bit_num == 8)
            {
                putc(byte_acc,stdout);
                byte_acc = 0;
                bit_num = 0;
            }
            else if(x == w-1)
            {
                byte_acc = byte_acc << (8-w%8);
                putc(byte_acc,stdout);
                byte_acc = 0;
                bit_num = 0;
            }
        }
    }	

    return(0);
}

--0-23165646-1103569434=:84653--