Optimised nibble array re-ordering (halved loop count)

This commit is contained in:
Dylan K. Taylor 2016-11-20 22:16:35 +00:00
parent 0be8fa4157
commit 2b46794ca2

View File

@ -728,30 +728,25 @@ class GenericChunk implements Chunk{
*/ */
public static final function reorderNibbleArray(string $array) : string{ public static final function reorderNibbleArray(string $array) : string{
$result = str_repeat("\x00", 2048); $result = str_repeat("\x00", 2048);
$i = 0;
for($x = 0; $x < 16; ++$x){ for($x = 0; $x < 16; ++$x){
for($z = 0; $z < 16; ++$z){ for($z = 0; $z < 16; ++$z){
$xz = (($x << 7) | ($z << 3));
$zx = (($z << 3) | ($x >> 1)); $zx = (($z << 3) | ($x >> 1));
for($y = 0; $y < 16; ++$y){ for($y = 0; $y < 8; ++$y){
$inputIndex = (($y << 7) | $zx); $j = (($y << 8) | $zx);
$outputIndex = ($xz | ($y >> 1)); $byte = ord($array{$j});
$current = ord($result{$outputIndex});
$byte = ord($array{$inputIndex});
if(($y & 1) === 0){ if(($x & 1) === 0){
if(($x & 1) === 0){ $current = ($byte & 0x0f);
$current |= ($byte & 0x0f); $byte = ord($array{$j | 0x80});
}else{ $current |= ($byte << 4);
$current |= (($byte >> 4) & 0x0f);
}
}else{ }else{
if(($x & 1) === 0){ $current = ($byte >> 4);
$current |= (($byte << 4) & 0xf0); $byte = ord($array{$j | 0x80});
}else{ $current |= ($byte & 0xf0);
$current |= ($byte & 0xf0);
}
} }
$result{$outputIndex} = chr($current);
$result{$i++} = chr($current);
} }
} }
} }