Skip to content
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

バイトオーダーのスワップ #4

Closed
kawasin73 opened this issue Nov 8, 2018 · 1 comment
Closed

バイトオーダーのスワップ #4

kawasin73 opened this issue Nov 8, 2018 · 1 comment
Assignees

Comments

@kawasin73
Copy link
Owner

Little Endian の値を Big Endian に変換する、またはその逆の変換を行う実装の高速な実装を調査する。

@kawasin73 kawasin73 self-assigned this Nov 8, 2018
@kawasin73 kawasin73 mentioned this issue Nov 8, 2018
15 tasks
@kawasin73
Copy link
Owner Author

kawasin73 commented Nov 8, 2018

エンディアンの違いをを吸収するライブラリ(
https://github.com/tsuna/endian )の実装を参考にすると、1番シンプルでポータブルな実装は以下のようになる。

// swapUint64 converts a uint16 to network byte order and back.
func swapUint64(n uint64) uint64 {
	return ((n & 0x00000000000000FF) << 56) |
		((n & 0x000000000000FF00) << 40) |
		((n & 0x0000000000FF0000) << 24) |
		((n & 0x00000000FF000000) << 8) |
		((n & 0x000000FF00000000) >> 8) |
		((n & 0x0000FF0000000000) >> 24) |
		((n & 0x00FF000000000000) >> 40) |
		((n & 0xFF00000000000000) >> 56)
}

https://github.com/tsuna/endian/blob/master/little.go#L21

また、C言語ではコンパイラによっては最適化されたbswap という関数を提供しているがこれはコンパイラ依存となる。

Regarding performance: Well this will reduce portability but MSVC, gcc and clang (same as gcc I think) all have intrinsics defined for bswap which will probably perform better than your implementations. At least on x86 they'll probably make use of the bswap instruction. So you might want to have a few #ifdef'ed alternatives for different compilers.
https://codereview.stackexchange.com/questions/64797/byte-swapping-functions

よって、前者のポータブルな実装を採用する

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

No branches or pull requests

1 participant