Home | History | Annotate | Download | only in shootout
      1 /*
      2 Redistribution and use in source and binary forms, with or without
      3 modification, are permitted provided that the following conditions are met:
      4 
      5     * Redistributions of source code must retain the above copyright
      6     notice, this list of conditions and the following disclaimer.
      7 
      8     * Redistributions in binary form must reproduce the above copyright
      9     notice, this list of conditions and the following disclaimer in the
     10     documentation and/or other materials provided with the distribution.
     11 
     12     * Neither the name of "The Computer Language Benchmarks Game" nor the
     13     name of "The Computer Language Shootout Benchmarks" nor the names of
     14     its contributors may be used to endorse or promote products derived
     15     from this software without specific prior written permission.
     16 
     17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27 POSSIBILITY OF SUCH DAMAGE.
     28 */
     29 
     30 /* The Computer Language Shootout
     31    http://shootout.alioth.debian.org/
     32 
     33    contributed by Greg Buchholz
     34 
     35    for the debian (AMD) machine...
     36    compile flags:  -O3 -ffast-math -march=athlon-xp -funroll-loops
     37 
     38    for the gp4 (Intel) machine...
     39    compile flags:  -O3 -ffast-math -march=pentium4 -funroll-loops
     40 */
     41 
     42 #include<stdio.h>
     43 
     44 int main (int argc, char **argv)
     45 {
     46     int w, h, bit_num = 0;
     47     char byte_acc = 0;
     48     int i, iter = 50;
     49     double x, y, limit = 2.0;
     50     double Zr, Zi, Cr, Ci, Tr, Ti;
     51 
     52     w = h = atoi(argv[1]);
     53 
     54     printf("P4\n%d %d\n",w,h);
     55 
     56     for(y=0;y<h;++y)
     57     {
     58         for(x=0;x<w;++x)
     59         {
     60             Zr = Zi = Tr = Ti = 0.0;
     61             Cr = (2.0*x/w - 1.5); Ci=(2.0*y/h - 1.0);
     62 
     63             for (i=0;i<iter && (Tr+Ti <= limit*limit);++i)
     64             {
     65                 Zi = 2.0*Zr*Zi + Ci;
     66                 Zr = Tr - Ti + Cr;
     67                 Tr = Zr * Zr;
     68                 Ti = Zi * Zi;
     69             }
     70 
     71             byte_acc <<= 1;
     72             if(Tr+Ti <= limit*limit) byte_acc |= 0x01;
     73 
     74             ++bit_num;
     75 
     76             if(bit_num == 8)
     77             {
     78                 putc(byte_acc,stdout);
     79                 byte_acc = 0;
     80                 bit_num = 0;
     81             }
     82             else if(x == w-1)
     83             {
     84                 byte_acc <<= (8-w%8);
     85                 putc(byte_acc,stdout);
     86                 byte_acc = 0;
     87                 bit_num = 0;
     88             }
     89         }
     90     }
     91 }
     92