-
-
Save andrewscaya/f8575024cbc100bb9e2988e6d9fb9fe6 to your computer and use it in GitHub Desktop.
Mandelbrot code demonstrating Recki-CT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @param double $cReal | |
* @param double $cImag | |
* @param int $maxIterations | |
* @return int | |
*/ | |
function mandelbrotIteration($cReal, $cImag, $maxIterations) | |
{ | |
$originalReal = $cReal; | |
$originalImag = $cImag; | |
$ni = 0; | |
while ($ni < $maxIterations && ($cReal * $cReal * $cImag * $cImag < 4.0)) { | |
$tmp = $cReal * $cReal - $cImag * $cImag + $originalReal; | |
$cImag = $cImag * $cReal * 2 + $originalImag; | |
$cReal = $tmp; | |
$ni++; | |
} | |
if ($ni == $maxIterations) { | |
$ni = 0; | |
} | |
return $ni; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set('memory_limit', -1); | |
define('MAX_ITERATIONS', 4096); | |
function mandelbrot($xMin, $xMax, $yMin, $yMax, $xPx, $yPx) { | |
$dx = ($xMax - $xMin) / $xPx; | |
$dy = ($yMax - $yMin) / $yPx; | |
$rows = []; | |
for ($iy = 0; $iy < $yPx; $iy++) { | |
$row = []; | |
for ($ix = 0; $ix < $xPx; $ix += 1) { | |
$cReal = $xMin + $ix * $dx; | |
$cImag = $yMax - $iy * $dy; | |
$originalReal = $cReal; | |
$originalImag = $cImag; | |
$ni = 0; | |
while ($ni < MAX_ITERATIONS && ($cReal * $cReal * $cImag * $cImag < 4.0)) { | |
$tmp = $cReal * $cReal - $cImag * $cImag + $originalReal; | |
$cImag = $cImag * $cReal * 2 + $originalImag; | |
$cReal = $tmp; | |
$ni++; | |
} | |
if ($ni == MAX_ITERATIONS) { | |
$ni = 0; | |
} | |
$row[] = $ni; | |
} | |
$rows[] = $row; | |
} | |
return $rows; | |
} | |
function render($filename, $xPx, $yPx, $data) | |
{ | |
$image = imagecreate($xPx, $yPx); | |
$colours = []; | |
for ($y = 0; $y < $yPx; $y++) { | |
for ($x = 0; $x < $xPx; $x++) { | |
$iterations = (int) $data[$y][$x]; | |
if ($iterations == MAX_ITERATIONS) { | |
$colour = [0, 0, 0]; /* In the set. Assign black. */ | |
} else if ($iterations < 64) { | |
$colour = [$iterations * 2, 0, 0]; /* 0x0000 to 0x007E */ | |
} else if ($iterations < 128) { | |
$colour = [((($iterations - 64) * 128) / 126) + 128, 0, 0]; /* 0x0080 to 0x00C0 */ | |
} else if ($iterations < 256) { | |
$colour = [((($iterations - 128) * 62) / 127) + 193, 0, 0]; /* 0x00C1 to 0x00FF */ | |
} else if ($iterations < 512) { | |
$colour = [255, ((($iterations - 256) * 62) / 255) + 1, 0]; /* 0x01FF to 0x3FFF */ | |
} else if ($iterations < 1024) { | |
$colour = [255, ((($iterations - 512) * 63) / 511) + 64, 0]; /* 0x40FF to 0x7FFF */ | |
} else if ($iterations < 2048) { | |
$colour = [255, ((($iterations - 1024) * 63) / 1023) + 128, 0]; /* 0x80FF to 0xBFFF */ | |
} else if ($iterations < 4096) { | |
$colour = [255, ((($iterations - 2048) * 63) / 2047) + 192, 0]; /* 0xC0FF to 0xFFFF */ | |
} else { | |
$colour = [255, 255, 0]; | |
} | |
$colourString = sprintf("rgb(%d,%d,%d)", $colour[0], $colour[1], $colour[2]); | |
if (isset($colours[$colourString])) { | |
$colour = $colours[$colourString]; | |
} else { | |
$colour = imagecolorallocate($image, $colour[0], $colour[1], $colour[2]); | |
$colours[$colourString] = $colour; | |
} | |
imagesetpixel($image, $x, $y, $colour); | |
} | |
} | |
imagepng($image, $filename); | |
} | |
function main() { | |
$xMin = -2.0; | |
$xMax = 1.0; | |
$yMin = -1.0; | |
$yMax = 1.0; | |
$xPx = 800; | |
$yPx = 512; | |
$start = microtime(true); | |
$data = mandelbrot($xMin, $xMax, $yMin, $yMax, $xPx, $yPx); | |
$mandelTime = microtime(true) - $start; | |
printf("Data took %F\n", $mandelTime); | |
$start = microtime(true); | |
render('mandel.png', $xPx, $yPx, $data); | |
$renderTime = microtime(true) - $start; | |
printf("Render took %F\n", $renderTime); | |
} | |
main(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set('memory_limit', -1); | |
define('MAX_ITERATIONS', 4096); | |
require('vendor/autoload.php'); | |
function mandelbrot($xMin, $xMax, $yMin, $yMax, $xPx, $yPx, $iterationFunc) { | |
$dx = ($xMax - $xMin) / $xPx; | |
$dy = ($yMax - $yMin) / $yPx; | |
$rows = []; | |
for ($iy = 0; $iy < $yPx; $iy++) { | |
$row = []; | |
for ($ix = 0; $ix < $xPx; $ix += 1) { | |
$cReal = $xMin + $ix * $dx; | |
$cImag = $yMax - $iy * $dy; | |
$row[] = $iterationFunc($cReal, $cImag, MAX_ITERATIONS); | |
} | |
$rows[] = $row; | |
} | |
return $rows; | |
} | |
/** | |
* @param double $cReal | |
* @param double $cImag | |
* @param int $maxIterations | |
* @return int | |
*/ | |
function mandelbrotIteration($cReal, $cImag, $maxIterations) | |
{ | |
$originalReal = $cReal; | |
$originalImag = $cImag; | |
$ni = 0; | |
while ($ni < $maxIterations && ($cReal * $cReal * $cImag * $cImag < 4.0)) { | |
$tmp = $cReal * $cReal - $cImag * $cImag + $originalReal; | |
$cImag = $cImag * $cReal * 2 + $originalImag; | |
$cReal = $tmp; | |
$ni++; | |
} | |
if ($ni == $maxIterations) { | |
$ni = 0; | |
} | |
return $ni; | |
} | |
function render($filename, $xPx, $yPx, $data) | |
{ | |
$image = imagecreate($xPx, $yPx); | |
$colours = []; | |
for ($y = 0; $y < $yPx; $y++) { | |
for ($x = 0; $x < $xPx; $x++) { | |
$iterations = (int) $data[$y][$x]; | |
if ($iterations == MAX_ITERATIONS) { | |
$colour = [0, 0, 0]; /* In the set. Assign black. */ | |
} else if ($iterations < 64) { | |
$colour = [$iterations * 2, 0, 0]; /* 0x0000 to 0x007E */ | |
} else if ($iterations < 128) { | |
$colour = [((($iterations - 64) * 128) / 126) + 128, 0, 0]; /* 0x0080 to 0x00C0 */ | |
} else if ($iterations < 256) { | |
$colour = [((($iterations - 128) * 62) / 127) + 193, 0, 0]; /* 0x00C1 to 0x00FF */ | |
} else if ($iterations < 512) { | |
$colour = [255, ((($iterations - 256) * 62) / 255) + 1, 0]; /* 0x01FF to 0x3FFF */ | |
} else if ($iterations < 1024) { | |
$colour = [255, ((($iterations - 512) * 63) / 511) + 64, 0]; /* 0x40FF to 0x7FFF */ | |
} else if ($iterations < 2048) { | |
$colour = [255, ((($iterations - 1024) * 63) / 1023) + 128, 0]; /* 0x80FF to 0xBFFF */ | |
} else if ($iterations < 4096) { | |
$colour = [255, ((($iterations - 2048) * 63) / 2047) + 192, 0]; /* 0xC0FF to 0xFFFF */ | |
} else { | |
$colour = [255, 255, 0]; | |
} | |
$colourString = sprintf("rgb(%d,%d,%d)", $colour[0], $colour[1], $colour[2]); | |
if (isset($colours[$colourString])) { | |
$colour = $colours[$colourString]; | |
} else { | |
$colour = imagecolorallocate($image, $colour[0], $colour[1], $colour[2]); | |
$colours[$colourString] = $colour; | |
} | |
imagesetpixel($image, $x, $y, $colour); | |
} | |
} | |
imagepng($image, $filename); | |
} | |
function main() { | |
$xMin = -2.0; | |
$xMax = 1.0; | |
$yMin = -1.0; | |
$yMax = 1.0; | |
$xPx = 800; | |
$yPx = 512; | |
$start = microtime(true); | |
$iterationFunc = ReckiCT\Jit::jitfu('mandelbrotIteration'); | |
$compileTime = microtime(true) - $start; | |
printf("Compiling took %F\n", $compileTime); | |
$start = microtime(true); | |
$data = mandelbrot($xMin, $xMax, $yMin, $yMax, $xPx, $yPx, $iterationFunc); | |
$mandelTime = microtime(true) - $start; | |
printf("Data took %F\n", $mandelTime); | |
$start = microtime(true); | |
render('mandel.png', $xPx, $yPx, $data); | |
$renderTime = microtime(true) - $start; | |
printf("Render took %F\n", $renderTime); | |
} | |
main(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set('memory_limit', -1); | |
define('MAX_ITERATIONS', 4096); | |
require('vendor/autoload.php'); | |
function mandelbrot($xMin, $xMax, $yMin, $yMax, $xPx, $yPx, $iterationFunc) { | |
$dx = ($xMax - $xMin) / $xPx; | |
$dy = ($yMax - $yMin) / $yPx; | |
$rows = []; | |
for ($iy = 0; $iy < $yPx; $iy++) { | |
$row = []; | |
for ($ix = 0; $ix < $xPx; $ix += 1) { | |
$cReal = $xMin + $ix * $dx; | |
$cImag = $yMax - $iy * $dy; | |
$row[] = $iterationFunc($cReal, $cImag, MAX_ITERATIONS); | |
} | |
$rows[] = $row; | |
} | |
return $rows; | |
} | |
/** | |
* @param double $cReal | |
* @param double $cImag | |
* @param int $maxIterations | |
* @return int | |
*/ | |
function mandelbrotIteration($cReal, $cImag, $maxIterations) | |
{ | |
$originalReal = $cReal; | |
$originalImag = $cImag; | |
$ni = 0; | |
while ($ni < $maxIterations && ($cReal * $cReal * $cImag * $cImag < 4.0)) { | |
$tmp = $cReal * $cReal - $cImag * $cImag + $originalReal; | |
$cImag = $cImag * $cReal * 2 + $originalImag; | |
$cReal = $tmp; | |
$ni++; | |
} | |
if ($ni == $maxIterations) { | |
$ni = 0; | |
} | |
return $ni; | |
} | |
function render($filename, $xPx, $yPx, $data) | |
{ | |
$image = imagecreate($xPx, $yPx); | |
$colours = []; | |
for ($y = 0; $y < $yPx; $y++) { | |
for ($x = 0; $x < $xPx; $x++) { | |
$iterations = (int) $data[$y][$x]; | |
if ($iterations == MAX_ITERATIONS) { | |
$colour = [0, 0, 0]; /* In the set. Assign black. */ | |
} else if ($iterations < 64) { | |
$colour = [$iterations * 2, 0, 0]; /* 0x0000 to 0x007E */ | |
} else if ($iterations < 128) { | |
$colour = [((($iterations - 64) * 128) / 126) + 128, 0, 0]; /* 0x0080 to 0x00C0 */ | |
} else if ($iterations < 256) { | |
$colour = [((($iterations - 128) * 62) / 127) + 193, 0, 0]; /* 0x00C1 to 0x00FF */ | |
} else if ($iterations < 512) { | |
$colour = [255, ((($iterations - 256) * 62) / 255) + 1, 0]; /* 0x01FF to 0x3FFF */ | |
} else if ($iterations < 1024) { | |
$colour = [255, ((($iterations - 512) * 63) / 511) + 64, 0]; /* 0x40FF to 0x7FFF */ | |
} else if ($iterations < 2048) { | |
$colour = [255, ((($iterations - 1024) * 63) / 1023) + 128, 0]; /* 0x80FF to 0xBFFF */ | |
} else if ($iterations < 4096) { | |
$colour = [255, ((($iterations - 2048) * 63) / 2047) + 192, 0]; /* 0xC0FF to 0xFFFF */ | |
} else { | |
$colour = [255, 255, 0]; | |
} | |
$colourString = sprintf("rgb(%d,%d,%d)", $colour[0], $colour[1], $colour[2]); | |
if (isset($colours[$colourString])) { | |
$colour = $colours[$colourString]; | |
} else { | |
$colour = imagecolorallocate($image, $colour[0], $colour[1], $colour[2]); | |
$colours[$colourString] = $colour; | |
} | |
imagesetpixel($image, $x, $y, $colour); | |
} | |
} | |
imagepng($image, $filename); | |
} | |
function main() { | |
$xMin = -2.0; | |
$xMax = 1.0; | |
$yMin = -1.0; | |
$yMax = 1.0; | |
$xPx = 800; | |
$yPx = 512; | |
if (file_exists('mandelbrotIteration.ir')) { | |
$start = microtime(true); | |
$ir = file_get_contents('mandelbrotIteration.ir'); | |
$iterationFunc = ReckiCT\Jit::getInstance()->compileIrJitFu($ir, 'fibo'); | |
$loadTime = microtime(true) - $start; | |
printf("Loading took %F\n", $loadTime); | |
} else { | |
$start = microtime(true); | |
$iterationFunc = ReckiCT\Jit::jitfu('mandelbrotIteration'); | |
$compileTime = microtime(true) - $start; | |
printf("Compiling took %F\n", $compileTime); | |
file_put_contents('mandelbrotIteration.ir', | |
ReckiCT\Jit::getInstance()->getFunctionIr('mandelbrotIteration')); | |
} | |
$start = microtime(true); | |
$data = mandelbrot($xMin, $xMax, $yMin, $yMax, $xPx, $yPx, $iterationFunc); | |
$mandelTime = microtime(true) - $start; | |
printf("Data took %F\n", $mandelTime); | |
$start = microtime(true); | |
render('mandel.png', $xPx, $yPx, $data); | |
$renderTime = microtime(true) - $start; | |
printf("Render took %F\n", $renderTime); | |
} | |
main(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set('memory_limit', -1); | |
define('MAX_ITERATIONS', 4096); | |
require('vendor/autoload.php'); | |
function mandelbrot($xMin, $xMax, $yMin, $yMax, $xPx, $yPx) { | |
$dx = ($xMax - $xMin) / $xPx; | |
$dy = ($yMax - $yMin) / $yPx; | |
$rows = []; | |
for ($iy = 0; $iy < $yPx; $iy++) { | |
$row = []; | |
for ($ix = 0; $ix < $xPx; $ix += 1) { | |
$cReal = $xMin + $ix * $dx; | |
$cImag = $yMax - $iy * $dy; | |
$row[] = mandelbrotIteration($cReal, $cImag, MAX_ITERATIONS); | |
} | |
$rows[] = $row; | |
} | |
return $rows; | |
} | |
if (!function_exists('mandelbrotIteration')) { | |
/** | |
* @param double $cReal | |
* @param double $cImag | |
* @param int $maxIterations | |
* @return int | |
*/ | |
function mandelbrotIteration($cReal, $cImag, $maxIterations) | |
{ | |
$originalReal = $cReal; | |
$originalImag = $cImag; | |
$ni = 0; | |
while ($ni < $maxIterations && ($cReal * $cReal * $cImag * $cImag < 4.0)) { | |
$tmp = $cReal * $cReal - $cImag * $cImag + $originalReal; | |
$cImag = $cImag * $cReal * 2 + $originalImag; | |
$cReal = $tmp; | |
$ni++; | |
} | |
if ($ni == $maxIterations) { | |
$ni = 0; | |
} | |
return $ni; | |
} | |
} | |
function render($filename, $xPx, $yPx, $data) | |
{ | |
$image = imagecreate($xPx, $yPx); | |
$colours = []; | |
for ($y = 0; $y < $yPx; $y++) { | |
for ($x = 0; $x < $xPx; $x++) { | |
$iterations = (int) $data[$y][$x]; | |
if ($iterations == MAX_ITERATIONS) { | |
$colour = [0, 0, 0]; /* In the set. Assign black. */ | |
} else if ($iterations < 64) { | |
$colour = [$iterations * 2, 0, 0]; /* 0x0000 to 0x007E */ | |
} else if ($iterations < 128) { | |
$colour = [((($iterations - 64) * 128) / 126) + 128, 0, 0]; /* 0x0080 to 0x00C0 */ | |
} else if ($iterations < 256) { | |
$colour = [((($iterations - 128) * 62) / 127) + 193, 0, 0]; /* 0x00C1 to 0x00FF */ | |
} else if ($iterations < 512) { | |
$colour = [255, ((($iterations - 256) * 62) / 255) + 1, 0]; /* 0x01FF to 0x3FFF */ | |
} else if ($iterations < 1024) { | |
$colour = [255, ((($iterations - 512) * 63) / 511) + 64, 0]; /* 0x40FF to 0x7FFF */ | |
} else if ($iterations < 2048) { | |
$colour = [255, ((($iterations - 1024) * 63) / 1023) + 128, 0]; /* 0x80FF to 0xBFFF */ | |
} else if ($iterations < 4096) { | |
$colour = [255, ((($iterations - 2048) * 63) / 2047) + 192, 0]; /* 0xC0FF to 0xFFFF */ | |
} else { | |
$colour = [255, 255, 0]; | |
} | |
$colourString = sprintf("rgb(%d,%d,%d)", $colour[0], $colour[1], $colour[2]); | |
if (isset($colours[$colourString])) { | |
$colour = $colours[$colourString]; | |
} else { | |
$colour = imagecolorallocate($image, $colour[0], $colour[1], $colour[2]); | |
$colours[$colourString] = $colour; | |
} | |
imagesetpixel($image, $x, $y, $colour); | |
} | |
} | |
imagepng($image, $filename); | |
} | |
function main() { | |
$xMin = -2.0; | |
$xMax = 1.0; | |
$yMin = -1.0; | |
$yMax = 1.0; | |
$xPx = 800; | |
$yPx = 512; | |
$start = microtime(true); | |
$data = mandelbrot($xMin, $xMax, $yMin, $yMax, $xPx, $yPx); | |
$mandelTime = microtime(true) - $start; | |
printf("Data took %F\n", $mandelTime); | |
$start = microtime(true); | |
render('mandel.png', $xPx, $yPx, $data); | |
$renderTime = microtime(true) - $start; | |
printf("Render took %F\n", $renderTime); | |
} | |
main(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set('memory_limit', -1); | |
define('MAX_ITERATIONS', 4096); | |
require('vendor/autoload.php'); | |
/** | |
* @param double $xMin | |
* @param double $xMax | |
* @param double $yMin | |
* @param double $yMax | |
* @param int $xPx | |
* @param int $yPx | |
* @return array | |
*/ | |
function mandelbrot($xMin, $xMax, $yMin, $yMax, $xPx, $yPx) { | |
$dx = ($xMax - $xMin) / $xPx; | |
$dy = ($yMax - $yMin) / $yPx; | |
$rows = []; | |
for ($iy = 0; $iy < $yPx; $iy++) { | |
$row = []; | |
for ($ix = 0; $ix < $xPx; $ix += 1) { | |
$cReal = $xMin + $ix * $dx; | |
$cImag = $yMax - $iy * $dy; | |
$originalReal = $cReal; | |
$originalImag = $cImag; | |
$ni = 0; | |
while ($ni < MAX_ITERATIONS && ($cReal * $cReal * $cImag * $cImag < 4.0)) { | |
$tmp = $cReal * $cReal - $cImag * $cImag + $originalReal; | |
$cImag = $cImag * $cReal * 2 + $originalImag; | |
$cReal = $tmp; | |
$ni++; | |
} | |
if ($ni == MAX_ITERATIONS) { | |
$ni = 0; | |
} | |
$row[] = $ni; | |
} | |
$rows[] = $row; | |
} | |
return $rows; | |
} | |
function render($filename, $xPx, $yPx, $data) | |
{ | |
$image = imagecreate($xPx, $yPx); | |
$colours = []; | |
for ($y = 0; $y < $yPx; $y++) { | |
for ($x = 0; $x < $xPx; $x++) { | |
$iterations = (int) $data[$y][$x]; | |
if ($iterations == MAX_ITERATIONS) { | |
$colour = [0, 0, 0]; /* In the set. Assign black. */ | |
} else if ($iterations < 64) { | |
$colour = [$iterations * 2, 0, 0]; /* 0x0000 to 0x007E */ | |
} else if ($iterations < 128) { | |
$colour = [((($iterations - 64) * 128) / 126) + 128, 0, 0]; /* 0x0080 to 0x00C0 */ | |
} else if ($iterations < 256) { | |
$colour = [((($iterations - 128) * 62) / 127) + 193, 0, 0]; /* 0x00C1 to 0x00FF */ | |
} else if ($iterations < 512) { | |
$colour = [255, ((($iterations - 256) * 62) / 255) + 1, 0]; /* 0x01FF to 0x3FFF */ | |
} else if ($iterations < 1024) { | |
$colour = [255, ((($iterations - 512) * 63) / 511) + 64, 0]; /* 0x40FF to 0x7FFF */ | |
} else if ($iterations < 2048) { | |
$colour = [255, ((($iterations - 1024) * 63) / 1023) + 128, 0]; /* 0x80FF to 0xBFFF */ | |
} else if ($iterations < 4096) { | |
$colour = [255, ((($iterations - 2048) * 63) / 2047) + 192, 0]; /* 0xC0FF to 0xFFFF */ | |
} else { | |
$colour = [255, 255, 0]; | |
} | |
$colourString = sprintf("rgb(%d,%d,%d)", $colour[0], $colour[1], $colour[2]); | |
if (isset($colours[$colourString])) { | |
$colour = $colours[$colourString]; | |
} else { | |
$colour = imagecolorallocate($image, $colour[0], $colour[1], $colour[2]); | |
$colours[$colourString] = $colour; | |
} | |
imagesetpixel($image, $x, $y, $colour); | |
} | |
} | |
imagepng($image, $filename); | |
} | |
function main() { | |
$xMin = -2.0; | |
$xMax = 1.0; | |
$yMin = -1.0; | |
$yMax = 1.0; | |
$xPx = 800; | |
$yPx = 512; | |
$start = microtime(true); | |
$function = ReckiCT\Jit::jitfu('mandelbrot'); | |
printf("Compiling took %F\n", $mandelTime); | |
$start = microtime(true); | |
$data = $function($xMin, $xMax, $yMin, $yMax, $xPx, $yPx); | |
$mandelTime = microtime(true) - $start; | |
printf("Data took %F\n", $mandelTime); | |
$start = microtime(true); | |
render('mandel.png', $xPx, $yPx, $data); | |
$renderTime = microtime(true) - $start; | |
printf("Render took %F\n", $renderTime); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment