Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $posN = gmp_init(0x0123456789ABCDEF);
- $negN = gmp_init(-0x0123456789ABCDEF);
- /**
- * A function to test the behavior of GMP given various bitfield options
- * @boolean $sign determines whether a positive or negative number is used
- * @boolean $word_size is the size of the word passed to gmp_export
- * @boolean $signifigant_w determine whether w is most significant or least signifigant
- * @boolean $endianness is whether big endian or little endian.
- *
- * @return string the calculated and formatted result of the options
- */
- function testGmpBehavior($sign, $word_size, $signifigant_w, $endianness) {
- global $posN, $negN;
- $num = $sign ? $posN : $negN;
- $sig = $signifigant_w ? GMP_MSW_FIRST : GMP_LSW_FIRST;
- $endian = $endianness ? GMP_LITTLE_ENDIAN : GMP_BIG_ENDIAN;
- $hex = bin2hex(gmp_export($num, $word_size, $sig | $endian));
- $chunked = strtoupper( chunk_split($hex, 2*$word_size, "_") );
- return substr($chunked, -1) === "_" ? substr($chunked, 0, -1) : $chunked;
- }
- echo "Sign,Word Size,Sig,Endian,gmp_export Hex Result", PHP_EOL;
- for ($sign=1; 0 <= $sign; $sign--)
- for ($wSize=1; $wSize <= 8; $wSize++)
- for ($sigW=1; 0 <= $sigW; $sigW--)
- for ($end=1; 0 <= $end; $end--) {
- echo !!$sign ? "+pos" : "-neg", ",";
- echo $wSize, " bytes", ",";
- echo !!$sigW ? "MSW" : "LSW", ",";
- echo !!$end ? "little" : "big", ",";
- echo testGmpBehavior(!!$sign, $wSize, !!$sigW, !!$end);
- echo PHP_EOL;
- }
Add Comment
Please, Sign In to add comment