Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ext/standard/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ public function jitInit(JIT\Context $context): void
$context->registerFunction('realpath', $fn);
}
$double = $context->getTypeFromString('double');
try {
$context->lookupFunction('fabs');
} catch (\Throwable $e) {
$ft = $context->context->functionType($double, false, $double);
$fn = $context->module->addFunction('fabs', $ft);
$context->registerFunction('fabs', $fn);
}
foreach (['ceil', 'floor', 'round', 'sqrt', 'log', 'exp', 'sin', 'cos', 'tan', 'pow', 'fmod'] as $name) {
try {
$context->lookupFunction($name);
Expand Down
100 changes: 100 additions & 0 deletions ext/standard/VmNumberFormat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

/**
* VM-runtime number_format() (C-style locale subset; no PHP userland builtins).
*/

namespace PHPCompiler\ext\standard;

final class VmNumberFormat
{
public static function format(
float $number,
int $decimals = 0,
string $decimalSeparator = '.',
string $thousandsSeparator = ','
): string {
$negative = $number < 0.0;
if ($negative) {
$number = -$number;
}

$pow = 1;
for ($i = 0; $i < $decimals; ++$i) {
$pow *= 10;
}

if ($decimals > 0) {
$rounded = round($number, $decimals);
} else {
$rounded = round($number, 0);
}

$intPart = (int) floor($rounded);
$fracPart = 0;
if ($decimals > 0) {
$fracPart = (int) round(($rounded - (float) $intPart) * $pow);
if ($fracPart >= $pow) {
++$intPart;
$fracPart = 0;
}
}

$intDigits = self::digitsFromInt($intPart);
$result = self::insertThousands($intDigits, $thousandsSeparator);

if ($decimals > 0) {
$fracDigits = self::padLeft(self::digitsFromInt($fracPart), $decimals, '0');
$result .= $decimalSeparator.$fracDigits;
}

if ($negative) {
return '-'.$result;
}

return $result;
}

private static function digitsFromInt(int $value): string
{
if (0 === $value) {
return '0';
}
$digits = '';
while ($value > 0) {
$digits = \chr(48 + ($value % 10)).$digits;
$value = (int) ($value / 10);
}

return $digits;
}

private static function padLeft(string $digits, int $length, string $pad): string
{
while (VmString::byteLength($digits) < $length) {
$digits = $pad.$digits;
}

return $digits;
}

private static function insertThousands(string $digits, string $separator): string
{
$len = VmString::byteLength($digits);
if ($len <= 3 || '' === $separator) {
return $digits;
}
$firstGroup = $len % 3;
if (0 === $firstGroup) {
$firstGroup = 3;
}
$out = VmString::byteSlice($digits, 0, $firstGroup);
for ($i = $firstGroup; $i < $len; $i += 3) {
$out .= $separator.VmString::byteSlice($digits, $i, 3);
}

return $out;
}
}
7 changes: 6 additions & 1 deletion ext/standard/number_format.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ public function execute(Frame $frame): void
}
$thousandsSeparator = $thouVar->toString();
}
$frame->returnVar->string(\number_format($num, $decimals, $decimalSeparator, $thousandsSeparator));
$frame->returnVar->string(VmNumberFormat::format(
$num,
$decimals,
$decimalSeparator,
$thousandsSeparator
));
}

public Context $context;
Expand Down
20 changes: 18 additions & 2 deletions lib/JIT/ArrayBuiltinHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,10 @@ public static function copyInto(Context $context, Value $dest, Value $src): void
);
$context->builder->branch($head);

$merge = BasicBlockHelper::append($context, 'merge_copy_exit');
$context->builder->positionAtEnd($done);
$context->builder->branch($merge);
$context->builder->positionAtEnd($merge);
}

public static function inArray(
Expand All @@ -318,6 +321,7 @@ public static function inArray(
$ht = self::loadHashTable($context, $haystack);
$sizeT = $context->getTypeFromString('size_t');
$zero = $sizeT->constInt(0, false);
$one = $sizeT->constInt(1, false);
$idxSlot = $context->builder->alloca($sizeT, 1, 'in_array_idx');
$context->builder->store($zero, $idxSlot);
$num = $context->builder->call(
Expand Down Expand Up @@ -346,15 +350,27 @@ public static function inArray(
$context->builder->positionAtEnd($body);
$candidate = self::readListElement($context, $ht, $idx, $needle->type);
$match = self::valuesEqual($context, $needle, $candidate, $strict);
$context->builder->branchIf($match, $foundBlock, $head);
$continueBlock = BasicBlockHelper::append($context, 'in_array_continue');
$context->builder->branchIf($match, $foundBlock, $continueBlock);

$context->builder->positionAtEnd($continueBlock);
$context->builder->store(
$context->builder->addNoSignedWrap($idx, $one),
$idxSlot
);
$context->builder->branch($head);

$context->builder->positionAtEnd($foundBlock);
$context->builder->store($context->getTypeFromString('int1')->constInt(1, false), $foundSlot);
$context->builder->branch($done);

$merge = BasicBlockHelper::append($context, 'in_array_merge');
$context->builder->positionAtEnd($done);
$result = $context->builder->load($foundSlot);
$context->builder->branch($merge);
$context->builder->positionAtEnd($merge);

return $context->builder->load($foundSlot);
return $result;
}

private static function listEntryAt(Context $context, Value $ht, Value $index): Value
Expand Down
11 changes: 11 additions & 0 deletions test/compliance/cases/stdlib/number_format_jit.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
stdlib number_format() JIT
--FILE--
<?php
echo number_format(1234.5, 2), "\n";
echo number_format(1000), "\n";
echo number_format(1234.567, 2, '.', ' '), "\n";
--EXPECT--
1,234.50
1,000
1 234.57
Loading