[Shootout-list] Mandelbrot Set for CMUCL and SBCL 2nd try

Greg Buchholz sleepingsquirrel@member.fsf.org
Fri, 1 Apr 2005 15:52:24 -0800 (PST)


--- Pascal Obry <pascal@obry.net> wrote:
> Yannick Gingras wrote:
> > As Pascal said, the other benchmarks achieved an output similar to
the
> > C one at the cost of doing it the C way instead of doing it the way
> > they would normally do it.
>
> I think so indeed. Let's take and example based on the Ada bench. The
> current implementation looks like:
> 

[snipped]

> 
> I wanted to introduced Zr2 = Zr ** 2; and Zi2 = Zi ** 2; The code would
> have
> looked something like:
> 
>    for Y in 0 .. Height - 1 loop
>       for X in 0 .. Width - 1 loop
>          Zr := 0.0;
>          Zi := 0.0;
>          Cr := 2.0 * Real (X) / Real (Width) - 1.5;
>          Ci := 2.0 * Real (Y) / Real (Height) - 1.0;
> 
>          Limit_Reached := False;
> 
>          for I in 1 .. Iter loop
>             Zr2 := Zr ** 2;
>             Zi2 := Zi ** 2;
>             Tr  := Zr2 - Zi2 + Cr;
>             Ti  := 2.0 * Zr * Zi + Ci;
>             Zr  := Tr;
>             Zi  := Ti;
> 
>             exit when Zr2 + Zi2 > Limit;
>          end loop;
> 
>          if Zr2 + Zi2 > Limit then
>             Byte_Acc := Shift_Left (Byte_Acc, 1) or 16#00#;
>          else
>             Byte_Acc := Shift_Left (Byte_Acc, 1) or 16#01#;
>          end if;
> 
> This is cleaner and faster. But it does not gives the same output (only
> 2 bytes are different).

    You might want to investigate this further then, because I don't
think the problems are caused by not matching the C program's instruction
ordering.  I translated the prefered Ada snippet directly into C, and the
results were exactly the same as before.  (I'd like to have done the test
with Ada, but I'm currently having problems installing the GNU Ada
compiler.  It keeps telling me the runtime system isn't installed
properly.  Go figure).  Here's the translated program...

//Mandelbrot benchmark translated from Pascal Obry's Ada example

#include<stdio.h>
#include<math.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_sqr = 4.0;
    double Zr, Zi, Zr2, Zi2, Cr, Ci, Tr, Ti;
    
    w = atoi(argv[1]);
    h = w;

    printf("P4\n%d %d\n",w,h);
  
  //for Y in 0 .. Height - 1 loop
    for(y=0;y<h;y++) 
    {
      //for X in 0 .. Width - 1 loop
        for(x=0;x<w;x++)
        {
          //Zr := 0.0;
          //Zi := 0.0;
            Zr  = 0.0; 
            Zi  = 0.0;
          //Cr := 2.0 * Real (X) / Real (Width) - 1.5;
          //Ci := 2.0 * Real (Y) / Real (Height) - 1.0;
            Cr  = 2.0 *(double)x / w - 1.5; 
            Ci  = 2.0 *(double)y / h - 1.0;
        
          //for I in 1 .. Iter loop
            for (i=0; i < Iter+1;i++) //Note we had to change the iter
                                      //limit, since the end condition
                                      //changed.
            {
              //Zr2 := Zr ** 2;
              //Zi2 := Zi ** 2;
                Zr2  = pow(Zr,2);
                Zi2  = pow(Zi,2);

              //Tr  := Zr2 - Zi2 + Cr;
              //Ti  := 2.0 * Zr * Zi + Ci;
                Tr   = Zr2 - Zi2 + Cr;
                Ti   = 2.0 * Zr * Zi + Ci;
              //Zr  := Tr;
              //Zi  := Ti;
                Zr   = Tr;
                Zi   = Ti;
    
              //exit when Zr2 + Zi2 > Limit;
                     if ( Zr2 + Zi2 > Limit_sqr)
                            break;
          //end loop;
            }
        
          //if Zr2 + Zi2 > Limit then
          //   Byte_Acc := Shift_Left (Byte_Acc, 1) or 16#00#;
            if(Zr2 + Zi2 > Limit_sqr) 
               Byte_acc  = (Byte_acc << 1) | 0x00;
          //else
          //   Byte_Acc := Shift_Left (Byte_Acc, 1) or 16#01#;
            else
               Byte_acc  = (Byte_acc << 1) | 0x01;
          //end if;
                
            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);
}




__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com