github.com/coyove/nj@v0.0.0-20221110084952-c7f8db1065c3/tests/bench/mandelbrot.txt (about)

     1  limit = 4.0 
     2  maxIter = 50 
     3  rows = 0 
     4  bytesPerRow = 0 
     5  initial_r = 0 
     6  initial_i = 0 
     7  
     8  function renderRow(y0)
     9      local row = "" 
    10      for xByte = 0, bytesPerRow do
    11          local res = 0 
    12          local Ci = initial_i[y0] 
    13  
    14          for i=0,8,2 do
    15              local x = xByte<<3 
    16              local Cr1 = initial_r[x+i] 
    17              local Cr2 = initial_r[x+i+1] 
    18  
    19              local Zr1=Cr1 
    20              local Zi1=Ci 
    21              local Zr2=Cr2 
    22              local Zi2=Ci 
    23  
    24              local b=0 
    25  
    26              for j = 0,maxIter do
    27                  local Tr1 = Zr1*Zr1 
    28                  local Ti1 = Zi1*Zi1 
    29                  Zi1 = (2*Zr1*Zi1)+Ci 
    30                  Zr1 = (Tr1-Ti1)+Cr1 
    31  
    32                  if (Tr1+Ti1>limit) then
    33                      b = b | 2 
    34                      if (b == 3) then break end
    35                  end
    36  
    37                  local Tr2 = Zr2*Zr2 
    38                  local Ti2 = Zi2*Zi2 
    39                  Zi2 = (2*Zr2*Zi2)+Ci 
    40                  Zr2 = Tr2-Ti2+Cr2 
    41  
    42                  if (Tr2+Ti2>limit) then
    43                      b = b | 1 
    44                      if (b == 3) then break end
    45                  end
    46              end
    47              res = (res<<2) | b 
    48          end
    49  
    50          row = row + byte((~res) & 0xFF)
    51          -- print((~res) & 0xFF)
    52      end
    53      return row 
    54  end
    55  
    56  size = 1600
    57  bytesPerRow = size >> 3 
    58  
    59  initial_r = array.make(size) 
    60  initial_i = array.make(size) 
    61  
    62  inv = 2 / size 
    63  for xy =0, size do
    64      i = inv * xy 
    65      initial_r[xy] = i - 1.5 
    66      initial_i[xy] = i - 1.0 
    67  end
    68  
    69  rows = renderRow.map(array.make(size), os.numcpus)
    70  
    71  f = open("1.pbm", "w+")
    72  io.write(f, "P4\n" + str(size) + " " + str(size) + "\n") 
    73  for i = 0, #(rows) do
    74      io.write(f, rows[i]) 
    75  end
    76  f.close()