Skip to content

Commit 600c16a

Browse files
committed
Double対応(?)ただし負の数は上手く挙動しないっぽい
1 parent 2fb203b commit 600c16a

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

PHPJava/Statements/JavaStatement_dstore.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ class JavaStatement_dstore extends JavaStatement {
88
public function execute () {
99

1010
$index = $this->getByteCodeStream()->readUnsignedByte();
11-
$this->setLocalstorage($index, $this->getStack());
11+
$value = $this->getStack();
12+
13+
$this->setLocalstorage($index, BinaryTools::convertDoubleToIEEE754($value));
1214

1315
}
1416

PHPJava/Utils/BinaryTools.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,5 +296,34 @@ public final static function andBits ($value1, $value2, $bytes) {
296296
return base_convert($build, 2, 10);
297297

298298
}
299+
300+
public final static function convertDoubleToIEEE754 ($doubleValue, $rounded = 8) {
301+
302+
$doubleValue = base_convert($doubleValue, 10, 2);
303+
304+
$sign = $doubleValue[0];
305+
$exponent = substr($doubleValue, 1, 10);
306+
$fraction = substr($doubleValue, 11);
307+
308+
// double scale
309+
$scale = 52;
310+
311+
$fractionData = 0;
312+
for ($i = 0; $i < 52; $i++) {
313+
$fractionData = bcadd($fractionData, bcmul($fraction[$i], bcpow(2, -1 * ($i + 1), $scale), $scale), $scale);
314+
}
315+
316+
// calc sign
317+
$operand1 = -1 * $sign;
318+
319+
// calc fraction
320+
$operand2 = bcadd(1, $fractionData, $scale);
321+
322+
// calc exponent and bias(?)
323+
$operand3 = bcpow(2, bindec($exponent), $scale);
324+
325+
return bcmul(-2, bcmul(bcmul($operand1, $operand2, $scale), $operand3, $scale), $rounded);
326+
327+
}
299328

300329
}

0 commit comments

Comments
 (0)