Skip to content

Pack/unpack double/float in Little Endian and Big Endian #1905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 3, 2017
Merged

Pack/unpack double/float in Little Endian and Big Endian #1905

merged 10 commits into from
Jan 3, 2017

Conversation

sskaje
Copy link
Contributor

@sskaje sskaje commented May 12, 2016

The pack/unpack's parameter 'd' and 'f' are machine order in PHP.

The number 236.0 packed to 'd' is: 0000000000806d40.

I was reversing a game save file, which uses 406D800000000000.
Here is how I decode that in PHP.

        public function readDouble()
        {
                $data = substr($this->stream, $this->ptr, 8);
                $this->ptr += 8;

                $data = implode('', array_reverse(str_split($data, 1)));

                $int = unpack('d', $data);
                return (double) ($int[1]);
        }

Since integers are allowed to pack/unpack in both little endian and big endian, I think double/float also need.

In this PR, I choose:
e: little endian double
E: big endian double
g: little endian float
G: big endian float.

BTW, these formats, 'e', 'E', 'g', and 'G' are not taken in Perl.

@bwoebi
Copy link
Member

bwoebi commented May 12, 2016

I think this addition is pretty much non-controversial and may be merged as-is. [i.e. without RFC]

@sskaje Have you noticed the tests failing with: ext/standard/tests/strings/unpack_error.phpt being the failing test? At least here on travis? May you please fix that, thanks!

@sskaje
Copy link
Contributor Author

sskaje commented May 13, 2016

@bwoebi done this time.

@hikari-no-yume
Copy link
Contributor

This feature gap seemed quite conspicuous when I was writing TypedArrays, so I'm glad to see an attempt to fill it.

{
int machine_endian_check = 1;

return ((char *)&machine_endian_check)[0];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could reuse "WORDS_BIGENDIAN" macro.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, do float and integer endianness always match?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they would not match, you could not use bit-tricks on floats with integers... (which we anyway do)

@laruence
Copy link
Member

and it's better also added some notes in UPGRADE, thanks

@sskaje
Copy link
Contributor Author

sskaje commented May 28, 2016

Out for beer, I'll fix those later.

2016年5月28日星期六,Xinchen Hui notifications@github.com 写道:

and it's better also added some notes in UPGRADE, thanks


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#1905 (comment), or mute
the thread
https://github.com/notifications/unsubscribe/AAMwNh0ITstc3vizYjpVd7j1HxTusN18ks5qF_0MgaJpZM4IdEOI
.

sskaje@gmail.com
https://sskaje.me/

@sskaje
Copy link
Contributor Author

sskaje commented Jun 2, 2016

@laruence tests passed on debian jessie x86_64(little endian) and mips32(big endian) + gcc, and osx 10.11 + llvm.

@krakjoe
Copy link
Member

krakjoe commented Jan 3, 2017

I'm not satisfied that this does not have BC implications, since you had to change an existing test.

Can I ask that you start an internals discussion to try to gather more consensus on this change please.

Please link to internals discussion here so it may be tracked.

UPGRADING has merge conflict that should be fixed at the last moment. If consensus emerges from discussion, fix this conflict and ping me directly, I will merge it quickly before more conflicts can arise.

@sskaje
Copy link
Contributor Author

sskaje commented Jan 3, 2017

@krakjoe You mean I have to provide PR for every active branch ?

btw, I'll fix UPGRADING later

@krakjoe
Copy link
Member

krakjoe commented Jan 3, 2017

No, a PR to master is fine, and is all that is possible right now because of BC.

@nikic
Copy link
Member

nikic commented Jan 3, 2017

@krakjoe The existing test only checks the error message is an unknown modifier is used, I don't think there's a BC concern there.

@krakjoe
Copy link
Member

krakjoe commented Jan 3, 2017

Yep, you're right

@sskaje
Copy link
Contributor Author

sskaje commented Jan 3, 2017

UPGRADING changed and conflict resolved.

@krakjoe
Copy link
Member

krakjoe commented Jan 3, 2017

Merged 7267d00

Thanks.

@krakjoe krakjoe closed this Jan 3, 2017
@php-pulls php-pulls merged commit 4935f79 into php:master Jan 3, 2017
@nikic
Copy link
Member

nikic commented Jan 3, 2017

Linkage fixed in b3889d4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants