Compare commits

...

135 Commits

Author SHA1 Message Date
a12049ae04 Document floating point typedefs 2019-10-15 03:11:55 +03:00
af83fc2537 Add float_typedef_test 2019-10-15 02:41:11 +03:00
b015a164f8 Add floating point typedefs to arithmetic.hpp; rename double64 to float64 in buffers.hpp 2019-10-15 02:26:58 +03:00
f992e9d3e4 Test floating point typedefs in buffer_test 2019-10-15 02:08:57 +03:00
ac0383f28d Test floating point in endian_arithmetic_test 2019-10-15 02:02:11 +03:00
e6136ddf27 Add floating point buffer typedefs 2019-10-15 01:54:26 +03:00
60a5068070 Fix whitespace 2019-10-12 22:27:04 +03:00
52bbc9147b Update changelog for constexpr endian_reverse 2019-10-12 21:55:11 +03:00
41ee26a8d8 Document convenience load/store functions 2019-10-12 20:25:28 +03:00
4054afda70 Add convenience store functions 2019-10-12 19:21:19 +03:00
22d0d7bcd1 Add convenience load functions 2019-10-12 18:50:59 +03:00
0704c271b1 Test clang-win with the VS2017 image 2019-10-12 02:29:39 +03:00
cd84a98a88 Add VS2019 to Appveyor 2019-10-12 00:48:41 +03:00
33dea996a7 Add constexpr tests for *_to_*, conditional_reverse 2019-10-11 19:57:49 +03:00
704cd4d9f7 Skip endian_reverse_cx_test when using non-constexpr intrinsics 2019-10-11 19:48:11 +03:00
a4bc0f1a44 Make endian_reverse constexpr when possible 2019-10-11 19:43:02 +03:00
3c296ff707 Add test/endian_reverse_cx_test 2019-10-11 19:42:32 +03:00
cc28f5d658 Add BOOST_CONSTEXPR to endian_reverse functions 2019-10-11 18:49:06 +03:00
80d926e700 Add clang-9 to Travis 2019-10-11 15:36:51 +03:00
4ef4d05cdf Update .travis.yml 2019-08-15 15:12:59 +03:00
b816d97235 Add cmake_subdir_test to Travis 2019-05-05 04:48:47 +03:00
96ef03b6cb Update CMakeLists 2019-05-05 04:46:43 +03:00
7555b7801d Update acknowledgments 2019-05-01 20:35:47 +03:00
e68d8f939c static_assert the requirements of endian_load and endian_store 2019-05-01 00:13:22 +03:00
79af9fba53 Update documentation 2019-04-30 23:59:28 +03:00
79c3add907 Support any expanding loads in endian_load 2019-04-30 23:53:39 +03:00
fe18fdbb3b Include endian/conversion.hpp in the store tests instead of the detail header 2019-04-30 21:29:34 +03:00
8df14956d4 Support any truncating store in endian_store 2019-04-30 20:39:17 +03:00
b0cc5e64d3 Update documentation 2019-04-30 05:15:18 +03:00
d40a3ac3f4 Switch the parameter order of endian_store 2019-04-30 05:13:47 +03:00
39eab827cf Update documentation 2019-04-30 04:54:20 +03:00
b7ffb62e09 Documentation fixes 2019-04-29 20:21:23 +03:00
f0f8e398d6 conversion.hpp: remove unused detail functions, static-assert that types are EndianReversible 2019-04-29 01:32:07 +03:00
b29b3dfc10 Move all deprecated names to endian.hpp; add deprecated_test.cpp to test/Jamfile 2019-04-29 00:36:21 +03:00
f2dc045c43 Update documentation 2019-04-28 20:41:55 +03:00
ff59429171 Remove obsolete std_pair.hpp 2019-04-28 16:30:51 +03:00
8200306bf1 More signed overflow avoidance 2019-04-28 06:37:29 +03:00
dad7086b37 Avoid signed integer overflow in endian_arithmetic_test 2019-04-28 04:12:09 +03:00
c59eb627ad Add endian_arithmetic_test 2019-04-28 00:58:51 +03:00
630e09942b Remove endian/detail/cover_operators.hpp 2019-04-28 00:58:29 +03:00
cd8fbbdbcb Remove use of cover_operators in arithmetic.hpp 2019-04-28 00:52:36 +03:00
9efbcd7c7b Simplify endian_arithmetic 2019-04-27 22:46:32 +03:00
43d21860a4 Do not #pragma pack(1) before including headers 2019-04-27 22:37:40 +03:00
15e6923325 Update buffer_test 2019-04-27 22:00:01 +03:00
6b5792647c Simplify endian_buffer specializations 2019-04-27 20:11:41 +03:00
f77a318c37 Remove BOOST_ENDIAN_LOG 2019-04-27 19:28:49 +03:00
8e3eccaa14 Do not include conversion.hpp from endian_load, endian_store.hpp 2019-04-27 19:15:28 +03:00
6047813081 Add endian_store_test 2019-04-27 18:03:43 +03:00
c42fcb09bc endian_load_test: Remove unnecessary #pragma warning 2019-04-27 17:37:02 +03:00
64607e2e2e Use -Wno-sign-compare on g++ 4.4.7 2019-04-27 06:49:39 +03:00
d9e5fb0d41 Add endian_ld_st_roundtrip_test 2019-04-27 02:40:47 +03:00
e58970b167 Remove endian/detail/lightweight_test.hpp 2019-04-27 02:32:24 +03:00
2df48f1427 Add endian_load_test 2019-04-27 02:15:39 +03:00
26b0170a6e Fix typo in conversion.hpp 2019-04-27 01:47:28 +03:00
18d86d3f36 Update conversion.adoc to reflect endian_reverse changes 2019-04-27 01:03:55 +03:00
19e382d25a Cosmetic doc changes 2019-04-27 00:53:19 +03:00
a2025a9320 Refactor endian_reverse into its own header, make it work for long and long long 2019-04-26 20:24:27 +03:00
e9feff0abb Merge branch 'develop' into feature/endian-load-store 2019-04-26 00:11:02 +03:00
7c35802f6a Switch Appveyor to 2015 image 2019-04-14 18:41:28 +03:00
ac96e4ac24 Remove project-id from doc/Jamfile 2019-03-25 19:23:25 +02:00
c84e015573 Merge branch 'feature/asciidoc' of https://github.com/boostorg/endian into feature/asciidoc 2019-03-25 15:47:47 +02:00
f4f3c433dd Fix index.html 2019-03-25 15:45:52 +02:00
cdfe2eb3cb Delete original .html files 2019-03-25 15:45:34 +02:00
59b4062507 Update index.html to point to new documentation 2019-03-25 09:38:54 -04:00
a6aaec46f5 Minor formatting changes in documentation 2019-03-25 09:32:54 -04:00
9ffe32f740 Convert Endian buffer types to asciidoc 2019-03-25 09:17:42 -04:00
0e349f3d5b Convert Endian Conversion Types to asciidoc 2019-03-24 22:18:41 -04:00
607cc7da73 Convert Endian Conversion Functions to asciidoc 2019-03-24 19:49:20 -04:00
cd17067afb Convert Choosing approach to asiidoc 2019-03-24 16:07:04 -04:00
01278f951d Convert Endian mini review to asciidoc 2019-03-24 14:22:36 -04:00
0427a95459 Convert Endian documentation to asciidoc 2019-03-24 12:30:47 -04:00
8f890ed3c3 Add -Wno-strict-aliasing for g++ 4.4.7 (false positive) 2019-03-20 19:54:15 +02:00
146c1af963 Merge branch 'develop' into feature/endian-load-store 2019-03-20 17:30:05 +02:00
6b65a4b6ed Add endian_reverse_test_ni 2019-03-20 02:25:54 +02:00
7afa3e307b Merge branch 'develop' into feature/endian-reverse-test 2019-03-19 07:16:56 +02:00
eee3e1bc7a Update appveyor.yml 2019-03-19 05:08:57 +02:00
185573f341 Re-enable warnings as errors for endian_reverse_test 2019-03-19 04:22:24 +02:00
01146c9558 Add char and char16_t overloads of endian_reverse 2019-03-19 01:06:53 +02:00
6a84756afb Disable long long tests; disable C4309 on msvc 2019-03-19 00:40:01 +02:00
2d60299b71 Add endian_reverse_test 2019-03-18 21:51:08 +02:00
3c6c06616d Disable warnings-as-errors=on for spirit_conflict_test 2019-03-13 19:00:45 +02:00
2bd479044b Add spirit_conflict_test.cpp (refs #33) 2019-03-13 02:12:48 +02:00
5623e48fb5 Comment out MyInt test, because MyInt is not TriviallyCopyable under C++03 2019-03-13 01:45:16 +02:00
de33887009 Identify and static-assert the requirements of endian_load/endian_store 2019-03-13 01:44:29 +02:00
95178e3cfb Add endian_store, use it in buffers.hpp 2019-03-12 20:21:11 +02:00
019fb85ea9 Add endian_load, use it in buffers.hpp 2019-03-12 19:42:35 +02:00
a564b97dab Avoid unreachable code warning in endian_in_union_test 2019-02-19 02:43:07 +02:00
e5a1b739c2 Update Jamfile handling of warnings 2019-02-19 01:50:23 +02:00
e08864d648 Fix msvc warnings 2019-02-19 01:13:31 +02:00
e6266a7a60 Move <warnings-as-errors>on to default-build 2019-02-19 01:05:40 +02:00
c6f6c45bb8 Add warnings-as-errors=on to .yml files 2019-02-19 00:47:34 +02:00
28615311d9 Use boost_add_subdir 2019-01-07 00:14:49 +02:00
2b91e9e911 Merge branch 'feature/quick-test' into feature/min-cmake 2019-01-06 20:33:00 +02:00
533c82fa92 Add test/quick.cpp 2019-01-06 20:32:19 +02:00
ebb282fbe0 Add CMakeLists.txt, test/cmake_subdir_test 2019-01-06 20:23:43 +02:00
a3f1d70453 Remove essentials from yml files 2018-12-22 05:13:20 +02:00
8aef2d08af Update .yml files 2018-12-18 21:46:36 +02:00
dafb9b1795 Use unsigned char in store_{big,little} 2018-12-06 20:44:42 +02:00
96c2e6dacb Cosmetic fixes 2018-12-06 20:34:15 +02:00
1b589e28d0 Add check_udt_le() to endian_test.cpp; make MyInt default-constructible 2018-12-06 20:33:42 +02:00
9f67f19d35 Merge branch 'unaligned-big-endian' of https://github.com/wijagels/endian into feature/pr-31 2018-12-06 19:57:58 +02:00
de4aadcb11 Use temporary variables to hold the result of BOOST_ENDIAN_INTRINSIC_* 2018-12-06 18:48:43 +02:00
84273c7139 Add BOOST_ENDIAN_NO_INTRINSICS tests to test/Jamfile 2018-12-06 18:02:52 +02:00
9cbd708ce5 Rename uint*_t types to avoid conflict 2018-12-06 18:02:23 +02:00
59126b39ae Add intrinsic_test.cpp to test/Jamfile 2018-12-06 17:51:38 +02:00
bde5937912 A header named _pop should presumably pop instead of push 2018-09-15 03:40:33 +03:00
e5753d13e2 Remove unused header 2018-09-15 03:38:52 +03:00
2837f3b9c5 g++ 4.5 and earlier do not support unrestricted unions 2018-09-11 20:18:13 +03:00
7edf9a030a Add libstdc++-4.9-dev to clang-3.6 2018-09-11 20:17:17 +03:00
553792d40c Update .travis.yml 2018-09-11 19:15:02 +03:00
07ec535600 boost/predef/detail/endian_compat.h is deprecated; switch to boost/predef/other/endian.h 2018-09-11 18:54:54 +03:00
327b501169 Take advantage of unaligned load/store for big endian 2018-03-24 20:41:40 -04:00
f6998d2406 Add .gitattributes 2018-01-15 19:37:16 +02:00
e93f6a2270 Fix unaligned loads and stores 2017-12-23 18:40:09 +02:00
62802fee96 Add unaligned test 2017-12-23 17:22:35 +02:00
10877d3667 Remove one more integer overflow 2017-12-23 04:47:54 +02:00
d4738d8598 Avoid undefined behavior due to signed integer overflow 2017-12-23 00:04:34 +02:00
0d5adab378 Refactor boundary values tests 2017-12-22 16:35:21 +02:00
4648d237f0 Merge branch 'add-test-for-boundary-values' of https://github.com/arvidn/endian into develop 2017-12-22 16:28:00 +02:00
134b225378 Shifting a negative number is an -fsanitize=undefined violation, use the unsigned type 2017-12-22 16:24:21 +02:00
674adbd592 add test to expose undefined behaviors endian_buffer when used with signed integers 2017-12-22 00:00:16 +01:00
69f5b950a2 Add -fno-sanitize-recover=undefined to Travis 2017-12-21 19:28:20 +02:00
34542134be Merge pull request #23 from arvidn/remove-trailing-whitespace
[cleanup] remove trailing whitespace from source code files
2017-12-21 17:42:03 +02:00
9c0e938c3c Add a macOs UBsan configuration to Travis 2017-12-21 17:19:59 +02:00
4bfe758320 Add -d0 to b2 headers on Appveyor 2017-12-21 17:19:09 +02:00
b5a79243a2 Add -fsanitize=undefined to Travis 2017-12-21 03:49:45 +02:00
b647051422 Add a construction/assignment/value test for buffer types 2017-12-21 03:32:38 +02:00
c6aac76029 Add .travis.yml, appveyor.yml 2017-12-20 19:38:59 +02:00
af744e9485 As presented in ABQ 2017-11-10 15:27:21 -07:00
4b4123a0fa [cleanup] remove trailing whitespace from source code files 2017-11-04 13:07:54 +01:00
a2b044f181 P0803r0 2017-10-16 07:14:41 -04:00
ac7a8e1696 P0803R0 2017-10-16 07:12:29 -04:00
d89e6ddc62 Initial commit 2017-10-15 10:03:37 -04:00
739bc40bfc Update tools version 2017-10-15 10:02:13 -04:00
754a4144ec Clear signed/unsigned warnings 2016-08-09 16:49:20 -04:00
85 changed files with 7394 additions and 4708 deletions

96
.gitattributes vendored Normal file
View File

@ -0,0 +1,96 @@
* text=auto !eol svneol=native#text/plain
*.gitattributes text svneol=native#text/plain
# Scriptish formats
*.bat text svneol=native#text/plain
*.bsh text svneol=native#text/x-beanshell
*.cgi text svneol=native#text/plain
*.cmd text svneol=native#text/plain
*.js text svneol=native#text/javascript
*.php text svneol=native#text/x-php
*.pl text svneol=native#text/x-perl
*.pm text svneol=native#text/x-perl
*.py text svneol=native#text/x-python
*.sh eol=lf svneol=LF#text/x-sh
configure eol=lf svneol=LF#text/x-sh
# Image formats
*.bmp binary svneol=unset#image/bmp
*.gif binary svneol=unset#image/gif
*.ico binary svneol=unset#image/ico
*.jpeg binary svneol=unset#image/jpeg
*.jpg binary svneol=unset#image/jpeg
*.png binary svneol=unset#image/png
*.tif binary svneol=unset#image/tiff
*.tiff binary svneol=unset#image/tiff
*.svg text svneol=native#image/svg%2Bxml
# Data formats
*.pdf binary svneol=unset#application/pdf
*.avi binary svneol=unset#video/avi
*.doc binary svneol=unset#application/msword
*.dsp text svneol=crlf#text/plain
*.dsw text svneol=crlf#text/plain
*.eps binary svneol=unset#application/postscript
*.gz binary svneol=unset#application/gzip
*.mov binary svneol=unset#video/quicktime
*.mp3 binary svneol=unset#audio/mpeg
*.ppt binary svneol=unset#application/vnd.ms-powerpoint
*.ps binary svneol=unset#application/postscript
*.psd binary svneol=unset#application/photoshop
*.rdf binary svneol=unset#text/rdf
*.rss text svneol=unset#text/xml
*.rtf binary svneol=unset#text/rtf
*.sln text svneol=native#text/plain
*.swf binary svneol=unset#application/x-shockwave-flash
*.tgz binary svneol=unset#application/gzip
*.vcproj text svneol=native#text/xml
*.vcxproj text svneol=native#text/xml
*.vsprops text svneol=native#text/xml
*.wav binary svneol=unset#audio/wav
*.xls binary svneol=unset#application/vnd.ms-excel
*.zip binary svneol=unset#application/zip
# Text formats
.htaccess text svneol=native#text/plain
*.bbk text svneol=native#text/xml
*.cmake text svneol=native#text/plain
*.css text svneol=native#text/css
*.dtd text svneol=native#text/xml
*.htm text svneol=native#text/html
*.html text svneol=native#text/html
*.ini text svneol=native#text/plain
*.log text svneol=native#text/plain
*.mak text svneol=native#text/plain
*.qbk text svneol=native#text/plain
*.rst text svneol=native#text/plain
*.sql text svneol=native#text/x-sql
*.txt text svneol=native#text/plain
*.xhtml text svneol=native#text/xhtml%2Bxml
*.xml text svneol=native#text/xml
*.xsd text svneol=native#text/xml
*.xsl text svneol=native#text/xml
*.xslt text svneol=native#text/xml
*.xul text svneol=native#text/xul
*.yml text svneol=native#text/plain
boost-no-inspect text svneol=native#text/plain
CHANGES text svneol=native#text/plain
COPYING text svneol=native#text/plain
INSTALL text svneol=native#text/plain
Jamfile text svneol=native#text/plain
Jamroot text svneol=native#text/plain
Jamfile.v2 text svneol=native#text/plain
Jamrules text svneol=native#text/plain
Makefile* text svneol=native#text/plain
README text svneol=native#text/plain
TODO text svneol=native#text/plain
# Code formats
*.c text svneol=native#text/plain
*.cpp text svneol=native#text/plain
*.h text svneol=native#text/plain
*.hpp text svneol=native#text/plain
*.ipp text svneol=native#text/plain
*.tpp text svneol=native#text/plain
*.jam text svneol=native#text/plain
*.java text svneol=native#text/plain

334
.travis.yml Normal file
View File

@ -0,0 +1,334 @@
# Copyright 2016-2018 Peter Dimov
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at http://boost.org/LICENSE_1_0.txt)
language: cpp
sudo: false
dist: trusty
branches:
only:
- master
- develop
- /feature\/.*/
env:
matrix:
- BOGUS_JOB=true
matrix:
exclude:
- env: BOGUS_JOB=true
include:
- os: linux
compiler: g++
env: TOOLSET=gcc COMPILER=g++ CXXSTD=03,11
- os: linux
compiler: g++-4.4
env: TOOLSET=gcc COMPILER=g++-4.4 CXXSTD=98,0x
addons:
apt:
packages:
- g++-4.4
sources:
- ubuntu-toolchain-r-test
- os: linux
compiler: g++-4.6
env: TOOLSET=gcc COMPILER=g++-4.6 CXXSTD=03,0x
addons:
apt:
packages:
- g++-4.6
sources:
- ubuntu-toolchain-r-test
- os: linux
compiler: g++-4.7
env: TOOLSET=gcc COMPILER=g++-4.7 CXXSTD=03,11
addons:
apt:
packages:
- g++-4.7
sources:
- ubuntu-toolchain-r-test
- os: linux
compiler: g++-4.8
env: TOOLSET=gcc COMPILER=g++-4.8 CXXSTD=03,11
addons:
apt:
packages:
- g++-4.8
sources:
- ubuntu-toolchain-r-test
- os: linux
compiler: g++-4.9
env: TOOLSET=gcc COMPILER=g++-4.9 CXXSTD=03,11
addons:
apt:
packages:
- g++-4.9
sources:
- ubuntu-toolchain-r-test
- os: linux
compiler: g++-5
env: TOOLSET=gcc COMPILER=g++-5 CXXSTD=03,11,14,1z
addons:
apt:
packages:
- g++-5
sources:
- ubuntu-toolchain-r-test
- os: linux
compiler: g++-6
env: TOOLSET=gcc COMPILER=g++-6 CXXSTD=03,11,14,1z
addons:
apt:
packages:
- g++-6
sources:
- ubuntu-toolchain-r-test
- os: linux
compiler: g++-7
env: TOOLSET=gcc COMPILER=g++-7 CXXSTD=03,11,14,17
addons:
apt:
packages:
- g++-7
sources:
- ubuntu-toolchain-r-test
- os: linux
compiler: g++-8
env: TOOLSET=gcc COMPILER=g++-8 CXXSTD=03,11,14,17
addons:
apt:
packages:
- g++-8
sources:
- ubuntu-toolchain-r-test
- os: linux
compiler: g++-9
env: TOOLSET=gcc COMPILER=g++-9 CXXSTD=03,11,14,17,2a
addons:
apt:
packages:
- g++-9
sources:
- ubuntu-toolchain-r-test
- os: linux
compiler: g++-9
env: UBSAN=1 TOOLSET=gcc COMPILER=g++-9 CXXSTD=03,11,14,17,2a UBSAN_OPTIONS=print_stacktrace=1 LINKFLAGS=-fuse-ld=gold
addons:
apt:
packages:
- g++-9
sources:
- ubuntu-toolchain-r-test
- os: linux
compiler: clang++
env: TOOLSET=clang COMPILER=clang++ CXXSTD=03,11
- os: linux
compiler: /usr/bin/clang++
env: TOOLSET=clang COMPILER=/usr/bin/clang++ CXXSTD=03,11
addons:
apt:
packages:
- clang-3.3
- os: linux
compiler: /usr/bin/clang++
env: TOOLSET=clang COMPILER=/usr/bin/clang++ CXXSTD=03,11
addons:
apt:
packages:
- clang-3.4
- os: linux
compiler: clang++-3.5
env: TOOLSET=clang COMPILER=clang++-3.5 CXXSTD=03,11,14,1z
addons:
apt:
packages:
- clang-3.5
- libstdc++-4.9-dev
sources:
- ubuntu-toolchain-r-test
- os: linux
compiler: clang++-3.6
env: TOOLSET=clang COMPILER=clang++-3.6 CXXSTD=03,11,14,1z
addons:
apt:
packages:
- clang-3.6
- libstdc++-4.9-dev
sources:
- ubuntu-toolchain-r-test
- os: linux
compiler: clang++-3.8
env: TOOLSET=clang COMPILER=clang++-3.8 CXXSTD=03,11,14,1z
addons:
apt:
packages:
- clang-3.8
- libstdc++-4.9-dev
sources:
- ubuntu-toolchain-r-test
- os: linux
compiler: clang++-3.9
env: TOOLSET=clang COMPILER=clang++-3.9 CXXSTD=03,11,14,1z
addons:
apt:
packages:
- clang-3.9
- libstdc++-4.9-dev
sources:
- ubuntu-toolchain-r-test
- os: linux
compiler: clang++-4.0
env: TOOLSET=clang COMPILER=clang++-4.0 CXXSTD=03,11,14,1z
addons:
apt:
packages:
- clang-4.0
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-trusty-4.0
- os: linux
compiler: clang++-5.0
env: TOOLSET=clang COMPILER=clang++-5.0 CXXSTD=03,11,14,1z
addons:
apt:
packages:
- clang-5.0
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-trusty-5.0
- os: linux
compiler: clang++-6.0
env: TOOLSET=clang COMPILER=clang++-6.0 CXXSTD=03,11,14,17
addons:
apt:
packages:
- clang-6.0
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-trusty-6.0
- os: linux
compiler: clang++-7
env: TOOLSET=clang COMPILER=clang++-7 CXXSTD=03,11,14,17,2a
addons:
apt:
packages:
- clang-7
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-trusty-7
- os: linux
compiler: clang++-8
env: TOOLSET=clang COMPILER=clang++-8 CXXSTD=03,11,14,17,2a
addons:
apt:
packages:
- clang-8
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-trusty-8
- os: linux
dist: xenial
compiler: clang++-9
env: TOOLSET=clang COMPILER=clang++-9 CXXSTD=03,11,14,17,2a
addons:
apt:
packages:
- clang-9
sources:
- ubuntu-toolchain-r-test
- sourceline: 'deb https://apt.llvm.org/xenial/ llvm-toolchain-xenial-9 main'
key_url: 'https://apt.llvm.org/llvm-snapshot.gpg.key'
- os: linux
compiler: clang++-8
env: UBSAN=1 TOOLSET=clang COMPILER=clang++-8 CXXSTD=03,11,14,17,2a UBSAN_OPTIONS=print_stacktrace=1
addons:
apt:
packages:
- clang-8
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-trusty-8
- os: linux
compiler: clang++-libc++
env: TOOLSET=clang COMPILER=clang++-libc++ CXXSTD=03,11,14,1z VARIANT=release
addons:
apt:
packages:
- libc++-dev
- os: linux
compiler: clang++-libc++
env: UBSAN=1 TOOLSET=clang COMPILER=clang++-libc++ CXXSTD=03,11,14,1z UBSAN_OPTIONS=print_stacktrace=1
addons:
apt:
packages:
- libc++-dev
- os: osx
compiler: clang++
env: TOOLSET=clang COMPILER=clang++ CXXSTD=03,11,14,1z
- os: osx
compiler: clang++
env: UBSAN=1 TOOLSET=clang COMPILER=clang++ CXXSTD=03,11,14,1z UBSAN_OPTIONS=print_stacktrace=1
- os: linux
compiler: g++
env: CMAKE_SUBDIR_TEST=1
script:
- cd libs/endian/test/cmake_subdir_test && mkdir __build__ && cd __build__
- cmake ..
- cmake --build .
- cmake --build . --target check
install:
- BOOST_BRANCH=develop && [ "$TRAVIS_BRANCH" == "master" ] && BOOST_BRANCH=master || true
- cd ..
- git clone -b $BOOST_BRANCH --depth 1 https://github.com/boostorg/boost.git boost-root
- cd boost-root
- git submodule update --init tools/boostdep
- cp -r $TRAVIS_BUILD_DIR/* libs/endian
- python tools/boostdep/depinst/depinst.py endian
- ./bootstrap.sh
- ./b2 headers
script:
- |-
echo "using $TOOLSET : : $COMPILER ;" > ~/user-config.jam
- ./b2 -j3 libs/endian/test toolset=$TOOLSET cxxstd=$CXXSTD variant=debug,release ${UBSAN:+cxxflags=-fsanitize=undefined cxxflags=-fno-sanitize-recover=undefined linkflags=-fsanitize=undefined debug-symbols=on} ${LINKFLAGS:+linkflags=$LINKFLAGS}
notifications:
email:
on_success: always

23
CMakeLists.txt Normal file
View File

@ -0,0 +1,23 @@
# Copyright 2019 Peter Dimov
# Distributed under the Boost Software License, Version 1.0.
# See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt
# Partial (add_subdirectory only) and experimental CMake support
# Subject to change; please do not rely on the contents of this file yet
cmake_minimum_required(VERSION 3.5)
project(BoostEndian LANGUAGES CXX)
add_library(boost_endian INTERFACE)
add_library(Boost::endian ALIAS boost_endian)
target_include_directories(boost_endian INTERFACE include)
target_link_libraries(boost_endian
INTERFACE
Boost::config
Boost::core
Boost::predef
Boost::static_assert
Boost::type_traits
)

65
appveyor.yml Normal file
View File

@ -0,0 +1,65 @@
# Copyright 2016-2019 Peter Dimov
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at http://boost.org/LICENSE_1_0.txt)
version: 1.0.{build}-{branch}
shallow_clone: true
branches:
only:
- master
- develop
- /feature\/.*/
environment:
matrix:
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
TOOLSET: msvc-9.0,msvc-10.0,msvc-11.0
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
TOOLSET: msvc-12.0,msvc-14.0
ADDRMD: 32,64
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
TOOLSET: msvc-14.1,clang-win
CXXSTD: 14,17
ADDRMD: 32,64
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
TOOLSET: msvc-14.2
CXXSTD: 14,17
ADDRMD: 32,64
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
ADDPATH: C:\cygwin\bin;
TOOLSET: gcc
CXXSTD: 03,11,14,1z
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
ADDPATH: C:\cygwin64\bin;
TOOLSET: gcc
CXXSTD: 03,11,14,1z
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
ADDPATH: C:\mingw\bin;
TOOLSET: gcc
CXXSTD: 03,11,14,1z
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
ADDPATH: C:\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev1\mingw64\bin;
TOOLSET: gcc
CXXSTD: 03,11,14,1z
install:
- set BOOST_BRANCH=develop
- if "%APPVEYOR_REPO_BRANCH%" == "master" set BOOST_BRANCH=master
- cd ..
- git clone -b %BOOST_BRANCH% --depth 1 https://github.com/boostorg/boost.git boost-root
- cd boost-root
- git submodule update --init tools/boostdep
- xcopy /s /e /q %APPVEYOR_BUILD_FOLDER% libs\endian\
- python tools/boostdep/depinst/depinst.py endian
- cmd /c bootstrap
- b2 -d0 headers
build: off
test_script:
- PATH=%ADDPATH%%PATH%
- if not "%CXXSTD%" == "" set CXXSTD=cxxstd=%CXXSTD%
- if not "%ADDRMD%" == "" set ADDRMD=address-model=%ADDRMD%
- b2 -j3 libs/endian/test toolset=%TOOLSET% %CXXSTD% %ADDRMD% variant=debug,release

2
doc/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/html/
/pdf/

27
doc/Jamfile Normal file
View File

@ -0,0 +1,27 @@
# Copyright 2019 Glen Joseph Fernandes
# (glenjofe@gmail.com)
#
# Distributed under the Boost Software License, Version 1.0.
# (http://www.boost.org/LICENSE_1_0.txt)
import asciidoctor ;
html endian.html : endian.adoc ;
install html_ : endian.html : <location>html ;
pdf endian.pdf : endian.adoc ;
explicit endian.pdf ;
install pdf_ : endian.pdf : <location>pdf ;
explicit pdf_ ;
alias boostdoc ;
explicit boostdoc ;
alias boostrelease : html_ ;
explicit boostrelease ;

View File

@ -1,639 +0,0 @@
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Endian Arithmetic Types</title>
<link href="styles.css" rel="stylesheet">
</style>
</head>
<body>
<table border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
<tr>
<td>
<a href="../../../index.html">
<img src="../../../boost.png" alt="Boost logo" align="middle" border="0" width="277" height="86"></a></td>
<td align="middle">
<b>
<font size="6">Endian Arithmetic Types</font> </b>
</td>
</tr>
</table>
<table border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF" width="100%">
<tr>
<td><b>
<a href="index.html">Endian Home</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="conversion.html">Conversion Functions</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="arithmetic.html">Arithmetic Types</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="buffers.html">Buffer Types</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="choosing_approach.html">Choosing Approach</a></b></td>
</tr>
</table>
<p></p>
<table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" align="right">
<tr>
<td width="100%" bgcolor="#D7EEFF" align="center">
<i><b>Contents</b></i></td>
</tr>
<tr>
<td width="100%" bgcolor="#E8F5FF">
<a href="#Introduction">Introduction</a><br>
<a href="#Example">Example</a><br>
<a href="#Limitations">Limitations</a><br>
<a href="#Feature-set">Feature set</a><br>
<a href="#Types">Enums and typedefs</a><br>
<a href="#Class_template_endian">Class template <code>endian</code></a><br>
&nbsp;&nbsp;&nbsp;
<a href="#Synopsis">Synopsis</a><br>
&nbsp;&nbsp;&nbsp; <a href="#Members">Members</a><br>
&nbsp;&nbsp;&nbsp; <a href="#Stream-inserter">Stream inserter</a><br>
&nbsp;&nbsp;&nbsp; <a href="#Stream-extractor">Stream extractor</a><br>
<a href="#FAQ">FAQ</a><br>
<a href="#Design">Design</a><br>
<a href="#Experience">Experience</a><br>
<a href="#Motivating-use-cases">Motivating use cases</a><br>
<a href="#C++0x">C++11</a><br>
<a href="#Compilation">Compilation</a><br>
<a href="#Acknowledgements">Acknowledgements</a>
</td>
</tr>
</table>
<h2><a name="Introduction">Introduction</a></h2>
<p>Header <a href="arithmetic.html">boost/endian/arithmetic.hpp</a>
provides integer binary types with control over
byte order, value type, size, and alignment. Typedefs provide easy-to-use names
for common configurations.</p>
<p>These types provide portable byte-holders for integer data, independent of
particular computer architectures. Use cases almost always involve I/O, either via files or
network connections. Although data portability is the primary motivation, these
integer byte-holders may
also be used to reduce memory use, file size, or network activity since they
provide binary integer sizes not otherwise available.</p>
<p>Such integer byte-holder types are traditionally called <b><i>
endian</i></b> types. See the
<a href="http://en.wikipedia.org/wiki/Endian" name="endianness">Wikipedia</a> for
a full
exploration of <b><i>endianness</i></b>, including definitions of <i><b>big
endian</b></i> and <i><b>little endian</b></i>.</p>
<p>Boost endian integers provide the same full set of C++ assignment,
arithmetic, and relational operators&nbsp;as C++ standard integral types, with
the standard semantics.</p>
<p>Unary arithmetic operators are <b> <code><font face="Courier New">+</font></code></b>,
<b> <code>-</code></b>, <b> <code>~</code></b>, <b>
<code>!</code></b>, plus both prefix and postfix <b> <code>--</code></b> and <b> <code>++</code></b>. Binary
arithmetic operators are <b> <code>+</code></b>, <b> <code>+=</code></b>, <b> <code>-</code></b>,
<b> <code>
-=</code></b>, <b> <code>*</code></b>, <b> <code>*=</code></b>, <b> <code>/</code></b>,
<b> <code>/=</code></b>, <b> <code>&amp;</code></b>, <b> <code>&amp;=</code></b>,
<b> <code>|</code></b>, <b> <code>|=</code></b>, <b>
<code>^</code></b>, <b> <code>^=</code></b>, <b> <code>&lt;&lt;</code></b>, <b> <code>&lt;&lt;=</code></b>, <code>
<b>&gt;&gt;</b></code>, and <b>
<code>&gt;&gt;=</code></b>. Binary relational operators are <b> <code>==</code></b>,
<b> <code>!=</code></b>, <b>
<code>&lt;</code></b>, <b> <code>&lt;=</code></b>, <b> <code>&gt;</code></b>,
and <b> <code>&gt;=</code></b>.</p>
<p>Implicit conversion to the underlying value type is provided. An implicit
constructor converting from the underlying value type is provided. </p>
<h2><a name="Example">Example</a></h2>
<p>The <a href="../example/endian_example.cpp">endian_example.cpp</a> program writes a
binary file containing four-byte, big-endian and little-endian integers:</p>
<blockquote>
<pre>#include &lt;iostream&gt;
#include &lt;cstdio&gt;
#include &lt;boost/endian/arithmetic.hpp&gt;
#include &lt;boost/static_assert.hpp&gt;
using namespace boost::endian;
namespace
{
// This is an extract from a very widely used GIS file format.
// Why the designer decided to mix big and little endians in
// the same file is not known. But this is a real-world format
// and users wishing to write low level code manipulating these
// files have to deal with the mixed endianness.
struct header
{
big_int32_t file_code;
big_int32_t file_length;
little_int32_t version;
little_int32_t shape_type;
};
const char* filename = &quot;test.dat&quot;;
}
int main(int, char* [])
{
header h;
BOOST_STATIC_ASSERT(sizeof(h) == 16U); // reality check
h.file_code = 0x01020304;
h.file_length = sizeof(header);
h.version = 1;
h.shape_type = 0x01020304;
// Low-level I/O such as POSIX read/write or &lt;cstdio&gt;
// fread/fwrite is sometimes used for binary file operations
// when ultimate efficiency is important. Such I/O is often
// performed in some C++ wrapper class, but to drive home the
// point that endian integers are often used in fairly
// low-level code that does bulk I/O operations, &lt;cstdio&gt;
// fopen/fwrite is used for I/O in this example.
std::FILE* fi = std::fopen(filename, &quot;wb&quot;); // MUST BE BINARY
if (!fi)
{
std::cout &lt;&lt; &quot;could not open &quot; &lt;&lt; filename &lt;&lt; '\n';
return 1;
}
if (std::fwrite(&amp;h, sizeof(header), 1, fi)!= 1)
{
std::cout &lt;&lt; &quot;write failure for &quot; &lt;&lt; filename &lt;&lt; '\n';
return 1;
}
std::fclose(fi);
std::cout &lt;&lt; &quot;created file &quot; &lt;&lt; filename &lt;&lt; '\n';
return 0;
}
</pre>
</blockquote>
<p>After compiling and executing <a href="../example/endian_example.cpp">endian_example.cpp</a>,
a hex dump of <code>test.dat</code> shows:</p>
<blockquote>
<pre>01020304 00000010 01000000 04030201</pre>
</blockquote>
<p>Notice that the first two 32-bit integers are big endian while the second two
are little endian, even though the machine this was compiled and run on was
little endian.</p>
<h2><a name="Limitations">Limitations</a></h2>
<p>Requires <code>&lt;climits&gt;</code> <code>CHAR_BIT == 8</code>. If <code>CHAR_BIT</code>
is some other value, compilation will result in an <code>#error</code>. This
restriction is in place because the design, implementation, testing, and
documentation has only considered issues related to 8-bit bytes, and there have
been no real-world use cases presented for other sizes.</p>
<p>In C++03, <code>endian_arithmetic</code> does not meet the requirements for POD types
because it has constructors, private data members, and a base class. This means
that common use cases are relying on unspecified behavior in that the C++
Standard does not guarantee memory layout for non-POD types. This has not been a
problem in practice since all known C++ compilers lay out memory as if <code>
endian</code> were a POD type. In C++11, it is possible to specify the
default constructor as trivial, and private data members and base classes no longer disqualify a type from being a POD
type. Thus under C++11, <code>endian_arithmetic</code>
will no longer be relying on unspecified behavior.</p>
<h2><a name="Feature-set">Feature set</a></h2>
<ul>
<li>Big endian| little endian | native endian byte ordering.</li>
<li>Signed | unsigned</li>
<li>Unaligned | aligned</li>
<li>1-8 byte (unaligned) | 1, 2, 4, 8 byte (aligned)</li>
<li>Choice of value type</li>
</ul>
<h2>Enums and t<a name="Types">ypedefs</a></h2>
<p>Two scoped enums are provided:</p>
<blockquote>
<pre>enum class order {big, little, native};
enum class align {no, yes}; </pre>
</blockquote>
<p>One class template is provided:</p>
<blockquote>
<pre>template &lt;order Order, typename T, std::size_t n_bits,
align Align = align::no&gt;
class endian_arithmetic;
</pre>
</blockquote>
<p>Typedefs, such as <code>big_int32_t</code>, provide convenient naming
conventions for common use cases:</p>
<blockquote>
<table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="49%">
<tr>
<td width="18%" align="center"><b><i>Name</i></b></td>
<td width="49%" align="center"><b><i>Alignment</i></b></td>
<td width="10%" align="center"><b><i>Endianness</i></b></td>
<td width="10%" align="center"><b><i>Sign</i></b></td>
<td width="15%" align="center"><b><i>Sizes in bits (n)</i></b></td>
</tr>
<tr>
<td width="18%"><code>big_int</code><b><i>n</i></b><code>_t</code></td>
<td width="49%" align="center"><code>no</code></td>
<td width="10%" align="center"><code>big</code></td>
<td width="10%" align="center">signed</td>
<td width="15%">8,16,24,32,40,48,56,64</td>
</tr>
<tr>
<td width="18%"><code>big_uint</code><i><b>n</b></i><code>_t</code></td>
<td width="49%" align="center"><code>no</code></td>
<td width="10%" align="center"><code>big</code></td>
<td width="10%" align="center">unsigned</td>
<td width="15%">8,16,24,32,40,48,56,64</td>
</tr>
<tr>
<td width="18%"><code>little_int</code><i><b>n</b></i><code>_t</code></td>
<td width="49%" align="center"><code>no</code></td>
<td width="10%" align="center"><code>little</code></td>
<td width="10%" align="center">signed</td>
<td width="15%">8,16,24,32,40,48,56,64</td>
</tr>
<tr>
<td width="18%"><code>little_uint</code><i><b>n</b></i><code>_t</code></td>
<td width="49%" align="center"><code>no</code></td>
<td width="10%" align="center"><code>little</code></td>
<td width="10%" align="center">unsigned</td>
<td width="15%">8,16,24,32,40,48,56,64</td>
</tr>
<tr>
<td width="18%"><code>native_int</code><i><b>n</b></i><code>_t</code></td>
<td width="49%" align="center"><code>no</code></td>
<td width="10%" align="center"><code>native</code></td>
<td width="10%" align="center">signed</td>
<td width="15%">8,16,24,32,40,48,56,64</td>
</tr>
<tr>
<td width="18%"><code>native_uint</code><i><b>n</b></i><code>_t</code></td>
<td width="49%" align="center"><code>no</code></td>
<td width="10%" align="center"><code>native</code></td>
<td width="10%" align="center">unsigned</td>
<td width="15%">8,16,24,32,40,48,56,64</td>
</tr>
<tr>
<td width="18%"><code>big_int</code><i><b>n</b></i><code>_at</code></td>
<td width="49%" align="center"><code>yes</code></td>
<td width="10%" align="center"><code>big</code></td>
<td width="10%" align="center">signed</td>
<td width="15%">8,16,32,64</td>
</tr>
<tr>
<td width="18%"><code>big_uint</code><i><b>n</b></i><code>_at</code></td>
<td width="49%" align="center"><code>yes</code></td>
<td width="10%" align="center"><code>big</code></td>
<td width="10%" align="center">unsigned</td>
<td width="15%">8,16,32,64</td>
</tr>
<tr>
<td width="18%" dir="ltr"><code>little_int</code><i><b>n</b></i><code>_at</code></td>
<td width="49%" align="center" dir="ltr"><code>yes</code></td>
<td width="10%" align="center" dir="ltr"><code>little</code></td>
<td width="10%" align="center" dir="ltr">signed</td>
<td width="15%" dir="ltr">8,16,32,64</td>
</tr>
<tr>
<td width="18%" dir="ltr"><code>little_uint</code><i><b>n</b></i><code>_at</code></td>
<td width="49%" align="center" dir="ltr"><code>yes</code></td>
<td width="10%" align="center" dir="ltr"><code>little</code></td>
<td width="10%" align="center" dir="ltr">unsigned</td>
<td width="15%" dir="ltr">8,16,32,64</td>
</tr>
</table>
</blockquote>
<p>The unaligned types do not cause compilers to insert padding bytes in classes
and structs. This is an important characteristic that can be exploited to minimize wasted space in
memory, files, and network transmissions. </p>
<p><font color="#FF0000"><b><i><span style="background-color: #FFFFFF">Warning:</span></i></b></font><span style="background-color: #FFFFFF">
Code that uses a</span>ligned types is possibly non-portable because alignment
requirements vary between hardware architectures and because alignment may be
affected by compiler switches or pragmas. For example, alignment of an 64-bit
integer may be to a 32-bit boundary on a 32-bit machine. Furthermore, aligned types
are only available on architectures with 8, 16, 32, and 64-bit integer types.</p>
<p><i><b>Recommendation:</b></i> Prefer unaligned arithmetic types.</p>
<p><i><b>Recommendation:</b></i> Protect yourself against alignment ills. For
example:</p>
<blockquote>
<pre>static_assert(sizeof(containing_struct) == 12, &quot;sizeof(containing_struct) is wrong&quot;); </pre>
</blockquote>
<p><b><i>Note:</i></b> <b><i>Note:</i></b> One-byte arithmetic types
have identical layout on all platforms, so they never actually reverse endianness. They are provided to enable generic code, and
to improve code readability and searchability.</p>
<h2><a name="Class_template_endian">Class template <code>endian</code></a><code>_arithmetic</code></h2>
<p>An <code>endian_integer</code> is an integer byte-holder with user-specified <a href="#endianness">
endianness</a>, value type, size, and <a href="#alignment">alignment</a>. The
usual operations on arithmetic types are supplied.</p>
<h3><a name="Synopsis">Synopsis</a></h3>
<pre>#include &lt;boost/endian/conversion.hpp&gt;
#include &lt;boost/endian/buffers.hpp&gt;
namespace boost
{
namespace endian
{
// C++11 features emulated if not available
enum class <a name="alignment">align</a> {no, yes};
template &lt;order Order, class T, std::size_t n_bits,
align Align = align::no&gt;
class endian_arithmetic
: public endian_buffer&lt;Order, T, n_bits, Align&gt;
{
public:
typedef T value_type;
// if BOOST_ENDIAN_FORCE_PODNESS is defined &amp;&amp; C++11 PODs are not
// available then these two constructors will not be present
<a href="#endian">endian_arithmetic</a>() noexcept = default;
<a href="#explicit-endian">endian_arithmetic</a>(T v) noexcept;
endian_arithmetic&amp; <a href="#operator-eq">operator=</a>(T v) noexcept;
<a href="#operator-T">operator value_type</a>() const noexcept;
value_type value() const noexcept; // for exposition; see endian_buffer
const char* <a href="#data">data</a>() const noexcept; // for exposition; see endian_buffer
// arithmetic operations
// note that additional operations are provided by the value_type
value_type operator+(const endian&amp; x) noexcept;
endian&amp; operator+=(endian&amp; x, value_type y) noexcept;
endian&amp; operator-=(endian&amp; x, value_type y) noexcept;
endian&amp; operator*=(endian&amp; x, value_type y) noexcept;
endian&amp; operator/=(endian&amp; x, value_type y) noexcept;
endian&amp; operator%=(endian&amp; x, value_type y) noexcept;
endian&amp; operator&amp;=(endian&amp; x, value_type y) noexcept;
endian&amp; operator|=(endian&amp; x, value_type y) noexcept;
endian&amp; operator^=(endian&amp; x, value_type y) noexcept;
endian&amp; operator&lt;&lt;=(endian&amp; x, value_type y) noexcept;
endian&amp; operator&gt;&gt;=(endian&amp; x, value_type y noexcept;
value_type operator&lt;&lt;(const endian&amp; x, value_type y) noexcept;
value_type operator&gt;&gt;(const endian&amp; x, value_type y) noexcept;
endian&amp; operator++(endian&amp; x) noexcept;
endian&amp; operator--(endian&amp; x) noexcept;
endian operator++(endian&amp; x, int) noexcept;
endian operator--(endian&amp; x, int) noexcept;
// Stream inserter
template &lt;class charT, class traits&gt;
friend std::basic_ostream&lt;charT, traits&gt;&amp;
operator&lt;&lt;(std::basic_ostream&lt;charT, traits&gt;&amp; os, const T&amp; x);
// Stream extractor
template &lt;class charT, class traits&gt;
friend std::basic_istream&lt;charT, traits&gt;&amp;
operator&gt;&gt;(std::basic_istream&lt;charT, traits&gt;&amp; is, T&amp; x);
};
// typedefs
// unaligned big endian signed integer types
typedef endian&lt;order::big, int_least8_t, 8&gt; big_int8_t;
typedef endian&lt;order::big, int_least16_t, 16&gt; big_int16_t;
typedef endian&lt;order::big, int_least32_t, 24&gt; big_int24_t;
typedef endian&lt;order::big, int_least32_t, 32&gt; big_int32_t;
typedef endian&lt;order::big, int_least64_t, 40&gt; big_int40_t;
typedef endian&lt;order::big, int_least64_t, 48&gt; big_int48_t;
typedef endian&lt;order::big, int_least64_t, 56&gt; big_int56_t;
typedef endian&lt;order::big, int_least64_t, 64&gt; big_int64_t;
// unaligned big endian unsigned integer types
typedef endian&lt;order::big, uint_least8_t, 8&gt; big_uint8_t;
typedef endian&lt;order::big, uint_least16_t, 16&gt; big_uint16_t;
typedef endian&lt;order::big, uint_least32_t, 24&gt; big_uint24_t;
typedef endian&lt;order::big, uint_least32_t, 32&gt; big_uint32_t;
typedef endian&lt;order::big, uint_least64_t, 40&gt; big_uint40_t;
typedef endian&lt;order::big, uint_least64_t, 48&gt; big_uint48_t;
typedef endian&lt;order::big, uint_least64_t, 56&gt; big_uint56_t;
typedef endian&lt;order::big, uint_least64_t, 64&gt; big_uint64_t;
// unaligned little endian signed integer types
typedef endian&lt;order::little, int_least8_t, 8&gt; little_int8_t;
typedef endian&lt;order::little, int_least16_t, 16&gt; little_int16_t;
typedef endian&lt;order::little, int_least32_t, 24&gt; little_int24_t;
typedef endian&lt;order::little, int_least32_t, 32&gt; little_int32_t;
typedef endian&lt;order::little, int_least64_t, 40&gt; little_int40_t;
typedef endian&lt;order::little, int_least64_t, 48&gt; little_int48_t;
typedef endian&lt;order::little, int_least64_t, 56&gt; little_int56_t;
typedef endian&lt;order::little, int_least64_t, 64&gt; little_int64_t;
// unaligned little endian unsigned integer types
typedef endian&lt;order::little, uint_least8_t, 8&gt; little_uint8_t;
typedef endian&lt;order::little, uint_least16_t, 16&gt; little_uint16_t;
typedef endian&lt;order::little, uint_least32_t, 24&gt; little_uint24_t;
typedef endian&lt;order::little, uint_least32_t, 32&gt; little_uint32_t;
typedef endian&lt;order::little, uint_least64_t, 40&gt; little_uint40_t;
typedef endian&lt;order::little, uint_least64_t, 48&gt; little_uint48_t;
typedef endian&lt;order::little, uint_least64_t, 56&gt; little_uint56_t;
typedef endian&lt;order::little, uint_least64_t, 64&gt; little_uint64_t;
// unaligned native endian signed integer types
typedef <b><i>implementation-defined</i></b>_int8_t native_int8_t;
typedef <b><i>implementation-defined</i></b>_int16_t native_int16_t;
typedef <b><i>implementation-defined</i></b>_int24_t native_int24_t;
typedef <b><i>implementation-defined</i></b>_int32_t native_int32_t;
typedef <b><i>implementation-defined</i></b>_int40_t native_int40_t;
typedef <b><i>implementation-defined</i></b>_int48_t native_int48_t;
typedef <b><i>implementation-defined</i></b>_int56_t native_int56_t;
typedef <b><i>implementation-defined</i></b>_int64_t native_int64_t;
// unaligned native endian unsigned integer types
typedef <b><i>implementation-defined</i></b>_uint8_t native_uint8_t;
typedef <b><i>implementation-defined</i></b>_uint16_t native_uint16_t;
typedef <b><i>implementation-defined</i></b>_uint24_t native_uint24_t;
typedef <b><i>implementation-defined</i></b>_uint32_t native_uint32_t;
typedef <b><i>implementation-defined</i></b>_uint40_t native_uint40_t;
typedef <b><i>implementation-defined</i></b>_uint48_t native_uint48_t;
typedef <b><i>implementation-defined</i></b>_uint56_t native_uint56_t;
typedef <b><i>implementation-defined</i></b>_uint64_t native_uint64_t;
// aligned big endian signed integer types
typedef endian&lt;order::big, int8_t, 8, align::yes&gt; big_int8_at;
typedef endian&lt;order::big, int16_t, 16, align::yes&gt; big_int16_at;
typedef endian&lt;order::big, int32_t, 32, align::yes&gt; big_int32_at;
typedef endian&lt;order::big, int64_t, 64, align::yes&gt; big_int64_at;
// aligned big endian unsigned integer types
typedef endian&lt;order::big, uint8_t, 8, align::yes&gt; big_uint8_at;
typedef endian&lt;order::big, uint16_t, 16, align::yes&gt; big_uint16_at;
typedef endian&lt;order::big, uint32_t, 32, align::yes&gt; big_uint32_at;
typedef endian&lt;order::big, uint64_t, 64, align::yes&gt; big_uint64_at;
// aligned little endian signed integer types
typedef endian&lt;order::little, int8_t, 8, align::yes&gt; little_int8_at;
typedef endian&lt;order::little, int16_t, 16, align::yes&gt; little_int16_at;
typedef endian&lt;order::little, int32_t, 32, align::yes&gt; little_int32_at;
typedef endian&lt;order::little, int64_t, 64, align::yes&gt; little_int64_at;
// aligned little endian unsigned integer types
typedef endian&lt;order::little, uint8_t, 8, align::yes&gt; little_uint8_at;
typedef endian&lt;order::little, uint16_t, 16, align::yes&gt; little_uint16_at;
typedef endian&lt;order::little, uint32_t, 32, align::yes&gt; little_uint32_at;
typedef endian&lt;order::little, uint64_t, 64, align::yes&gt; little_uint64_at;
// aligned native endian typedefs are not provided because
// &lt;cstdint&gt; types are superior for that use case
} // namespace endian
} // namespace boost</pre>
<p>The <i><b><code>implementation-defined</code></b></i> text above is either
<code>big</code> or <code>little</code> according to the endianness of the
platform.</p>
<h3><a name="Members">Members</a></h3>
<div dir="ltr">
<pre><code><a name="endian">endian</a>() = default; // C++03: endian(){}</code></pre>
</div>
<blockquote>
<p><i>Effects:</i> Constructs an uninitialized object of type <code>endian_arithmetic&lt;E, T, n_bits, A&gt;</code>.</p>
</blockquote>
<pre><code><a name="explicit-endian">endian</a>(T v);</code></pre>
<blockquote>
<p><i>Effects:</i> Constructs an object of type <code>endian_arithmetic&lt;E, T, n_bits, A&gt;</code>.</p>
<p><i>Postcondition:</i> <code>x == v,</code> where <code>x</code> is the
constructed object.</p>
</blockquote>
<pre><code>endian&amp; <a name="operator-eq">operator=</a>(T v);</code></pre>
<blockquote>
<p><i>Postcondition:</i> <code>x == v,</code> where <code>x</code> is the
constructed object.</p>
<p><i>Returns:</i> <code>*this</code>.</p>
</blockquote>
<pre><code><a name="operator-T">operator T</a>() const;</code></pre>
<blockquote>
<p><i>Returns:</i> The current value stored in <code>*this</code>, converted to
<code>value_type</code>.</p>
</blockquote>
<pre><code>const char* <a name="data">data</a>() const;</code></pre>
<blockquote>
<p><i>Returns:</i> A pointer to the first byte of the endian binary value stored
in <code>*this</code>.</p>
</blockquote>
<h3>Other operators</h3>
<p>Other operators on endian objects are forwarded to the equivalent
operator on <code>value_type</code>.</p>
<h3><a name="Stream-inserter">Stream inserter</a></h3>
<pre>template &lt;class charT, class traits&gt;
friend std::basic_ostream&lt;charT, traits&gt;&amp;
operator&lt;&lt;(std::basic_ostream&lt;charT, traits&gt;&amp; os, const T&amp; x);
</pre>
<blockquote>
<p><i>Returns:</i> <code>os &lt;&lt; +x</code>.</p>
</blockquote>
<h3><a name="Stream-extractor">Stream extractor</a></h3>
<pre>template &lt;class charT, class traits&gt;
friend std::basic_istream&lt;charT, traits&gt;&amp;
operator&gt;&gt;(std::basic_istream&lt;charT, traits&gt;&amp; is, T&amp; x);
</pre>
<blockquote>
<p><i>Effects: </i>As if:</p>
<blockquote>
<pre>T i;
if (is &gt;&gt; i)
x = i;
</pre>
</blockquote>
<p><i>Returns: </i><code>is</code><i>.</i></p>
</blockquote>
<h2><a name="FAQ">FAQ</a></h2>
<p>See the <a href="index.html#FAQ">Endian home page</a> FAQ for a library-wide
FAQ.</p>
<p><b>Why not just use Boost.Serialization?</b> Serialization involves a
conversion for every object involved in I/O. Endian integers require no
conversion or copying. They are already in the desired format for binary I/O.
Thus they can be read or written in bulk.</p>
<p><b>Are endian types PODs?</b> Yes for C++11. No for C++03, although several
<a href="#Compilation">macros</a> are available to force PODness in all cases.</p>
<p><b>What are the implications of endian integer types not being PODs with C++03
compilers?</b> They
can't be used in unions. Also, compilers aren't required to align or lay
out storage in portable ways, although this potential problem hasn't prevented
use of Boost.Endian with
real compilers.</p>
<p><b>What good is <i>native </i>endianness?</b> It provides alignment and
size guarantees not available from the built-in types. It eases generic
programming.</p>
<p><b>Why bother with the aligned endian types?</b> Aligned integer operations
may be faster (as much as 10 to 20 times faster) if the endianness and alignment of
the type matches the endianness and alignment requirements of the machine. The code,
however, will be somewhat less portable than with the unaligned types.</p>
<p><b>Why provide the arithmetic operations?</b> Providing a full set of operations reduces program
clutter and makes code both easier to write and to read. Consider
incrementing a variable in a record. It is very convenient to write:</p>
<pre wrap> ++record.foo;</pre>
<p wrap>Rather than:</p>
<pre wrap> int temp(record.foo);
++temp;
record.foo = temp;</pre>
<h2><a name="Design">Design</a> considerations for Boost.Endian types</h2>
<ul>
<li>Must be suitable for I/O - in other words, must be memcpyable.</li>
<li>Must provide exactly the size and internal byte ordering specified.</li>
<li>Must work correctly when the internal integer representation has more bits
that the sum of the bits in the external byte representation. Sign extension
must work correctly when the internal integer representation type has more
bits than the sum of the bits in the external bytes. For example, using
a 64-bit integer internally to represent 40-bit (5 byte) numbers must work for
both positive and negative values.</li>
<li>Must work correctly (including using the same defined external
representation) regardless of whether a compiler treats char as signed or
unsigned.</li>
<li>Unaligned types must not cause compilers to insert padding bytes.</li>
<li>The implementation should supply optimizations with great care. Experience has shown that optimizations of endian
integers often become pessimizations when changing
machines or compilers. Pessimizations can also happen when changing compiler switches,
compiler versions, or CPU models of the same architecture.</li>
</ul>
<h2><a name="Experience">Experience</a></h2>
<p>Classes with similar functionality have been independently developed by
several Boost programmers and used very successful in high-value, high-use
applications for many years. These independently developed endian libraries
often evolved from C libraries that were also widely used. Endian types have proven widely useful across a wide
range of computer architectures and applications.</p>
<h2><a name="Motivating-use-cases">Motivating use cases</a></h2>
<p>Neil Mayhew writes: &quot;I can also provide a meaningful use-case for this
library: reading TrueType font files from disk and processing the contents. The
data format has fixed endianness (big) and has unaligned values in various
places. Using Boost.Endian simplifies and cleans the code wonderfully.&quot;</p>
<h2><a name="C++0x">C++11</a></h2>
<p>The availability of the C++11
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm">
Defaulted Functions</a> feature is detected automatically, and will be used if
present to ensure that objects of <code>class endian_arithmetic</code> are trivial, and
thus PODs.</p>
<h2><a name="Compilation">Compilation</a></h2>
<p>Boost.Endian is implemented entirely within headers, with no need to link to
any Boost object libraries.</p>
<p>Several macros allow user control over features:</p>
<ul>
<li>BOOST_ENDIAN_NO_CTORS causes <code>class endian_arithmetic</code> to have no
constructors. The intended use is for compiling user code that must be
portable between compilers regardless of C++11
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm">
Defaulted Functions</a> support. Use of constructors will always fail, <br>
&nbsp;</li>
<li>BOOST_ENDIAN_FORCE_PODNESS causes BOOST_ENDIAN_NO_CTORS to be defined if
the compiler does not support C++11
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm">
Defaulted Functions</a>. This is ensures that objects of <code>class endian_arithmetic</code>
are PODs, and so can be used in C++03 unions.
In C++11, <code>class endian_arithmetic</code> objects are PODs, even though they have
constructors, so can always be used in unions.</li>
</ul>
<h2><a name="Acknowledgements">Acknowledgements</a></h2>
<p>Original design developed by Darin Adler based on classes developed by Mark
Borgerding. Four original class templates combined into a single <code>endian_arithmetic</code>
class template by Beman Dawes, who put the library together, provided
documentation, added the typedefs, and also added the <code>unrolled_byte_loops</code>
sign partial specialization to correctly extend the sign when cover integer size
differs from endian representation size.</p>
<hr>
<p>Last revised:
<!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->14 October, 2015<!--webbot bot="Timestamp" endspan i-checksum="38874" --></p>
<p>© Copyright Beman Dawes, 2006-2009, 2013</p>
<p>Distributed under the Boost Software License, Version 1.0. See
<a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/ LICENSE_1_0.txt</a></p>
</body>
</html>

View File

@ -1,79 +0,0 @@
Conversion function naming bikeshed
return-by-value modify-argument
------------------ ---------------
reverse_endianness reverse_endianness_in_place
" reverse_endianness_arg
endian_reverse endian_reverse_in_place
" endian_reverse_inplace
" endian_reverse_replace
" endian_reverse_in_situ
" endian_reverse_here
" endian_reverse_this
" endian_reverse_self
" endian_reverse_arg
" endian_reverse_in
reverse reverse_in_place
reverse_endian reverse_endian_in_place
swap_endianness swap_endianness_in_place
swap_endian swap_endian_in_place
endian_swap endian_swap_this
flip_endianness flip_endianness_in_place
flip_endian flip_endian_in_place
endian_flip endian_flip_in_place
reverse_order reverse_order_in_place
Key points:
* The above names are defined in a user namespace as customization points to be found by
ADL, and so cannot depend on the enclosing namespace name to signal readers that they
are related to endianness.
* The above functions are rarely called directly by user code, which is more likely to use
the various conditional functions instead. So explicitness is more important than
brevity.
Conditional names
big_to_native native_to_big little_to_native native_to_little
big_to_host host_to_big
be_to_ne ne_to_be
from_big, to_big
big_to_native big_to_native
native_to_big native_to_big
conditional_reverse runtime_conditional_reverse
conditional_reverse conditional_reverse <------
merriam-webster.com/dictionary
reverse [1] (adjective): opposite or contrary to a previous or normal condition <reverse order>
reverse [2] (verb) : to change (something) to an opposite state or condition
swap (verb) : to give something to someone and receive something in return : to trade or exchange (things)
flip (verb)
: to turn (something) over by throwing it up in the air with a quick movement
: to cause (something) to turn or turn over quickly
: to move (something) with a quick light movement
--------------------------------------------------
Copyright Beman Dawes, 2014
Distributed under the Boost Software License, Version 1.0.
See www.boost.org/LICENSE_1_0.txt

View File

@ -1,600 +0,0 @@
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Endian Buffer Types</title>
<link href="styles.css" rel="stylesheet">
</style>
</head>
<body>
<table border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
<tr>
<td>
<a href="../../../index.html">
<img src="../../../boost.png" alt="Boost logo" align="middle" border="0" width="277" height="86"></a></td>
<td align="middle">
<b>
<font size="6">Endian Buffer Types</font> </b>
</td>
</tr>
</table>
<table border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF" width="100%">
<tr>
<td><b>
<a href="index.html">Endian Home</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="conversion.html">Conversion Functions</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="arithmetic.html">Arithmetic Types</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="buffers.html">Buffer Types</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="choosing_approach.html">Choosing Approach</a></b></td>
</tr>
</table>
<p></p>
<table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" align="right">
<tr>
<td width="100%" bgcolor="#D7EEFF" align="center">
<i><b>Contents</b></i></td>
</tr>
<tr>
<td width="100%" bgcolor="#E8F5FF">
<a href="#Introduction">Introduction</a><br>
<a href="#Example">Example</a><br>
<a href="#Limitations">Limitations</a><br>
<a href="#Feature-set">Feature set</a><br>
<a href="#Types">Enums and typedefs</a><br>
<a href="#Class_template_endian">Class template <code>endian_buffer</code></a><br>
&nbsp;&nbsp;&nbsp;
<a href="#Synopsis">Synopsis</a><br>
&nbsp;&nbsp;&nbsp; <a href="#Members">Members</a><br>
&nbsp;&nbsp;&nbsp; <a href="#Non-member-functions">Non-Members</a><br>
<a href="#FAQ">FAQ</a><br>
<a href="#Design">Design</a><br>
<a href="#C++0x">C++11</a><br>
<a href="#Compilation">Compilation</a></td>
</tr>
</table>
<h2><a name="Introduction">Introduction</a></h2>
<p>The internal byte order of arithmetic types is traditionally called <b><i>endianness</i></b>. See
the
<a href="http://en.wikipedia.org/wiki/Endian" name="endianness">Wikipedia</a> for
a full
exploration of <b><i>endianness</i></b>, including definitions of <i><b>big
endian</b></i> and <i><b>little endian</b></i>.</p>
<p>Header <b><code>boost/endian/buffers.hpp</code></b>
provides <code>endian_buffer</code>, a portable endian integer binary buffer
class template with control over
byte order, value type, size, and alignment independent of the platform&#39;s native
endianness. Typedefs provide easy-to-use names
for common configurations.</p>
<p>Use cases primarily involve data portability, either via files or network
connections, but these byte-holders may
also be used to reduce memory use, file size, or network activity since they
provide binary numeric sizes not otherwise available.</p>
<p dir="ltr">Class <code>endian_buffer</code> is aimed at users who wish
explicit control over when endianness conversions occur. It also serves as the
base class for the <code><a href="arithmetic.html">endian_arithmetic</a></code>
class template, which is aimed at users who wish fully automatic endianness
conversion and direct support for all normal arithmetic operations.</p>
<h2><a name="Example">Example</a></h2>
<p>The <b><code>example/endian_example.cpp</code></b> program writes a
binary file containing four-byte, big-endian and little-endian integers:</p>
<blockquote>
<pre>#include &lt;iostream&gt;
#include &lt;cstdio&gt;
#include &lt;boost/endian/buffers.hpp&gt; // see <a href="#Synopsis">Synopsis</a> below
#include &lt;boost/static_assert.hpp&gt;
using namespace boost::endian;
namespace
{
// This is an extract from a very widely used GIS file format.
// Why the designer decided to mix big and little endians in
// the same file is not known. But this is a real-world format
// and users wishing to write low level code manipulating these
// files have to deal with the mixed endianness.
struct header
{
big_int32_<code>buf_</code>t file_code;
big_int32_<code>buf_</code>t file_length;
little_int32_<code>buf_</code>t version;
little_int32_<code>buf_</code>t shape_type;
};
const char* filename = &quot;test.dat&quot;;
}
int main(int, char* [])
{
header h;
BOOST_STATIC_ASSERT(sizeof(h) == 16U); // reality check
h.file_code = 0x01020304;
h.file_length = sizeof(header);
h.version = 1;
h.shape_type = 0x01020304;
// Low-level I/O such as POSIX read/write or &lt;cstdio&gt;
// fread/fwrite is sometimes used for binary file operations
// when ultimate efficiency is important. Such I/O is often
// performed in some C++ wrapper class, but to drive home the
// point that endian integers are often used in fairly
// low-level code that does bulk I/O operations, &lt;cstdio&gt;
// fopen/fwrite is used for I/O in this example.
std::FILE* fi = std::fopen(filename, &quot;wb&quot;); // MUST BE BINARY
if (!fi)
{
std::cout &lt;&lt; &quot;could not open &quot; &lt;&lt; filename &lt;&lt; '\n';
return 1;
}
if (std::fwrite(&amp;h, sizeof(header), 1, fi)!= 1)
{
std::cout &lt;&lt; &quot;write failure for &quot; &lt;&lt; filename &lt;&lt; '\n';
return 1;
}
std::fclose(fi);
std::cout &lt;&lt; &quot;created file &quot; &lt;&lt; filename &lt;&lt; '\n';
return 0;
}
</pre>
</blockquote>
<p>After compiling and executing <b><code>example/endian_example.cpp</code></b>,
a hex dump of <code>test.dat</code> shows:</p>
<blockquote>
<pre>01020304 00000010 01000000 04030201</pre>
</blockquote>
<p>Notice that the first two 32-bit integers are big endian while the second two
are little endian, even though the machine this was compiled and run on was
little endian.</p>
<h2><a name="Limitations">Limitations</a></h2>
<p>Requires <code>&lt;climits&gt;</code> <code>CHAR_BIT == 8</code>. If <code>CHAR_BIT</code>
is some other value, compilation will result in an <code>#error</code>. This
restriction is in place because the design, implementation, testing, and
documentation has only considered issues related to 8-bit bytes, and there have
been no real-world use cases presented for other sizes.</p>
<p>In C++03, <code>endian_buffer</code> does not meet the requirements for POD types
because it has constructors, private data members, and a base class. This means
that common use cases are relying on unspecified behavior in that the C++
Standard does not guarantee memory layout for non-POD types. This has not been a
problem in practice since all known C++ compilers lay out memory as if <code>
endian</code> were a POD type. In C++11, it is possible to specify the
default constructor as trivial, and private data members and base classes no longer disqualify a type from being a POD
type. Thus under C++11, <code>endian_buffer</code>
will no longer be relying on unspecified behavior.</p>
<h2><a name="Feature-set">Feature set</a></h2>
<ul>
<li>Big endian| little endian | native endian byte ordering.</li>
<li>Signed | unsigned</li>
<li>Unaligned | aligned</li>
<li>1-8 byte (unaligned) | 1, 2, 4, 8 byte (aligned)</li>
<li>Choice of value type</li>
</ul>
<h2>Enums and t<a name="Types">ypedefs</a></h2>
<p>Two scoped enums are provided:</p>
<blockquote>
<pre>enum class order {big, little, native};
enum class align {no, yes}; </pre>
</blockquote>
<p>One class template is provided:</p>
<blockquote>
<pre>template &lt;order Order, typename T, std::size_t Nbits,
align Align = align::no&gt;
class endian_buffer;
</pre>
</blockquote>
<p>Typedefs, such as <code>big_int32_buf_t</code>, provide convenient naming
conventions for common use cases:</p>
<blockquote>
<table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="49%">
<tr>
<td width="18%" align="center"><b><i>Name</i></b></td>
<td width="49%" align="center"><b><i>Alignment</i></b></td>
<td width="10%" align="center"><b><i>Endianness</i></b></td>
<td width="10%" align="center"><b><i>Sign</i></b></td>
<td width="15%" align="center"><b><i>Sizes in bits (n)</i></b></td>
</tr>
<tr>
<td width="18%" dir="ltr"><code>big_int</code><b><i>n</i></b><code>_buf_t</code></td>
<td width="49%" align="center" dir="ltr"><code>no</code></td>
<td width="10%" align="center" dir="ltr"><code>big</code></td>
<td width="10%" align="center" dir="ltr">signed</td>
<td width="15%" dir="ltr">8,16,24,32,40,48,56,64</td>
</tr>
<tr>
<td width="18%" dir="ltr"><code>big_uint</code><i><b>n</b></i><code>_buf_t</code></td>
<td width="49%" align="center" dir="ltr"><code>no</code></td>
<td width="10%" align="center" dir="ltr"><code>big</code></td>
<td width="10%" align="center" dir="ltr">unsigned</td>
<td width="15%" dir="ltr">8,16,24,32,40,48,56,64</td>
</tr>
<tr>
<td width="18%"><code>little_int</code><i><b>n</b></i><code>_buf_t</code></td>
<td width="49%" align="center"><code>no</code></td>
<td width="10%" align="center"><code>little</code></td>
<td width="10%" align="center">signed</td>
<td width="15%">8,16,24,32,40,48,56,64</td>
</tr>
<tr>
<td width="18%"><code>little_uint</code><i><b>n</b></i><code>_buf_t</code></td>
<td width="49%" align="center"><code>no</code></td>
<td width="10%" align="center"><code>little</code></td>
<td width="10%" align="center">unsigned</td>
<td width="15%">8,16,24,32,40,48,56,64</td>
</tr>
<tr>
<td width="18%"><code>native_int</code><i><b>n</b></i><code>_buf_t</code></td>
<td width="49%" align="center"><code>no</code></td>
<td width="10%" align="center"><code>native</code></td>
<td width="10%" align="center">signed</td>
<td width="15%">8,16,24,32,40,48,56,64</td>
</tr>
<tr>
<td width="18%"><code>native_uint</code><i><b>n</b></i><code>_buf_t</code></td>
<td width="49%" align="center"><code>no</code></td>
<td width="10%" align="center"><code>native</code></td>
<td width="10%" align="center">unsigned</td>
<td width="15%">8,16,24,32,40,48,56,64</td>
</tr>
<tr>
<td width="18%"><code>big_int</code><i><b>n</b></i><code>_buf_at</code></td>
<td width="49%" align="center"><code>yes</code></td>
<td width="10%" align="center"><code>big</code></td>
<td width="10%" align="center">signed</td>
<td width="15%">8,16,32,64</td>
</tr>
<tr>
<td width="18%"><code>big_uint</code><i><b>n</b></i><code>_</code><code>buf_at</code></td>
<td width="49%" align="center"><code>yes</code></td>
<td width="10%" align="center"><code>big</code></td>
<td width="10%" align="center">unsigned</td>
<td width="15%">8,16,32,64</td>
</tr>
<tr>
<td width="18%"><code>little_int</code><i><b>n</b></i><code>_</code><code>buf_at</code></td>
<td width="49%" align="center"><code>yes</code></td>
<td width="10%" align="center"><code>little</code></td>
<td width="10%" align="center">signed</td>
<td width="15%">8,16,32,64</td>
</tr>
<tr>
<td width="18%"><code>little_uint</code><i><b>n</b></i><code>_</code><code>buf_at</code></td>
<td width="49%" align="center"><code>yes</code></td>
<td width="10%" align="center"><code>little</code></td>
<td width="10%" align="center">unsigned</td>
<td width="15%">8,16,32,64</td>
</tr>
</table>
</blockquote>
<p>The unaligned types do not cause compilers to insert padding bytes in classes
and structs. This is an important characteristic that can be exploited to minimize wasted space in
memory, files, and network transmissions. </p>
<p><font color="#FF0000"><b><i><span style="background-color: #FFFFFF">Warning:</span></i></b></font><span style="background-color: #FFFFFF">
Code that uses a</span>ligned types is possibly non-portable because alignment
requirements vary between hardware architectures and because alignment may be
affected by compiler switches or pragmas. For example, alignment of an 64-bit
integer may be to a 32-bit boundary on a 32-bit machine and to a 64-bit boundary
on a 64-bit machine. Furthermore, aligned types
are only available on architectures with 8, 16, 32, and 64-bit integer types. </p>
<p><i><b>Recommendation:</b></i> Prefer unaligned buffer types.</p>
<p><i><b>Recommendation:</b></i> Protect yourself against alignment ills. For
example:</p>
<blockquote>
<pre>static_assert(sizeof(containing_struct) == 12, &quot;sizeof(containing_struct) is wrong&quot;); </pre>
</blockquote>
<p><b><i>Note:</i></b> One-byte big and little buffer types
have identical layout on all platforms, so they never actually reverse endianness. They are provided to enable generic code, and
to improve code readability and searchability.</p>
<h2><a name="Class_template_endian">Class template <code>endian</code></a><code>_buffer</code></h2>
<p>An <code>endian_buffer</code> is a byte-holder for arithmetic types with user-specified <a href="#endianness">
endianness</a>, value type, size, and <a href="#alignment">alignment</a>.</p>
<h3><a name="Synopsis">Synopsis</a></h3>
<pre>#include &lt;boost/endian/conversion.hpp
namespace boost
{
namespace endian
{
// C++11 features emulated if not available
enum class <a name="alignment">align</a> {no, yes};
template &lt;order Order, class T, std::size_t Nbits,
align Align = align::no&gt;
class endian_buffer
{
public:
typedef T value_type;
<a href="#endian">endian_buffer</a>() noexcept = default;
explicit <a href="#explicit-endian">endian_buffer</a>(T v) noexcept;
endian_buffer&amp; <a href="#operator-eq">operator=</a>(T v) noexcept;
value_type <a href="#value">value</a>() const noexcept;
const char* <a href="#data">data</a>() const noexcept;
protected:
<b><i>implementaton-defined</i></b> endian_value; // for exposition only
};
// stream inserter
template &lt;class charT, class traits, order Order, class T,
std::size_t n_bits, align Align&gt;
std::basic_ostream&lt;charT, traits&gt;&amp;
<a href="#inserter">operator&lt;&lt;</a>(std::basic_ostream&lt;charT, traits&gt;&amp; os,
const endian_buffer&lt;Order, T, n_bits, Align&gt;&amp; x);
// stream extractor
template &lt;class charT, class traits, order Order, class T,
std::size_t n_bits, align A&gt;
std::basic_istream&lt;charT, traits&gt;&amp;
<a href="#extractor">operator&gt;&gt;</a>(std::basic_istream&lt;charT, traits&gt;&amp; is,
endian_buffer&lt;Order, T, n_bits, Align&gt;&amp; x);
// typedefs
// unaligned big endian signed integer buffers
typedef endian_buffer&lt;order::big, int_least8_t, 8&gt; big_int8_buf_t;
typedef endian_buffer&lt;order::big, int_least16_t, 16&gt; big_int16_buf_t;
typedef endian_buffer&lt;order::big, int_least32_t, 24&gt; big_int24_buf_t;
typedef endian_buffer&lt;order::big, int_least32_t, 32&gt; big_int32_buf_t;
typedef endian_buffer&lt;order::big, int_least64_t, 40&gt; big_int40_buf_t;
typedef endian_buffer&lt;order::big, int_least64_t, 48&gt; big_int48_buf_t;
typedef endian_buffer&lt;order::big, int_least64_t, 56&gt; big_int56_buf_t;
typedef endian_buffer&lt;order::big, int_least64_t, 64&gt; big_int64_buf_t;
// unaligned big endian unsigned integer buffers
typedef endian_buffer&lt;order::big, uint_least8_t, 8&gt; big_uint8_buf_t;
typedef endian_buffer&lt;order::big, uint_least16_t, 16&gt; big_uint16_buf_t;
typedef endian_buffer&lt;order::big, uint_least32_t, 24&gt; big_uint24_buf_t;
typedef endian_buffer&lt;order::big, uint_least32_t, 32&gt; big_uint32_buf_t;
typedef endian_buffer&lt;order::big, uint_least64_t, 40&gt; big_uint40_buf_t;
typedef endian_buffer&lt;order::big, uint_least64_t, 48&gt; big_uint48_buf_t;
typedef endian_buffer&lt;order::big, uint_least64_t, 56&gt; big_uint56_buf_t;
typedef endian_buffer&lt;order::big, uint_least64_t, 64&gt; big_uint64_buf_t;
// unaligned little endian signed integer buffers
typedef endian_buffer&lt;order::little, int_least8_t, 8&gt; little_int8_buf_t;
typedef endian_buffer&lt;order::little, int_least16_t, 16&gt; little_int16_buf_t;
typedef endian_buffer&lt;order::little, int_least32_t, 24&gt; little_int24_buf_t;
typedef endian_buffer&lt;order::little, int_least32_t, 32&gt; little_int32_buf_t;
typedef endian_buffer&lt;order::little, int_least64_t, 40&gt; little_int40_buf_t;
typedef endian_buffer&lt;order::little, int_least64_t, 48&gt; little_int48_buf_t;
typedef endian_buffer&lt;order::little, int_least64_t, 56&gt; little_int56_buf_t;
typedef endian_buffer&lt;order::little, int_least64_t, 64&gt; little_int64_buf_t;
// unaligned little endian unsigned integer buffers
typedef endian_buffer&lt;order::little, uint_least8_t, 8&gt; little_uint8_buf_t;
typedef endian_buffer&lt;order::little, uint_least16_t, 16&gt; little_uint16_buf_t;
typedef endian_buffer&lt;order::little, uint_least32_t, 24&gt; little_uint24_buf_t;
typedef endian_buffer&lt;order::little, uint_least32_t, 32&gt; little_uint32_buf_t;
typedef endian_buffer&lt;order::little, uint_least64_t, 40&gt; little_uint40_buf_t;
typedef endian_buffer&lt;order::little, uint_least64_t, 48&gt; little_uint48_buf_t;
typedef endian_buffer&lt;order::little, uint_least64_t, 56&gt; little_uint56_buf_t;
typedef endian_buffer&lt;order::little, uint_least64_t, 64&gt; little_uint64_buf_t;
// unaligned native endian signed integer types
typedef <b><i>implementation-defined</i></b>_int8_buf_t native_int8_buf_t;
typedef <b><i>implementation-defined</i></b>_int16_buf_t native_int16_buf_t;
typedef <b><i>implementation-defined</i></b>_int24_buf_t native_int24_buf_t;
typedef <b><i>implementation-defined</i></b>_int32_buf_t native_int32_buf_t;
typedef <b><i>implementation-defined</i></b>_int40_buf_t native_int40_buf_t;
typedef <b><i>implementation-defined</i></b>_int48_buf_t native_int48_buf_t;
typedef <b><i>implementation-defined</i></b>_int56_buf_t native_int56_buf_t;
typedef <b><i>implementation-defined</i></b>_int64_buf_t native_int64_buf_t;
// unaligned native endian unsigned integer types
typedef <b><i>implementation-defined</i></b>_uint8_buf_t native_uint8_buf_t;
typedef <b><i>implementation-defined</i></b>_uint16_buf_t native_uint16_buf_t;
typedef <b><i>implementation-defined</i></b>_uint24_buf_t native_uint24_buf_t;
typedef <b><i>implementation-defined</i></b>_uint32_buf_t native_uint32_buf_t;
typedef <b><i>implementation-defined</i></b>_uint40_buf_t native_uint40_buf_t;
typedef <b><i>implementation-defined</i></b>_uint48_buf_t native_uint48_buf_t;
typedef <b><i>implementation-defined</i></b>_uint56_buf_t native_uint56_buf_t;
typedef <b><i>implementation-defined</i></b>_uint64_buf_t native_uint64_buf_t;
// aligned big endian signed integer buffers
typedef endian_buffer&lt;order::big, int8_t, 8, align::yes&gt; big_int8_buf_at;
typedef endian_buffer&lt;order::big, int16_t, 16, align::yes&gt; big_int16_buf_at;
typedef endian_buffer&lt;order::big, int32_t, 32, align::yes&gt; big_int32_buf_at;
typedef endian_buffer&lt;order::big, int64_t, 64, align::yes&gt; big_int64_buf_at;
// aligned big endian unsigned integer buffers
typedef endian_buffer&lt;order::big, uint8_t, 8, align::yes&gt; big_uint8_buf_at;
typedef endian_buffer&lt;order::big, uint16_t, 16, align::yes&gt; big_uint16_buf_at;
typedef endian_buffer&lt;order::big, uint32_t, 32, align::yes&gt; big_uint32_buf_at;
typedef endian_buffer&lt;order::big, uint64_t, 64, align::yes&gt; big_uint64_buf_at;
// aligned little endian signed integer buffers
typedef endian_buffer&lt;order::little, int8_t, 8, align::yes&gt; little_int8_buf_at;
typedef endian_buffer&lt;order::little, int16_t, 16, align::yes&gt; little_int16_buf_at;
typedef endian_buffer&lt;order::little, int32_t, 32, align::yes&gt; little_int32_buf_at;
typedef endian_buffer&lt;order::little, int64_t, 64, align::yes&gt; little_int64_buf_at;
// aligned little endian unsigned integer buffers
typedef endian_buffer&lt;order::little, uint8_t, 8, align::yes&gt; little_uint8_buf_at;
typedef endian_buffer&lt;order::little, uint16_t, 16, align::yes&gt; little_uint16_buf_at;
typedef endian_buffer&lt;order::little, uint32_t, 32, align::yes&gt; little_uint32_buf_at;
typedef endian_buffer&lt;order::little, uint64_t, 64, align::yes&gt; little_uint64_buf_at;
// aligned native endian typedefs are not provided because
// &lt;cstdint&gt; types are superior for this use case
} // namespace endian
} // namespace boost</pre>
<p>The <i><b><code>implementation-defined</code></b></i> text in typedefs above is either
<code>big</code> or <code>little</code> according to the native endianness of the
platform.</p>
<p>The expository data member <code>endian_value</code> stores the current value
of an <code>endian_value</code> object as a sequence of bytes ordered as
specified by the <code>Order</code> template parameter.&nbsp; The <i><b><code>
implementation-defined</code></b></i> type of <code>endian_value</code> is a
type such as <code><span style="font-size: 85%">char[Nbits/CHAR_BIT]</span></code>
or <code><span style="font-size: 85%">T</span></code> that meets the
requirements imposed by the <code>Nbits</code> and <code>Align</code> template
parameters.&nbsp; The <code><span style="font-size: 85%">CHAR_BIT</span></code>
macro is defined in <code><span style="font-size: 85%">&lt;climits&gt;</span></code>.
The only value of <code><span style="font-size: 85%">CHAR_BIT</span></code> that
is required to be supported is 8.</p>
<p>Template parameter <code><span style="font-size: 85%">T</span></code> is
required to be a standard integer type (C++std, 3.9.1) and <code>
<span style="font-size: 85%">sizeof(T)*CHAR_BIT</span></code> is required to be
greater or equal to <span style="font-size: 85%"> <code>Nbits</code>.</span></p>
<h3><a name="Members">Members</a></h3>
<pre><code><a name="endian">endian</a>_buffer() noexcept = default;</code></pre>
<blockquote>
<p><i>Effects:</i> Constructs an uninitialized object of type <code>endian_buffer&lt;Order, T,
Nbits, Align&gt;</code>.</p>
</blockquote>
<pre><code>explicit <a name="explicit-endian">endian</a>_buffer(T v) noexcept;</code></pre>
<blockquote>
<p><i>Effects:</i> Constructs an object of type <code>endian_buffer&lt;Order, T,
Nbits, Align&gt;</code>.</p>
<p><i>Postcondition:</i> <code>value() == v &amp; mask</code>, where <code>mask</code>
is a constant of type <code>value_type</code> with <code>Nbits</code> low-order
bits set to one.</p>
<p><i>Remarks:</i> If <code>Align</code> is <code>align::yes</code> then
endianness conversion, if required, is performed by <code>
boost::endian::endian_reverse</code>.</p>
</blockquote>
<pre><code>endian_buffer&amp; <a name="operator-eq">operator=</a>(T v) noexcept;</code></pre>
<blockquote>
<p><i>Postcondition:</i> <code>value() == v &amp; mask</code>, where <code>mask</code>
is a constant of type <code>value_type</code> with <code>Nbits</code>
low-order bits set to one.</p>
<p><i>Returns:</i> <code>*this</code>.</p>
<p><i>Remarks:</i> If <code>Align</code> is <code>align::yes</code> then
endianness conversion, if required, is performed by <code>
boost::endian::endian_reverse</code>.</p>
</blockquote>
<pre>value_type <a name="value">value</a>()<code> const noexcept;</code></pre>
<blockquote>
<p><i>Returns:</i> <code>endian_value</code>, converted to <code>value_type</code>,
if required, and having the endianness of the native platform.</p>
<p><i>Remarks:</i> If <code>Align</code> is <code>align::yes</code> then
endianness conversion, if required, is performed by <code>
boost::endian::endian_reverse</code>.</p>
</blockquote>
<pre><code>const char* <a name="data">data</a>() const noexcept;</code></pre>
<blockquote>
<p><i>Returns:</i> A pointer to the first byte of <code>endian_value</code>.</p>
</blockquote>
<h3><a name="Non-member-functions">Non-member functions</a></h3>
<pre>template &lt;class charT, class traits, order Order, class T,
std::size_t n_bits, align Align&gt;
std::basic_ostream&lt;charT, traits&gt;&amp; <a name="inserter">operator&lt;&lt;</a>(std::basic_ostream&lt;charT, traits&gt;&amp; os,
const endian_buffer&lt;Order, T, n_bits, Align&gt;&amp; x);
</pre>
<blockquote>
<p><i>Returns:</i> <code>os &lt;&lt; x.value()</code>.</p>
</blockquote>
<pre>template &lt;class charT, class traits, order Order, class T,
std::size_t n_bits, align A&gt;
std::basic_istream&lt;charT, traits&gt;&amp; <a name="extractor">operator&gt;&gt;</a>(std::basic_istream&lt;charT, traits&gt;&amp; is,
endian_buffer&lt;Order, T, n_bits, Align&gt;&amp; x);
</pre>
<blockquote>
<p><i>Effects: </i>As if:</p>
<blockquote>
<pre>T i;
if (is &gt;&gt; i)
x = i;
</pre>
</blockquote>
<p><i>Returns:</i> <code>is</code>.</p>
</blockquote>
<h2><a name="FAQ">FAQ</a></h2>
<p>See the <a href="index.html#FAQ">Endian home page</a> FAQ for a library-wide
FAQ.</p>
<p><b>Why not just use Boost.Serialization?</b> Serialization involves a
conversion for every object involved in I/O. Endian integers require no
conversion or copying. They are already in the desired format for binary I/O.
Thus they can be read or written in bulk.</p>
<p><b>Are endian types PODs?</b> Yes for C++11. No for C++03, although several
<a href="#Compilation">macros</a> are available to force PODness in all cases.</p>
<p><b>What are the implications of endian integer types not being PODs with C++03
compilers?</b> They
can't be used in unions. Also, compilers aren't required to align or lay
out storage in portable ways, although this potential problem hasn't prevented
use of Boost.Endian with
real compilers.</p>
<p><b>What good is <i>native </i>endianness?</b> It provides alignment and
size guarantees not available from the built-in types. It eases generic
programming.</p>
<p><b>Why bother with the aligned endian types?</b> Aligned integer operations
may be faster (as much as 10 to 20 times faster) if the endianness and alignment of
the type matches the endianness and alignment requirements of the machine. The code,
however, is
likely to be somewhat less portable than with the unaligned types.</p>
<h2><a name="Design">Design</a> considerations for Boost.Endian buffers</h2>
<ul>
<li>Must be suitable for I/O - in other words, must be memcpyable.</li>
<li>Must provide exactly the size and internal byte ordering specified.</li>
<li>Must work correctly when the internal integer representation has more bits
that the sum of the bits in the external byte representation. Sign extension
must work correctly when the internal integer representation type has more
bits than the sum of the bits in the external bytes. For example, using
a 64-bit integer internally to represent 40-bit (5 byte) numbers must work for
both positive and negative values.</li>
<li>Must work correctly (including using the same defined external
representation) regardless of whether a compiler treats char as signed or
unsigned.</li>
<li>Unaligned types must not cause compilers to insert padding bytes.</li>
<li>The implementation should supply optimizations with great care. Experience has shown that optimizations of endian
integers often become pessimizations when changing
machines or compilers. Pessimizations can also happen when changing compiler switches,
compiler versions, or CPU models of the same architecture.</li>
</ul>
<h2><a name="C++0x">C++11</a></h2>
<p>The availability of the C++11
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm">
Defaulted Functions</a> feature is detected automatically, and will be used if
present to ensure that objects of <code>class endian_buffer</code> are trivial, and
thus PODs.</p>
<h2><a name="Compilation">Compilation</a></h2>
<p>Boost.Endian is implemented entirely within headers, with no need to link to
any Boost object libraries.</p>
<p>Several macros allow user control over features:</p>
<ul>
<li>BOOST_ENDIAN_NO_CTORS causes <code>class endian_buffer</code> to have no
constructors. The intended use is for compiling user code that must be
portable between compilers regardless of C++11
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm">
Defaulted Functions</a> support. Use of constructors will always fail, <br>
&nbsp;</li>
<li>BOOST_ENDIAN_FORCE_PODNESS causes BOOST_ENDIAN_NO_CTORS to be defined if
the compiler does not support C++11
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm">
Defaulted Functions</a>. This is ensures that objects of <code>class endian_buffer</code>
are PODs, and so can be used in C++03 unions.
In C++11, <code>class endian_buffer</code> objects are PODs, even though they have
constructors, so can always be used in unions.</li>
</ul>
<hr>
<p>Last revised:
<!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->14 October, 2015<!--webbot bot="Timestamp" endspan i-checksum="38874" --></p>
<p>© Copyright Beman Dawes, 2006-2009, 2013</p>
<p>Distributed under the Boost Software License, Version 1.0. See
<a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/ LICENSE_1_0.txt</a></p>
</body>
</html>

View File

@ -1,412 +0,0 @@
<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Choosing Approach</title>
<link href="styles.css" rel="stylesheet">
</head>
<body>
<table border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
<tr>
<td width="339">
<a href="../../../index.html">
<img src="../../../boost.png" alt="Boost logo" align="middle" border="0" width="277" height="86"></a></td>
<td align="middle" width="1253">
<font size="6"><b>Choosing the Approach</b></font></td>
</tr>
</table>
<table border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse"
bordercolor="#111111" bgcolor="#D7EEFF" width="100%">
<tr>
<td><b>
<a href="index.html">Endian Home</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="conversion.html">Conversion Functions</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="arithmetic.html">Arithmetic Types</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="buffers.html">Buffer Types</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="choosing_approach.html">Choosing Approach</a></b></td>
</tr>
</table>
<p></p>
<table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" align="right">
<tr>
<td width="100%" bgcolor="#D7EEFF" align="center">
<i><b>Contents</b></i></td>
</tr>
<tr>
<td width="100%" bgcolor="#E8F5FF">
<a href="#Introduction">Introduction</a><br>
<a href="#Choosing">Choosing between conversion functions,</a><br>
&nbsp; <a href="#Choosing">buffer types, and arithmetic types</a><br>
&nbsp;&nbsp;&nbsp;<a href="#Characteristics">Characteristics</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Endianness-invariants">Endianness invariants</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Conversion-explicitness">Conversion explicitness</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Arithmetic-operations">Arithmetic operations</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Sizes">Sizes</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Alignments">Alignments</a><br>
&nbsp;&nbsp;&nbsp;<a href="#Design-patterns">Design patterns</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#As-needed">Convert only as needed (i.e. lazy)</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Anticipating-need">Convert in anticipation of need</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Convert-generally-as-needed-locally-in-anticipation">Generally
as needed, locally in anticipation</a><br>
&nbsp;&nbsp;&nbsp;<a href="#Use-cases">Use case examples</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Porting-endian-unaware-codebase">Porting endian unaware codebase</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Porting-endian-aware-codebase">Porting endian aware codebase</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Reliability-arithmetic-speed">Reliability and arithmetic-speed</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Reliability-ease-of-use">Reliability and ease-of-use</a></td>
</tr>
</table>
<h2><a name="Introduction">Introduction</a></h2>
<p>Deciding which is the best endianness approach (conversion functions, buffer
types, or arithmetic types) for a particular application involves complex
engineering trade-offs. It is hard to assess those trade-offs without some
understanding of the different interfaces, so you might want to read the
<a href="conversion.html">conversion functions</a>, <a href="buffers.html">
buffer types</a>, and <a href="arithmetic.html">arithmetic types</a> pages
before diving into this page.</p>
<h2><a name="Choosing">Choosing</a> between conversion functions, buffer types,
and arithmetic types</h2>
<p>The best approach to endianness for a particular application depends on the interaction between
the application&#39;s needs and the characteristics of each of the three approaches.</p>
<p><b>Recommendation:</b> If you are new to endianness, uncertain, or don&#39;t want to invest
the time to
study
engineering trade-offs, use <a href="arithmetic.html">endian arithmetic types</a>. They are safe, easy
to use, and easy to maintain. Use the
<a href="#Anticipating-need"> <i>
anticipating need</i></a> design pattern locally around performance hot spots
like lengthy loops, if needed.</p>
<h3><a name="Background">Background</a> </h3>
<p>A dealing with endianness usually implies a program portability or a data
portability requirement, and often both. That means real programs dealing with
endianness are usually complex, so the examples shown here would really be
written as multiple functions spread across multiple translation units. They
would involve interfaces that can not be altered as they are supplied by
third-parties or the standard library. </p>
<h3><a name="Characteristics">Characteristics</a></h3>
<p>The characteristics that differentiate the three approaches to endianness are the endianness
invariants, conversion explicitness, arithmetic operations, sizes available, and
alignment requirements.</p>
<h4><a name="Endianness-invariants">Endianness invariants</a></h4>
<blockquote>
<p><b>Endian conversion functions</b> use objects of the ordinary C++ arithmetic
types like <code>int</code> or <code>unsigned short</code> to hold values. That
breaks the implicit invariant that the C++ language rules apply. The usual
language rules only apply if the endianness of the object is currently set to the native endianness for the platform. That can
make it very hard to reason about logic flow, and result in difficult to
find bugs.</p>
<p>For example:</p>
<blockquote>
<pre>struct data_t // big endian
{
int32_t v1; // description ...
int32_t v2; // description ...
... additional character data members (i.e. non-endian)
int32_t v3; // description ...
};
data_t data;
read(data);
big_to_native_inplace(data.v1);
big_to_native_inplace(data.v2);
...
++v1;
third_party::func(data.v2);
...
native_to_big_inplace(data.v1);
native_to_big_inplace(data.v2);
write(data);
</pre>
<p>The programmer didn&#39;t bother to convert <code>data.v3</code> to native
endianness because that member isn&#39;t used. A later maintainer needs to pass
<code>data.v3</code> to the third-party function, so adds <code>third_party::func(data.v3);</code>
somewhere deep in the code. This causes a silent failure because the usual
invariant that an object of type <code>int32_t</code> holds a value as
described by the C++ core language does not apply.</p>
</blockquote>
<p><b>Endian buffer and arithmetic types</b> hold values internally as arrays of
characters with an invariant that the endianness of the array never changes.
That makes these types easier to use and programs easier to maintain. </p>
<p>Here is the same example, using an endian arithmetic type:</p>
<blockquote>
<pre>struct data_t
{
big_int32_t v1; // description ...
big_int32_t v2; // description ...
... additional character data members (i.e. non-endian)
big_int32_t v3; // description ...
};
data_t data;
read(data);
...
++v1;
third_party::func(data.v2);
...
write(data);
</pre>
<p>A later maintainer can add <code>third_party::func(data.v3)</code>and it
will just-work.</p>
</blockquote>
</blockquote>
<h4><a name="Conversion-explicitness">Conversion explicitness</a></h4>
<blockquote>
<p><b>Endian conversion functions</b> and <b>buffer types</b> never perform
implicit conversions. This gives users explicit control of when conversion
occurs, and may help avoid unnecessary conversions.</p>
<p><b>Endian arithmetic types</b> perform conversion implicitly. That makes
these types very easy to use, but can result in unnecessary conversions. Failure
to hoist conversions out of inner loops can bring a performance penalty.</p>
</blockquote>
<h4><a name="Arithmetic-operations">Arithmetic operations</a></h4>
<blockquote>
<p><b>Endian conversion functions</b> do not supply arithmetic
operations, but this is not a concern since this approach uses ordinary C++
arithmetic types to hold values.</p>
<p><b>Endian buffer types</b> do not supply arithmetic operations. Although this
approach avoids unnecessary conversions, it can result in the introduction of
additional variables and confuse maintenance programmers.</p>
<p><b>Endian</b> <b>arithmetic types</b> do supply arithmetic operations. They
are very easy to use if lots of arithmetic is involved. </p>
</blockquote>
<h4><a name="Sizes">Sizes</a></h4>
<blockquote>
<p><b>Endianness conversion functions</b> only support 1, 2, 4, and 8 byte
integers. That&#39;s sufficient for many applications.</p>
<p><b>Endian buffer and arithmetic types</b> support 1, 2, 3, 4, 5, 6, 7, and 8
byte integers. For an application where memory use or I/O speed is the limiting
factor, using sizes tailored to application needs can be useful.</p>
</blockquote>
<h4><a name="Alignments">Alignments</a></h4>
<blockquote>
<p><b>Endianness conversion functions</b> only support aligned integer and
floating-point types. That&#39;s sufficient for most applications.</p>
<p><b>Endian buffer and arithmetic types</b> support both aligned and unaligned
integer and floating-point types. Unaligned types are rarely needed, but when
needed they are often very useful and workarounds are painful. For example,</p>
<blockquote>
<p>Non-portable code like this:<blockquote>
<pre>struct S {
uint16_t a;&nbsp; // big endian
uint32_t b;&nbsp; // big endian
} __attribute__ ((packed));</pre>
</blockquote>
<p>Can be replaced with portable code like this:</p>
<blockquote>
<pre>struct S {
big_uint16_ut a;
big_uint32_ut b;
};</pre>
</blockquote>
</blockquote>
</blockquote>
<h3><a name="Design-patterns">Design patterns</a></h3>
<p>Applications often traffic in endian data as records or packets containing
multiple endian data elements. For simplicity, we will just call them records.</p>
<p>If desired endianness differs from native endianness, a conversion has to be
performed. When should that conversion occur? Three design patterns have
evolved.</p>
<h4><a name="As-needed">Convert only as needed</a> (i.e. lazy)</h4>
<p>This pattern defers conversion to the point in the code where the data
element is actually used.</p>
<p>This pattern is appropriate when which endian element is actually used varies
greatly according to record content or other circumstances</p>
<h4><a name="Anticipating-need">Convert in anticipation of need</a></h4>
<p>This pattern performs conversion to native endianness in anticipation of use,
such as immediately after reading records. If needed, conversion to the output
endianness is performed after all possible needs have passed, such as just
before writing records.</p>
<p>One implementation of this pattern is to create a proxy record with
endianness converted to native in a read function, and expose only that proxy to
the rest of the implementation. If a write function, if needed, handles the
conversion from native to the desired output endianness.</p>
<p>This pattern is appropriate when all endian elements in a record are
typically used regardless of record content or other circumstances</p>
<h4><a name="Convert-generally-as-needed-locally-in-anticipation">Convert
only as needed, except locally in anticipation of need</a></h4>
<p>This pattern in general defers conversion but for specific local needs does
anticipatory conversion. Although particularly appropriate when coupled with the endian buffer
or arithmetic types, it also works well with the conversion functions.</p>
<p>Example:</p>
<blockquote>
<pre>struct data_t
{
big_int32_t v1;
big_int32_t v2;
big_int32_t v3;
};
data_t data;
read(data);
...
++v1;
...
int32_t v3_temp = data.v3; // hoist conversion out of loop
for (int32_t i = 0; i &lt; <i><b>large-number</b></i>; ++i)
{
... <i><b>lengthy computation that accesses </b></i>v3_temp<i><b> many times</b></i> ...
}
data.v3 = v3_temp;
write(data);
</pre>
</blockquote>
<p dir="ltr">In general the above pseudo-code leaves conversion up to the endian
arithmetic type <code>big_int32_t</code>. But to avoid conversion inside the
loop, a temporary is created before the loop is entered, and then used to set
the new value of <code>data.v3</code> after the loop is complete.</p>
<blockquote>
<p dir="ltr">Question: Won&#39;t the compiler&#39;s optimizer hoist the conversion out
of the loop anyhow?</p>
<p dir="ltr">Answer: VC++ 2015 Preview, and probably others, does not, even for
a toy test program. Although the savings is small (two register <code>
<span style="font-size: 85%">bswap</span></code> instructions), the cost might
be significant if the loop is repeated enough times. On the other hand, the
program may be so dominated by I/O time that even a lengthy loop will be
immaterial.</p>
</blockquote>
<h3><a name="Use-cases">Use case examples</a></h3>
<h4><a name="Porting-endian-unaware-codebase">Porting endian unaware codebase</a></h4>
<p>An existing codebase runs on big endian systems. It does not
currently deal with endianness. The codebase needs to be modified so it can run
on&nbsp; little endian systems under various operating systems. To ease
transition and protect value of existing files, external data will continue to
be maintained as big endian.</p>
<p dir="ltr">The <a href="arithmetic.html">endian
arithmetic approach</a> is recommended to meet these needs. A relatively small
number of header files dealing with binary I/O layouts need to change types. For
example,&nbsp;
<code>short</code> or <code>int16_t</code> would change to <code>big_int16_t</code>. No
changes are required for <code>.cpp</code> files.</p>
<h4><a name="Porting-endian-aware-codebase">Porting endian aware codebase</a></h4>
<p>An existing codebase runs on little-endian Linux systems. It already
deals with endianness via
<a href="http://man7.org/linux/man-pages/man3/endian.3.html">Linux provided
functions</a>. Because of a business merger, the codebase has to be quickly
modified for Windows and possibly other operating systems, while still
supporting Linux. The codebase is reliable and the programmers are all
well-aware of endian issues. </p>
<p dir="ltr">These factors all argue for an <a href="conversion.html">endian conversion
approach</a> that just mechanically changes the calls to <code>htobe32</code>,
etc. to <code>boost::endian::native_to_big</code>, etc. and replaces <code>&lt;endian.h&gt;</code>
with <code>&lt;boost/endian/conversion.hpp&gt;</code>.</p>
<h4><a name="Reliability-arithmetic-speed">Reliability and arithmetic-speed</a></h4>
<p>A new, complex, multi-threaded application is to be developed that must run
on little endian machines, but do big endian network I/O. The developers believe
computational speed for endian variable is critical but have seen numerous bugs
result from inability to reason about endian conversion state. They are also
worried that future maintenance changes could inadvertently introduce a lot of
slow conversions if full-blown endian arithmetic types are used.</p>
<p>The <a href="buffers.html">endian buffers</a> approach is made-to-order for
this use case.</p>
<h4><a name="Reliability-ease-of-use">Reliability and ease-of-use</a></h4>
<p>A new, complex, multi-threaded application is to be developed that must run
on little endian machines, but do big endian network I/O. The developers believe
computational speed for endian variables is <b>not critical</b> but have seen
numerous bugs result from inability to reason about endian conversion state.
They are also concerned about ease-of-use both during development and long-term
maintenance.</p>
<p>Removing concern about conversion speed and adding concern about ease-of-use
tips the balance strongly in favor the <a href="arithmetic.html">endian
arithmetic approach</a>.</p>
<hr>
<p>Last revised:
<!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->19 January, 2015<!--webbot bot="Timestamp" endspan i-checksum="38903" --></p>
<p>© Copyright Beman Dawes, 2011, 2013, 2014</p>
<p>Distributed under the Boost Software License, Version 1.0. See
<a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/ LICENSE_1_0.txt</a></p>
<p>&nbsp;</p>
</body>
</html>

View File

@ -1,405 +0,0 @@
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Endian Conversion Functions</title>
<link href="styles.css" rel="stylesheet">
</head>
<body>
<table border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
<tr>
<td>
<a href="../../../index.html">
<img src="../../../boost.png" alt="Boost logo" align="middle" border="0" width="277" height="86" ></a></td>
<td align="middle">
<b>
<font size="6">Endian Conversion Functions</font></b></td>
</tr>
</table>
<table border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF" width="100%">
<tr>
<td><b>
<a href="index.html">Endian Home</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="conversion.html">Conversion Functions</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="arithmetic.html">Arithmetic Types</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="buffers.html">Buffer Types</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="choosing_approach.html">Choosing Approach</a></b></td>
</tr>
</table>
<p></p>
<table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" align="right">
<tr>
<td width="100%" bgcolor="#D7EEFF" align="center">
<i><b>Contents</b></i></td>
</tr>
<tr>
<td width="100%" bgcolor="#E8F5FF">
<a href="#Introduction">Introduction</a><br>
<a href="#Reference">Reference</a><br>
&nbsp;&nbsp;&nbsp; <a href="#Synopsis">Synopsis</a><br>
&nbsp;&nbsp;&nbsp; <a href="#Requirements">Requirements</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <code><a href="#EndianReversible">EndianReversible</a></code><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#Customization-points">Customization for
UDTs</a><br>
<a href="#Functions">Functions</a><br>
<a href="#FAQ">FAQ</a><br>
<a href="#Acknowledgements">Acknowledgements</a></td>
</tr>
</table>
<h2><a name="Introduction">Introduction</a></h2>
<p>Header <a href="../include/boost/endian/conversion.hpp">boost/endian/conversion.hpp</a>
provides byte order reversal and conversion functions that convert objects of
the built-in
integer types
between native, big, or little endian byte
ordering. User defined types are also supported.</p>
<h2><a name="Reference">Reference</a></h2>
<p>Functions are implemented <code>inline</code> if appropriate.<code> </code>
For C++03 compilers, <code> noexcept</code> is
elided.
Boost scoped enum emulation is used so that the library still works for compilers that do not support scoped enums.</p>
<h3><a name="Definitions">Definitions</a></h3>
<p><b><i>Endianness</i></b> refers to the ordering of bytes within internal or
external integers and other arithmetic data. Most-significant byte first is
called <b><i>big endian</i></b> ordering. Least-significant byte first is called
<b><i>little endian</i></b> ordering. Other orderings are possible and some CPU
architectures support both big and little ordering.</p>
<blockquote>
<p>[<i>Note:</i> The names are derived from
<a href="http://en.wikipedia.org/wiki/Jonathan_Swift" title="Jonathan Swift">
Jonathan Swift</a>'s satirical novel <i>
<a href="http://en.wikipedia.org/wiki/Gulliver's_Travels" title="Gulliver's Travels">
Gullivers Travels</a></i>, where rival kingdoms opened their soft-boiled eggs
at different ends. Wikipedia has an extensive description of
<a href="https://en.wikipedia.org/wiki/Endianness">Endianness</a>. <i>—end note</i>]</p>
</blockquote>
<p>The standard integral types (C++std 3.9.1) except <code>bool</code>
are collectively called the <b> <i>endian types</i></b>.</p>
<h3>
Header <code>&lt;boost/endian/conversion.hpp&gt;</code>
<a name="Synopsis">Synopsis</a></h3>
<pre>#define BOOST_ENDIAN_INTRINSIC_MSG \
&quot;<b><font face="Arial"><i>message describing presence or absence of intrinsics</i></font></b>&quot;
namespace boost
{
namespace endian
{
enum class <a name="order">order</a>
{
native = <b><i>see below,
</i></b> big = <b><i>see below</i></b>,
little = <b><i>see below</i></b>,
<b><i> </i></b>};
int8_t <a href="#endian_reverse">endian_reverse</a>(int8_t x) noexcept;
int16_t <a href="#endian_reverse">endian_reverse</a>(int16_t x) noexcept;
int32_t <a href="#endian_reverse">endian_reverse</a>(int32_t x) noexcept;
int64_t <a href="#endian_reverse">endian_reverse</a>(int64_t x) noexcept;
uint8_t <a href="#endian_reverse">endian_reverse</a>(uint8_t x) noexcept;
uint16_t <a href="#endian_reverse">endian_reverse</a>(uint16_t x) noexcept;
uint32_t <a href="#endian_reverse">endian_reverse</a>(uint32_t x) noexcept;
uint64_t <a href="#endian_reverse">endian_reverse</a>(uint64_t x) noexcept;
template &lt;class EndianReversible&gt;
EndianReversible big_to_native(EndianReversible x) noexcept;
template &lt;class EndianReversible&gt;
EndianReversible native_to_big(EndianReversible x) noexcept;
template &lt;class EndianReversible&gt;
EndianReversible little_to_native(EndianReversible x) noexcept;
template &lt;class EndianReversible&gt;
EndianReversible native_to_little(EndianReversible x) noexcept;
template &lt;order O1, order O2, class EndianReversible&gt;
EndianReversible conditional_reverse(EndianReversible x) noexcept;
template &lt;class EndianReversible&gt;
EndianReversible conditional_reverse(EndianReversible x,
order order1, order order2) noexcept;
template &lt;class EndianReversible&gt;
void endian_reverse_inplace(EndianReversible&amp; x) noexcept;
template &lt;class EndianReversibleInplace&gt;
void big_to_native_inplace(EndianReversibleInplace&amp; x) noexcept;
template &lt;class EndianReversibleInplace&gt;
void native_to_big_inplace(EndianReversibleInplace&amp; x) noexcept;
template &lt;class EndianReversibleInplace&gt;
void little_to_native_inplace(EndianReversibleInplace&amp; x) noexcept;
template &lt;class EndianReversibleInplace&gt;
void native_to_little_inplace(EndianReversibleInplace&amp; x) noexcept;
template &lt;order O1, order O2, class EndianReversibleInplace&gt;
void conditional_reverse_inplace(EndianReversibleInplace&amp; x) noexcept;
template &lt;class EndianReversibleInplace&gt;
void conditional_reverse_inplace(EndianReversibleInplace&amp; x,
order order1, order order2) noexcept;
} // namespace endian
} // namespace boost</pre>
<p>The values of <code>order::little</code> and <code>order::big</code> shall
not be equal to one another. </p>
<p><a name="native-order-specification"></a>The value of <code>order::native</code>
shall be:</p>
<ul>
<li>equal to <code>order::big</code> if the execution environment is big
endian, otherwise</li>
<li>equal to <code>order::little</code> if the execution environment is little
endian, otherwise</li>
<li>unequal to both <code>order::little</code> and <code>order::big</code>.</li>
</ul>
<h3><a name="Requirements">Requirements</a></h3>
<h4><a name="Template-argument-requirements">Template argument requirements</a></h4>
<p>The template definitions in the <code>boost/endian/conversion.hpp</code>
header refer to various named requirements whose details are set out in the
tables in this subsection. In these tables, <code>T</code> is an object or
reference type to be supplied by a C++ program instantiating a template; <code>x</code>
is a value of type (possibly <code>const</code>) <code>T</code>; <code>mlx</code> is a
modifiable lvalue of type <code>T</code>.</p>
<table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
<tr>
<td colspan="3" align="center"><b><code><a name="EndianReversible">EndianReversible</a></code></b>
requirements (in addition to <b><code>CopyConstructible</code></b>)</td>
</tr>
<tr>
<td><b>Expression</b></td>
<td><b>Return<br>
type</b></td>
<td><b>Requirements</b></td>
</tr>
<tr>
<td valign="top"><code>endian_reverse(x)</code></td>
<td align="center" valign="top"><code>T</code></td>
<td> <code>T</code> is an endian type or a class type.<p>If <code>T</code> is
an endian type, returns the value of <code>x</code> with the order of bytes
reversed.</p>
<p>If <code>T</code> is a class type, the function:</p>
<ul>
<li>Returns the value of <code>x</code>
with the order of bytes reversed for all data members of types or arrays of
types that meet the <code>EndianReversible</code> requirements, and;</li>
<li>Is a non-member function in the same namespace as <code>T</code> that
can be found by argument dependent lookup (ADL). </li>
</ul>
</td>
</tr>
</table>
<p>&nbsp;</p>
<table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
<tr>
<td colspan="2" align="center"><b><code><a name="EndianReversibleInplace">EndianReversibleInplace</a></code></b>
requirements (in addition to <b><code>CopyConstructible</code></b>)</td>
</tr>
<tr>
<td><b>Expression</b></td>
<td><b>Requirements</b></td>
</tr>
<tr>
<td valign="top"><code>endian_reverse_inplace(mlx)</code></td>
<td> <code>T</code> is an endian type or a class type.<p>If <code>T</code> is
an endian type, reverses the order of bytes in <code>mlx</code>.</p>
<p>If <code>T</code> is a class type, the function:</p>
<ul>
<li>Reverses the order of bytes of all data members of <code>mlx</code>
that have types or arrays of
types that meet the <code>EndianReversible</code> or <code>EndianReversibleInplace</code>
requirements, and;</li>
<li>Is a non-member function in the same namespace as <code>T</code> that
can be found by argument dependent lookup (ADL).&nbsp; </li>
</ul>
</td>
</tr>
</table>
<p> [<i>Note:</i> Because there is a function template for <code>endian_reverse_inplace</code>
that calls <code>endian_reverse</code>, only <code>endian_reverse</code>
is required for a user-defined type to meet the <code>EndianReversibleInplace</code>
requirements. Although User-defined types are not required to supply an <code>endian_reverse_inplace</code>
function, doing so may improve efficiency. <i>&nbsp;&mdash;end note</i>]</p>
<h4> <a name="Customization-points">Customization points</a> for user-defined types (<a name="UDT">UDT</a>s)</h4>
<p> This subsection describes requirements on the Endian library&#39;s implementation.</p>
<p> The library&#39;s function templates requiring <code>
<a href="#EndianReversible">EndianReversible</a></code> are
required to perform reversal of endianness if needed by making an unqualified
call to <code>endian_reverse()</code>.</p>
<p> The library&#39;s function templates requiring <code>
<a href="#EndianReversibleInplace">EndianReversibleInplace</a></code> are required to perform reversal of endianness if needed by making an
unqualified call to <code>endian_reverse_inplace()</code>.</p>
<p> See <b><code>example/udt_conversion_example.cpp</code></b> for an example user-defined type.</p>
<h3><a name="Functions">Functions</a></h3>
<pre><a name="endian_reverse"></a>int8_t endian_reverse(int8_t x) noexcept;
int16_t endian_reverse(int16_t x) noexcept;
int32_t endian_reverse(int32_t x) noexcept;
int64_t endian_reverse(int64_t x) noexcept;
uint8_t endian_reverse(uint8_t x) noexcept;
uint16_t endian_reverse(uint16_t x) noexcept;
uint32_t endian_reverse(uint32_t x) noexcept;
uint64_t endian_reverse(uint64_t x) noexcept;</pre>
<blockquote>
<p><i>Returns:</i> <i><code>x</code></i>, with the order of its
constituent bytes reversed.</p>
<p><i>Remarks:</i> The type of <i><code>x</code></i> meets the <code>EndianReversible</code> requirements.</p>
<p>[<i>Note:</i> The Boost.Endian library does not provide overloads for the C++ standard library
supplied types. <i>&mdash;end note</i>]</p>
</blockquote>
<pre>template &lt;class EndianReversible&gt;
EndianReversible big_to_native(EndianReversible x) noexcept;</pre>
<blockquote>
<p>
<i>Returns:</i> <code>conditional_reverse&lt;order::big, order::native&gt;(x)</code>.</p>
</blockquote>
<pre>template &lt;class EndianReversible&gt;
EndianReversible native_to_big(EndianReversible x) noexcept; </pre>
<blockquote>
<p><i>Returns:</i> <code>conditional_reverse&lt;order::native, order::big&gt;(x)</code>.</p>
</blockquote>
<pre>template &lt;class EndianReversible&gt;
EndianReversible little_to_native(EndianReversible x) noexcept; </pre>
<blockquote>
<p><i>Returns:</i> <code>conditional_reverse&lt;order::little, order::native&gt;(x)</code>.</p>
</blockquote>
<pre>template &lt;class EndianReversible&gt;
EndianReversible native_to_little(EndianReversible x) noexcept; </pre>
<blockquote>
<p><i>Returns:</i> <code>conditional_reverse&lt;order::native, order::little&gt;(x)</code>.</p>
</blockquote>
<pre>template &lt;order O1, order O2, class EndianReversible&gt;
EndianReversible conditional_reverse(EndianReversible x) noexcept; </pre>
<blockquote>
<p><i>Returns:</i> <code>x</code> if <code>O1 == O2,</code> otherwise <code>endian_reverse(x)</code>.</p>
<p><i>Remarks: </i>Whether <code>x</code> or <code>endian_reverse(x)</code> is to be returned shall be determined at compile time.</p>
</blockquote>
<pre>template &lt;class EndianReversible&gt;
EndianReversible conditional_reverse(EndianReversible x,
order order1, order order2) noexcept; </pre>
<blockquote>
<p><i>Returns:</i> <code>order1 == order2 ? x : endian_reverse(x)</code>.</p>
</blockquote>
<pre>template &lt;class EndianReversible&gt;
void endian_reverse_inplace(EndianReversible&amp; x) noexcept; </pre>
<blockquote>
<p><i>Effects:</i> <code>x</code> <code>= endian_reverse(x)</code>.</p>
</blockquote>
<pre>template &lt;class EndianReversibleInplace&gt;
void big_to_native_inplace(EndianReversibleInplace&amp; x) noexcept; </pre>
<blockquote>
<p>
<i>Effects:</i> <code>conditional_reverse_inplace&lt;order::big, order::native&gt;(x)</code>.</p>
</blockquote>
<pre>template &lt;class EndianReversibleInplace&gt;
void native_to_big_inplace(EndianReversibleInplace&amp; x) noexcept; </pre>
<blockquote>
<p>
<i>Effects:</i> <code>conditional_reverse_inplace&lt;order::native, order::big&gt;(x)</code>.</p>
</blockquote>
<pre>template &lt;class EndianReversibleInplace&gt;
void little_to_native_inplace(EndianReversibleInplace&amp; x) noexcept; </pre>
<blockquote>
<p>
<i>Effects:</i> <code>conditional_reverse_inplace&lt;order::little, order::native&gt;(x)</code>.</p>
</blockquote>
<pre>template &lt;class EndianReversibleInplace&gt;
void native_to_little_inplace(EndianReversibleInplace&amp; x) noexcept; </pre>
<blockquote>
<p>
<i>Effects:</i> <code>conditional_reverse_inplace&lt;order::native, order::little&gt;(x)</code>.</p>
</blockquote>
<pre>template &lt;order O1, order O2, class EndianReversibleInplace&gt;
void conditional_reverse_inplace(EndianReversibleInplace&amp; x) noexcept; </pre>
<blockquote>
<p><i>Effects:</i> None if <code>O1 == O2,</code> otherwise <code>endian_reverse_inplace(x)</code>.</p>
<p><i>Remarks: </i>Which effect applies shall be determined at compile time.</p>
</blockquote>
<pre>template &lt;class EndianReversibleInplace&gt;
void conditional_reverse_inplace(EndianReversibleInplace&amp; x,
order order1, order order2) noexcept; </pre>
<blockquote>
<p><i>Effects: </i>If <code>order1 == order2</code> then <code>endian_reverse_inplace(x)</code>.</p>
</blockquote>
<h2> <a name="FAQ">FAQ</a></h2>
<p>See the <a href="index.html#FAQ">Endian home page</a> FAQ for a library-wide
FAQ.</p>
<p><b>Why are both value returning and modify-in-place functions provided?</b></p>
<blockquote>
<p>Returning the result by value is the standard C and C++ idiom for functions that compute a
value from an argument. Modify-in-place functions allow cleaner code in many real-world
endian use cases and are more efficient for user-defined types that have
members such as string data that do not need to be reversed. Thus both forms are
provided.</p>
</blockquote>
<p><b>Why are exact-length 8, 16, 32, and 64-bit integers supported rather than the built-in
char, short, int, long, long long, etc?</b></p>
<blockquote>
<p>The primary use case, portable file or network data, needs these de facto
standard sizes. Using types that vary with the platform would greatly limit
portability for both programs and data.</p>
</blockquote>
<p><b>Why not use the Linux names (htobe16, htole16, be16toh, le16toh, etc.) ?</b></p>
<blockquote>
<p>Those names are non-standard and vary even between POSIX-like operating
systems. A C++ library TS was going to use those names, but found they were
sometimes implemented as macros. Since macros do not respect scoping and
namespace rules, to use them would be very error prone.</p>
</blockquote>
<h2><a name="Acknowledgements">Acknowledgements</a></h2><p>Tomas Puverle was instrumental
in identifying and articulating the need to support endian conversion as separate from
endian integer types. Phil Endecott suggested the form of the value returning signatures.
Vicente Botet and other reviewers suggested supporting user defined types.
General reverse template implementation approach using std::reverse suggested by Mathias Gaunard.
Portable implementation approach for 16, 32, and 64-bit integers suggested by tymofey,
with avoidance of undefined behavior as suggested by Giovanni Piero Deretta,
and a further refinement suggested by Pyry Jahkola.
Intrinsic builtins implementation approach for 16, 32, and 64-bit integers suggested by
several reviewers, and by David Stone, who provided his Boost licensed macro implementation
that became the starting point for <b><code>boost/endian/detail/intrinsic.hpp</code></b>.
Pierre Talbot provided the <code>int8_t endian_reverse()</code> and templated
<code>endian_reverse_inplace()</code> implementations.</p>
<hr>
<p>Last revised:
<!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->14 October, 2015<!--webbot bot="Timestamp" endspan i-checksum="38874" --></p>
<p>© Copyright Beman Dawes, 2011, 2013</p>
<p>Distributed under the Boost Software License, Version 1.0. See <a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/ LICENSE_1_0.txt</a></p>
</body>
</html>

View File

@ -0,0 +1,7 @@
<style>
*:not(pre)>code { background: none; color: #600000; }
:not(pre):not([class^=L])>code { background: none; color: #600000; }
table tr.even, table tr.alt, table tr:nth-of-type(even) { background: none; }
</style>

43
doc/endian.adoc Normal file
View File

@ -0,0 +1,43 @@
////
Copyright 2019 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
////
# Boost.Endian: The Boost Endian Library
Beman Dawes
:toc: left
:toclevels: 2
:idprefix:
:listing-caption: Code Example
:docinfo: private-footer
:leveloffset: +1
include::endian/overview.adoc[]
include::endian/changelog.adoc[]
include::endian/conversion.adoc[]
include::endian/buffers.adoc[]
include::endian/arithmetic.adoc[]
include::endian/choosing_approach.adoc[]
include::endian/mini_review_topics.adoc[]
:leveloffset: -1
[appendix]
## Copyright and License
This documentation is
* Copyright 2011-2016 Beman Dawes
* Copyright 2019 Peter Dimov
and is distributed under the http://www.boost.org/LICENSE_1_0.txt[Boost Software License, Version 1.0].

598
doc/endian/arithmetic.adoc Normal file
View File

@ -0,0 +1,598 @@
////
Copyright 2011-2016 Beman Dawes
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
////
[#arithmetic]
# Endian Arithmetic Types
:idprefix: arithmetic_
## Introduction
Header `boost/endian/arithmetic.hpp` provides integer binary types with
control over byte order, value type, size, and alignment. Typedefs provide
easy-to-use names for common configurations.
These types provide portable byte-holders for integer data, independent of
particular computer architectures. Use cases almost always involve I/O, either
via files or network connections. Although data portability is the primary
motivation, these integer byte-holders may also be used to reduce memory use,
file size, or network activity since they provide binary integer sizes not
otherwise available.
Such integer byte-holder types are traditionally called *endian* types. See the
http://en.wikipedia.org/wiki/Endian[Wikipedia] for a full exploration of
*endianness*, including definitions of *big endian* and *little endian*.
Boost endian integers provide the same full set of {cpp} assignment, arithmetic,
and relational operators as {cpp} standard integral types, with the standard
semantics.
Unary arithmetic operators are `+`, `-`, `~`, `!`, plus both prefix and postfix
`--` and `++`. Binary arithmetic operators are `+`, `+=`, `-`, `-=`, `\*`,
``*=``, `/`, `/=`, `&`, `&=`, `|`, `|=`, `^`, `^=`, `<<`, `<\<=`, `>>`, and
`>>=`. Binary relational operators are `==`, `!=`, `<`, `\<=`, `>`, and `>=`.
Implicit conversion to the underlying value type is provided. An implicit
constructor converting from the underlying value type is provided.
## Example
The `endian_example.cpp` program writes a binary file containing four-byte,
big-endian and little-endian integers:
```
#include <iostream>
#include <cstdio>
#include <boost/endian/arithmetic.hpp>
#include <boost/static_assert.hpp>
using namespace boost::endian;
namespace
{
// This is an extract from a very widely used GIS file format.
// Why the designer decided to mix big and little endians in
// the same file is not known. But this is a real-world format
// and users wishing to write low level code manipulating these
// files have to deal with the mixed endianness.
struct header
{
big_int32_t file_code;
big_int32_t file_length;
little_int32_t version;
little_int32_t shape_type;
};
const char* filename = "test.dat";
}
int main(int, char* [])
{
header h;
BOOST_STATIC_ASSERT(sizeof(h) == 16U); // reality check
h.file_code = 0x01020304;
h.file_length = sizeof(header);
h.version = 1;
h.shape_type = 0x01020304;
// Low-level I/O such as POSIX read/write or <cstdio>
// fread/fwrite is sometimes used for binary file operations
// when ultimate efficiency is important. Such I/O is often
// performed in some C++ wrapper class, but to drive home the
// point that endian integers are often used in fairly
// low-level code that does bulk I/O operations, <cstdio>
// fopen/fwrite is used for I/O in this example.
std::FILE* fi = std::fopen(filename, "wb"); // MUST BE BINARY
if (!fi)
{
std::cout << "could not open " << filename << '\n';
return 1;
}
if (std::fwrite(&h, sizeof(header), 1, fi) != 1)
{
std::cout << "write failure for " << filename << '\n';
return 1;
}
std::fclose(fi);
std::cout << "created file " << filename << '\n';
return 0;
}
```
After compiling and executing `endian_example.cpp`, a hex dump of `test.dat`
shows:
```
01020304 00000010 01000000 04030201
```
Notice that the first two 32-bit integers are big endian while the second two
are little endian, even though the machine this was compiled and run on was
little endian.
## Limitations
Requires `<climits>`, `CHAR_BIT == 8`. If `CHAR_BIT` is some other value,
compilation will result in an `#error`. This restriction is in place because the
design, implementation, testing, and documentation has only considered issues
related to 8-bit bytes, and there have been no real-world use cases presented
for other sizes.
In {cpp}03, `endian_arithmetic` does not meet the requirements for POD types
because it has constructors, private data members, and a base class. This means
that common use cases are relying on unspecified behavior in that the {cpp}
Standard does not guarantee memory layout for non-POD types. This has not been a
problem in practice since all known {cpp} compilers lay out memory as if
`endian` were a POD type. In {cpp}11, it is possible to specify the default
constructor as trivial, and private data members and base classes no longer
disqualify a type from being a POD type. Thus under {cpp}11, `endian_arithmetic`
will no longer be relying on unspecified behavior.
## Feature set
* Big endian| little endian | native endian byte ordering.
* Signed | unsigned
* Unaligned | aligned
* 1-8 byte (unaligned) | 1, 2, 4, 8 byte (aligned)
* Choice of value type
## Enums and typedefs
Two scoped enums are provided:
```
enum class order { big, little, native };
enum class align { no, yes };
```
One class template is provided:
```
template <order Order, typename T, std::size_t n_bits,
align Align = align::no>
class endian_arithmetic;
```
Typedefs, such as `big_int32_t`, provide convenient naming conventions for
common use cases:
[%header,cols=5*]
|===
|Name |Alignment |Endianness |Sign |Sizes in bits (n)
|`big_intN_t` |no |big |signed |8,16,24,32,40,48,56,64
|`big_uintN_t` |no |big |unsigned |8,16,24,32,40,48,56,64
|`little_intN_t` |no |little |signed |8,16,24,32,40,48,56,64
|`little_uintN_t` |no |little |unsigned |8,16,24,32,40,48,56,64
|`native_intN_t` |no |native |signed |8,16,24,32,40,48,56,64
|`native_uintN_t` |no |native |unsigned |8,16,24,32,40,48,56,64
|`big_intN_at` |yes |big |signed |8,16,32,64
|`big_uintN_at` |yes |big |unsigned |8,16,32,64
|`little_intN_at` |yes |little |signed |8,16,32,64
|`little_uintN_at` |yes |little |unsigned |8,16,32,64
|===
The unaligned types do not cause compilers to insert padding bytes in classes
and structs. This is an important characteristic that can be exploited to
minimize wasted space in memory, files, and network transmissions.
CAUTION: Code that uses aligned types is possibly non-portable because
alignment requirements vary between hardware architectures and because
alignment may be affected by compiler switches or pragmas. For example,
alignment of an 64-bit integer may be to a 32-bit boundary on a 32-bit machine.
Furthermore, aligned types are only available on architectures with 8, 16, 32,
and 64-bit integer types.
TIP: Prefer unaligned arithmetic types.
TIP: Protect yourself against alignment ills. For example:
[none]
{blank}::
+
```
static_assert(sizeof(containing_struct) == 12, "sizeof(containing_struct) is wrong");
```
NOTE: One-byte arithmetic types have identical layout on all platforms, so they
never actually reverse endianness. They are provided to enable generic code,
and to improve code readability and searchability.
## Class template `endian_arithmetic`
An `endian_integer` is an integer byte-holder with user-specified endianness,
value type, size, and alignment. The usual operations on arithmetic types are
supplied.
### Synopsis
```
#include <boost/endian/buffers.hpp>
namespace boost
{
namespace endian
{
// C++11 features emulated if not available
enum class align { no, yes };
template <order Order, class T, std::size_t n_bits,
align Align = align::no>
class endian_arithmetic
: public endian_buffer<Order, T, n_bits, Align>
{
public:
typedef T value_type;
// if BOOST_ENDIAN_FORCE_PODNESS is defined && C++11 PODs are not
// available then these two constructors will not be present
endian_arithmetic() noexcept = default;
endian_arithmetic(T v) noexcept;
endian_arithmetic& operator=(T v) noexcept;
operator value_type() const noexcept;
value_type value() const noexcept; // for exposition; see endian_buffer
const char* data() const noexcept; // for exposition; see endian_buffer
// arithmetic operations
// note that additional operations are provided by the value_type
value_type operator+() const noexcept;
endian_arithmetic& operator+=(value_type y) noexcept;
endian_arithmetic& operator-=(value_type y) noexcept;
endian_arithmetic& operator*=(value_type y) noexcept;
endian_arithmetic& operator/=(value_type y) noexcept;
endian_arithmetic& operator%=(value_type y) noexcept;
endian_arithmetic& operator&=(value_type y) noexcept;
endian_arithmetic& operator|=(value_type y) noexcept;
endian_arithmetic& operator^=(value_type y) noexcept;
endian_arithmetic& operator<<=(value_type y) noexcept;
endian_arithmetic& operator>>=(value_type y) noexcept;
endian_arithmetic& operator++() noexcept;
endian_arithmetic& operator--() noexcept;
endian_arithmetic operator++(int) noexcept;
endian_arithmetic operator--(int) noexcept;
// Stream inserter
template <class charT, class traits>
friend std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& os, const endian_arithmetic& x);
// Stream extractor
template <class charT, class traits>
friend std::basic_istream<charT, traits>&
operator>>(std::basic_istream<charT, traits>& is, endian_arithmetic& x);
};
// typedefs
// unaligned big endian signed integer types
typedef endian_arithmetic<order::big, int_least8_t, 8> big_int8_t;
typedef endian_arithmetic<order::big, int_least16_t, 16> big_int16_t;
typedef endian_arithmetic<order::big, int_least32_t, 24> big_int24_t;
typedef endian_arithmetic<order::big, int_least32_t, 32> big_int32_t;
typedef endian_arithmetic<order::big, int_least64_t, 40> big_int40_t;
typedef endian_arithmetic<order::big, int_least64_t, 48> big_int48_t;
typedef endian_arithmetic<order::big, int_least64_t, 56> big_int56_t;
typedef endian_arithmetic<order::big, int_least64_t, 64> big_int64_t;
// unaligned big endian unsigned integer types
typedef endian_arithmetic<order::big, uint_least8_t, 8> big_uint8_t;
typedef endian_arithmetic<order::big, uint_least16_t, 16> big_uint16_t;
typedef endian_arithmetic<order::big, uint_least32_t, 24> big_uint24_t;
typedef endian_arithmetic<order::big, uint_least32_t, 32> big_uint32_t;
typedef endian_arithmetic<order::big, uint_least64_t, 40> big_uint40_t;
typedef endian_arithmetic<order::big, uint_least64_t, 48> big_uint48_t;
typedef endian_arithmetic<order::big, uint_least64_t, 56> big_uint56_t;
typedef endian_arithmetic<order::big, uint_least64_t, 64> big_uint64_t;
// unaligned big endian floating point types
typedef endian_arithmetic<order::big, float, 32> big_float32_t;
typedef endian_arithmetic<order::big, double, 64> big_float64_t;
// unaligned little endian signed integer types
typedef endian_arithmetic<order::little, int_least8_t, 8> little_int8_t;
typedef endian_arithmetic<order::little, int_least16_t, 16> little_int16_t;
typedef endian_arithmetic<order::little, int_least32_t, 24> little_int24_t;
typedef endian_arithmetic<order::little, int_least32_t, 32> little_int32_t;
typedef endian_arithmetic<order::little, int_least64_t, 40> little_int40_t;
typedef endian_arithmetic<order::little, int_least64_t, 48> little_int48_t;
typedef endian_arithmetic<order::little, int_least64_t, 56> little_int56_t;
typedef endian_arithmetic<order::little, int_least64_t, 64> little_int64_t;
// unaligned little endian unsigned integer types
typedef endian_arithmetic<order::little, uint_least8_t, 8> little_uint8_t;
typedef endian_arithmetic<order::little, uint_least16_t, 16> little_uint16_t;
typedef endian_arithmetic<order::little, uint_least32_t, 24> little_uint24_t;
typedef endian_arithmetic<order::little, uint_least32_t, 32> little_uint32_t;
typedef endian_arithmetic<order::little, uint_least64_t, 40> little_uint40_t;
typedef endian_arithmetic<order::little, uint_least64_t, 48> little_uint48_t;
typedef endian_arithmetic<order::little, uint_least64_t, 56> little_uint56_t;
typedef endian_arithmetic<order::little, uint_least64_t, 64> little_uint64_t;
// unaligned little endian floating point types
typedef endian_arithmetic<order::little, float, 32> little_float32_t;
typedef endian_arithmetic<order::little, double, 64> little_float64_t;
// unaligned native endian signed integer types
typedef implementation-defined_int8_t native_int8_t;
typedef implementation-defined_int16_t native_int16_t;
typedef implementation-defined_int24_t native_int24_t;
typedef implementation-defined_int32_t native_int32_t;
typedef implementation-defined_int40_t native_int40_t;
typedef implementation-defined_int48_t native_int48_t;
typedef implementation-defined_int56_t native_int56_t;
typedef implementation-defined_int64_t native_int64_t;
// unaligned native endian unsigned integer types
typedef implementation-defined_uint8_t native_uint8_t;
typedef implementation-defined_uint16_t native_uint16_t;
typedef implementation-defined_uint24_t native_uint24_t;
typedef implementation-defined_uint32_t native_uint32_t;
typedef implementation-defined_uint40_t native_uint40_t;
typedef implementation-defined_uint48_t native_uint48_t;
typedef implementation-defined_uint56_t native_uint56_t;
typedef implementation-defined_uint64_t native_uint64_t;
// unaligned native endian floating point types
typedef implementation-defined_float32_t native_float32_t;
typedef implementation-defined_float64_t native_float64_t;
// aligned big endian signed integer types
typedef endian_arithmetic<order::big, int8_t, 8, align::yes> big_int8_at;
typedef endian_arithmetic<order::big, int16_t, 16, align::yes> big_int16_at;
typedef endian_arithmetic<order::big, int32_t, 32, align::yes> big_int32_at;
typedef endian_arithmetic<order::big, int64_t, 64, align::yes> big_int64_at;
// aligned big endian unsigned integer types
typedef endian_arithmetic<order::big, uint8_t, 8, align::yes> big_uint8_at;
typedef endian_arithmetic<order::big, uint16_t, 16, align::yes> big_uint16_at;
typedef endian_arithmetic<order::big, uint32_t, 32, align::yes> big_uint32_at;
typedef endian_arithmetic<order::big, uint64_t, 64, align::yes> big_uint64_at;
// aligned big endian floating point types
typedef endian_arithmetic<order::big, float, 32, align::yes> big_float32_at;
typedef endian_arithmetic<order::big, double, 64, align::yes> big_float64_at;
// aligned little endian signed integer types
typedef endian_arithmetic<order::little, int8_t, 8, align::yes> little_int8_at;
typedef endian_arithmetic<order::little, int16_t, 16, align::yes> little_int16_at;
typedef endian_arithmetic<order::little, int32_t, 32, align::yes> little_int32_at;
typedef endian_arithmetic<order::little, int64_t, 64, align::yes> little_int64_at;
// aligned little endian unsigned integer types
typedef endian_arithmetic<order::little, uint8_t, 8, align::yes> little_uint8_at;
typedef endian_arithmetic<order::little, uint16_t, 16, align::yes> little_uint16_at;
typedef endian_arithmetic<order::little, uint32_t, 32, align::yes> little_uint32_at;
typedef endian_arithmetic<order::little, uint64_t, 64, align::yes> little_uint64_at;
// aligned little endian floating point types
typedef endian_arithmetic<order::little, float, 32, align::yes> little_float32_at;
typedef endian_arithmetic<order::little, double, 64, align::yes> little_float64_at;
// aligned native endian typedefs are not provided because
// <cstdint> types are superior for that use case
} // namespace endian
} // namespace boost
```
The `implementation-defined` text above is either `big` or `little` according
to the endianness of the platform.
The only supported value of `CHAR_BIT` is 8.
The valid values of `Nbits` are as follows:
* When `sizeof(T)` is 1, `Nbits` shall be 8;
* When `sizeof(T)` is 2, `Nbits` shall be 16;
* When `sizeof(T)` is 4, `Nbits` shall be 24 or 32;
* When `sizeof(T)` is 8, `Nbits` shall be 40, 48, 56, or 64.
Other values of `sizeof(T)` are not supported.
When `Nbits` is equal to `sizeof(T)*8`, `T` must be a standard arithmetic type.
When `Nbits` is less than `sizeof(T)*8`, `T` must be a standard integral type
({cpp}std, [basic.fundamental]) that is not `bool`.
### Members
```
endian_arithmetic() noexcept = default; // C++03: endian(){}
```
[none]
* {blank}
+
Effects:: Constructs an uninitialized object.
```
endian_arithmetic(T v) noexcept;
```
[none]
* {blank}
+
Effects:: See `endian_buffer::endian_buffer(T)`.
```
endian_arithmetic& operator=(T v) noexcept;
```
[none]
* {blank}
+
Effects:: See `endian_buffer::operator=(T)`.
Returns:: `*this`.
```
operator T() const noexcept;
```
[none]
* {blank}
+
Returns::
`value()`.
### Other operators
Other operators on endian objects are forwarded to the equivalent operator on
`value_type`.
### Stream inserter
```
template <class charT, class traits>
friend std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& os, const endian_arithmetic& x);
```
[none]
* {blank}
+
Returns:: `os << +x`.
[none]
### Stream extractor
```
template <class charT, class traits>
friend std::basic_istream<charT, traits>&
operator>>(std::basic_istream<charT, traits>& is, endian_arithmetic& x);
```
[none]
* {blank}
+
Effects:: As if:
+
```
T i;
if (is >> i)
x = i;
```
Returns:: `is`.
## FAQ
See the <<overview_faq,Overview FAQ>> for a library-wide FAQ.
Why not just use Boost.Serialization?::
Serialization involves a conversion for every object involved in I/O. Endian
integers require no conversion or copying. They are already in the desired
format for binary I/O. Thus they can be read or written in bulk.
Are endian types PODs?::
Yes for {cpp}11. No for {cpp}03, although several
<<arithmetic_compilation,macros>> are available to force PODness in all cases.
What are the implications of endian integer types not being PODs with {cpp}03 compilers?::
They can't be used in unions. Also, compilers aren't required to align or lay
out storage in portable ways, although this potential problem hasn't prevented
use of Boost.Endian with real compilers.
What good is native endianness?::
It provides alignment and size guarantees not available from the built-in
types. It eases generic programming.
Why bother with the aligned endian types?::
Aligned integer operations may be faster (as much as 10 to 20 times faster)
if the endianness and alignment of the type matches the endianness and
alignment requirements of the machine. The code, however, will be somewhat less
portable than with the unaligned types.
Why provide the arithmetic operations?::
Providing a full set of operations reduces program clutter and makes code
both easier to write and to read. Consider incrementing a variable in a record.
It is very convenient to write:
+
```
++record.foo;
```
+
Rather than:
+
```
int temp(record.foo);
++temp;
record.foo = temp;
```
## Design considerations for Boost.Endian types
* Must be suitable for I/O - in other words, must be memcpyable.
* Must provide exactly the size and internal byte ordering specified.
* Must work correctly when the internal integer representation has more bits
that the sum of the bits in the external byte representation. Sign extension
must work correctly when the internal integer representation type has more
bits than the sum of the bits in the external bytes. For example, using
a 64-bit integer internally to represent 40-bit (5 byte) numbers must work for
both positive and negative values.
* Must work correctly (including using the same defined external
representation) regardless of whether a compiler treats char as signed or
unsigned.
* Unaligned types must not cause compilers to insert padding bytes.
* The implementation should supply optimizations with great care. Experience
has shown that optimizations of endian integers often become pessimizations
when changing machines or compilers. Pessimizations can also happen when
changing compiler switches, compiler versions, or CPU models of the same
architecture.
## Experience
Classes with similar functionality have been independently developed by
several Boost programmers and used very successful in high-value, high-use
applications for many years. These independently developed endian libraries
often evolved from C libraries that were also widely used. Endian types have
proven widely useful across a wide range of computer architectures and
applications.
## Motivating use cases
Neil Mayhew writes: "I can also provide a meaningful use-case for this
library: reading TrueType font files from disk and processing the contents. The
data format has fixed endianness (big) and has unaligned values in various
places. Using Boost.Endian simplifies and cleans the code wonderfully."
## {cpp}11
The availability of the {cpp}11
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm[Defaulted
Functions] feature is detected automatically, and will be used if present to
ensure that objects of `class endian_arithmetic` are trivial, and thus PODs.
## Compilation
Boost.Endian is implemented entirely within headers, with no need to link to any
Boost object libraries.
Several macros allow user control over features:
* BOOST_ENDIAN_NO_CTORS causes `class endian_arithmetic` to have no
constructors. The intended use is for compiling user code that must be portable
between compilers regardless of {cpp}11
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm[Defaulted
Functions] support. Use of constructors will always fail,
* BOOST_ENDIAN_FORCE_PODNESS causes BOOST_ENDIAN_NO_CTORS to be defined if
the compiler does not support {cpp}11
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm[Defaulted
Functions]. This is ensures that objects of `class endian_arithmetic` are PODs,
and so can be used in {cpp}03 unions. In {cpp}11, `class endian_arithmetic`
objects are PODs, even though they have constructors, so can always be used in
unions.
## Acknowledgements
Original design developed by Darin Adler based on classes developed by Mark
Borgerding. Four original class templates combined into a single
`endian_arithmetic` class template by Beman Dawes, who put the library together,
provided documentation, added the typedefs, and also added the
`unrolled_byte_loops` sign partial specialization to correctly extend the sign
when cover integer size differs from endian representation size.

540
doc/endian/buffers.adoc Normal file
View File

@ -0,0 +1,540 @@
////
Copyright 2011-2016 Beman Dawes
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
////
[#buffers]
# Endian Buffer Types
:idprefix: buffers_
## Introduction
The internal byte order of arithmetic types is traditionally called
*endianness*. See the http://en.wikipedia.org/wiki/Endian[Wikipedia] for a full
exploration of *endianness*, including definitions of *big endian* and *little
endian*.
Header `boost/endian/buffers.hpp` provides `endian_buffer`, a portable endian
integer binary buffer class template with control over byte order, value type,
size, and alignment independent of the platform's native endianness. Typedefs
provide easy-to-use names for common configurations.
Use cases primarily involve data portability, either via files or network
connections, but these byte-holders may also be used to reduce memory use, file
size, or network activity since they provide binary numeric sizes not otherwise
available.
Class `endian_buffer` is aimed at users who wish explicit control over when
endianness conversions occur. It also serves as the base class for the
<<arithmetic,endian_arithmetic>> class template, which is aimed at users who
wish fully automatic endianness conversion and direct support for all normal
arithmetic operations.
## Example
The `example/endian_example.cpp` program writes a binary file containing
four-byte, big-endian and little-endian integers:
```
#include <iostream>
#include <cstdio>
#include <boost/endian/buffers.hpp> // see Synopsis below
#include <boost/static_assert.hpp>
using namespace boost::endian;
namespace
{
// This is an extract from a very widely used GIS file format.
// Why the designer decided to mix big and little endians in
// the same file is not known. But this is a real-world format
// and users wishing to write low level code manipulating these
// files have to deal with the mixed endianness.
struct header
{
big_int32_buf_t file_code;
big_int32_buf_t file_length;
little_int32_buf_t version;
little_int32_buf_t shape_type;
};
const char* filename = "test.dat";
}
int main(int, char* [])
{
header h;
BOOST_STATIC_ASSERT(sizeof(h) == 16U); // reality check
h.file_code = 0x01020304;
h.file_length = sizeof(header);
h.version = 1;
h.shape_type = 0x01020304;
// Low-level I/O such as POSIX read/write or <cstdio>
// fread/fwrite is sometimes used for binary file operations
// when ultimate efficiency is important. Such I/O is often
// performed in some C++ wrapper class, but to drive home the
// point that endian integers are often used in fairly
// low-level code that does bulk I/O operations, <cstdio>
// fopen/fwrite is used for I/O in this example.
std::FILE* fi = std::fopen(filename, "wb"); // MUST BE BINARY
if (!fi)
{
std::cout << "could not open " << filename << '\n';
return 1;
}
if (std::fwrite(&h, sizeof(header), 1, fi) != 1)
{
std::cout << "write failure for " << filename << '\n';
return 1;
}
std::fclose(fi);
std::cout << "created file " << filename << '\n';
return 0;
}
```
After compiling and executing `example/endian_example.cpp`, a hex dump of
`test.dat` shows:
```
01020304 00000010 01000000 04030201
```
Notice that the first two 32-bit integers are big endian while the second two
are little endian, even though the machine this was compiled and run on was
little endian.
## Limitations
Requires `<climits>`, `CHAR_BIT == 8`. If `CHAR_BIT` is some other value,
compilation will result in an `#error`. This restriction is in place because the
design, implementation, testing, and documentation has only considered issues
related to 8-bit bytes, and there have been no real-world use cases presented
for other sizes.
In {cpp}03, `endian_buffer` does not meet the requirements for POD types because
it has constructors and a private data member. This means that
common use cases are relying on unspecified behavior in that the {cpp} Standard
does not guarantee memory layout for non-POD types. This has not been a problem
in practice since all known {cpp} compilers lay out memory as if `endian` were
a POD type. In {cpp}11, it is possible to specify the default constructor as
trivial, and private data members and base classes no longer disqualify a type
from being a POD type. Thus under {cpp}11, `endian_buffer` will no longer be
relying on unspecified behavior.
## Feature set
* Big endian| little endian | native endian byte ordering.
* Signed | unsigned
* Unaligned | aligned
* 1-8 byte (unaligned) | 1, 2, 4, 8 byte (aligned)
* Choice of value type
## Enums and typedefs
Two scoped enums are provided:
```
enum class order { big, little, native };
enum class align { no, yes };
```
One class template is provided:
```
template <order Order, typename T, std::size_t Nbits,
align Align = align::no>
class endian_buffer;
```
Typedefs, such as `big_int32_buf_t`, provide convenient naming conventions for
common use cases:
[%header,cols=5*]
|===
|Name |Alignment |Endianness |Sign |Sizes in bits (n)
|`big_intN_buf_t` |no |big |signed |8,16,24,32,40,48,56,64
|`big_uintN_buf_t` |no |big |unsigned |8,16,24,32,40,48,56,64
|`little_intN_buf_t` |no |little |signed |8,16,24,32,40,48,56,64
|`little_uintN_buf_t` |no |little |unsigned |8,16,24,32,40,48,56,64
|`native_intN_buf_t` |no |native |signed |8,16,24,32,40,48,56,64
|`native_uintN_buf_t` |no |native |unsigned |8,16,24,32,40,48,56,64
|`big_intN_buf_at` |yes |big |signed |8,16,32,64
|`big_uintN_buf_at` |yes |big |unsigned |8,16,32,64
|`little_intN_buf_at` |yes |little |signed |8,16,32,64
|`little_uintN_buf_at` |yes |little |unsigned |8,16,32,64
|===
The unaligned types do not cause compilers to insert padding bytes in classes
and structs. This is an important characteristic that can be exploited to
minimize wasted space in memory, files, and network transmissions.
CAUTION: Code that uses aligned types is possibly non-portable because alignment
requirements vary between hardware architectures and because alignment may be
affected by compiler switches or pragmas. For example, alignment of an 64-bit
integer may be to a 32-bit boundary on a 32-bit machine and to a 64-bit boundary
on a 64-bit machine. Furthermore, aligned types are only available on
architectures with 8, 16, 32, and 64-bit integer types.
TIP: Prefer unaligned buffer types.
TIP: Protect yourself against alignment ills. For example:
[none]
{blank}::
+
```
static_assert(sizeof(containing_struct) == 12, "sizeof(containing_struct) is wrong");
```
Note: One-byte big and little buffer types have identical layout on all
platforms, so they never actually reverse endianness. They are provided to
enable generic code, and to improve code readability and searchability.
## Class template `endian_buffer`
An `endian_buffer` is a byte-holder for arithmetic types with
user-specified endianness, value type, size, and alignment.
### Synopsis
```
namespace boost
{
namespace endian
{
// C++11 features emulated if not available
enum class align { no, yes };
template <order Order, class T, std::size_t Nbits,
align Align = align::no>
class endian_buffer
{
public:
typedef T value_type;
endian_buffer() noexcept = default;
explicit endian_buffer(T v) noexcept;
endian_buffer& operator=(T v) noexcept;
value_type value() const noexcept;
const char* data() const noexcept;
private:
unsigned char value_[ Nbits / CHAR_BIT]; // exposition only
};
// stream inserter
template <class charT, class traits, order Order, class T,
std::size_t n_bits, align Align>
std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& os,
const endian_buffer<Order, T, n_bits, Align>& x);
// stream extractor
template <class charT, class traits, order Order, class T,
std::size_t n_bits, align A>
std::basic_istream<charT, traits>&
operator>>(std::basic_istream<charT, traits>& is,
endian_buffer<Order, T, n_bits, Align>& x);
// typedefs
// unaligned big endian signed integer buffers
typedef endian_buffer<order::big, int_least8_t, 8> big_int8_buf_t;
typedef endian_buffer<order::big, int_least16_t, 16> big_int16_buf_t;
typedef endian_buffer<order::big, int_least32_t, 24> big_int24_buf_t;
typedef endian_buffer<order::big, int_least32_t, 32> big_int32_buf_t;
typedef endian_buffer<order::big, int_least64_t, 40> big_int40_buf_t;
typedef endian_buffer<order::big, int_least64_t, 48> big_int48_buf_t;
typedef endian_buffer<order::big, int_least64_t, 56> big_int56_buf_t;
typedef endian_buffer<order::big, int_least64_t, 64> big_int64_buf_t;
// unaligned big endian unsigned integer buffers
typedef endian_buffer<order::big, uint_least8_t, 8> big_uint8_buf_t;
typedef endian_buffer<order::big, uint_least16_t, 16> big_uint16_buf_t;
typedef endian_buffer<order::big, uint_least32_t, 24> big_uint24_buf_t;
typedef endian_buffer<order::big, uint_least32_t, 32> big_uint32_buf_t;
typedef endian_buffer<order::big, uint_least64_t, 40> big_uint40_buf_t;
typedef endian_buffer<order::big, uint_least64_t, 48> big_uint48_buf_t;
typedef endian_buffer<order::big, uint_least64_t, 56> big_uint56_buf_t;
typedef endian_buffer<order::big, uint_least64_t, 64> big_uint64_buf_t;
// unaligned big endian floating point buffers
typedef endian_buffer<order::big, float, 32> big_float32_buf_t;
typedef endian_buffer<order::big, double, 64> big_float64_buf_t;
// unaligned little endian signed integer buffers
typedef endian_buffer<order::little, int_least8_t, 8> little_int8_buf_t;
typedef endian_buffer<order::little, int_least16_t, 16> little_int16_buf_t;
typedef endian_buffer<order::little, int_least32_t, 24> little_int24_buf_t;
typedef endian_buffer<order::little, int_least32_t, 32> little_int32_buf_t;
typedef endian_buffer<order::little, int_least64_t, 40> little_int40_buf_t;
typedef endian_buffer<order::little, int_least64_t, 48> little_int48_buf_t;
typedef endian_buffer<order::little, int_least64_t, 56> little_int56_buf_t;
typedef endian_buffer<order::little, int_least64_t, 64> little_int64_buf_t;
// unaligned little endian unsigned integer buffers
typedef endian_buffer<order::little, uint_least8_t, 8> little_uint8_buf_t;
typedef endian_buffer<order::little, uint_least16_t, 16> little_uint16_buf_t;
typedef endian_buffer<order::little, uint_least32_t, 24> little_uint24_buf_t;
typedef endian_buffer<order::little, uint_least32_t, 32> little_uint32_buf_t;
typedef endian_buffer<order::little, uint_least64_t, 40> little_uint40_buf_t;
typedef endian_buffer<order::little, uint_least64_t, 48> little_uint48_buf_t;
typedef endian_buffer<order::little, uint_least64_t, 56> little_uint56_buf_t;
typedef endian_buffer<order::little, uint_least64_t, 64> little_uint64_buf_t;
// unaligned little endian floating point buffers
typedef endian_buffer<order::little, float, 32> little_float32_buf_t;
typedef endian_buffer<order::little, double, 64> little_float64_buf_t;
// unaligned native endian signed integer types
typedef implementation-defined_int8_buf_t native_int8_buf_t;
typedef implementation-defined_int16_buf_t native_int16_buf_t;
typedef implementation-defined_int24_buf_t native_int24_buf_t;
typedef implementation-defined_int32_buf_t native_int32_buf_t;
typedef implementation-defined_int40_buf_t native_int40_buf_t;
typedef implementation-defined_int48_buf_t native_int48_buf_t;
typedef implementation-defined_int56_buf_t native_int56_buf_t;
typedef implementation-defined_int64_buf_t native_int64_buf_t;
// unaligned native endian unsigned integer types
typedef implementation-defined_uint8_buf_t native_uint8_buf_t;
typedef implementation-defined_uint16_buf_t native_uint16_buf_t;
typedef implementation-defined_uint24_buf_t native_uint24_buf_t;
typedef implementation-defined_uint32_buf_t native_uint32_buf_t;
typedef implementation-defined_uint40_buf_t native_uint40_buf_t;
typedef implementation-defined_uint48_buf_t native_uint48_buf_t;
typedef implementation-defined_uint56_buf_t native_uint56_buf_t;
typedef implementation-defined_uint64_buf_t native_uint64_buf_t;
// unaligned native endian floating point types
typedef implementation-defined_float32_buf_t native_float32_buf_t;
typedef implementation-defined_float64_buf_t native_float64_buf_t;
// aligned big endian signed integer buffers
typedef endian_buffer<order::big, int8_t, 8, align::yes> big_int8_buf_at;
typedef endian_buffer<order::big, int16_t, 16, align::yes> big_int16_buf_at;
typedef endian_buffer<order::big, int32_t, 32, align::yes> big_int32_buf_at;
typedef endian_buffer<order::big, int64_t, 64, align::yes> big_int64_buf_at;
// aligned big endian unsigned integer buffers
typedef endian_buffer<order::big, uint8_t, 8, align::yes> big_uint8_buf_at;
typedef endian_buffer<order::big, uint16_t, 16, align::yes> big_uint16_buf_at;
typedef endian_buffer<order::big, uint32_t, 32, align::yes> big_uint32_buf_at;
typedef endian_buffer<order::big, uint64_t, 64, align::yes> big_uint64_buf_at;
// aligned big endian floating point buffers
typedef endian_buffer<order::big, float, 32, align::yes> big_float32_buf_at;
typedef endian_buffer<order::big, double, 64, align::yes> big_float64_buf_at;
// aligned little endian signed integer buffers
typedef endian_buffer<order::little, int8_t, 8, align::yes> little_int8_buf_at;
typedef endian_buffer<order::little, int16_t, 16, align::yes> little_int16_buf_at;
typedef endian_buffer<order::little, int32_t, 32, align::yes> little_int32_buf_at;
typedef endian_buffer<order::little, int64_t, 64, align::yes> little_int64_buf_at;
// aligned little endian unsigned integer buffers
typedef endian_buffer<order::little, uint8_t, 8, align::yes> little_uint8_buf_at;
typedef endian_buffer<order::little, uint16_t, 16, align::yes> little_uint16_buf_at;
typedef endian_buffer<order::little, uint32_t, 32, align::yes> little_uint32_buf_at;
typedef endian_buffer<order::little, uint64_t, 64, align::yes> little_uint64_buf_at;
// aligned little endian floating point buffers
typedef endian_buffer<order::little, float, 32, align::yes> little_float32_buf_at;
typedef endian_buffer<order::little, double, 64, align::yes> little_float64_buf_at;
// aligned native endian typedefs are not provided because
// <cstdint> types are superior for this use case
} // namespace endian
} // namespace boost
```
The `implementation-defined` text in typedefs above is either `big` or `little`
according to the native endianness of the platform.
The expository data member `value_` stores the current value of the
`endian_buffer` object as a sequence of bytes ordered as specified by the
`Order` template parameter. The `CHAR_BIT` macro is defined in `<climits>`.
The only supported value of `CHAR_BIT` is 8.
The valid values of `Nbits` are as follows:
* When `sizeof(T)` is 1, `Nbits` shall be 8;
* When `sizeof(T)` is 2, `Nbits` shall be 16;
* When `sizeof(T)` is 4, `Nbits` shall be 24 or 32;
* When `sizeof(T)` is 8, `Nbits` shall be 40, 48, 56, or 64.
Other values of `sizeof(T)` are not supported.
When `Nbits` is equal to `sizeof(T)*8`, `T` must be a trivially copyable type
(such as `float`) that is assumed to have the same endianness as `uintNbits_t`.
When `Nbits` is less than `sizeof(T)*8`, `T` must be either a standard integral
type ({cpp}std, [basic.fundamental]) or an `enum`.
### Members
```
endian_buffer() noexcept = default;
```
[none]
* {blank}
+
Effects:: Constructs an uninitialized object.
```
explicit endian_buffer(T v) noexcept;
```
[none]
* {blank}
+
Effects:: `endian_store<T, Nbits/8, Order>( value_, v )`.
```
endian_buffer& operator=(T v) noexcept;
```
[none]
* {blank}
+
Effects:: `endian_store<T, Nbits/8, Order>( value_, v )`.
Returns:: `*this`.
```
value_type value() const noexcept;
```
[none]
* {blank}
+
Returns:: `endian_load<T, Nbits/8, Order>( value_ )`.
```
const char* data() const noexcept;
```
[none]
* {blank}
+
Returns::
A pointer to the first byte of `value_`.
### Non-member functions
```
template <class charT, class traits, order Order, class T,
std::size_t n_bits, align Align>
std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os,
const endian_buffer<Order, T, n_bits, Align>& x);
```
[none]
* {blank}
+
Returns:: `os << x.value()`.
```
template <class charT, class traits, order Order, class T,
std::size_t n_bits, align A>
std::basic_istream<charT, traits>& operator>>(std::basic_istream<charT, traits>& is,
endian_buffer<Order, T, n_bits, Align>& x);
```
[none]
* {blank}
+
Effects:: As if:
+
```
T i;
if (is >> i)
x = i;
```
Returns:: `is`.
## FAQ
See the <<overview_faq,Overview FAQ>> for a library-wide FAQ.
Why not just use Boost.Serialization?::
Serialization involves a conversion for every object involved in I/O. Endian
integers require no conversion or copying. They are already in the desired
format for binary I/O. Thus they can be read or written in bulk.
Are endian types PODs?::
Yes for {cpp}11. No for {cpp}03, although several
<<buffers_compilation,macros>> are available to force PODness in all cases.
What are the implications of endian integer types not being PODs with {cpp}03 compilers?::
They can't be used in unions. Also, compilers aren't required to align or lay
out storage in portable ways, although this potential problem hasn't prevented
use of Boost.Endian with real compilers.
What good is native endianness?::
It provides alignment and size guarantees not available from the built-in
types. It eases generic programming.
Why bother with the aligned endian types?::
Aligned integer operations may be faster (as much as 10 to 20 times faster) if
the endianness and alignment of the type matches the endianness and alignment
requirements of the machine. The code, however, is likely to be somewhat less
portable than with the unaligned types.
## Design considerations for Boost.Endian buffers
* Must be suitable for I/O - in other words, must be memcpyable.
* Must provide exactly the size and internal byte ordering specified.
* Must work correctly when the internal integer representation has more bits
that the sum of the bits in the external byte representation. Sign extension
must work correctly when the internal integer representation type has more
bits than the sum of the bits in the external bytes. For example, using
a 64-bit integer internally to represent 40-bit (5 byte) numbers must work for
both positive and negative values.
* Must work correctly (including using the same defined external
representation) regardless of whether a compiler treats char as signed or
unsigned.
* Unaligned types must not cause compilers to insert padding bytes.
* The implementation should supply optimizations with great care. Experience
has shown that optimizations of endian integers often become pessimizations
when changing machines or compilers. Pessimizations can also happen when
changing compiler switches, compiler versions, or CPU models of the same
architecture.
## {cpp}11
The availability of the {cpp}11
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm[Defaulted
Functions] feature is detected automatically, and will be used if present to
ensure that objects of `class endian_buffer` are trivial, and thus
PODs.
## Compilation
Boost.Endian is implemented entirely within headers, with no need to link to
any Boost object libraries.
Several macros allow user control over features:
* `BOOST_ENDIAN_NO_CTORS` causes `class endian_buffer` to have no
constructors. The intended use is for compiling user code that must be
portable between compilers regardless of {cpp}11
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm[Defaulted
Functions] support. Use of constructors will always fail,
* `BOOST_ENDIAN_FORCE_PODNESS` causes `BOOST_ENDIAN_NO_CTORS` to be defined if
the compiler does not support {cpp}11
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm[Defaulted
Functions]. This is ensures that objects of `class endian_buffer` are PODs, and
so can be used in {cpp}03 unions. In {cpp}11, `class endian_buffer` objects are
PODs, even though they have constructors, so can always be used in unions.

26
doc/endian/changelog.adoc Normal file
View File

@ -0,0 +1,26 @@
////
Copyright 2019 Peter Dimov
Distributed under the Boost Software License, Version 1.0.
See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt
////
[#changelog]
# Revision History
## Changes in 1.72.0
* Made `endian_reverse`, `conditional_reverse` and `\*\_to_*` `constexpr`
on GCC and Clang
* Added convenience load and store functions
* Added floating point convenience typedefs
## Changes in 1.71.0
* Clarified requirements on the value type template parameter
* Added support for `float` and `double`
* Added `endian_load`, `endian_store`
* Updated `endian_reverse` to correctly support all non-`bool` integral types
* Moved deprecated names to the deprecated header `endian.hpp`

View File

@ -0,0 +1,318 @@
////
Copyright 2011-2016 Beman Dawes
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
////
[#choosing]
# Choosing Approach
:idprefix: choosing_
## Introduction
Deciding which is the best endianness approach (conversion functions, buffer
types, or arithmetic types) for a particular application involves complex
engineering trade-offs. It is hard to assess those trade-offs without some
understanding of the different interfaces, so you might want to read the
<<conversion,conversion functions>>, <<buffers,buffer types>>, and
<<arithmetic,arithmetic types>> pages before diving into this page.
## Choosing between conversion functions, buffer types, and arithmetic types
The best approach to endianness for a particular application depends on the
interaction between the application's needs and the characteristics of each of
the three approaches.
*Recommendation:* If you are new to endianness, uncertain, or don't want to
invest the time to study engineering trade-offs, use
<<arithmetic,endian arithmetic types>>. They are safe, easy to use, and easy to
maintain. Use the _<<choosing_anticipating_need,anticipating need>>_ design
pattern locally around performance hot spots like lengthy loops, if needed.
### Background
A dealing with endianness usually implies a program portability or a data
portability requirement, and often both. That means real programs dealing with
endianness are usually complex, so the examples shown here would really be
written as multiple functions spread across multiple translation units. They
would involve interfaces that can not be altered as they are supplied by
third-parties or the standard library.
### Characteristics
The characteristics that differentiate the three approaches to endianness are
the endianness invariants, conversion explicitness, arithmetic operations, sizes
available, and alignment requirements.
#### Endianness invariants
*Endian conversion functions* use objects of the ordinary {cpp} arithmetic types
like `int` or `unsigned short` to hold values. That breaks the implicit
invariant that the {cpp} language rules apply. The usual language rules only apply
if the endianness of the object is currently set to the native endianness for
the platform. That can make it very hard to reason about logic flow, and result
in difficult to find bugs.
For example:
```
struct data_t // big endian
{
int32_t v1; // description ...
int32_t v2; // description ...
... additional character data members (i.e. non-endian)
int32_t v3; // description ...
};
data_t data;
read(data);
big_to_native_inplace(data.v1);
big_to_native_inplace(data.v2);
...
++v1;
third_party::func(data.v2);
...
native_to_big_inplace(data.v1);
native_to_big_inplace(data.v2);
write(data);
```
The programmer didn't bother to convert `data.v3` to native endianness because
that member isn't used. A later maintainer needs to pass `data.v3` to the
third-party function, so adds `third_party::func(data.v3);` somewhere deep in
the code. This causes a silent failure because the usual invariant that an
object of type `int32_t` holds a value as described by the {cpp} core language
does not apply.
*Endian buffer and arithmetic types* hold values internally as arrays of
characters with an invariant that the endianness of the array never changes.
That makes these types easier to use and programs easier to maintain.
Here is the same example, using an endian arithmetic type:
```
struct data_t
{
big_int32_t v1; // description ...
big_int32_t v2; // description ...
... additional character data members (i.e. non-endian)
big_int32_t v3; // description ...
};
data_t data;
read(data);
...
++v1;
third_party::func(data.v2);
...
write(data);
```
A later maintainer can add `third_party::func(data.v3)` and it will just-work.
#### Conversion explicitness
*Endian conversion functions* and *buffer types* never perform implicit
conversions. This gives users explicit control of when conversion occurs, and
may help avoid unnecessary conversions.
*Endian arithmetic types* perform conversion implicitly. That makes these types
very easy to use, but can result in unnecessary conversions. Failure to hoist
conversions out of inner loops can bring a performance penalty.
#### Arithmetic operations
*Endian conversion functions* do not supply arithmetic operations, but this is
not a concern since this approach uses ordinary {cpp} arithmetic types to hold
values.
*Endian buffer types* do not supply arithmetic operations. Although this
approach avoids unnecessary conversions, it can result in the introduction of
additional variables and confuse maintenance programmers.
*Endian arithmetic types* do supply arithmetic operations. They are very easy to
use if lots of arithmetic is involved.
### Sizes
*Endianness conversion functions* only support 1, 2, 4, and 8 byte integers.
That's sufficient for many applications.
*Endian buffer and arithmetic types* support 1, 2, 3, 4, 5, 6, 7, and 8 byte
integers. For an application where memory use or I/O speed is the limiting
factor, using sizes tailored to application needs can be useful.
#### Alignments
*Endianness conversion functions* only support aligned integer and
floating-point types. That's sufficient for most applications.
*Endian buffer and arithmetic types* support both aligned and unaligned
integer and floating-point types. Unaligned types are rarely needed, but when
needed they are often very useful and workarounds are painful. For example:
Non-portable code like this:
```
struct S {
uint16_t a; // big endian
uint32_t b; // big endian
} __attribute__ ((packed));
```
Can be replaced with portable code like this:
```
struct S {
big_uint16_ut a;
big_uint32_ut b;
};
```
### Design patterns
Applications often traffic in endian data as records or packets containing
multiple endian data elements. For simplicity, we will just call them records.
If desired endianness differs from native endianness, a conversion has to be
performed. When should that conversion occur? Three design patterns have
evolved.
#### Convert only as needed (i.e. lazy)
This pattern defers conversion to the point in the code where the data
element is actually used.
This pattern is appropriate when which endian element is actually used varies
greatly according to record content or other circumstances
[#choosing_anticipating_need]
#### Convert in anticipation of need
This pattern performs conversion to native endianness in anticipation of use,
such as immediately after reading records. If needed, conversion to the output
endianness is performed after all possible needs have passed, such as just
before writing records.
One implementation of this pattern is to create a proxy record with endianness
converted to native in a read function, and expose only that proxy to the rest
of the implementation. If a write function, if needed, handles the conversion
from native to the desired output endianness.
This pattern is appropriate when all endian elements in a record are typically
used regardless of record content or other circumstances.
#### Convert only as needed, except locally in anticipation of need
This pattern in general defers conversion but for specific local needs does
anticipatory conversion. Although particularly appropriate when coupled with the
endian buffer or arithmetic types, it also works well with the conversion
functions.
Example:
[subs=+quotes]
```
struct data_t
{
big_int32_t v1;
big_int32_t v2;
big_int32_t v3;
};
data_t data;
read(data);
...
++v1;
...
int32_t v3_temp = data.v3; // hoist conversion out of loop
for (int32_t i = 0; i < `large-number`; ++i)
{
... `lengthy computation that accesses v3_temp` ...
}
data.v3 = v3_temp;
write(data);
```
In general the above pseudo-code leaves conversion up to the endian arithmetic
type `big_int32_t`. But to avoid conversion inside the loop, a temporary is
created before the loop is entered, and then used to set the new value of
`data.v3` after the loop is complete.
Question: Won't the compiler's optimizer hoist the conversion out of the loop
anyhow?
Answer: V{cpp} 2015 Preview, and probably others, does not, even for a toy test
program. Although the savings is small (two register `bswap` instructions), the
cost might be significant if the loop is repeated enough times. On the other
hand, the program may be so dominated by I/O time that even a lengthy loop will
be immaterial.
### Use case examples
#### Porting endian unaware codebase
An existing codebase runs on big endian systems. It does not currently deal
with endianness. The codebase needs to be modified so it can run on little
endian systems under various operating systems. To ease transition and protect
value of existing files, external data will continue to be maintained as big
endian.
The <<arithmetic,endian arithmetic approach>> is recommended to meet these
needs. A relatively small number of header files dealing with binary I/O layouts
need to change types. For example, `short` or `int16_t` would change to
`big_int16_t`. No changes are required for `.cpp` files.
#### Porting endian aware codebase
An existing codebase runs on little-endian Linux systems. It already deals with
endianness via
http://man7.org/linux/man-pages/man3/endian.3.html[Linux provided functions].
Because of a business merger, the codebase has to be quickly modified for
Windows and possibly other operating systems, while still supporting Linux. The
codebase is reliable and the programmers are all well-aware of endian issues.
These factors all argue for an <<conversion, endian conversion approach>> that
just mechanically changes the calls to `htobe32`, etc. to
`boost::endian::native_to_big`, etc. and replaces `<endian.h>` with
`<boost/endian/conversion.hpp>`.
#### Reliability and arithmetic-speed
A new, complex, multi-threaded application is to be developed that must run
on little endian machines, but do big endian network I/O. The developers believe
computational speed for endian variable is critical but have seen numerous bugs
result from inability to reason about endian conversion state. They are also
worried that future maintenance changes could inadvertently introduce a lot of
slow conversions if full-blown endian arithmetic types are used.
The <<buffers,endian buffers>> approach is made-to-order for this use case.
#### Reliability and ease-of-use
A new, complex, multi-threaded application is to be developed that must run on
little endian machines, but do big endian network I/O. The developers believe
computational speed for endian variables is *not critical* but have seen
numerous bugs result from inability to reason about endian conversion state.
They are also concerned about ease-of-use both during development and long-term
maintenance.
Removing concern about conversion speed and adding concern about ease-of-use
tips the balance strongly in favor the
<<arithmetic,endian arithmetic approach>>.

559
doc/endian/conversion.adoc Normal file
View File

@ -0,0 +1,559 @@
////
Copyright 2011-2016 Beman Dawes
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
////
[#conversion]
# Endian Conversion Functions
:idprefix: conversion_
## Introduction
Header `boost/endian/conversion.hpp` provides byte order reversal and conversion
functions that convert objects of the built-in integer types between native,
big, or little endian byte ordering. User defined types are also supported.
## Reference
Functions are implemented `inline` if appropriate. For {cpp}03 compilers,
`noexcept` is elided. Boost scoped enum emulation is used so that the library
still works for compilers that do not support scoped enums.
### Definitions
*Endianness* refers to the ordering of bytes within internal or external
integers and other arithmetic data. Most-significant byte first is called
*big endian* ordering. Least-significant byte first is called
*little endian* ordering. Other orderings are possible and some CPU
architectures support both big and little ordering.
NOTE: The names are derived from
http://en.wikipedia.org/wiki/Jonathan_Swift[Jonathan Swift]'s satirical novel
_http://en.wikipedia.org/wiki/Gulliver's_Travels[Gulliver's Travels]_, where
rival kingdoms opened their soft-boiled eggs at different ends. Wikipedia has an
extensive description of https://en.wikipedia.org/wiki/Endianness[Endianness].
The standard integral types ({cpp}std 3.9.1) except `bool` are collectively
called the *endian types*.
### Header `<boost/endian/conversion.hpp>` Synopsis
[subs=+quotes]
```
#define BOOST_ENDIAN_INTRINSIC_MSG \
"`message describing presence or absence of intrinsics`"
namespace boost
{
namespace endian
{
enum class order
{
native = `see below`,
big = `see below`,
little = `see below`,
};
// Byte reversal functions
template <class Endian>
Endian endian_reverse(Endian x) noexcept;
template <class EndianReversible>
EndianReversible big_to_native(EndianReversible x) noexcept;
template <class EndianReversible>
EndianReversible native_to_big(EndianReversible x) noexcept;
template <class EndianReversible>
EndianReversible little_to_native(EndianReversible x) noexcept;
template <class EndianReversible>
EndianReversible native_to_little(EndianReversible x) noexcept;
template <order O1, order O2, class EndianReversible>
EndianReversible conditional_reverse(EndianReversible x) noexcept;
template <class EndianReversible>
EndianReversible conditional_reverse(EndianReversible x,
order order1, order order2) noexcept;
// In-place byte reversal functions
template <class EndianReversible>
void endian_reverse_inplace(EndianReversible& x) noexcept;
template <class EndianReversibleInplace>
void big_to_native_inplace(EndianReversibleInplace& x) noexcept;
template <class EndianReversibleInplace>
void native_to_big_inplace(EndianReversibleInplace& x) noexcept;
template <class EndianReversibleInplace>
void little_to_native_inplace(EndianReversibleInplace& x) noexcept;
template <class EndianReversibleInplace>
void native_to_little_inplace(EndianReversibleInplace& x) noexcept;
template <order O1, order O2, class EndianReversibleInplace>
void conditional_reverse_inplace(EndianReversibleInplace& x) noexcept;
template <class EndianReversibleInplace>
void conditional_reverse_inplace(EndianReversibleInplace& x,
order order1, order order2) noexcept;
// Generic load and store functions
template<class T, std::size_t N, order Order>
T endian_load( unsigned char const * p ) noexcept;
template<class T, std::size_t N, order Order>
void endian_store( unsigned char * p, T const & v ) noexcept;
// Convenience load functions
boost::int16_t load_little_s16( unsigned char const * p ) noexcept;
boost::uint16_t load_little_u16( unsigned char const * p ) noexcept;
boost::int16_t load_big_s16( unsigned char const * p ) noexcept;
boost::uint16_t load_big_u16( unsigned char const * p ) noexcept;
boost::int32_t load_little_s24( unsigned char const * p ) noexcept;
boost::uint32_t load_little_u24( unsigned char const * p ) noexcept;
boost::int32_t load_big_s24( unsigned char const * p ) noexcept;
boost::uint32_t load_big_u24( unsigned char const * p ) noexcept;
boost::int32_t load_little_s32( unsigned char const * p ) noexcept;
boost::uint32_t load_little_u32( unsigned char const * p ) noexcept;
boost::int32_t load_big_s32( unsigned char const * p ) noexcept;
boost::uint32_t load_big_u32( unsigned char const * p ) noexcept;
boost::int64_t load_little_s40( unsigned char const * p ) noexcept;
boost::uint64_t load_little_u40( unsigned char const * p ) noexcept;
boost::int64_t load_big_s40( unsigned char const * p ) noexcept;
boost::uint64_t load_big_u40( unsigned char const * p ) noexcept;
boost::int64_t load_little_s48( unsigned char const * p ) noexcept;
boost::uint64_t load_little_u48( unsigned char const * p ) noexcept;
boost::int64_t load_big_s48( unsigned char const * p ) noexcept;
boost::uint64_t load_big_u48( unsigned char const * p ) noexcept;
boost::int64_t load_little_s56( unsigned char const * p ) noexcept;
boost::uint64_t load_little_u56( unsigned char const * p ) noexcept;
boost::int64_t load_big_s56( unsigned char const * p ) noexcept;
boost::uint64_t load_big_u56( unsigned char const * p ) noexcept;
boost::int64_t load_little_s64( unsigned char const * p ) noexcept;
boost::uint64_t load_little_u64( unsigned char const * p ) noexcept;
boost::int64_t load_big_s64( unsigned char const * p ) noexcept;
boost::uint64_t load_big_u64( unsigned char const * p ) noexcept;
// Convenience store functions
void store_little_s16( unsigned char * p, boost::int16_t v ) noexcept;
void store_little_u16( unsigned char * p, boost::uint16_t v ) noexcept;
void store_big_s16( unsigned char * p, boost::int16_t v ) noexcept;
void store_big_u16( unsigned char * p, boost::uint16_t v ) noexcept;
void store_little_s24( unsigned char * p, boost::int32_t v ) noexcept;
void store_little_u24( unsigned char * p, boost::uint32_t v ) noexcept;
void store_big_s24( unsigned char * p, boost::int32_t v ) noexcept;
void store_big_u24( unsigned char * p, boost::uint32_t v ) noexcept;
void store_little_s32( unsigned char * p, boost::int32_t v ) noexcept;
void store_little_u32( unsigned char * p, boost::uint32_t v ) noexcept;
void store_big_s32( unsigned char * p, boost::int32_t v ) noexcept;
void store_big_u32( unsigned char * p, boost::uint32_t v ) noexcept;
void store_little_s40( unsigned char * p, boost::int64_t v ) noexcept;
void store_little_u40( unsigned char * p, boost::uint64_t v ) noexcept;
void store_big_s40( unsigned char * p, boost::int64_t v ) noexcept;
void store_big_u40( unsigned char * p, boost::uint64_t v ) noexcept;
void store_little_s48( unsigned char * p, boost::int64_t v ) noexcept;
void store_little_u48( unsigned char * p, boost::uint64_t v ) noexcept;
void store_big_s48( unsigned char * p, boost::int64_t v ) noexcept;
void store_big_u48( unsigned char * p, boost::uint64_t v ) noexcept;
void store_little_s56( unsigned char * p, boost::int64_t v ) noexcept;
void store_little_u56( unsigned char * p, boost::uint64_t v ) noexcept;
void store_big_s56( unsigned char * p, boost::int64_t v ) noexcept;
void store_big_u56( unsigned char * p, boost::uint64_t v ) noexcept;
void store_little_s64( unsigned char * p, boost::int64_t v ) noexcept;
void store_little_u64( unsigned char * p, boost::uint64_t v ) noexcept;
void store_big_s64( unsigned char * p, boost::int64_t v ) noexcept;
void store_big_u64( unsigned char * p, boost::uint64_t v ) noexcept;
} // namespace endian
} // namespace boost
```
The values of `order::little` and `order::big` shall not be equal to one
another.
The value of `order::native` shall be:
* equal to `order::big` if the execution environment is big endian, otherwise
* equal to `order::little` if the execution environment is little endian,
otherwise
* unequal to both `order::little` and `order::big`.
### Requirements
#### Template argument requirements
The template definitions in the `boost/endian/conversion.hpp` header refer to
various named requirements whose details are set out in the tables in this
subsection. In these tables, `T` is an object or reference type to be supplied
by a {cpp} program instantiating a template; `x` is a value of type (possibly
`const`) `T`; `mlx` is a modifiable lvalue of type `T`.
[#conversion_endianreversible]
##### EndianReversible requirements (in addition to `CopyConstructible`)
[%header,cols=3*]
|===
|Expression |Return |Requirements
|`endian_reverse(x)` |`T`
a|`T` is an endian type or a class type.
If `T` is an endian type, returns the value of `x` with the order of bytes
reversed.
If `T` is a class type, the function:
* Returns the value of `x` with the order of bytes reversed for all data members
of types or arrays of types that meet the `EndianReversible` requirements, and;
* Is a non-member function in the same namespace as `T` that can be found by
argument dependent lookup (ADL).
|===
[#conversion_endianreversibleinplace]
##### EndianReversibleInplace requirements (in addition to `CopyConstructible`)
[%header,cols=2*]
|===
|Expression |Requirements
|`endian_reverse_inplace(mlx)`
a|`T` is an endian type or a class type.
If `T` is an endian type, reverses the order of bytes in `mlx`.
If `T` is a class type, the function:
* Reverses the order of bytes of all data members of `mlx` that have types or
arrays of types that meet the `EndianReversible` or `EndianReversibleInplace`
requirements, and;
* Is a non-member function in the same namespace as `T` that can be found by
argument dependent lookup (ADL).
|===
NOTE: Because there is a function template for `endian_reverse_inplace` that
calls `endian_reverse`, only `endian_reverse` is required for a user-defined
type to meet the `EndianReversibleInplace` requirements. Although User-defined
types are not required to supply an `endian_reverse_inplace` function, doing so
may improve efficiency.
#### Customization points for user-defined types (UDTs)
This subsection describes requirements on the Endian library's implementation.
The library's function templates requiring
`<<conversion_endianreversible,EndianReversible>>` are required to perform
reversal of endianness if needed by making an unqualified call to
`endian_reverse()`.
The library's function templates requiring
`<<conversion_endianreversibleinplace,EndianReversibleInplace>>` are required to
perform reversal of endianness if needed by making an unqualified call to
`endian_reverse_inplace()`.
See `example/udt_conversion_example.cpp` for an example user-defined type.
### Byte Reversal Functions
```
template <class Endian>
Endian endian_reverse(Endian x) noexcept;
```
[none]
* {blank}
+
Requires:: `Endian` must be a standard integral type that is not `bool`.
Returns:: `x`, with the order of its constituent bytes reversed.
```
template <class EndianReversible>
EndianReversible big_to_native(EndianReversible x) noexcept;
```
[none]
* {blank}
+
Returns:: `conditional_reverse<order::big, order::native>(x)`.
```
template <class EndianReversible>
EndianReversible native_to_big(EndianReversible x) noexcept;
```
[none]
* {blank}
+
Returns:: `conditional_reverse<order::native, order::big>(x)`.
```
template <class EndianReversible>
EndianReversible little_to_native(EndianReversible x) noexcept;
```
[none]
* {blank}
+
Returns:: `conditional_reverse<order::little, order::native>(x)`.
```
template <class EndianReversible>
EndianReversible native_to_little(EndianReversible x) noexcept;
```
[none]
* {blank}
+
Returns:: `conditional_reverse<order::native, order::little>(x)`.
```
template <order O1, order O2, class EndianReversible>
EndianReversible conditional_reverse(EndianReversible x) noexcept;
```
[none]
* {blank}
+
Returns:: `x` if `O1 == O2,` otherwise `endian_reverse(x)`.
Remarks:: Whether `x` or `endian_reverse(x)` is to be returned shall be
determined at compile time.
```
template <class EndianReversible>
EndianReversible conditional_reverse(EndianReversible x,
order order1, order order2) noexcept;
```
[none]
* {blank}
+
Returns::
`order1 == order2? x: endian_reverse(x)`.
### In-place Byte Reversal Functions
```
template <class EndianReversible>
void endian_reverse_inplace(EndianReversible& x) noexcept;
```
[none]
* {blank}
+
Effects:: `x = endian_reverse(x)`.
```
template <class EndianReversibleInplace>
void big_to_native_inplace(EndianReversibleInplace& x) noexcept;
```
[none]
* {blank}
+
Effects:: `conditional_reverse_inplace<order::big, order::native>(x)`.
```
template <class EndianReversibleInplace>
void native_to_big_inplace(EndianReversibleInplace& x) noexcept;
```
[none]
* {blank}
+
Effects:: `conditional_reverse_inplace<order::native, order::big>(x)`.
```
template <class EndianReversibleInplace>
void little_to_native_inplace(EndianReversibleInplace& x) noexcept;
```
[none]
* {blank}
+
Effects:: `conditional_reverse_inplace<order::little, order::native>(x)`.
```
template <class EndianReversibleInplace>
void native_to_little_inplace(EndianReversibleInplace& x) noexcept;
```
[none]
* {blank}
+
Effects:: `conditional_reverse_inplace<order::native, order::little>(x)`.
```
template <order O1, order O2, class EndianReversibleInplace>
void conditional_reverse_inplace(EndianReversibleInplace& x) noexcept;
```
[none]
* {blank}
+
Effects:: None if `O1 == O2,` otherwise `endian_reverse_inplace(x)`.
Remarks:: Which effect applies shall be determined at compile time.
```
template <class EndianReversibleInplace>
void conditional_reverse_inplace(EndianReversibleInplace& x,
order order1, order order2) noexcept;
```
[none]
* {blank}
+
Effects::
If `order1 == order2` then `endian_reverse_inplace(x)`.
### Generic Load and Store Functions
```
template<class T, std::size_t N, order Order>
T endian_load( unsigned char const * p ) noexcept;
```
[none]
* {blank}
+
Requires:: `sizeof(T)` must be 1, 2, 4, or 8. `N` must be between 1 and
`sizeof(T)`, inclusive. `T` must be trivially copyable. If `N` is not
equal to `sizeof(T)`, `T` must be integral or `enum`.
Effects:: Reads `N` bytes starting from `p`, in forward or reverse order
depending on whether `Order` matches the native endianness or not,
interprets the resulting bit pattern as a value of type `T`, and returns it.
If `sizeof(T)` is bigger than `N`, zero-extends when `T` is unsigned,
sign-extends otherwise.
```
template<class T, std::size_t N, order Order>
void endian_store( unsigned char * p, T const & v ) noexcept;
```
[none]
* {blank}
+
Requires:: `sizeof(T)` must be 1, 2, 4, or 8. `N` must be between 1 and
`sizeof(T)`, inclusive. `T` must be trivially copyable. If `N` is not
equal to `sizeof(T)`, `T` must be integral or `enum`.
Effects:: Writes to `p` the `N` least significant bytes from the object
representation of `v`, in forward or reverse order depending on whether
`Order` matches the native endianness or not.
### Convenience Load Functions
```
inline boost::intM_t load_little_sN( unsigned char const * p ) noexcept;
```
[none]
* {blank}
+
Reads an N-bit signed little-endian integer from `p`.
+
Returns:: `endian_load<boost::intM_t, N/8, order::little>( p )`.
```
inline boost::uintM_t load_little_uN( unsigned char const * p ) noexcept;
```
[none]
* {blank}
+
Reads an N-bit unsigned little-endian integer from `p`.
+
Returns:: `endian_load<boost::uintM_t, N/8, order::little>( p )`.
```
inline boost::intM_t load_big_sN( unsigned char const * p ) noexcept;
```
[none]
* {blank}
+
Reads an N-bit signed big-endian integer from `p`.
+
Returns:: `endian_load<boost::intM_t, N/8, order::big>( p )`.
```
inline boost::uintM_t load_big_uN( unsigned char const * p ) noexcept;
```
[none]
* {blank}
+
Reads an N-bit unsigned big-endian integer from `p`.
+
Returns::
`endian_load<boost::uintM_t, N/8, order::big>( p )`.
### Convenience Store Functions
```
inline void store_little_sN( unsigned char * p, boost::intM_t v ) noexcept;
```
[none]
* {blank}
+
Writes an N-bit signed little-endian integer to `p`.
+
Effects:: `endian_store<boost::intM_t, N/8, order::little>( p, v )`.
```
inline void store_little_uN( unsigned char * p, boost::uintM_t v ) noexcept;
```
[none]
* {blank}
+
Writes an N-bit unsigned little-endian integer to `p`.
+
Effects:: `endian_store<boost::uintM_t, N/8, order::little>( p, v )`.
```
inline void store_big_sN( unsigned char * p, boost::intM_t v ) noexcept;
```
[none]
* {blank}
+
Writes an N-bit signed big-endian integer to `p`.
+
Effects:: `endian_store<boost::intM_t, N/8, order::big>( p, v )`.
```
inline void store_big_uN( unsigned char * p, boost::uintM_t v ) noexcept;
```
[none]
* {blank}
+
Writes an N-bit unsigned big-endian integer to `p`.
+
Effects::
`endian_store<boost::uintM_t, N/8, order::big>( p, v )`.
## FAQ
See the <<overview_faq,Overview FAQ>> for a library-wide FAQ.
*Why are both value returning and modify-in-place functions provided?*
* Returning the result by value is the standard C and {cpp} idiom for functions
that compute a value from an argument. Modify-in-place functions allow cleaner
code in many real-world endian use cases and are more efficient for user-defined
types that have members such as string data that do not need to be reversed.
Thus both forms are provided.
*Why not use the Linux names (htobe16, htole16, be16toh, le16toh, etc.) ?*
* Those names are non-standard and vary even between POSIX-like operating
systems. A {cpp} library TS was going to use those names, but found they were
sometimes implemented as macros. Since macros do not respect scoping and
namespace rules, to use them would be very error prone.
## Acknowledgements
Tomas Puverle was instrumental in identifying and articulating the need to
support endian conversion as separate from endian integer types. Phil Endecott
suggested the form of the value returning signatures. Vicente Botet and other
reviewers suggested supporting user defined types. General reverse template
implementation approach using `std::reverse` suggested by Mathias Gaunard.
Portable implementation approach for 16, 32, and 64-bit integers suggested by
tymofey, with avoidance of undefined behavior as suggested by Giovanni Piero
Deretta, and a further refinement suggested by Pyry Jahkola. Intrinsic builtins
implementation approach for 16, 32, and 64-bit integers suggested by several
reviewers, and by David Stone, who provided his Boost licensed macro
implementation that became the starting point for
`boost/endian/detail/intrinsic.hpp`. Pierre Talbot provided the
`int8_t endian_reverse()` and templated `endian_reverse_inplace()`
implementations.

View File

@ -0,0 +1,79 @@
////
Copyright 2011-2016 Beman Dawes
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
////
[#appendix_mini_review_topics]
[appendix]
# Endian Mini-Review
The results of the Boost.Endian formal review included a list of issues to be
resolved before a mini-review.
The issues are shown in *bold* below, with the resolution indicated.
Common use case scenarios should be developed.::
Done. The documentation have been refactored. A page is now devoted to
<<choosing,Choosing the Approach>> to endianness. See
<<choosing_use_cases,Use cases>> for use case scenarios.
Example programs should be developed for the common use case scenarios.::
Done. See <<choosing,Choosing the Approach>>. Example code has been added
throughout.
Documentation should illuminate the differences between endian integer/float type and endian conversion approaches to the common use case scenarios, and provide guidelines for choosing the most appropriate approach in user's applications.::
Done. See <<choosing,Choosing the Approach>>.
Conversion functions supplying results via return should be provided.::
Done. See <<conversion,Conversion Functions>>.
Platform specific performance enhancements such as use of compiler intrinsics or relaxed alignment requirements should be supported.::
Done. Compiler (Clang, GCC, Visual{cpp}, etc.) intrinsics and built-in
functions are used in the implementation where appropriate, as requested. See
<<overview_intrinsic,Built-in support for Intrinsics>>. See
<<overview_timings,Timings for Example 2>> to gauge the impact of intrinsics.
Endian integer (and floating) types should be implemented via the conversion functions. If that can't be done efficiently, consideration should be given to expanding the conversion function signatures to resolve the inefficiencies.::
Done. For the endian types, the implementation uses the endian conversion
functions, and thus the intrinsics, as requested.
Benchmarks that measure performance should be provided. It should be possible to compare platform specific performance enhancements against portable base implementations, and to compare endian integer approaches against endian conversion approaches for the common use case scenarios.::
Done. See <<overview_timings,Timings for Example 2>>. The `endian/test`
directory also contains several additional benchmark and speed test programs.
Float (32-bits) and double (64-bits) should be supported. IEEE 754 is the primary use case.::
Done. The <<buffers,endian buffer types>>,
<<arithmetic,endian arithmetic types>> and
<<conversion,endian conversion functions>> now support 32-bit `(float)`
and 64-bit `(double)` floating point, as requested.
Support for user defined types (UDTs) is desirable, and should be provided where there would be no conflict with the other concerns.::
Done. See <<conversion_customization,Customization points for user-defined
types (UDTs)>>.
There is some concern that endian integer/float arithmetic operations might used inadvertently or inappropriately. The impact of adding an endian_buffer class without arithmetic operations should be investigated.::
Done. The endian types have been decomposed into class template
`<<buffers,endian_buffer>>` and class template
`<<arithmetic,endian_arithmetic>>`. Class `endian_buffer` is a public base
class for `endian_arithmetic`, and can also be used by users as a stand-alone
class.
Stream insertion and extraction of the endian integer/float types should be documented and included in the test coverage.::
Done. See <<buffers_stream_inserter,Stream inserter>> and
<<buffers_stream_extractor,Stream extractor>>.
Binary I/O support that was investigated during development of the Endian library should be put up for mini-review for inclusion in the Boost I/O library.::
Not done yet. Will be handled as a separate min-review soon after the Endian
mini-review.
Other requested changes.::
In addition to the named-endianness conversion functions, functions that
perform compile-time (via template) and run-time (via function argument)
dispatch are now provided.
`order*native` is now a synonym for `order*big` or `order*little` according
to the endianness of the platform. This reduces the number of template
specializations required.
Headers have been reorganized to make them easier to read, with a synopsis
at the front and implementation following.

453
doc/endian/overview.adoc Normal file
View File

@ -0,0 +1,453 @@
////
Copyright 2011-2016 Beman Dawes
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
////
[#overview]
# Overview
:idprefix: overview_
## Abstract
Boost.Endian provides facilities to manipulate the
<<overview_endianness,endianness>> of integers and user-defined types.
* Three approaches to endianness are supported. Each has a long history of
successful use, and each approach has use cases where it is preferred over the
other approaches.
* Primary uses:
** Data portability. The Endian library supports binary data exchange, via
either external media or network transmission, regardless of platform
endianness.
** Program portability. POSIX-based and Windows-based operating systems
traditionally supply libraries with non-portable functions to perform endian
conversion. There are at least four incompatible sets of functions in common
use. The Endian library is portable across all {cpp} platforms.
* Secondary use: Minimizing data size via sizes and/or alignments not supported
by the standard {cpp} integer types.
[#overview_endianness]
## Introduction to endianness
Consider the following code:
```
int16_t i = 0x0102;
FILE * file = fopen("test.bin", "wb"); // binary file!
fwrite(&i, sizeof(int16_t), 1, file);
fclose(file);
```
On OS X, Linux, or Windows systems with an Intel CPU, a hex dump of the
"test.bin" output file produces:
```
0201
```
On OS X systems with a PowerPC CPU, or Solaris systems with a SPARC CPU, a hex
dump of the "test.bin" output file produces:
```
0102
```
What's happening here is that Intel CPUs order the bytes of an integer with the
least-significant byte first, while SPARC CPUs place the most-significant byte
first. Some CPUs, such as the PowerPC, allow the operating system to choose
which ordering applies.
Most-significant-byte-first ordering is traditionally called "big endian"
ordering and least-significant-byte-first is traditionally called
"little-endian" ordering. The names are derived from
http://en.wikipedia.org/wiki/Jonathan_Swift[Jonathan Swift]'s satirical novel
_http://en.wikipedia.org/wiki/Gulliver's_Travels[Gulliver's Travels]_, where
rival kingdoms opened their soft-boiled eggs at different ends.
See Wikipedia's http://en.wikipedia.org/wiki/Endianness[Endianness] article for
an extensive discussion of endianness.
Programmers can usually ignore endianness, except when reading a core dump on
little-endian systems. But programmers have to deal with endianness when
exchanging binary integers and binary floating point values between computer
systems with differing endianness, whether by physical file transfer or over a
network. And programmers may also want to use the library when minimizing either
internal or external data sizes is advantageous.
[#overview_introduction]
## Introduction to the Boost.Endian library
Boost.Endian provides three different approaches to dealing with endianness. All
three approaches support integers and user-define types (UDTs).
Each approach has a long history of successful use, and each approach has use
cases where it is preferred to the other approaches.
<<conversion,Endian conversion functions>>::
The application uses the built-in integer types to hold values, and calls the
provided conversion functions to convert byte ordering as needed. Both mutating
and non-mutating conversions are supplied, and each comes in unconditional and
conditional variants.
<<buffers, Endian buffer types>>::
The application uses the provided endian buffer types to hold values, and
explicitly converts to and from the built-in integer types. Buffer sizes of 8,
16, 24, 32, 40, 48, 56, and 64 bits (i.e. 1, 2, 3, 4, 5, 6, 7, and 8 bytes) are
provided. Unaligned integer buffer types are provided for all sizes, and aligned
buffer types are provided for 16, 32, and 64-bit sizes. The provided specific
types are typedefs for a generic class template that may be used directly for
less common use cases.
<<arithmetic, Endian arithmetic types>>::
The application uses the provided endian arithmetic types, which supply the same
operations as the built-in {cpp} arithmetic types. All conversions are implicit.
Arithmetic sizes of 8, 16, 24, 32, 40, 48, 56, and 64 bits (i.e. 1, 2, 3, 4, 5,
6, 7, and 8 bytes) are provided. Unaligned integer types are provided for all
sizes and aligned arithmetic types are provided for 16, 32, and 64-bit sizes.
The provided specific types are typedefs for a generic class template that may
be used directly in generic code of for less common use cases.
Boost Endian is a header-only library. {cpp}11 features affecting interfaces,
such as `noexcept`, are used only if available. See
<<overview_cpp03_support,{cpp}03 support for {cpp}11 features>> for details.
## Choosing between conversion functions, buffer types, and arithmetic types
This section has been moved to its own <<choosing,Choosing the Approach>> page.
[#overview_intrinsics]
## Built-in support for Intrinsics
Most compilers, including GCC, Clang, and Visual {cpp}, supply built-in support
for byte swapping intrinsics. The Endian library uses these intrinsics when
available since they may result in smaller and faster generated code,
particularly for optimized builds.
Defining the macro `BOOST_ENDIAN_NO_INTRINSICS` will suppress use of the
intrinsics. This is useful when a compiler has no intrinsic support or fails to
locate the appropriate header, perhaps because it is an older release or has
very limited supporting libraries.
The macro `BOOST_ENDIAN_INTRINSIC_MSG` is defined as either
`"no byte swap intrinsics"` or a string describing the particular set of
intrinsics being used. This is useful for eliminating missing intrinsics as a
source of performance issues.
## Performance
Consider this problem:
### Example 1
Add 100 to a big endian value in a file, then write the result to a file
[%header,cols=2*]
|===
|Endian arithmetic type approach |Endian conversion function approach
a|
----
big_int32_at x;
... read into x from a file ...
x += 100;
... write x to a file ...
----
a|
----
int32_t x;
... read into x from a file ...
big_to_native_inplace(x);
x += 100;
native_to_big_inplace(x);
... write x to a file ...
----
|===
*There will be no performance difference between the two approaches in optimized
builds, regardless of the native endianness of the machine.* That's because
optimizing compilers will generate exactly the same code for each. That
conclusion was confirmed by studying the generated assembly code for GCC and
Visual {cpp}. Furthermore, time spent doing I/O will determine the speed of this
application.
Now consider a slightly different problem:
### Example 2
Add a million values to a big endian value in a file, then write the result to a
file
[%header,cols=2*]
|===
|Endian arithmetic type approach |Endian conversion function approach
a|
----
big_int32_at x;
... read into x from a file ...
for (int32_t i = 0; i < 1000000; ++i)
x += i;
... write x to a file ...
----
a|
----
int32_t x;
... read into x from a file ...
big_to_native_inplace(x);
for (int32_t i = 0; i < 1000000; ++i)
x += i;
native_to_big_inplace(x);
... write x to a file ...
----
|===
With the Endian arithmetic approach, on little endian platforms an implicit
conversion from and then back to big endian is done inside the loop. With the
Endian conversion function approach, the user has ensured the conversions are
done outside the loop, so the code may run more quickly on little endian
platforms.
### Timings
These tests were run against release builds on a circa 2012 4-core little endian
X64 Intel Core i5-3570K CPU @ 3.40GHz under Windows 7.
CAUTION: The Windows CPU timer has very high granularity. Repeated runs of the
same tests often yield considerably different results.
See `test/loop_time_test.cpp` for the actual code and `benchmark/Jamfile.v2` for
the build setup.
#### GNU C++ version 4.8.2 on Linux virtual machine
Iterations: 10'000'000'000, Intrinsics: `__builtin_bswap16`, etc.
[%header,cols=3*]
|===
|Test Case |Endian arithmetic type |Endian conversion function
|16-bit aligned big endian |8.46 s |5.28 s
|16-bit aligned little endian |5.28 s |5.22 s
|32-bit aligned big endian |8.40 s |2.11 s
|32-bit aligned little endian |2.11 s |2.10 s
|64-bit aligned big endian |14.02 s |3.10 s
|64-bit aligned little endian |3.00 s |3.03 s
|===
#### Microsoft Visual C++ version 14.0
Iterations: 10'000'000'000, Intrinsics: `<cstdlib>` `_byteswap_ushort`, etc.
[%header,cols=3*]
|===
|Test Case |Endian arithmetic type |Endian conversion function
|16-bit aligned big endian |8.27 s |5.26 s
|16-bit aligned little endian |5.29 s |5.32 s
|32-bit aligned big endian |8.36 s |5.24 s
|32-bit aligned little endian |5.24 s |5.24 s
|64-bit aligned big endian |13.65 s |3.34 s
|64-bit aligned little endian |3.35 s |2.73 s
|===
[#overview_faq]
## Overall FAQ
Is the implementation header only?::
Yes.
Are {cpp}03 compilers supported?::
Yes.
Does the implementation use compiler intrinsic built-in byte swapping?::
Yes, if available. See <<overview_intrinsics,Intrinsic built-in support>>.
Why bother with endianness?::
Binary data portability is the primary use case.
Does endianness have any uses outside of portable binary file or network I/O formats?::
Using the unaligned integer types with a size tailored to the application's
needs is a minor secondary use that saves internal or external memory space. For
example, using `big_int40_buf_t` or `big_int40_t` in a large array saves a lot
of space compared to one of the 64-bit types.
Why bother with binary I/O? Why not just use {cpp} Standard Library stream inserters and extractors?::
* Data interchange formats often specify binary integer data. Binary integer
data is smaller and therefore I/O is faster and file sizes are smaller. Transfer
between systems is less expensive.
* Furthermore, binary integer data is of fixed size, and so fixed-size disk
records are possible without padding, easing sorting and allowing random access.
* Disadvantages, such as the inability to use text utilities on the resulting
files, limit usefulness to applications where the binary I/O advantages are
paramount.
Which is better, big-endian or little-endian?::
Big-endian tends to be preferred in a networking environment and is a bit more
of an industry standard, but little-endian may be preferred for applications
that run primarily on x86, x86-64, and other little-endian CPU's. The
http://en.wikipedia.org/wiki/Endian[Wikipedia] article gives more pros and cons.
Why are only big and little native endianness supported?::
These are the only endian schemes that have any practical value today. PDP-11
and the other middle endian approaches are interesting curiosities but have no
relevance for today's {cpp} developers. The same is true for architectures that
allow runtime endianness switching. The
<<conversion_native_order_specification,specification for native ordering>> has
been carefully crafted to allow support for such orderings in the future, should
the need arise. Thanks to Howard Hinnant for suggesting this.
Why do both the buffer and arithmetic types exist?::
Conversions in the buffer types are explicit. Conversions in the arithmetic
types are implicit. This fundamental difference is a deliberate design feature
that would be lost if the inheritance hierarchy were collapsed.
The original design provided only arithmetic types. Buffer types were requested
during formal review by those wishing total control over when conversion occurs.
They also felt that buffer types would be less likely to be misused by
maintenance programmers not familiar with the implications of performing a lot
of integer operations on the endian arithmetic integer types.
What is gained by using the buffer types rather than always just using the arithmetic types?::
Assurance that hidden conversions are not performed. This is of overriding
importance to users concerned about achieving the ultimate in terms of speed.
"Always just using the arithmetic types" is fine for other users. When the
ultimate in speed needs to be ensured, the arithmetic types can be used in the
same design patterns or idioms that would be used for buffer types, resulting in
the same code being generated for either types.
What are the limitations of integer support?::
Tests have only been performed on machines that use two's complement
arithmetic. The Endian conversion functions only support 16, 32, and 64-bit
aligned integers. The endian types only support 8, 16, 24, 32, 40, 48, 56, and
64-bit unaligned integers, and 8, 16, 32, and 64-bit aligned integers.
Why is there no floating point support?::
An attempt was made to support four-byte ``float``s and eight-byte
``double``s, limited to
http://en.wikipedia.org/wiki/IEEE_floating_point[IEEE 754] (also known as
ISO/IEC/IEEE 60559) floating point and further limited to systems where floating
point endianness does not differ from integer endianness. Even with those
limitations, support for floating point types was not reliable and was removed.
For example, simply reversing the endianness of a floating point number can
result in a signaling-NAN. For all practical purposes, binary serialization and
endianness for integers are one and the same problem. That is not true for
floating point numbers, so binary serialization interfaces and formats for
floating point does not fit well in an endian-based library.
## History
### Changes requested by formal review
The library was reworked from top to bottom to accommodate changes requested
during the formal review. See <<appendix_mini_review_topics,Mini-Review>>
page for details.
### Other changes since formal review
* Header `boost/endian/endian.hpp` has been renamed to
`boost/endian/arithmetic.hpp`. Headers
`boost/endian/conversion.hpp` and `boost/endian/buffers.hpp` have been added.
Infrastructure file names were changed accordingly.
* The endian arithmetic type aliases have been renamed, using a naming pattern
that is consistent for both integer and floating point, and a consistent set of
aliases supplied for the endian buffer types.
* The unaligned-type alias names still have the `_t` suffix, but the
aligned-type alias names now have an `_at` suffix.
* `endian_reverse()` overloads for `int8_t` and `uint8_t` have been added for
improved generality. (Pierre Talbot)
* Overloads of `endian_reverse_inplace()` have been replaced with a single
`endian_reverse_inplace()` template. (Pierre Talbot)
* For X86 and X64 architectures, which permit unaligned loads and stores,
unaligned little endian buffer and arithmetic types use regular loads and
stores when the size is exact. This makes unaligned little endian buffer and
arithmetic types significantly more efficient on these architectures. (Jeremy
Maitin-Shepard)
* {cpp}11 features affecting interfaces, such as `noexcept`, are now used.
{cpp}03 compilers are still supported.
* Acknowledgements have been updated.
## Compatibility with interim releases
Prior to the official Boost release, class template `endian_arithmetic` has been
used for a decade or more with the same functionality but under the name
`endian`. Other names also changed in the official release. If the macro
`BOOST_ENDIAN_DEPRECATED_NAMES` is defined, those old now deprecated names are
still supported. However, the class template `endian` name is only provided for
compilers supporting {cpp}11 template aliases. For {cpp}03 compilers, the name
will have to be changed to `endian_arithmetic`.
To support backward header compatibility, deprecated header
`boost/endian/endian.hpp` forwards to `boost/endian/arithmetic.hpp`. It requires
`BOOST_ENDIAN_DEPRECATED_NAMES` be defined. It should only be used while
transitioning to the official Boost release of the library as it will be removed
in some future release.
[#overview_cpp03_support]
## {cpp}03 support for {cpp}11 features
[%header,cols=2*]
|===
|{cpp}11 Feature
|Action with {cpp}03 Compilers
|Scoped enums
|Uses header
http://www.boost.org/libs/core/doc/html/core/scoped_enum.html[boost/core/scoped_enum.hpp]
to emulate {cpp}11 scoped enums.
|`noexcept`
|Uses `BOOST_NOEXCEPT` macro, which is defined as null for compilers not
supporting this {cpp}11 feature.
|{cpp}11 PODs
(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2342.htm[N2342])
|Takes advantage of {cpp}03 compilers that relax {cpp}03 POD rules, but see
Limitations <<buffers_limitations,here>> and <<arithmetic_limitations,here>>.
Also see macros for explicit POD control <<buffers_compilation,here>> and
<<arithmetic_compilation,here>>
|===
## Future directions
Standardization.::
The plan is to submit Boost.Endian to the {cpp} standards committee for possible
inclusion in a Technical Specification or the {cpp} standard itself.
Specializations for `numeric_limits`.::
Roger Leigh requested that all `boost::endian` types provide `numeric_limits`
specializations.
See https://github.com/boostorg/endian/issues/4[GitHub issue 4].
Character buffer support.::
Peter Dimov pointed out during the mini-review that getting and setting basic
arithmetic types (or `<cstdint>` equivalents) from/to an offset into an array of
unsigned char is a common need. See
http://lists.boost.org/Archives/boost/2015/01/219574.php[Boost.Endian
mini-review posting].
Out-of-range detection.::
Peter Dimov pointed suggested during the mini-review that throwing an exception
on buffer values being out-of-range might be desirable. See the end of
http://lists.boost.org/Archives/boost/2015/01/219659.php[this posting] and
subsequent replies.
## Acknowledgements
Comments and suggestions were received from Adder, Benaka Moorthi, Christopher
Kohlhoff, Cliff Green, Daniel James, Dave Handley, Gennaro Proto, Giovanni Piero
Deretta, Gordon Woodhull, dizzy, Hartmut Kaiser, Howard Hinnant, Jason Newton,
Jeff Flinn, Jeremy Maitin-Shepard, John Filo, John Maddock, Kim Barrett, Marsh
Ray, Martin Bonner, Mathias Gaunard, Matias Capeletto, Neil Mayhew, Nevin Liber,
Olaf van der Spek, Paul Bristow, Peter Dimov, Pierre Talbot, Phil Endecott,
Philip Bennefall, Pyry Jahkola, Rene Rivera, Robert Stewart, Roger Leigh, Roland
Schwarz, Scott McMurray, Sebastian Redl, Tim Blechmann, Tim Moore, tymofey,
Tomas Puverle, Vincente Botet, Yuval Ronen and Vitaly Budovsk. Apologies if
anyone has been missed.
The documentation was converted into Asciidoc format by Glen Fernandes.

View File

@ -1,97 +0,0 @@
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Floating Point Concerns</title>
<link href="styles.css" rel="stylesheet">
</head>
<body>
<h1>Floating Point Concerns</h1>
<p>Consider this simple implementation of <code>endian_reverse</code>:</p>
<blockquote>
<pre>template &lt;class T&gt;
inline T endian_reverse(T x) BOOST_NOEXCEPT
{
std::reverse(reinterpret_cast&lt;unsigned char*&gt;(&amp;x),
reinterpret_cast&lt;unsigned char*&gt;(&amp;x) + sizeof(T));
return x;
}</pre>
</blockquote>
<p><b>Under what conditions with this code fail?</b></p>
<p dir="ltr">It will fail if an object of type <code>T</code> has one or more
bit patterns that cause a failure. Failures usually occur when&nbsp; an invalid
or otherwise special bit pattern is loaded into or saved from a hardware
register.</p>
<p dir="ltr">The problem could in theory occur with both integers and floating
point numbers, but the <a href="http://en.wikipedia.org/wiki/Two's_complement">
two's complement integers</a> ubiquitous in modern computer architectures do not
have any invalid or otherwise special bit patterns that cause failure when
byte-wise reversed.</p>
<p dir="ltr">But floating point numbers are a different story. Even if we limit
discussion to IEEE 754 (aka ISO/IEC/IEEE 60559) binary representations of 4 and
8 byte sizes, several problems are easy to demonstrate:</p>
<ul>
<li dir="ltr">
<p dir="ltr">...</li>
</ul>
<h2 dir="ltr">Safe interfaces and possible reference implementations</h2>
<h3 dir="ltr">In-place interface</h3>
<blockquote>
<pre dir="ltr">template &lt;class T&gt;
inline void endian_reverse_inplace(T&amp; x)
{
std::reverse(reinterpret_cast&lt;unsigned char*&gt;(&amp;x),
reinterpret_cast&lt;unsigned char*&gt;(&amp;x) + sizeof(T));
}</pre>
</blockquote>
<p dir="ltr">This is the same as the current (i.e integer) customization point
interface, so there is no need for any change.</p>
<p dir="ltr"><b>Warning:</b> Even thought <code>x</code> may have had a valid
value on the originating platform, after calling this function the value of
<code>x</code> may differ or be invalid on this platform.</p>
<h3 dir="ltr">Copy interface</h3>
<blockquote>
<pre dir="ltr">template &lt;class T&gt;
inline void endian_reverse_copy(const T&amp; from, T&amp; to)
{
std::reverse_copy(reinterpret_cast&lt;const unsigned char*&gt;(&amp;from),
reinterpret_cast&lt;const unsigned char*&gt;(&amp;from) + sizeof(T),
reinterpret_cast&lt;unsigned char*&gt;(&amp;to));
}</pre>
</blockquote>
<p><b>Warning:</b> Even thought <code>from</code> may have been a valid value on
the originating platform, after calling this function the value of <code>to</code>
may differ or be invalid on this platform.</p>
<h3>Return-by-value interface</h3>
<blockquote>
<pre>template &lt;class T&gt;
inline T endian_reverse_to_native(<span style="background-color: #FFFF00">const</span><span style="background-color: #FFFF00"> T&amp;</span> x) BOOST_NOEXCEPT
{
T tmp;
std::reverse_copy(reinterpret_cast&lt;const unsigned char*&gt;(&amp;x),
reinterpret_cast&lt;const unsigned char*&gt;(&amp;x) + sizeof(T),
reinterpret_cast&lt;unsigned char*&gt;(&amp;tmp));
return tmp;
}</pre>
</blockquote>
<p><b>Warning:</b> Even thought <code>x</code> may have had a valid value on the
originating platform, the value of returned by this function may differ or be
invalid on this platform.</p>
<h2>Acknowledgements</h2>
<hr>
<p>Last revised: <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->27 March, 2015<!--webbot bot="Timestamp" endspan i-checksum="28924" --></p>
<p><EFBFBD> Copyright Beman Dawes, 2015</p>
<p>Distributed under the Boost Software License, Version 1.0. See <a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/ LICENSE_1_0.txt</a></p>
<p>&nbsp;</p>
</body>
</html>

View File

@ -1,606 +0,0 @@
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Endian Library</title>
<link href="styles.css" rel="stylesheet">
</head>
<body>
<table border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
<tr>
<td width="339">
<a href="../../../index.html">
<img src="../../../boost.png" alt="Boost logo" align="middle" border="0" width="277" height="86"></a></td>
<td align="middle" width="1253">
<b>
<font size="6">Endian Library</font></b></td>
</tr>
</table>
<table border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse"
bordercolor="#111111" bgcolor="#D7EEFF" width="100%">
<tr>
<td><b>
<a href="index.html">Endian Home</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="conversion.html">Conversion Functions</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="arithmetic.html">Arithmetic Types</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="buffers.html">Buffer Types</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="choosing_approach.html">Choosing Approach</a></b></td>
</tr>
</table>
<p></p>
<table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" align="right">
<tr>
<td width="100%" bgcolor="#D7EEFF" align="center">
<i><b>Contents</b></i></td>
</tr>
<tr>
<td width="100%" bgcolor="#E8F5FF">
<a href="#Abstract">Abstract</a><br>
<a href="#Introduction-to-endianness">Introduction to endianness</a><br>
<a href="#Introduction">Introduction to the Boost.Endian library</a><br>
<a href="#Choosing">Choosing between conversion functions,</a><br>
&nbsp; <a href="#Choosing">buffer types, and arithmetic types</a><br>
<a href="#Intrinsic">Built-in support for Intrinsics</a><br>
<a href="#Performance">Performance</a><br>
&nbsp;&nbsp;&nbsp;<a href="#Timings">Timings</a><br>
<a href="#FAQ">Overall FAQ</a><br>
<a href="#Release-history">Release history</a><br>
&nbsp;&nbsp;&nbsp;<a href="#Changes-requested-by-formal-review">Changes
requested by formal review</a><br>
&nbsp;&nbsp; <a href="#Other-changes-since-formal-review">Other changes since
formal review</a><br>
<a href="#Compatibility">Compatibility with interim releases</a><br>
<a href="#C++03-support">C++03 support for C++11 features</a><br>
<a href="#Future-directions">Future directions</a><br>
<a href="#Acknowledgements">Acknowledgements</a><br>
</td>
</tr>
</table>
<h2><a name="Abstract">Abstract</a></h2>
<p>Boost.Endian provides facilities to manipulate the
<a href="#Introduction-to-endianness">endianness</a> of integers and user-defined types.</p>
<ul>
<li>Three approaches to endianness are supported. Each has a
long history of successful use, and each approach has use cases where it is
preferred over the other approaches.<br>
&nbsp;</li>
<li>Primary uses:<br>
&nbsp;<ul>
<li>Data portability. The Endian library supports binary data exchange, via either external media or network transmission,
regardless of platform endianness.<br>
&nbsp;</li>
<li>Program portability. POSIX-based and
Windows-based operating systems traditionally supply libraries with
non-portable functions to perform endian conversion. There are at least four
incompatible sets of functions in common use. The Endian library is
portable across all C++ platforms.<br>
&nbsp;</li>
</ul>
</li>
<li>Secondary use: Minimizing data size via sizes and/or alignments not supported by the
standard C++ integer types.</li>
</ul>
<h2><a name="Introduction-to-endianness">Introduction to endianness</a></h2>
<p>Consider the following code:</p>
<blockquote>
<pre>int16_t i = 0x0102;
FILE * file = fopen(&quot;test.bin&quot;, &quot;wb&quot;); // binary file!
fwrite(&amp;i, sizeof(int16_t), 1, file);
fclose(file);</pre>
</blockquote>
<p>On OS X, Linux, or Windows systems with an Intel CPU, a hex dump
of the &quot;test.bin&quot; output file produces:</p>
<blockquote>
<p><code>0201</code></p>
</blockquote>
<p>On OS X systems with a PowerPC CPU, or Solaris systems with a SPARC CPU, a hex dump of the &quot;test.bin&quot;
output file produces:</p>
<blockquote>
<p><code>0102</code></p>
</blockquote>
<p>What's happening here is that Intel CPUs order the bytes of an integer with
the least-significant byte first, while SPARC CPUs place the most-significant
byte first. Some CPUs, such as the PowerPC, allow the operating system to
choose which ordering applies.</p>
<p><a name="definition"></a>Most-significant-byte-first ordering is traditionally called &quot;big endian&quot;
ordering and least-significant-byte-first is traditionally called
&quot;little-endian&quot; ordering. The names are derived from
<a href="http://en.wikipedia.org/wiki/Jonathan_Swift" title="Jonathan Swift">
Jonathan Swift</a>'s satirical novel <i>
<a href="http://en.wikipedia.org/wiki/Gulliver's_Travels" title="Gulliver's Travels">
Gullivers Travels</a></i>, where rival kingdoms opened their soft-boiled eggs
at different ends.</p>
<p>See Wikipedia's
<a href="http://en.wikipedia.org/wiki/Endianness">Endianness</a> article for an
extensive discussion of endianness.</p>
<p>Programmers can usually ignore endianness, except when reading a core
dump on little-endian systems. But programmers have to deal with endianness when exchanging binary integers and binary floating point
values between computer systems with differing endianness, whether by physical file transfer or over a network.
And programmers may also want to use the library when minimizing either internal or
external data sizes is advantageous.</p>
<h2><a name="Introduction">Introduction</a> to the Boost.Endian library</h2>
<p>Boost.Endian provides three different approaches to dealing with
endianness. All three approaches support integers and user-define types (UDTs).</p>
<p>Each approach has a long history of successful use, and each approach has use
cases where it is preferred to the other approaches.</p>
<blockquote>
<p><b><a href="conversion.html">Endian conversion functions</a> -</b> The
application uses the built-in integer types to hold values, and calls the
provided conversion functions to convert byte ordering as needed. Both mutating
and non-mutating conversions are supplied, and each comes in unconditional and
conditional variants.</p>
<p><b><a href="buffers.html">Endian buffer types</a> -</b> The application uses the provided endian
buffer types
to hold values, and explicitly converts to and from the built-in integer types. Buffer sizes of 8, 16, 24, 32, 40, 48, 56, and 64 bits (i.e.
1, 2, 3, 4, 5, 6, 7, and 8 bytes) are provided. Unaligned integer buffer types
are provided for all sizes, and aligned buffer types are provided for 16, 32, and
64-bit sizes. The provided specific types are typedefs for a generic class
template that may be used directly for less common use cases.</p>
<p><b><a href="arithmetic.html">Endian arithmetic types</a> -</b> The
application uses the provided endian arithmetic types, which supply the same
operations as the built-in C++ arithmetic types. All conversions are implicit.
Arithmetic sizes of 8, 16, 24, 32, 40, 48, 56, and 64 bits (i.e. 1, 2, 3, 4, 5,
6, 7, and 8 bytes) are provided. Unaligned integer types are provided for all
sizes and aligned
arithmetic types are provided for 16, 32, and 64-bit sizes. The provided
specific types are typedefs for a generic class template that may be used
directly in generic code of for less common use cases.</p>
</blockquote>
<p>Boost Endian is a header-only library. C++11 features
affecting interfaces, such as <code>noexcept</code>, are used only if available.
See <a href="#C++03-support">C++03 support for C++11 features</a> for details.</p>
<h2><a name="Choosing">Choosing</a> between conversion functions, buffer types,
and arithmetic types</h2>
<p>This section has been moved to its own <a href="choosing_approach.html">
Choosing the Approach</a> page. </p>
<h2>Built-in support for <a name="Intrinsic">Intrinsic</a>s</h2>
<p>Most compilers, including GCC, Clang, and Visual C++, supply built-in support for byte swapping intrinsics.
The Endian library uses these intrinsics when available since they may result in smaller and faster generated code, particularly for
optimized
builds.</p>
<p>Defining the macro <code>BOOST_ENDIAN_NO_INTRINSICS</code> will suppress use
of the intrinsics. This is useful when a compiler has no intrinsic support or
fails to locate the appropriate header, perhaps because it
is an older release or has very limited supporting libraries.</p>
<p>The macro <code>BOOST_ENDIAN_INTRINSIC_MSG</code> is defined as
either <code>&quot;no byte swap intrinsics&quot;</code> or a string describing the
particular set of intrinsics being used. This is useful for eliminating missing
intrinsics as a source of performance issues.</p>
<h2><a name="Performance">Performance</a></h2>
<p>Consider this problem:</p>
<div align="center">
<center>
<table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
<tr>
<td colspan="2">
<p align="center"><i><b><a name="Example-1">Example 1</a></b></i></td>
</tr>
<tr>
<td colspan="2"><b><i>Add 100 to a big endian value in a file, then write the
result to a file</i> </b> </td>
</tr>
<tr>
<td><i><b>Endian arithmetic type approach</b></i></td>
<td><i><b>Endian conversion function approach</b></i></td>
</tr>
<tr>
<td valign="top">
<pre>big_int32_at x;
... read into x from a file ...
x += 100;
... write x to a file ...
</pre>
</td>
<td>
<pre>
int32_t x;
... read into x from a file ...
big_to_native_inplace(x);
x += 100;
native_to_big_inplace(x);
... write x to a file ...
</pre>
</td>
</tr>
</table>
</center>
</div>
<p><b>There will be no performance difference between the two approaches in
optimized builds,
regardless of the native endianness of the machine.</b> That&#39;s because optimizing compilers will generate exactly the same code for each. That conclusion was confirmed by
studying the generated assembly code for GCC and Visual C++. Furthermore, time
spent doing I/O will determine the speed of this application.</p>
<p>Now consider a slightly different problem:&nbsp; </p>
<div align="center">
<center>
<table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
<tr>
<td colspan="2">
<p align="center"><b><i><a name="Example-2">Example 2</a></i></b></td>
</tr>
<tr>
<td colspan="2"><i><b>Add a million values to a big endian value in a file, then write the
result to a file </b></i> </td>
</tr>
<tr>
<td><i><b>Endian arithmetic type approach</b></i></td>
<td><i><b>Endian conversion function approach</b></i></td>
</tr>
<tr>
<td valign="top">
<pre>big_int32_at x;
... read into x from a file ...
for (int32_t i = 0; i &lt; 1000000; ++i)
x += i;
... write x to a file ...
</pre>
</td>
<td>
<pre>int32_t x;
... read into x from a file ...
big_to_native_inplace(x);
for (int32_t i = 0; i &lt; 1000000; ++i)
x += i;
native_to_big_inplace(x);
... write x to a file ...
</pre>
</td>
</tr>
</table>
</center>
</div>
<p>With the Endian arithmetic approach, on little endian platforms an implicit conversion from and then back to
big endian is done inside the loop. With the Endian conversion function
approach, the user has ensured the conversions are done outside the loop, so the
code may run more quickly on little endian platforms.</p>
<h3><a name="Timings">Timings</a></h3>
<p>These tests were run against release builds on a circa 2012 4-core little endian X64 Intel Core i5-3570K
CPU @ 3.40GHz under Windows 7.</p>
<p><b>Caveat emptor: The Windows CPU timer has very high granularity. Repeated
runs of the same tests often yield considerably different results.</b></p>
<p>See <b>test/loop_time_test.cpp</b> for the actual code and <b>benchmark/Jamfile.v2</b> for the build
setup.</p>
<div align="center">
<center>
<table border="1" cellpadding="5" cellspacing="0"style="border-collapse: collapse" bordercolor="#111111">
<tr><td colspan="6" align="center"><b>GNU C++ version 4.8.2 on Linux virtual
machine</b></td></tr>
<tr><td colspan="6" align="center"><b> Iterations: 10'000'000'000, Intrinsics: __builtin_bswap16, etc.</b></td></tr>
<tr><td><b>Test Case</b></td>
<td align="center"><b>Endian<br>arithmetic<br>type</b></td>
<td align="center"><b>Endian<br>conversion<br>function</b></td>
</tr>
<tr><td>16-bit aligned big endian</td><td align="right">8.46 s</td><td align="right">5.28 s</td></tr>
<tr><td>16-bit aligned little endian</td><td align="right">5.28 s</td><td align="right">5.22 s</td></tr>
<tr><td>32-bit aligned big endian</td><td align="right">8.40 s</td><td align="right">2.11 s</td></tr>
<tr><td>32-bit aligned little endian</td><td align="right">2.11 s</td><td align="right">2.10 s</td></tr>
<tr><td>64-bit aligned big endian</td><td align="right">14.02 s</td><td align="right">3.10 s</td></tr>
<tr><td>64-bit aligned little endian</td><td align="right">3.00 s</td><td align="right">3.03 s</td></tr>
</table>
</center>
</div>
<p></p>
<div align="center"> <center>
<table border="1" cellpadding="5" cellspacing="0"style="border-collapse: collapse" bordercolor="#111111">
<tr><td colspan="6" align="center"><b>Microsoft Visual C++ version 14.0</b></td></tr>
<tr><td colspan="6" align="center"><b> Iterations: 10'000'000'000, Intrinsics: cstdlib _byteswap_ushort, etc.</b></td></tr>
<tr><td><b>Test Case</b></td>
<td align="center"><b>Endian<br>arithmetic<br>type</b></td>
<td align="center"><b>Endian<br>conversion<br>function</b></td>
</tr>
<tr><td>16-bit aligned big endian</td><td align="right">8.27 s</td><td align="right">5.26 s</td></tr>
<tr><td>16-bit aligned little endian</td><td align="right">5.29 s</td><td align="right">5.32 s</td></tr>
<tr><td>32-bit aligned big endian</td><td align="right">8.36 s</td><td align="right">5.24 s</td></tr>
<tr><td>32-bit aligned little endian</td><td align="right">5.24 s</td><td align="right">5.24 s</td></tr>
<tr><td>64-bit aligned big endian</td><td align="right">13.65 s</td><td align="right">3.34 s</td></tr>
<tr><td>64-bit aligned little endian</td><td align="right">3.35 s</td><td align="right">2.73 s</td></tr>
</table>
</center></div>
<h2>Overall <a name="FAQ">FAQ</a></h2>
<p><b>Is the implementation header only?</b></p>
<blockquote>
<p>Yes.</p>
</blockquote>
<p><b>Are C++03 compilers supported?</b></p>
<blockquote>
<p>Yes.</p>
</blockquote>
<p><b>Does the implementation use compiler intrinsic built-in byte swapping?</b></p>
<blockquote>
<p>Yes, if available. See <a href="#Intrinsic">Intrinsic built-in support</a>.</p>
</blockquote>
<p><b>Why bother with endianness?</b></p>
<blockquote>
<p>Binary data portability is the primary use case.</p>
</blockquote>
<p><b>Does endianness have any uses outside of portable binary file or network
I/O formats?</b> </p>
<blockquote>
<p>Using the unaligned integer types with a size tailored to the application&#39;s
needs is a minor secondary use that saves internal or external memory space. For
example, using <code>big_int40_buf_t</code> or <code>big_int40_t</code> in a
large array saves a lot of space compared to one of the 64-bit types.</p>
</blockquote>
<p><b>Why bother with binary I/O? Why not just use C++ Standard Library stream
inserters and extractors?</b></p>
<blockquote>
<p>Data interchange formats often specify binary integer data.</p>
<p>Binary integer data is smaller and therefore I/O is faster and file sizes
are smaller. Transfer between systems is less expensive.</p>
<p >Furthermore, binary integer data is of fixed size, and so fixed-size disk
records are possible without padding, easing sorting and allowing random access.</p>
<p >Disadvantages, such as the inability to use text utilities on the
resulting files, limit usefulness to applications where the binary I/O
advantages are paramount.</p>
</blockquote>
<p><b>Which is better, big-endian or little-endian?</b></p>
<blockquote>
<p>Big-endian tends to be preferred in a networking environment and is a bit
more of an industry standard, but little-endian may be preferred for
applications that run primarily on x86, x86-64, and other little-endian
CPU's. The <a href="http://en.wikipedia.org/wiki/Endian">Wikipedia</a> article
gives more pros and cons.</p>
</blockquote>
<p><b>Why are only big and little native endianness supported?</b></p>
<blockquote>
<p>These are the only endian schemes that have any practical value today. PDP-11
and the other middle endian approaches are interesting curiosities
but have no relevance for today&#39;s C++ developers. The same is true for
architectures that allow runtime endianness switching. The
<a href="conversion.html#native-order-specification">specification for native
ordering</a> has been carefully crafted to allow support for such orderings in
the future, should the need arise. Thanks to Howard Hinnant for suggesting this. </p>
</blockquote>
<p><b>Why do both the buffer and arithmetic types exist?</b></p>
<blockquote>
<p>Conversions in the buffer types are explicit. Conversions in the arithmetic
types are implicit. This fundamental difference is a deliberate design feature
that would be lost if the inheritance hierarchy were collapsed.</p>
<p>The original design provided only arithmetic types. Buffer types were
requested during formal review by those wishing total control over when
conversion occurs. They also felt that buffer types would be less likely to be
misused by maintenance programmers not familiar with the implications of
performing a lot of integer operations on the endian arithmetic integer types.</p>
</blockquote>
<p><b>What is gained by using the buffer types rather than always just using the
arithmetic types?</b></p>
<blockquote>
<p>Assurance that hidden conversions are not performed. This is of overriding
importance to users concerned about achieving the ultimate in terms of speed. </p>
<p>&quot;Always just using the arithmetic types&quot; is fine for other users. When the
ultimate in speed needs to be ensured, the arithmetic types can be used in the
same design patterns or idioms that would be used for buffer types, resulting in
the same code being generated for either types.</p>
</blockquote>
<p><b>What are the limitations of integer support?</b></p>
<blockquote>
<p>Tests have only been
performed on machines that use two's complement arithmetic. The Endian
conversion functions only support 16, 32, and 64-bit aligned integers. The
endian types only support 8, 16, 24, 32, 40, 48, 56, and 64-bit unaligned integers,
and 8, 16, 32, and 64-bit aligned integers.</p>
</blockquote>
<p><b>Why is there no floating point support?</b></p>
<blockquote>
<p>An attempt was made to support four-byte <code>float</code>s and eight-byte
<code>double</code>s, limited to
<a href="http://en.wikipedia.org/wiki/IEEE_floating_point">IEEE 754</a> (also
know as ISO/IEC/IEEE 60559) floating point and further limited to systems where
floating point endianness does not differ from integer
endianness.</p>
<p>Even with those limitations, support for floating point types was not
reliable and was removed. For example, simply reversing the endianness of a
floating point number can result in a signaling-NAN. For all practical purposes,
binary serialization and endianness for integers are one and the same problem.
That is not true for floating point numbers, so binary serialization interfaces
and formats for floating point does not fit well in an endian-based library.</p>
</blockquote>
<h2><a name="Release-history">Release history</a></h2>
<h3><a name="Changes-requested-by-formal-review">Changes requested by formal review</a></h3>
<p>The library was reworked from top to bottom to accommodate changes requested
during the formal review. See <a href="mini_review_topics.html">Mini-Review</a>
page for details.</p>
<h3><a name="Other-changes-since-formal-review">Other changes since formal
review</a></h3>
<ul>
<li>Header <code>boost/endian/endian.hpp</code> has been renamed to <code>
boost/endian/arithmetic.hpp</code>. Headers
<code>boost/endian/conversion.hpp</code> and <code>boost/endian/buffers.hpp</code> have been
added.
Infrastructure file names were changed accordingly.</li>
<li>The endian arithmetic type aliases have been renamed,
using a naming pattern that is consistent for both integer and floating point,
and a consistent set of aliases supplied for the endian buffer types.</li>
<li>The unaligned-type alias names still have the <code>_t</code> suffix, but
the aligned-type alias names now have an <code>_at</code> suffix..</li>
<li><code>endian_reverse()</code> overloads for <code>int8_t</code> and <code>
uint8_t</code> have been added for improved generality. (Pierre Talbot)</li>
<li>Overloads of <code>endian_reverse_inplace()</code> have been replaced with a single <code>
endian_reverse_inplace()</code> template. (Pierre Talbot)</li>
<li>For X86 and X64 architectures, which permit unaligned loads and stores,
unaligned little endian buffer and arithmetic types use regular loads and
stores when the size is exact. This makes unaligned little endian buffer and
arithmetic types significantly more efficient on these architectures. (Jeremy
Maitin-Shepard)</li>
<li>C++11 features affecting interfaces, such as <code>noexcept</code>, are now used.
C++03 compilers are still
supported.</li>
<li>Acknowledgements have been updated.</li>
</ul>
<h2><a name="Compatibility">Compatibility</a> with interim releases</h2>
<p>Prior to the official Boost release, class template <code>
endian_arithmetic</code> has been used for a decade or more with the same
functionality but under the name <code>endian</code>. Other names also changed
in the official release. If the macro <code>BOOST_ENDIAN_DEPRECATED_NAMES</code>
is defined, those old now deprecated names are still supported. However, the
class template <code>endian</code> name is only provided for compilers
supporting C++11 template aliases. For C++03 compilers, the name will have to be
changed to <code>endian_arithmetic</code>.</p>
<p>To support backward header compatibility, deprecated header <code>boost/endian/endian.hpp</code>
forwards to <code>boost/endian/arithmetic.hpp</code>. It requires <code>
BOOST_ENDIAN_DEPRECATED_NAMES</code> be defined. It should only be used while
transitioning to the official Boost release of the library as it will be removed
in some future release.</p>
<h2><a name="C++03-support">C++03 support</a> for C++11 features</h2>
<table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
<tr>
<td><b>C++11 Feature</b></td>
<td><b>Action with C++03 Compilers </b></td>
</tr>
<tr>
<td>Scoped enums </td>
<td>Uses header <code class="computeroutput">
<a href="http://www.boost.org/libs/core/doc/html/core/scoped_enum.html">
<span class="identifier">boost</span><span class="special">/</span><span class="identifier">core</span><span class="special">/</span><span class="identifier">scoped_enum</span><span class="special">.</span><span class="identifier">hpp</span></a></code><span class="identifier">
to emulate C++11 scoped enums.</span></td>
</tr>
<tr>
<td><code>noexcept</code></td>
<td><span class="identifier">Uses BOOST_NOEXCEPT macro, which is defined as
null for compilers not supporting this C++11 feature.</span></td>
</tr>
<tr>
<td>C++11 PODs (<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2342.htm">N2342</a>)</td>
<td><span class="identifier">Takes advantage of C++03 compilers that
relax C++03 POD rules, but see Limitations
<a href="buffers.html#Limitations">here</a> and
<a href="arithmetic.html#Limitations">here</a>. Also see macros for explicit
POD control <a href="buffers.html#Compilation">here</a> and
<a href="arithmetic.html#Compilation">here</a>.</span></td>
</tr>
</table>
<h2><a name="Future-directions">Future directions</a></h2>
<p><b>Standardization.</b> The plan is to submit Boost.Endian to the C++
standards committee for possible inclusion in a Technical Specification or the
C++ standard itself.</p>
<p><b>Specializations for <code>numeric_limits</code>.</b> Roger Leigh
requested that all <code>boost::endian</code> types provide <code>numeric_limits</code>
specializations. See <a href="https://github.com/boostorg/endian/issues/4">
GitHub issue 4</a>.</p>
<p><b>Character buffer support.</b> Peter Dimov pointed out during the
mini-review that getting and setting basic arithmetic types (or <code>&lt;cstdint&gt;</code>
equivalents) from/to an offset into an array of unsigned char is a common need.
See <a href="http://lists.boost.org/Archives/boost/2015/01/219574.php">
Boost.Endian mini-review posting</a>.</p>
<p><b>Out-of-range detection.</b> Peter Dimov pointed suggested during the
mini-review that throwing an exception on buffer values being out-of-range might
be desirable. See the end of
<a href="http://lists.boost.org/Archives/boost/2015/01/219659.php">this posting</a>
and subsequent replies.</p>
<h2><a name="Acknowledgements">Acknowledgements</a></h2>
<p>Comments and suggestions were received from Adder, Benaka Moorthi,
Christopher Kohlhoff, Cliff Green, Daniel James, Dave Handley, Gennaro Proto, Giovanni Piero
Deretta, Gordon Woodhull, dizzy, Hartmut Kaiser, Howard Hinnant, Jason Newton, Jeff Flinn, Jeremy Maitin-Shepard, John Filo, John
Maddock, Kim Barrett, Marsh Ray, Martin Bonner, Mathias Gaunard, Matias
Capeletto, Neil Mayhew, Nevin Liber,
Olaf van der Spek, Paul Bristow, Peter Dimov, Pierre Talbot, Phil Endecott,
Philip Bennefall, Pyry Jahkola,
Rene Rivera, Robert Stewart, Roger Leigh, Roland Schwarz, Scott McMurray, Sebastian Redl, Tim
Blechmann, Tim Moore, tymofey, Tomas Puverle, Vincente Botet, Yuval Ronen and
Vitaly Budovsk. Apologies if anyone has been missed.</p>
<hr>
<p>Last revised:
<!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->05 April, 2016<!--webbot bot="Timestamp" endspan i-checksum="29990" --></p>
<p>© Copyright Beman Dawes, 2011, 2013</p>
<p>Distributed under the Boost Software License, Version 1.0. See
<a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/ LICENSE_1_0.txt</a></p>
</body>
</html>

View File

@ -1,150 +0,0 @@
<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Endian Mini-Review</title>
</head>
<link href="styles.css" rel="stylesheet">
<body>
<table border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
<tr>
<td width="339">
<a href="../../../index.html">
<img src="../../../boost.png" alt="Boost logo" align="middle" border="0" width="277" height="86"></a></td>
<td align="middle" width="1253">
<b>
<font size="6">Endian </font></b><font size="6"><b>Mini-Review</b></font></td>
</tr>
</table>
<table border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse"
bordercolor="#111111" bgcolor="#D7EEFF" width="100%">
<tr>
<td><b>
<a href="index.html">Endian Home</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="conversion.html">Conversion Functions</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="arithmetic.html">Arithmetic Types</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="buffers.html">Buffer Types</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="choosing_approach.html">Choosing Approach</a></b></td>
</tr>
</table>
<p></p>
<p>The results of the Boost.Endian formal review included a list of issues to be
resolved before a mini-review.</p>
<p>The issues are shown in <b>bold</b> below, with the resolution indicated.</p>
<p><b>1. Common use case scenarios should be developed.</b></p>
<blockquote>
<p>Done. The documentation have been refactored. A page is now devoted to
<a href="choosing_approach.html">Choosing the Approach</a> to endianness. See
<a href="choosing_approach.html#Use-cases">Use cases</a> for use case
scenarios.</p>
</blockquote>
<p><b>2. Example programs should be developed for the common use case scenarios.</b></p>
<blockquote>
<p>Done. See <a href="choosing_approach.html">Choosing the Approach</a>.
Example code has been added throughout.</p>
</blockquote>
<p><b>3. Documentation should illuminate the differences between endian
integer/float type and endian conversion approaches to the common use
case scenarios, and provide guidelines for choosing the most appropriate
approach in user's applications.</b></p>
<blockquote>
<p>Done. See <a href="choosing_approach.html">Choosing the Approach</a>.</p>
</blockquote>
<p><b>4 .Conversion functions supplying results via return should be provided.</b></p>
<blockquote>
<p>Done. See <a href="conversion.html">ConversionFunctions</a>.</p>
</blockquote>
<p><b>5. Platform specific performance enhancements such as use of compiler intrinsics or relaxed alignment requirements should be supported.</b></p>
<blockquote>
<p>Done. Compiler (Clang, GCC, VisualC++, etc.) intrinsics and built-in
functions are used in the implementation where appropriate, as requested. See
<a href="index.html#Intrinsic">Built-in support for Intrinsics</a>. See
<a href="index.html#Timings">Timings for Example 2</a> to gauge the impact of
intrinsics.</p>
</blockquote>
<p><b>6. Endian integer (and floating) types should be implemented via the
conversion functions. If that can't be done efficiently, consideration
should be given to expanding the conversion function signatures to
resolve the inefficiencies.</b></p>
<blockquote>
<p>Done. For the endian types, the implementation uses the endian conversion
functions, and thus the intrinsics, as requested.</p>
</blockquote>
<p><b>7. Benchmarks that measure performance should be provided. It should be
possible to compare platform specific performance enhancements against
portable base implementations, and to compare endian integer approaches
against endian conversion approaches for the common use case scenarios.</b></p>
<blockquote>
<p>Done. See <a href="index.html#Timings">Timings for Example 2</a>. The <code>endian/test</code> directory
also contains several additional benchmark and speed test programs.</p>
</blockquote>
<p><b>8. Float (32-bits) and double (64-bits) should be supported. IEEE 754 is
the primary use case.</b></p>
<blockquote>
<p>Done. The <a href="buffers.html">endian buffer types</a>,&nbsp;
<a href="arithmetic.html">endian arithmetic types</a> and
<a href="conversion.html">endian conversion functions</a> now support 32-bit (<code>float)</code>
and 64-bit <code>(double)</code> floating point, as requested.</p>
</blockquote>
<p><b>9. Support for user defined types (UDTs) is desirable, and should be
provided where there would be no conflict with the other concerns.</b></p>
<blockquote>
<p>Done. See <a href="conversion.html#Customization-points">Customization
points for user-defined types (UDTs)</a>.</p>
</blockquote>
<p><b>10. There is some concern that endian integer/float arithmetic operations
might used inadvertently or inappropriately. The impact of adding an endian_buffer
class without arithmetic operations should be investigated.</b></p>
<blockquote>
<p>Done. The endian types have been decomposed into class template <code>
<a href="buffers.html">endian_buffer</a></code> and class template <code>
<a href="arithmetic.html">endian_arithmetic</a></code>. Class
<code>endian_buffer</code> is a public base class for <code>endian_arithmetic</code>,
and can also be used by users as a stand-alone class.</p>
</blockquote>
<p><b>11. Stream insertion and extraction of the endian integer/float types should
be documented and included in the test coverage.</b></p>
<blockquote>
<p>Done. See <a href="buffers.html#Stream-inserter">Stream inserter</a> and
<a href="buffers.html#Stream-extractor">Stream extractor</a>.</p>
</blockquote>
<p><b>12. Binary I/O support that was investigated during development of the Endian
library should be put up for mini-review for inclusion in the Boost I/O
library.</b></p>
<blockquote>
<p>Not done yet. Will be handled as a separate min-review soon after the
Endian mini-review.</p>
</blockquote>
<p><b>13. Other requested changes.</b></p>
<blockquote>
<ul>
<li>In addition to the named-endianness conversion functions, functions that perform
compile-time (via template) and run-time (via function argument) dispatch
are now provided.</li>
<li><code>order::native</code> is now a synonym for <code>order::big</code>
or <code>order::little</code> according to the endianness of the platform. This reduces the number of template specializations required.</li>
<li>Headers have been reorganized to make them easier to read,
with a synopsis at the front and implementation following.</li>
</ul>
</blockquote>
<hr>
<p>Last revised:
<!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->19 January, 2015<!--webbot bot="Timestamp" endspan i-checksum="38903" --></p>
<p>© Copyright Beman Dawes, 2014</p>
<p>Distributed under the Boost Software License, Version 1.0. See
<a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/ LICENSE_1_0.txt</a></p>
</body>
</html>

View File

@ -1,31 +0,0 @@
Windows
-------
Prerequisites
Boost libraries available in boost-root\stage\lib. Example:
cd boost-root
.\bootstrap
.\b2 --with-system --with-chrono --with-timer link=shared stage
The provided Visual Studio solution (endian/test/msvc/endian.sln) has a property page
(endian/test/msvc/common.prop) with these Common Properties set (do not include the
double quotes):
VC++ Directores|Executable Directories: prefix default value with "..\..\..\..\..\stage\lib;"
(Click "Inherit from parent or project defaults" if not checked)
C/C++|General|Additional Include Directories: prefix default value with "..\..\..\..\..\stage\lib;"
Linker|General|Additional Library Directories: prefix default value with "..\..\..\..\..\stage\lib;"
C/C++|Preprocessor: prefix default value with "BOOST_ALL_DYN_LINK;"
IMPORTANT: If Preprocessor macros are supplied via a common property page,
<inherit from parent or project defaults> must be set for each project!
------------------------------------------------------------------------------------------
Copyright Beman Dawes, 2013
Distributed under the Boost Software License, Version 1.0.
See http://www.boost.org/LICENSE_1_0.txt

View File

@ -1,8 +0,0 @@
copy /y c:\boost\develop\libs\endian\doc\* d:\boost\endian-gh-pages
pushd d:\boost\endian-gh-pages
git commit -a -m "copy from develop"
git push
popd
rem Copyright Beman Dawes, 2014
rem Distributed under the Boost Software License, Version 1.0.
rem See www.boost.org/LICENSE_1_0.txt

View File

@ -1,20 +0,0 @@
body
{
font-family: arial, sans-serif;
max-width: 6.5in;
margin: 0px auto;
font-size: 85%;
}
ins {background-color: #CCFFCC;}
del {background-color: #FFCACA;}
pre {background-color: #D7EEFF; font-size: 95%; font-family: "courier new", courier, serif;}
code {font-size: 110%; font-family: "courier new", courier, serif;}
table {font-size: 100%;}
/*
<20> Copyright Beman Dawes, 2014
Distributed under the Boost Software License, Version 1.0.
See www.boost.org/LICENSE_1_0.txt
*/

View File

@ -1,106 +0,0 @@
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Endian Library Do List</title>
</head>
<body>
<h1>Endian Library TODO List</h1>
<h2>To Do</h2>
<h2>Format Review Comments</h2>
<h3 dir="ltr">Interesting</h3>
<ul>
<li dir="ltr">
<p dir="ltr">John Filo - &quot;Absolutely. I'd like to see support for float and
double, but<br>
even without those additions, I still vote yes.&quot; &quot;For those who deal with
non-native endian data, this library is<br>
extremely useful. It automatically eliminates a whole class of common<br>
programming errors when dealing with such data.&quot;<br>
&nbsp;</li>
<li dir="ltr">
<p dir="ltr">Hartmut Kaiser - &quot;Even if this is not a full review, I would like
to vote YES to include this <br>
library into Boost.
<p>Boost.Spirit is using (and shipping) with an older version of this library
<br>
for several years now and we never had any problems with its usage in <br>
Spirit. It is used as the underlying framework for the binary parsers and <br>
generators and it is functioning as advertised.</p>
<p>As a quick test I replaced the internal (older) version of Boost.Endian in
<br>
Spirit with the reviewed version. All of Spirits regression tests still <br>
pass. &quot;<br>
&nbsp;</li>
</ul>
<h3>Executive summary</h3>
<ul>
<li>Common use case scenarios should be developed.</li>
<li>Example programs should be developed for the common use case scenarios.</li>
<li>Documentation should illuminate the differences between endian
integer/float type and endian conversion approaches to the common use case
scenarios, and provide guidelines for choosing the most appropriate approach
for user's applications.</li>
<li>Conversion functions supplying results via <code>return</code> should be
provided.</li>
<li>Platform specific performance enhancements such as use of compiler
intrinsics or relaxed alignment requirements should be supported.</li>
<li>Endian integer (and floating) types should be implemented via the
conversion functions. If that can't be done efficiently, consideration should
be given to expanding the conversion function signatures to resolve the
inefficiencies.</li>
<li>Benchmarks that measure performance should be provided. It should be
possible to compare platform specific performance enhancements against
portable base implementations, and to compare endian integer approaches
against endian conversion approaches for the common use case scenarios.</li>
<li>Float (32-bits) and double (64-bits) should be supported. IEEE 754 is the
primary use case.</li>
<li>Support for user defined types (UDTs) is desirable, and should be
supported where there would be no conflict with the other concerns.</li>
<li>There is some concern that endian integer/float arithmetic operations
might used
inadvertently or inappropriately. The impact of adding an endian_buffer class without arithmetic
operations should be investigated.</li>
<li>Stream insertion and extraction of the endian integer/float types should
be documented and included in the test coverage.</li>
<li>Binary I/O support that was investigated during development of the Endian
library should be put up for min-review for inclusion in the Boost I/O
library.</li>
</ul>
<h3>Docs</h3>
<ul>
<li>one other point ... the help file seems to directly link to the c++
headers.<br>
this should be changed:<br>
<br>
* some browsers (at least chromium) will not display the header when clicking<br>
&nbsp;the link, but will save them on disk.<br>
<br>
* providing a direct link to the source code from the docs implies that the<br>
&nbsp;user will get some information that are necessary to use the library by<br>
&nbsp;reading the sources. imo, this is not the case for using boost.endian.<br>
<br>
* if a user opens integer.hpp, the first 60 lines just contain copyright, some<br>
&nbsp;historical notes, compiler-specific stuff, includes and ifdefs. imo, this is<br>
&nbsp;the implementation part, which should not be exposed to a user.<br>
<br>
so i'd suggest to completely remove the links to the c++ headers.<br>
&nbsp;</li>
</ul>
<hr>
<p>Last revised:
<!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->17 January, 2015<!--webbot bot="Timestamp" endspan i-checksum="38899" --></p>
<p>© Copyright Beman Dawes, 2012</p>
<p>Distributed under the Boost Software License, Version 1.0. See
<a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a></p>
<p>&nbsp;</p>
</body>
</html>

View File

@ -1,4 +1,4 @@
// endian/example/conversion_use_case.cpp
// endian/example/conversion_use_case.cpp
// Copyright Beman Dawes 2014
@ -47,5 +47,5 @@ int main()
out_rec.balance = boost::endian::native_to_big(out_rec.balance); // reverse if needed
out.write((const char*)&out_rec, sizeof(out_rec));
}
}

View File

@ -18,7 +18,7 @@
using namespace boost::endian;
namespace
namespace
{
// This is an extract from a very widely used GIS file format. Why the designer
// decided to mix big and little endians in the same file is not known. But
@ -39,7 +39,7 @@ namespace
int main(int, char* [])
{
header h;
BOOST_STATIC_ASSERT(sizeof(h) == 16U); // reality check
h.file_code = 0x01020304;
@ -54,7 +54,7 @@ int main(int, char* [])
// does bulk I/O operations, <cstdio> fopen/fwrite is used for I/O in this example.
std::FILE* fi = std::fopen(filename, "wb"); // MUST BE BINARY
if (!fi)
{
std::cout << "could not open " << filename << '\n';

View File

@ -121,7 +121,7 @@ using std::endl;
// Recommended approach when conversion time is a concern
//
// Conversion time is a concert with this application because (1) any conversions
// performed in the loop will consume a great deal of time and because (2)
// performed in the loop will consume a great deal of time and because (2)
// computation time will be much greater than I/O time.
{

View File

@ -2,6 +2,7 @@
// (C) Copyright Darin Adler 2000
// (C) Copyright Beman Dawes 2006, 2009, 2014
// (C) Copyright Peter Dimov 2019
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
@ -21,33 +22,25 @@
#ifndef BOOST_ENDIAN_ARITHMETIC_HPP
#define BOOST_ENDIAN_ARITHMETIC_HPP
#if defined(_MSC_VER)
# pragma warning(push)
#if defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable:4365) // conversion ... signed/unsigned mismatch
#endif
#ifdef BOOST_ENDIAN_LOG
# include <iostream>
#endif
#include <boost/endian/buffers.hpp>
#include <boost/core/scoped_enum.hpp>
#include <boost/predef/other/endian.h>
#include <boost/static_assert.hpp>
#include <boost/cstdint.hpp>
#include <boost/config.hpp>
#include <boost/config/workaround.hpp>
#include <iosfwd>
#include <climits>
#if defined(__BORLANDC__) || defined( __CODEGEARC__)
# pragma pack(push, 1)
#endif
#include <boost/config.hpp>
#include <boost/predef/detail/endian_compat.h>
#include <boost/endian/conversion.hpp>
#include <boost/endian/buffers.hpp>
#define BOOST_ENDIAN_MINIMAL_COVER_OPERATORS
#include <boost/endian/detail/cover_operators.hpp>
#undef BOOST_ENDIAN_MINIMAL_COVER_OPERATORS
#include <boost/type_traits/is_signed.hpp>
#include <boost/cstdint.hpp>
#include <boost/static_assert.hpp>
#include <boost/core/scoped_enum.hpp>
#include <iosfwd>
#include <climits>
# if CHAR_BIT != 8
# error Platforms with CHAR_BIT != 8 are not supported
# endif
@ -58,7 +51,8 @@
# define BOOST_ENDIAN_DEFAULT_CONSTRUCT = default; // C++0x
# endif
# if defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) && defined(BOOST_ENDIAN_FORCE_PODNESS)
// g++ pre-4.6 does not support unrestricted unions, but we have no Config macro for that
# if (defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) || BOOST_WORKAROUND(BOOST_GCC, < 40600)) && defined(BOOST_ENDIAN_FORCE_PODNESS)
# define BOOST_ENDIAN_NO_CTORS
# endif
@ -76,7 +70,7 @@ namespace endian
{
template <BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits,
BOOST_SCOPED_ENUM(align) A = align::no>
BOOST_SCOPED_ENUM(align) Align = align::no>
class endian_arithmetic;
// big endian signed integer aligned types
@ -103,6 +97,12 @@ namespace endian
typedef endian_arithmetic<order::little, uint32_t, 32, align::yes> little_uint32_at;
typedef endian_arithmetic<order::little, uint64_t, 64, align::yes> little_uint64_at;
// aligned floating point types
typedef endian_arithmetic<order::big, float, 32, align::yes> big_float32_at;
typedef endian_arithmetic<order::big, double, 64, align::yes> big_float64_at;
typedef endian_arithmetic<order::little, float, 32, align::yes> little_float32_at;
typedef endian_arithmetic<order::little, double, 64, align::yes> little_float64_at;
// aligned native endian typedefs are not provided because
// <cstdint> types are superior for this use case
@ -146,7 +146,7 @@ namespace endian
typedef endian_arithmetic<order::little, uint_least64_t, 56> little_uint56_t;
typedef endian_arithmetic<order::little, uint_least64_t, 64> little_uint64_t;
# ifdef BOOST_BIG_ENDIAN
# if BOOST_ENDIAN_BIG_BYTE
// native endian signed integer unaligned types
typedef big_int8_t native_int8_t;
typedef big_int16_t native_int16_t;
@ -188,216 +188,164 @@ namespace endian
typedef little_uint64_t native_uint64_t;
# endif
# ifdef BOOST_ENDIAN_DEPRECATED_NAMES
typedef order endianness;
typedef align alignment;
# ifndef BOOST_NO_CXX11_TEMPLATE_ALIASES
template <BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits,
BOOST_SCOPED_ENUM(align) Align = align::no>
using endian = endian_arithmetic<Order, T, n_bits, Align>;
# endif
// unaligned big endian signed integer types
typedef endian_arithmetic< order::big, int_least8_t, 8 > big8_t;
typedef endian_arithmetic< order::big, int_least16_t, 16 > big16_t;
typedef endian_arithmetic< order::big, int_least32_t, 24 > big24_t;
typedef endian_arithmetic< order::big, int_least32_t, 32 > big32_t;
typedef endian_arithmetic< order::big, int_least64_t, 40 > big40_t;
typedef endian_arithmetic< order::big, int_least64_t, 48 > big48_t;
typedef endian_arithmetic< order::big, int_least64_t, 56 > big56_t;
typedef endian_arithmetic< order::big, int_least64_t, 64 > big64_t;
// unaligned big endian_arithmetic unsigned integer types
typedef endian_arithmetic< order::big, uint_least8_t, 8 > ubig8_t;
typedef endian_arithmetic< order::big, uint_least16_t, 16 > ubig16_t;
typedef endian_arithmetic< order::big, uint_least32_t, 24 > ubig24_t;
typedef endian_arithmetic< order::big, uint_least32_t, 32 > ubig32_t;
typedef endian_arithmetic< order::big, uint_least64_t, 40 > ubig40_t;
typedef endian_arithmetic< order::big, uint_least64_t, 48 > ubig48_t;
typedef endian_arithmetic< order::big, uint_least64_t, 56 > ubig56_t;
typedef endian_arithmetic< order::big, uint_least64_t, 64 > ubig64_t;
// unaligned little endian_arithmetic signed integer types
typedef endian_arithmetic< order::little, int_least8_t, 8 > little8_t;
typedef endian_arithmetic< order::little, int_least16_t, 16 > little16_t;
typedef endian_arithmetic< order::little, int_least32_t, 24 > little24_t;
typedef endian_arithmetic< order::little, int_least32_t, 32 > little32_t;
typedef endian_arithmetic< order::little, int_least64_t, 40 > little40_t;
typedef endian_arithmetic< order::little, int_least64_t, 48 > little48_t;
typedef endian_arithmetic< order::little, int_least64_t, 56 > little56_t;
typedef endian_arithmetic< order::little, int_least64_t, 64 > little64_t;
// unaligned little endian_arithmetic unsigned integer types
typedef endian_arithmetic< order::little, uint_least8_t, 8 > ulittle8_t;
typedef endian_arithmetic< order::little, uint_least16_t, 16 > ulittle16_t;
typedef endian_arithmetic< order::little, uint_least32_t, 24 > ulittle24_t;
typedef endian_arithmetic< order::little, uint_least32_t, 32 > ulittle32_t;
typedef endian_arithmetic< order::little, uint_least64_t, 40 > ulittle40_t;
typedef endian_arithmetic< order::little, uint_least64_t, 48 > ulittle48_t;
typedef endian_arithmetic< order::little, uint_least64_t, 56 > ulittle56_t;
typedef endian_arithmetic< order::little, uint_least64_t, 64 > ulittle64_t;
// unaligned native endian_arithmetic signed integer types
typedef endian_arithmetic< order::native, int_least8_t, 8 > native8_t;
typedef endian_arithmetic< order::native, int_least16_t, 16 > native16_t;
typedef endian_arithmetic< order::native, int_least32_t, 24 > native24_t;
typedef endian_arithmetic< order::native, int_least32_t, 32 > native32_t;
typedef endian_arithmetic< order::native, int_least64_t, 40 > native40_t;
typedef endian_arithmetic< order::native, int_least64_t, 48 > native48_t;
typedef endian_arithmetic< order::native, int_least64_t, 56 > native56_t;
typedef endian_arithmetic< order::native, int_least64_t, 64 > native64_t;
// unaligned native endian_arithmetic unsigned integer types
typedef endian_arithmetic< order::native, uint_least8_t, 8 > unative8_t;
typedef endian_arithmetic< order::native, uint_least16_t, 16 > unative16_t;
typedef endian_arithmetic< order::native, uint_least32_t, 24 > unative24_t;
typedef endian_arithmetic< order::native, uint_least32_t, 32 > unative32_t;
typedef endian_arithmetic< order::native, uint_least64_t, 40 > unative40_t;
typedef endian_arithmetic< order::native, uint_least64_t, 48 > unative48_t;
typedef endian_arithmetic< order::native, uint_least64_t, 56 > unative56_t;
typedef endian_arithmetic< order::native, uint_least64_t, 64 > unative64_t;
// aligned native endian_arithmetic typedefs are not provided because
// <cstdint> types are superior for this use case
typedef endian_arithmetic< order::big, int16_t, 16, align::yes > aligned_big16_t;
typedef endian_arithmetic< order::big, uint16_t, 16, align::yes > aligned_ubig16_t;
typedef endian_arithmetic< order::little, int16_t, 16, align::yes > aligned_little16_t;
typedef endian_arithmetic< order::little, uint16_t, 16, align::yes > aligned_ulittle16_t;
typedef endian_arithmetic< order::big, int32_t, 32, align::yes > aligned_big32_t;
typedef endian_arithmetic< order::big, uint32_t, 32, align::yes > aligned_ubig32_t;
typedef endian_arithmetic< order::little, int32_t, 32, align::yes > aligned_little32_t;
typedef endian_arithmetic< order::little, uint32_t, 32, align::yes > aligned_ulittle32_t;
typedef endian_arithmetic< order::big, int64_t, 64, align::yes > aligned_big64_t;
typedef endian_arithmetic< order::big, uint64_t, 64, align::yes > aligned_ubig64_t;
typedef endian_arithmetic< order::little, int64_t, 64, align::yes > aligned_little64_t;
typedef endian_arithmetic< order::little, uint64_t, 64, align::yes > aligned_ulittle64_t;
# endif
// unaligned floating point types
typedef endian_arithmetic<order::big, float, 32, align::no> big_float32_t;
typedef endian_arithmetic<order::big, double, 64, align::no> big_float64_t;
typedef endian_arithmetic<order::little, float, 32, align::no> little_float32_t;
typedef endian_arithmetic<order::little, double, 64, align::no> little_float64_t;
typedef endian_arithmetic<order::native, float, 32, align::no> native_float32_t;
typedef endian_arithmetic<order::native, double, 64, align::no> native_float64_t;
//---------------------------------- end synopsis ------------------------------------//
// endian class template specializations ---------------------------------------------//
template <BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits,
BOOST_SCOPED_ENUM(align) Align>
class endian_arithmetic:
public endian_buffer<Order, T, n_bits, Align>
{
private:
// Specializations that represent unaligned bytes.
// Taking an integer type as a parameter provides a nice way to pass both
// the size and signness of the desired integer and get the appropriate
// corresponding integer type for the interface.
typedef endian_buffer<Order, T, n_bits, Align> inherited;
// unaligned integer big endian specialization
template <typename T, std::size_t n_bits>
class endian_arithmetic< order::big, T, n_bits, align::no >
: public endian_buffer< order::big, T, n_bits, align::no >,
cover_operators<endian_arithmetic<order::big, T, n_bits>, T>
public:
typedef T value_type;
#ifndef BOOST_ENDIAN_NO_CTORS
endian_arithmetic() BOOST_ENDIAN_DEFAULT_CONSTRUCT
BOOST_ENDIAN_EXPLICIT_OPT endian_arithmetic( T val ) BOOST_NOEXCEPT: inherited( val )
{
BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
public:
typedef T value_type;
# ifndef BOOST_ENDIAN_NO_CTORS
endian_arithmetic() BOOST_ENDIAN_DEFAULT_CONSTRUCT
BOOST_ENDIAN_EXPLICIT_OPT endian_arithmetic(T val) BOOST_NOEXCEPT
{
# ifdef BOOST_ENDIAN_LOG
if ( endian_log )
std::cout << "big, unaligned, " << n_bits << "-bits, construct(" << val << ")\n";
# endif
detail::store_big_endian<T, n_bits/8>(this->m_value, val);
}
# endif
endian_arithmetic& operator=(T val) BOOST_NOEXCEPT
{ detail::store_big_endian<T, n_bits/8>(this->m_value, val); return *this; }
operator value_type() const BOOST_NOEXCEPT { return this->value(); }
};
}
// unaligned little endian specialization
template <typename T, std::size_t n_bits>
class endian_arithmetic< order::little, T, n_bits, align::no >
: public endian_buffer< order::little, T, n_bits, align::no >,
cover_operators< endian_arithmetic< order::little, T, n_bits >, T >
#endif
endian_arithmetic& operator=( T val ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
public:
typedef T value_type;
# ifndef BOOST_ENDIAN_NO_CTORS
endian_arithmetic() BOOST_ENDIAN_DEFAULT_CONSTRUCT
BOOST_ENDIAN_EXPLICIT_OPT endian_arithmetic(T val) BOOST_NOEXCEPT
{
# ifdef BOOST_ENDIAN_LOG
if ( endian_log )
std::cout << "little, unaligned, " << n_bits << "-bits, construct(" << val << ")\n";
# endif
detail::store_little_endian<T, n_bits/8>(this->m_value, val);
}
# endif
endian_arithmetic& operator=(T val) BOOST_NOEXCEPT
{ detail::store_little_endian<T, n_bits/8>(this->m_value, val); return *this; }
operator value_type() const BOOST_NOEXCEPT { return this->value(); }
};
inherited::operator=( val );
return *this;
}
// align::yes specializations; only n_bits == 16/32/64 supported
// aligned big endian specialization
template <typename T, std::size_t n_bits>
class endian_arithmetic<order::big, T, n_bits, align::yes>
: public endian_buffer< order::big, T, n_bits, align::yes >,
cover_operators<endian_arithmetic<order::big, T, n_bits, align::yes>, T>
operator value_type() const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 );
public:
typedef T value_type;
# ifndef BOOST_ENDIAN_NO_CTORS
endian_arithmetic() BOOST_ENDIAN_DEFAULT_CONSTRUCT
BOOST_ENDIAN_EXPLICIT_OPT endian_arithmetic(T val) BOOST_NOEXCEPT
{
# ifdef BOOST_ENDIAN_LOG
if ( endian_log )
std::cout << "big, aligned, " << n_bits << "-bits, construct(" << val << ")\n";
# endif
this->m_value = ::boost::endian::native_to_big(val);
}
return this->value();
}
# endif
endian_arithmetic& operator=(T val) BOOST_NOEXCEPT
{
this->m_value = ::boost::endian::native_to_big(val);
return *this;
}
operator value_type() const BOOST_NOEXCEPT { return this->value(); }
};
// operators
// aligned little endian specialization
template <typename T, std::size_t n_bits>
class endian_arithmetic<order::little, T, n_bits, align::yes>
: public endian_buffer< order::little, T, n_bits, align::yes >,
cover_operators<endian_arithmetic<order::little, T, n_bits, align::yes>, T>
T operator+() const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 );
public:
typedef T value_type;
# ifndef BOOST_ENDIAN_NO_CTORS
endian_arithmetic() BOOST_ENDIAN_DEFAULT_CONSTRUCT
BOOST_ENDIAN_EXPLICIT_OPT endian_arithmetic(T val) BOOST_NOEXCEPT
return this->value();
}
endian_arithmetic& operator+=( T y ) BOOST_NOEXCEPT
{
*this = static_cast<T>( this->value() + y );
return *this;
}
endian_arithmetic& operator-=( T y ) BOOST_NOEXCEPT
{
*this = static_cast<T>( this->value() - y );
return *this;
}
endian_arithmetic& operator*=( T y ) BOOST_NOEXCEPT
{
*this = static_cast<T>( this->value() * y );
return *this;
}
endian_arithmetic& operator/=( T y ) BOOST_NOEXCEPT
{
*this = static_cast<T>( this->value() / y );
return *this;
}
endian_arithmetic& operator%=( T y ) BOOST_NOEXCEPT
{
*this = static_cast<T>( this->value() % y );
return *this;
}
endian_arithmetic& operator&=( T y ) BOOST_NOEXCEPT
{
*this = static_cast<T>( this->value() & y );
return *this;
}
endian_arithmetic& operator|=( T y ) BOOST_NOEXCEPT
{
*this = static_cast<T>( this->value() | y );
return *this;
}
endian_arithmetic& operator^=( T y ) BOOST_NOEXCEPT
{
*this = static_cast<T>( this->value() ^ y );
return *this;
}
endian_arithmetic& operator<<=( T y ) BOOST_NOEXCEPT
{
*this = static_cast<T>( this->value() << y );
return *this;
}
endian_arithmetic& operator>>=( T y ) BOOST_NOEXCEPT
{
*this = static_cast<T>( this->value() >> y );
return *this;
}
endian_arithmetic& operator++() BOOST_NOEXCEPT
{
*this += 1;
return *this;
}
endian_arithmetic& operator--() BOOST_NOEXCEPT
{
*this -= 1;
return *this;
}
endian_arithmetic operator++(int) BOOST_NOEXCEPT
{
endian_arithmetic tmp( *this );
*this += 1;
return tmp;
}
endian_arithmetic operator--(int) BOOST_NOEXCEPT
{
endian_arithmetic tmp( *this );
*this -= 1;
return tmp;
}
template<class Ch, class Tr>
friend std::basic_ostream<Ch, Tr>&
operator<<( std::basic_ostream<Ch, Tr>& os, endian_arithmetic const& x )
{
return os << x.value();
}
template<class Ch, class Tr>
friend std::basic_istream<Ch, Tr>&
operator>>( std::basic_istream<Ch, Tr>& is, endian_arithmetic& x )
{
T i;
if( is >> i )
{
# ifdef BOOST_ENDIAN_LOG
if ( endian_log )
std::cout << "little, aligned, " << n_bits << "-bits, construct(" << val << ")\n";
# endif
this->m_value = ::boost::endian::native_to_little(val);
x = i;
}
# endif
endian_arithmetic& operator=(T val) BOOST_NOEXCEPT
{
this->m_value = ::boost::endian::native_to_little(val);
return *this;
}
operator value_type() const BOOST_NOEXCEPT { return this->value(); }
};
return is;
}
};
} // namespace endian
} // namespace boost
@ -406,8 +354,8 @@ namespace endian
# pragma pack(pop)
#endif
#if defined(_MSC_VER)
# pragma warning(pop)
#endif
#if defined(_MSC_VER)
# pragma warning(pop)
#endif
#endif // BOOST_ENDIAN_ARITHMETIC_HPP

View File

@ -2,6 +2,7 @@
// (C) Copyright Darin Adler 2000
// (C) Copyright Beman Dawes 2006, 2009, 2014
// (C) Copyright Peter Dimov 2019
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
@ -21,29 +22,27 @@
#ifndef BOOST_ENDIAN_BUFFERS_HPP
#define BOOST_ENDIAN_BUFFERS_HPP
#if defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable:4365) // conversion ... signed/unsigned mismatch
#if defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable: 4127) // conditional expression is constant
#endif
#ifdef BOOST_ENDIAN_LOG
# include <iostream>
#endif
#include <boost/endian/detail/endian_store.hpp>
#include <boost/endian/detail/endian_load.hpp>
#include <boost/core/scoped_enum.hpp>
#include <boost/predef/other/endian.h>
#include <boost/static_assert.hpp>
#include <boost/cstdint.hpp>
#include <boost/config.hpp>
#include <boost/config/workaround.hpp>
#include <iosfwd>
#include <climits>
#include <cstring>
#if defined(__BORLANDC__) || defined( __CODEGEARC__)
# pragma pack(push, 1)
#endif
#include <boost/config.hpp>
#include <boost/predef/detail/endian_compat.h>
#include <boost/endian/conversion.hpp>
#include <boost/type_traits/is_signed.hpp>
#include <boost/cstdint.hpp>
#include <boost/static_assert.hpp>
#include <boost/core/scoped_enum.hpp>
#include <iosfwd>
#include <climits>
# if CHAR_BIT != 8
# error Platforms with CHAR_BIT != 8 are not supported
# endif
@ -54,7 +53,8 @@
# define BOOST_ENDIAN_DEFAULT_CONSTRUCT = default; // C++0x
# endif
# if defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) && defined(BOOST_ENDIAN_FORCE_PODNESS)
// g++ pre-4.6 does not support unrestricted unions, but we have no Config macro for that
# if (defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) || BOOST_WORKAROUND(BOOST_GCC, < 40600)) && defined(BOOST_ENDIAN_FORCE_PODNESS)
# define BOOST_ENDIAN_NO_CTORS
# endif
@ -100,6 +100,12 @@ namespace endian
typedef endian_buffer<order::little, uint32_t, 32, align::yes> little_uint32_buf_at;
typedef endian_buffer<order::little, uint64_t, 64, align::yes> little_uint64_buf_at;
// aligned floating point buffers
typedef endian_buffer<order::big, float, 32, align::yes> big_float32_buf_at;
typedef endian_buffer<order::big, double, 64, align::yes> big_float64_buf_at;
typedef endian_buffer<order::little, float, 32, align::yes> little_float32_buf_at;
typedef endian_buffer<order::little, double, 64, align::yes> little_float64_buf_at;
// aligned native endian typedefs are not provided because
// <cstdint> types are superior for this use case
@ -143,7 +149,7 @@ namespace endian
typedef endian_buffer<order::little, uint_least64_t, 56> little_uint56_buf_t;
typedef endian_buffer<order::little, uint_least64_t, 64> little_uint64_buf_t;
# ifdef BOOST_BIG_ENDIAN
# if BOOST_ENDIAN_BIG_BYTE
// unaligned native endian signed integer buffers
typedef big_int8_buf_t native_int8_buf_t;
typedef big_int16_buf_t native_int16_buf_t;
@ -185,6 +191,14 @@ namespace endian
typedef little_uint64_buf_t native_uint64_buf_t;
# endif
// unaligned floating point buffers
typedef endian_buffer<order::big, float, 32, align::no> big_float32_buf_t;
typedef endian_buffer<order::big, double, 64, align::no> big_float64_buf_t;
typedef endian_buffer<order::little, float, 32, align::no> little_float32_buf_t;
typedef endian_buffer<order::little, double, 64, align::no> little_float64_buf_t;
typedef endian_buffer<order::native, float, 32, align::no> native_float32_buf_t;
typedef endian_buffer<order::native, double, 64, align::no> native_float64_buf_t;
// Stream inserter
template <class charT, class traits, BOOST_SCOPED_ENUM(order) Order, class T,
std::size_t n_bits, BOOST_SCOPED_ENUM(align) A>
@ -195,7 +209,7 @@ namespace endian
return os << x.value();
}
// Stream extractor
// Stream extractor
template <class charT, class traits, BOOST_SCOPED_ENUM(order) Order, class T,
std::size_t n_bits, BOOST_SCOPED_ENUM(align) A>
std::basic_istream<charT, traits>&
@ -210,296 +224,153 @@ namespace endian
//---------------------------------- end synopsis ------------------------------------//
namespace detail
{
// Unrolled loops for loading and storing streams of bytes.
template <typename T, std::size_t n_bytes,
bool sign=boost::is_signed<T>::value >
struct unrolled_byte_loops
{
typedef unrolled_byte_loops<T, n_bytes - 1, sign> next;
static T load_big(const unsigned char* bytes) BOOST_NOEXCEPT
{ return static_cast<T>(*(bytes - 1) | (next::load_big(bytes - 1) << 8)); }
static T load_little(const unsigned char* bytes) BOOST_NOEXCEPT
{ return static_cast<T>(*bytes | (next::load_little(bytes + 1) << 8)); }
static void store_big(char* bytes, T value) BOOST_NOEXCEPT
{
*(bytes - 1) = static_cast<char>(value);
next::store_big(bytes - 1, static_cast<T>(value >> 8));
}
static void store_little(char* bytes, T value) BOOST_NOEXCEPT
{
*bytes = static_cast<char>(value);
next::store_little(bytes + 1, static_cast<T>(value >> 8));
}
};
template <typename T>
struct unrolled_byte_loops<T, 1, false>
{
static T load_big(const unsigned char* bytes) BOOST_NOEXCEPT
{ return *(bytes - 1); }
static T load_little(const unsigned char* bytes) BOOST_NOEXCEPT
{ return *bytes; }
static void store_big(char* bytes, T value) BOOST_NOEXCEPT
{ *(bytes - 1) = static_cast<char>(value); }
static void store_little(char* bytes, T value) BOOST_NOEXCEPT
{ *bytes = static_cast<char>(value); }
};
template <typename T>
struct unrolled_byte_loops<T, 1, true>
{
static T load_big(const unsigned char* bytes) BOOST_NOEXCEPT
{ return *reinterpret_cast<const signed char*>(bytes - 1); }
static T load_little(const unsigned char* bytes) BOOST_NOEXCEPT
{ return *reinterpret_cast<const signed char*>(bytes); }
static void store_big(char* bytes, T value) BOOST_NOEXCEPT
{ *(bytes - 1) = static_cast<char>(value); }
static void store_little(char* bytes, T value) BOOST_NOEXCEPT
{ *bytes = static_cast<char>(value); }
};
template <typename T, std::size_t n_bytes>
inline
T load_big_endian(const void* bytes) BOOST_NOEXCEPT
{
return unrolled_byte_loops<T, n_bytes>::load_big
(static_cast<const unsigned char*>(bytes) + n_bytes);
}
template <typename T, std::size_t n_bytes>
inline
T load_little_endian(const void* bytes) BOOST_NOEXCEPT
{
# if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86)
// On x86 (which is little endian), unaligned loads are permitted
if (sizeof(T) == n_bytes) // GCC 4.9, VC++ 14.0, and probably others, elide this
// test and generate code only for the applicable return
// case since sizeof(T) and n_bytes are known at compile
// time.
{
return *reinterpret_cast<T const *>(bytes);
}
# endif
return unrolled_byte_loops<T, n_bytes>::load_little
(static_cast<const unsigned char*>(bytes));
}
template <typename T, std::size_t n_bytes>
inline
void store_big_endian(void* bytes, T value) BOOST_NOEXCEPT
{
unrolled_byte_loops<T, n_bytes>::store_big
(static_cast<char*>(bytes) + n_bytes, value);
}
template <typename T, std::size_t n_bytes>
inline
void store_little_endian(void* bytes, T value) BOOST_NOEXCEPT
{
# if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86)
// On x86 (which is little endian), unaligned stores are permitted
if (sizeof(T) == n_bytes) // GCC 4.9, VC++ 14.0, and probably others, elide this
// test and generate code only for the applicable return
// case since sizeof(T) and n_bytes are known at compile
// time.
{
*reinterpret_cast<T *>(bytes) = value;
return;
}
# endif
unrolled_byte_loops<T, n_bytes>::store_little
(static_cast<char*>(bytes), value);
}
} // namespace detail
# ifdef BOOST_ENDIAN_LOG
bool endian_log(true);
# endif
// endian_buffer class template specializations --------------------------------------//
// Specializations that represent unaligned bytes.
// Taking an integer type as a parameter provides a nice way to pass both
// the size and signedness of the desired integer and get the appropriate
// corresponding integer type for the interface.
// Specializations that represent unaligned bytes.
// Taking an integer type as a parameter provides a nice way to pass both
// the size and signedness of the desired integer and get the appropriate
// corresponding integer type for the interface.
// Q: Should endian_buffer supply "value_type operator value_type() const noexcept"?
// A: No. The rationale for endian_buffers is to prevent high-cost hidden
// conversions. If an implicit conversion operator is supplied, hidden conversions
// can occur.
// Q: Should endian_buffer supply "value_type operator value_type() const noexcept"?
// A: No. The rationale for endian_buffers is to prevent high-cost hidden
// conversions. If an implicit conversion operator is supplied, hidden conversions
// can occur.
// unaligned big endian_buffer specialization
template <typename T, std::size_t n_bits>
class endian_buffer< order::big, T, n_bits, align::no >
// unaligned endian_buffer specialization
template< BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits >
class endian_buffer<Order, T, n_bits, align::no>
{
private:
BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
unsigned char value_[ n_bits / 8 ];
public:
typedef T value_type;
#ifndef BOOST_ENDIAN_NO_CTORS
endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT
explicit endian_buffer( T val ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
public:
typedef T value_type;
# ifndef BOOST_ENDIAN_NO_CTORS
endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT
explicit endian_buffer(T val) BOOST_NOEXCEPT
{
# ifdef BOOST_ENDIAN_LOG
if ( endian_log )
std::cout << "big, unaligned, "
<< n_bits << "-bits, construct(" << val << ")\n";
# endif
detail::store_big_endian<T, n_bits/8>(m_value, val);
}
# endif
endian_buffer & operator=(T val) BOOST_NOEXCEPT
{
# ifdef BOOST_ENDIAN_LOG
if (endian_log)
std::cout << "big, unaligned, " << n_bits << "-bits, assign(" << val << ")\n";
# endif
detail::store_big_endian<T, n_bits/8>(m_value, val);
return *this;
}
value_type value() const BOOST_NOEXCEPT
{
# ifdef BOOST_ENDIAN_LOG
if ( endian_log )
std::cout << "big, unaligned, " << n_bits << "-bits, convert("
<< detail::load_big_endian<T, n_bits/8>(m_value) << ")\n";
# endif
return detail::load_big_endian<T, n_bits/8>(m_value);
}
const char* data() const BOOST_NOEXCEPT { return m_value; }
protected:
char m_value[n_bits/8];
boost::endian::endian_store<T, n_bits / 8, Order>( value_, val );
}
#endif
endian_buffer& operator=( T val ) BOOST_NOEXCEPT
{
boost::endian::endian_store<T, n_bits / 8, Order>( value_, val );
return *this;
}
value_type value() const BOOST_NOEXCEPT
{
return boost::endian::endian_load<T, n_bits / 8, Order>( value_ );
}
char const * data() const BOOST_NOEXCEPT
{
return reinterpret_cast< char const* >( value_ );
}
};
// aligned specializations; only n_bits == 16/32/64 supported
// aligned endian_buffer specialization
template< BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits >
class endian_buffer<Order, T, n_bits, align::yes>
{
private:
BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 );
union
{
unsigned char value_[ n_bits / 8 ];
T align_;
};
// unaligned little endian_buffer specialization
template <typename T, std::size_t n_bits>
class endian_buffer< order::little, T, n_bits, align::no >
public:
typedef T value_type;
#ifndef BOOST_ENDIAN_NO_CTORS
endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT
explicit endian_buffer( T val ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
public:
typedef T value_type;
# ifndef BOOST_ENDIAN_NO_CTORS
endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT
explicit endian_buffer(T val) BOOST_NOEXCEPT
{
# ifdef BOOST_ENDIAN_LOG
if ( endian_log )
std::cout << "little, unaligned, " << n_bits << "-bits, construct("
<< val << ")\n";
# endif
detail::store_little_endian<T, n_bits/8>(m_value, val);
}
# endif
endian_buffer & operator=(T val) BOOST_NOEXCEPT
{ detail::store_little_endian<T, n_bits/8>(m_value, val); return *this; }
value_type value() const BOOST_NOEXCEPT
{
# ifdef BOOST_ENDIAN_LOG
if ( endian_log )
std::cout << "little, unaligned, " << n_bits << "-bits, convert("
<< detail::load_little_endian<T, n_bits/8>(m_value) << ")\n";
# endif
return detail::load_little_endian<T, n_bits/8>(m_value);
}
const char* data() const BOOST_NOEXCEPT { return m_value; }
protected:
char m_value[n_bits/8];
};
boost::endian::endian_store<T, n_bits / 8, Order>( value_, val );
}
// align::yes specializations; only n_bits == 16/32/64 supported
#endif
// aligned big endian_buffer specialization
template <typename T, std::size_t n_bits>
class endian_buffer<order::big, T, n_bits, align::yes>
endian_buffer& operator=( T val ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 );
public:
typedef T value_type;
# ifndef BOOST_ENDIAN_NO_CTORS
endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT
explicit endian_buffer(T val) BOOST_NOEXCEPT
{
# ifdef BOOST_ENDIAN_LOG
if ( endian_log )
std::cout << "big, aligned, " << n_bits
<< "-bits, construct(" << val << ")\n";
# endif
m_value = ::boost::endian::native_to_big(val);
}
boost::endian::endian_store<T, n_bits / 8, Order>( value_, val );
return *this;
}
# endif
endian_buffer& operator=(T val) BOOST_NOEXCEPT
{
m_value = ::boost::endian::native_to_big(val);
return *this;
}
//operator value_type() const BOOST_NOEXCEPT
//{
// return ::boost::endian::big_to_native(m_value);
//}
value_type value() const BOOST_NOEXCEPT
{
# ifdef BOOST_ENDIAN_LOG
if ( endian_log )
std::cout << "big, aligned, " << n_bits << "-bits, convert("
<< ::boost::endian::big_to_native(m_value) << ")\n";
# endif
return ::boost::endian::big_to_native(m_value);
}
const char* data() const BOOST_NOEXCEPT
{return reinterpret_cast<const char*>(&m_value);}
protected:
T m_value;
};
// aligned little endian_buffer specialization
template <typename T, std::size_t n_bits>
class endian_buffer<order::little, T, n_bits, align::yes>
value_type value() const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 );
public:
typedef T value_type;
# ifndef BOOST_ENDIAN_NO_CTORS
endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT
explicit endian_buffer(T val) BOOST_NOEXCEPT
{
# ifdef BOOST_ENDIAN_LOG
if ( endian_log )
std::cout << "little, aligned, " << n_bits
<< "-bits, construct(" << val << ")\n";
# endif
m_value = ::boost::endian::native_to_little(val);
}
return boost::endian::endian_load<T, n_bits / 8, Order>( value_ );
}
# endif
endian_buffer& operator=(T val) BOOST_NOEXCEPT
{
m_value = ::boost::endian::native_to_little(val);
return *this;
}
value_type value() const BOOST_NOEXCEPT
{
# ifdef BOOST_ENDIAN_LOG
if ( endian_log )
std::cout << "little, aligned, " << n_bits << "-bits, convert("
<< ::boost::endian::little_to_native(m_value) << ")\n";
# endif
return ::boost::endian::little_to_native(m_value);
}
const char* data() const BOOST_NOEXCEPT
{return reinterpret_cast<const char*>(&m_value);}
protected:
T m_value;
};
char const * data() const BOOST_NOEXCEPT
{
return reinterpret_cast< char const* >( value_ );
}
};
// aligned native endian_buffer specialization
template< class T, std::size_t n_bits >
class endian_buffer<order::native, T, n_bits, align::yes>
{
private:
BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 );
T value_;
public:
typedef T value_type;
#ifndef BOOST_ENDIAN_NO_CTORS
endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT
explicit endian_buffer( T val ) BOOST_NOEXCEPT: value_( val )
{
}
#endif
endian_buffer& operator=( T val ) BOOST_NOEXCEPT
{
value_ = val;
return *this;
}
value_type value() const BOOST_NOEXCEPT
{
return value_;
}
char const * data() const BOOST_NOEXCEPT
{
return reinterpret_cast< char const* >( &value_ );
}
};
} // namespace endian
} // namespace boost
@ -508,8 +379,8 @@ namespace endian
# pragma pack(pop)
#endif
#if defined(_MSC_VER)
# pragma warning(pop)
#endif
#if defined(_MSC_VER)
# pragma warning(pop)
#endif
#endif // BOOST_ENDIAN_BUFFERS_HPP

View File

@ -8,14 +8,18 @@
#ifndef BOOST_ENDIAN_CONVERSION_HPP
#define BOOST_ENDIAN_CONVERSION_HPP
#include <boost/config.hpp>
#include <boost/predef/detail/endian_compat.h>
#include <boost/cstdint.hpp>
#include <boost/endian/detail/intrinsic.hpp>
#include <boost/core/scoped_enum.hpp>
#include <boost/endian/detail/endian_reverse.hpp>
#include <boost/endian/detail/endian_load.hpp>
#include <boost/endian/detail/endian_store.hpp>
#include <boost/endian/detail/order.hpp>
#include <boost/type_traits/is_class.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/integral_constant.hpp>
#include <boost/predef/other/endian.h>
#include <boost/static_assert.hpp>
#include <algorithm>
#include <cstring> // for memcpy
#include <boost/cstdint.hpp>
#include <boost/config.hpp>
//------------------------------------- synopsis ---------------------------------------//
@ -23,15 +27,6 @@ namespace boost
{
namespace endian
{
BOOST_SCOPED_ENUM_START(order)
{
big, little,
# ifdef BOOST_BIG_ENDIAN
native = big
# else
native = little
# endif
}; BOOST_SCOPED_ENUM_END
//--------------------------------------------------------------------------------------//
// //
@ -48,41 +43,32 @@ namespace endian
// by argument dependent lookup (ADL). //
// //
//--------------------------------------------------------------------------------------//
// customization for exact-length arithmetic types. See doc/conversion.html/#FAQ.
// Note: The omission of a overloads for the arithmetic type (typically long, or
// long long) not assigned to one of the exact length typedefs is a deliberate
// design decision. Such overloads would be non-portable and thus error prone.
inline int8_t endian_reverse(int8_t x) BOOST_NOEXCEPT;
inline int16_t endian_reverse(int16_t x) BOOST_NOEXCEPT;
inline int32_t endian_reverse(int32_t x) BOOST_NOEXCEPT;
inline int64_t endian_reverse(int64_t x) BOOST_NOEXCEPT;
inline uint8_t endian_reverse(uint8_t x) BOOST_NOEXCEPT;
inline uint16_t endian_reverse(uint16_t x) BOOST_NOEXCEPT;
inline uint32_t endian_reverse(uint32_t x) BOOST_NOEXCEPT;
inline uint64_t endian_reverse(uint64_t x) BOOST_NOEXCEPT;
// reverse byte order
// requires T to be a non-bool integral type
// in detail/endian_reverse.hpp
template<class T> inline BOOST_CONSTEXPR T endian_reverse( T x ) BOOST_NOEXCEPT;
// reverse byte order unless native endianness is big
template <class EndianReversible >
inline EndianReversible big_to_native(EndianReversible x) BOOST_NOEXCEPT;
inline BOOST_CONSTEXPR EndianReversible big_to_native(EndianReversible x) BOOST_NOEXCEPT;
// Returns: x if native endian order is big, otherwise endian_reverse(x)
template <class EndianReversible >
inline EndianReversible native_to_big(EndianReversible x) BOOST_NOEXCEPT;
inline BOOST_CONSTEXPR EndianReversible native_to_big(EndianReversible x) BOOST_NOEXCEPT;
// Returns: x if native endian order is big, otherwise endian_reverse(x)
// reverse byte order unless native endianness is little
template <class EndianReversible >
inline EndianReversible little_to_native(EndianReversible x) BOOST_NOEXCEPT;
inline BOOST_CONSTEXPR EndianReversible little_to_native(EndianReversible x) BOOST_NOEXCEPT;
// Returns: x if native endian order is little, otherwise endian_reverse(x)
template <class EndianReversible >
inline EndianReversible native_to_little(EndianReversible x) BOOST_NOEXCEPT;
inline BOOST_CONSTEXPR EndianReversible native_to_little(EndianReversible x) BOOST_NOEXCEPT;
// Returns: x if native endian order is little, otherwise endian_reverse(x)
// generic conditional reverse byte order
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To,
class EndianReversible>
inline EndianReversible conditional_reverse(EndianReversible from) BOOST_NOEXCEPT;
inline BOOST_CONSTEXPR EndianReversible conditional_reverse(EndianReversible from) BOOST_NOEXCEPT;
// Returns: If From == To have different values, from.
// Otherwise endian_reverse(from).
// Remarks: The From == To test, and as a consequence which form the return takes, is
@ -90,7 +76,7 @@ namespace endian
// runtime conditional reverse byte order
template <class EndianReversible >
inline EndianReversible conditional_reverse(EndianReversible from,
inline BOOST_CONSTEXPR EndianReversible conditional_reverse(EndianReversible from,
BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order)
BOOST_NOEXCEPT;
// Returns: from_order == to_order ? from : endian_reverse(from).
@ -112,7 +98,7 @@ namespace endian
// //
// user-defined types (UDTs) //
// //
// All reverse in place function templates are required to be implemented in terms //
// All reverse in place function templates are required to be implemented in terms //
// of an unqualified call to "endian_reverse_inplace(x)", a function reversing //
// the endianness of x, which is a non-const reference. This provides a //
// customization point for any UDT that provides a "reverse_inplace" free-function //
@ -123,6 +109,7 @@ namespace endian
//------------------------------------------------------------------------------------//
// reverse in place
// in detail/endian_reverse.hpp
template <class EndianReversible>
inline void endian_reverse_inplace(EndianReversible& x) BOOST_NOEXCEPT;
// Effects: x = endian_reverse(x)
@ -146,7 +133,7 @@ namespace endian
// generic conditional reverse in place
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To,
class EndianReversibleInplace>
inline void conditional_reverse_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT;
inline void conditional_reverse_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT;
// runtime reverse in place
template <class EndianReversibleInplace>
@ -156,332 +143,551 @@ namespace endian
//----------------------------------- end synopsis -------------------------------------//
namespace detail
{
// generic reverse function template implementation approach using std::reverse
// suggested by Mathias Gaunard. Primary motivation for inclusion is to have an
// independent implementation to test against.
namespace detail
{
template <class T>
inline T std_endian_reverse(T x) BOOST_NOEXCEPT
{
T tmp(x);
std::reverse(
reinterpret_cast<unsigned char*>(&tmp),
reinterpret_cast<unsigned char*>(&tmp) + sizeof(T));
return tmp;
}
template<class T> struct is_endian_reversible: boost::integral_constant<bool,
boost::is_class<T>::value || ( boost::is_integral<T>::value && !boost::is_same<T, bool>::value )>
{
};
// conditional unaligned reverse copy, patterned after std::reverse_copy
template <class T>
inline void big_reverse_copy(T from, char* to) BOOST_NOEXCEPT;
template <class T>
inline void big_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT;
template <class T>
inline void little_reverse_copy(T from, char* to) BOOST_NOEXCEPT;
template <class T>
inline void little_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT;
} // namespace detail
} // namespace detail
//--------------------------------------------------------------------------------------//
// //
// return-by-value implementation //
// //
// -- portable approach suggested by tymofey, with avoidance of undefined behavior //
// as suggested by Giovanni Piero Deretta, with a further refinement suggested //
// by Pyry Jahkola. //
// -- intrinsic approach suggested by reviewers, and by David Stone, who provided //
// his Boost licensed macro implementation (detail/intrinsic.hpp) //
// //
//--------------------------------------------------------------------------------------//
template <class EndianReversible>
inline BOOST_CONSTEXPR EndianReversible big_to_native( EndianReversible x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible<EndianReversible>::value );
#if BOOST_ENDIAN_BIG_BYTE
inline int8_t endian_reverse(int8_t x) BOOST_NOEXCEPT
{
return x;
}
inline int16_t endian_reverse(int16_t x) BOOST_NOEXCEPT
{
# ifdef BOOST_ENDIAN_NO_INTRINSICS
return (static_cast<uint16_t>(x) << 8)
| (static_cast<uint16_t>(x) >> 8);
# else
return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(static_cast<uint16_t>(x));
# endif
}
inline int32_t endian_reverse(int32_t x) BOOST_NOEXCEPT
{
# ifdef BOOST_ENDIAN_NO_INTRINSICS
uint32_t step16;
step16 = static_cast<uint32_t>(x) << 16 | static_cast<uint32_t>(x) >> 16;
return
((static_cast<uint32_t>(step16) << 8) & 0xff00ff00)
| ((static_cast<uint32_t>(step16) >> 8) & 0x00ff00ff);
# else
return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(static_cast<uint32_t>(x));
# endif
}
#else
inline int64_t endian_reverse(int64_t x) BOOST_NOEXCEPT
{
# ifdef BOOST_ENDIAN_NO_INTRINSICS
uint64_t step32, step16;
step32 = static_cast<uint64_t>(x) << 32 | static_cast<uint64_t>(x) >> 32;
step16 = (step32 & 0x0000FFFF0000FFFFULL) << 16
| (step32 & 0xFFFF0000FFFF0000ULL) >> 16;
return static_cast<int64_t>((step16 & 0x00FF00FF00FF00FFULL) << 8
| (step16 & 0xFF00FF00FF00FF00ULL) >> 8);
# else
return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(static_cast<uint64_t>(x));
# endif
}
inline uint8_t endian_reverse(uint8_t x) BOOST_NOEXCEPT
{
return x;
}
inline uint16_t endian_reverse(uint16_t x) BOOST_NOEXCEPT
{
# ifdef BOOST_ENDIAN_NO_INTRINSICS
return (x << 8)
| (x >> 8);
# else
return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x);
# endif
}
inline uint32_t endian_reverse(uint32_t x) BOOST_NOEXCEPT
{
# ifdef BOOST_ENDIAN_NO_INTRINSICS
uint32_t step16;
step16 = x << 16 | x >> 16;
return
((step16 << 8) & 0xff00ff00)
| ((step16 >> 8) & 0x00ff00ff);
# else
return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x);
# endif
}
inline uint64_t endian_reverse(uint64_t x) BOOST_NOEXCEPT
{
# ifdef BOOST_ENDIAN_NO_INTRINSICS
uint64_t step32, step16;
step32 = x << 32 | x >> 32;
step16 = (step32 & 0x0000FFFF0000FFFFULL) << 16
| (step32 & 0xFFFF0000FFFF0000ULL) >> 16;
return (step16 & 0x00FF00FF00FF00FFULL) << 8
| (step16 & 0xFF00FF00FF00FF00ULL) >> 8;
# else
return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x);
# endif
}
template <class EndianReversible >
inline EndianReversible big_to_native(EndianReversible x) BOOST_NOEXCEPT
{
# ifdef BOOST_BIG_ENDIAN
return x;
# else
return endian_reverse(x);
# endif
#endif
}
template <class EndianReversible >
inline EndianReversible native_to_big(EndianReversible x) BOOST_NOEXCEPT
{
# ifdef BOOST_BIG_ENDIAN
template <class EndianReversible>
inline BOOST_CONSTEXPR EndianReversible native_to_big( EndianReversible x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible<EndianReversible>::value );
#if BOOST_ENDIAN_BIG_BYTE
return x;
# else
return endian_reverse(x);
# endif
}
template <class EndianReversible >
inline EndianReversible little_to_native(EndianReversible x) BOOST_NOEXCEPT
{
# ifdef BOOST_LITTLE_ENDIAN
#else
return endian_reverse(x);
#endif
}
template <class EndianReversible>
inline BOOST_CONSTEXPR EndianReversible little_to_native( EndianReversible x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible<EndianReversible>::value );
#if BOOST_ENDIAN_LITTLE_BYTE
return x;
# else
return endian_reverse(x);
# endif
}
template <class EndianReversible >
inline EndianReversible native_to_little(EndianReversible x) BOOST_NOEXCEPT
{
# ifdef BOOST_LITTLE_ENDIAN
#else
return endian_reverse(x);
#endif
}
template <class EndianReversible>
inline BOOST_CONSTEXPR EndianReversible native_to_little( EndianReversible x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible<EndianReversible>::value );
#if BOOST_ENDIAN_LITTLE_BYTE
return x;
# else
#else
return endian_reverse(x);
# endif
}
namespace detail
{
// Primary template and specializations to support endian_reverse().
// See rationale in endian_reverse() below.
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To,
class EndianReversible>
class value_converter ; // primary template
template <class T> class value_converter <order::big, order::big, T>
{public: T operator()(T x) BOOST_NOEXCEPT {return x;}};
template <class T> class value_converter <order::little, order::little, T>
{public: T operator()(T x) BOOST_NOEXCEPT {return x;}};
template <class T> class value_converter <order::big, order::little, T>
{public: T operator()(T x) BOOST_NOEXCEPT {return endian_reverse(x);}};
template <class T> class value_converter <order::little, order::big, T>
{public: T operator()(T x) BOOST_NOEXCEPT {return endian_reverse(x);}};
}
#endif
}
// generic conditional reverse
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To,
class EndianReversible>
inline EndianReversible conditional_reverse(EndianReversible from) BOOST_NOEXCEPT {
// work around lack of function template partial specialization by instantiating
// a function object of a class that is partially specialized on the two order
// template parameters, and then calling its operator().
detail::value_converter <From, To, EndianReversible> tmp;
return tmp(from);
}
namespace detail
{
// runtime conditional reverse
template <class EndianReversible >
inline EndianReversible conditional_reverse(EndianReversible from,
BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order) BOOST_NOEXCEPT
{
return from_order == to_order ? from : endian_reverse(from);
}
template<class EndianReversible>
inline BOOST_CONSTEXPR EndianReversible conditional_reverse_impl( EndianReversible x, boost::true_type ) BOOST_NOEXCEPT
{
return x;
}
template<class EndianReversible>
inline BOOST_CONSTEXPR EndianReversible conditional_reverse_impl( EndianReversible x, boost::false_type ) BOOST_NOEXCEPT
{
return endian_reverse( x );
}
} // namespace detail
// generic conditional reverse
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, class EndianReversible>
inline BOOST_CONSTEXPR EndianReversible conditional_reverse( EndianReversible x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible<EndianReversible>::value );
return detail::conditional_reverse_impl( x, boost::integral_constant<bool, From == To>() );
}
// runtime conditional reverse
template <class EndianReversible>
inline BOOST_CONSTEXPR EndianReversible conditional_reverse( EndianReversible x,
BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible<EndianReversible>::value );
return from_order == to_order? x: endian_reverse( x );
}
//--------------------------------------------------------------------------------------//
// reverse-in-place implementation //
//--------------------------------------------------------------------------------------//
// reverse in place
template <class EndianReversible>
inline void endian_reverse_inplace(EndianReversible& x) BOOST_NOEXCEPT
{
x = endian_reverse(x);
}
namespace detail
{
template <class EndianReversibleInplace>
# ifdef BOOST_BIG_ENDIAN
inline void big_to_native_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {}
# else
inline void big_to_native_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT
{ endian_reverse_inplace(x); }
# endif
template <class EndianReversibleInplace>
# ifdef BOOST_BIG_ENDIAN
inline void native_to_big_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {}
# else
inline void native_to_big_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT
{
endian_reverse_inplace(x);
}
# endif
template<class T> struct is_endian_reversible_inplace: boost::integral_constant<bool,
boost::is_class<T>::value || ( boost::is_integral<T>::value && !boost::is_same<T, bool>::value )>
{
};
template <class EndianReversibleInplace>
# ifdef BOOST_LITTLE_ENDIAN
inline void little_to_native_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {}
# else
inline void little_to_native_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT
{ endian_reverse_inplace(x); }
# endif
template <class EndianReversibleInplace>
# ifdef BOOST_LITTLE_ENDIAN
inline void native_to_little_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {}
# else
inline void native_to_little_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT
{
endian_reverse_inplace(x);
}
# endif
} // namespace detail
namespace detail
{
// Primary template and specializations support generic
// endian_reverse_inplace().
// See rationale in endian_reverse_inplace() below.
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To,
class EndianReversibleInplace>
class converter; // primary template
template <class T> class converter<order::big, order::big, T>
{public: void operator()(T&) BOOST_NOEXCEPT {/*no effect*/}};
template <class T> class converter<order::little, order::little, T>
{public: void operator()(T&) BOOST_NOEXCEPT {/*no effect*/}};
template <class T> class converter<order::big, order::little, T>
{public: void operator()(T& x) BOOST_NOEXCEPT { endian_reverse_inplace(x); }};
template <class T> class converter<order::little, order::big, T>
{public: void operator()(T& x) BOOST_NOEXCEPT { endian_reverse_inplace(x); }};
} // namespace detail
#if BOOST_ENDIAN_BIG_BYTE
// generic conditional reverse in place
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To,
class EndianReversibleInplace>
inline void conditional_reverse_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT
{
// work around lack of function template partial specialization by instantiating
// a function object of a class that is partially specialized on the two order
// template parameters, and then calling its operator().
detail::converter<From, To, EndianReversibleInplace> tmp;
tmp(x); // call operator ()
}
template <class EndianReversibleInplace>
inline void big_to_native_inplace( EndianReversibleInplace& ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
}
// runtime reverse in place
template <class EndianReversibleInplace>
inline void conditional_reverse_inplace(EndianReversibleInplace& x,
BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order)
BOOST_NOEXCEPT
{
if (from_order != to_order)
endian_reverse_inplace(x);
}
#else
template <class EndianReversibleInplace>
inline void big_to_native_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
endian_reverse_inplace( x );
}
namespace detail
{
template <class T>
inline void big_reverse_copy(T from, char* to) BOOST_NOEXCEPT
#endif
#if BOOST_ENDIAN_BIG_BYTE
template <class EndianReversibleInplace>
inline void native_to_big_inplace( EndianReversibleInplace& ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
}
#else
template <class EndianReversibleInplace>
inline void native_to_big_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
endian_reverse_inplace( x );
}
#endif
#if BOOST_ENDIAN_LITTLE_BYTE
template <class EndianReversibleInplace>
inline void little_to_native_inplace( EndianReversibleInplace& ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
}
#else
template <class EndianReversibleInplace>
inline void little_to_native_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
endian_reverse_inplace( x );
}
#endif
#if BOOST_ENDIAN_LITTLE_BYTE
template <class EndianReversibleInplace>
inline void native_to_little_inplace( EndianReversibleInplace& ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
}
#else
template <class EndianReversibleInplace>
inline void native_to_little_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
endian_reverse_inplace( x );
}
#endif
namespace detail
{
template<class EndianReversibleInplace>
inline void conditional_reverse_inplace_impl( EndianReversibleInplace&, boost::true_type ) BOOST_NOEXCEPT
{
}
template<class EndianReversibleInplace>
inline void conditional_reverse_inplace_impl( EndianReversibleInplace& x, boost::false_type ) BOOST_NOEXCEPT
{
endian_reverse_inplace( x );
}
} // namespace detail
// generic conditional reverse in place
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, class EndianReversibleInplace>
inline void conditional_reverse_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
detail::conditional_reverse_inplace_impl( x, boost::integral_constant<bool, From == To>() );
}
// runtime reverse in place
template <class EndianReversibleInplace>
inline void conditional_reverse_inplace( EndianReversibleInplace& x,
BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
if( from_order != to_order )
{
# ifdef BOOST_BIG_ENDIAN
std::memcpy(to, reinterpret_cast<const char*>(&from), sizeof(T));
# else
std::reverse_copy(reinterpret_cast<const char*>(&from),
reinterpret_cast<const char*>(&from) + sizeof(T), to);
# endif
endian_reverse_inplace( x );
}
template <class T>
inline void big_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT
{
# ifdef BOOST_BIG_ENDIAN
std::memcpy(reinterpret_cast<char*>(&to), from, sizeof(T));
# else
std::reverse_copy(from, from + sizeof(T), reinterpret_cast<char*>(&to));
# endif
}
template <class T>
inline void little_reverse_copy(T from, char* to) BOOST_NOEXCEPT
{
# ifdef BOOST_LITTLE_ENDIAN
std::memcpy(to, reinterpret_cast<const char*>(&from), sizeof(T));
# else
std::reverse_copy(reinterpret_cast<const char*>(&from),
reinterpret_cast<const char*>(&from) + sizeof(T), to);
# endif
}
template <class T>
inline void little_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT
{
# ifdef BOOST_LITTLE_ENDIAN
std::memcpy(reinterpret_cast<char*>(&to), from, sizeof(T));
# else
std::reverse_copy(from, from + sizeof(T), reinterpret_cast<char*>(&to));
# endif
}
} // namespace detail
}
// load/store convenience functions
// load 16
inline boost::int16_t load_little_s16( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int16_t, 2, order::little>( p );
}
inline boost::uint16_t load_little_u16( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint16_t, 2, order::little>( p );
}
inline boost::int16_t load_big_s16( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int16_t, 2, order::big>( p );
}
inline boost::uint16_t load_big_u16( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint16_t, 2, order::big>( p );
}
// load 24
inline boost::int32_t load_little_s24( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int32_t, 3, order::little>( p );
}
inline boost::uint32_t load_little_u24( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint32_t, 3, order::little>( p );
}
inline boost::int32_t load_big_s24( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int32_t, 3, order::big>( p );
}
inline boost::uint32_t load_big_u24( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint32_t, 3, order::big>( p );
}
// load 32
inline boost::int32_t load_little_s32( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int32_t, 4, order::little>( p );
}
inline boost::uint32_t load_little_u32( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint32_t, 4, order::little>( p );
}
inline boost::int32_t load_big_s32( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int32_t, 4, order::big>( p );
}
inline boost::uint32_t load_big_u32( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint32_t, 4, order::big>( p );
}
// load 40
inline boost::int64_t load_little_s40( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int64_t, 5, order::little>( p );
}
inline boost::uint64_t load_little_u40( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint64_t, 5, order::little>( p );
}
inline boost::int64_t load_big_s40( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int64_t, 5, order::big>( p );
}
inline boost::uint64_t load_big_u40( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint64_t, 5, order::big>( p );
}
// load 48
inline boost::int64_t load_little_s48( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int64_t, 6, order::little>( p );
}
inline boost::uint64_t load_little_u48( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint64_t, 6, order::little>( p );
}
inline boost::int64_t load_big_s48( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int64_t, 6, order::big>( p );
}
inline boost::uint64_t load_big_u48( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint64_t, 6, order::big>( p );
}
// load 56
inline boost::int64_t load_little_s56( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int64_t, 7, order::little>( p );
}
inline boost::uint64_t load_little_u56( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint64_t, 7, order::little>( p );
}
inline boost::int64_t load_big_s56( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int64_t, 7, order::big>( p );
}
inline boost::uint64_t load_big_u56( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint64_t, 7, order::big>( p );
}
// load 64
inline boost::int64_t load_little_s64( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int64_t, 8, order::little>( p );
}
inline boost::uint64_t load_little_u64( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint64_t, 8, order::little>( p );
}
inline boost::int64_t load_big_s64( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int64_t, 8, order::big>( p );
}
inline boost::uint64_t load_big_u64( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint64_t, 8, order::big>( p );
}
// store 16
inline void store_little_s16( unsigned char * p, boost::int16_t v )
{
boost::endian::endian_store<boost::int16_t, 2, order::little>( p, v );
}
inline void store_little_u16( unsigned char * p, boost::uint16_t v )
{
boost::endian::endian_store<boost::uint16_t, 2, order::little>( p, v );
}
inline void store_big_s16( unsigned char * p, boost::int16_t v )
{
boost::endian::endian_store<boost::int16_t, 2, order::big>( p, v );
}
inline void store_big_u16( unsigned char * p, boost::uint16_t v )
{
boost::endian::endian_store<boost::uint16_t, 2, order::big>( p, v );
}
// store 24
inline void store_little_s24( unsigned char * p, boost::int32_t v )
{
boost::endian::endian_store<boost::int32_t, 3, order::little>( p, v );
}
inline void store_little_u24( unsigned char * p, boost::uint32_t v )
{
boost::endian::endian_store<boost::uint32_t, 3, order::little>( p, v );
}
inline void store_big_s24( unsigned char * p, boost::int32_t v )
{
boost::endian::endian_store<boost::int32_t, 3, order::big>( p, v );
}
inline void store_big_u24( unsigned char * p, boost::uint32_t v )
{
boost::endian::endian_store<boost::uint32_t, 3, order::big>( p, v );
}
// store 32
inline void store_little_s32( unsigned char * p, boost::int32_t v )
{
boost::endian::endian_store<boost::int32_t, 4, order::little>( p, v );
}
inline void store_little_u32( unsigned char * p, boost::uint32_t v )
{
boost::endian::endian_store<boost::uint32_t, 4, order::little>( p, v );
}
inline void store_big_s32( unsigned char * p, boost::int32_t v )
{
boost::endian::endian_store<boost::int32_t, 4, order::big>( p, v );
}
inline void store_big_u32( unsigned char * p, boost::uint32_t v )
{
boost::endian::endian_store<boost::uint32_t, 4, order::big>( p, v );
}
// store 40
inline void store_little_s40( unsigned char * p, boost::int64_t v )
{
boost::endian::endian_store<boost::int64_t, 5, order::little>( p, v );
}
inline void store_little_u40( unsigned char * p, boost::uint64_t v )
{
boost::endian::endian_store<boost::uint64_t, 5, order::little>( p, v );
}
inline void store_big_s40( unsigned char * p, boost::int64_t v )
{
boost::endian::endian_store<boost::int64_t, 5, order::big>( p, v );
}
inline void store_big_u40( unsigned char * p, boost::uint64_t v )
{
boost::endian::endian_store<boost::uint64_t, 5, order::big>( p, v );
}
// store 48
inline void store_little_s48( unsigned char * p, boost::int64_t v )
{
boost::endian::endian_store<boost::int64_t, 6, order::little>( p, v );
}
inline void store_little_u48( unsigned char * p, boost::uint64_t v )
{
boost::endian::endian_store<boost::uint64_t, 6, order::little>( p, v );
}
inline void store_big_s48( unsigned char * p, boost::int64_t v )
{
boost::endian::endian_store<boost::int64_t, 6, order::big>( p, v );
}
inline void store_big_u48( unsigned char * p, boost::uint64_t v )
{
boost::endian::endian_store<boost::uint64_t, 6, order::big>( p, v );
}
// store 56
inline void store_little_s56( unsigned char * p, boost::int64_t v )
{
boost::endian::endian_store<boost::int64_t, 7, order::little>( p, v );
}
inline void store_little_u56( unsigned char * p, boost::uint64_t v )
{
boost::endian::endian_store<boost::uint64_t, 7, order::little>( p, v );
}
inline void store_big_s56( unsigned char * p, boost::int64_t v )
{
boost::endian::endian_store<boost::int64_t, 7, order::big>( p, v );
}
inline void store_big_u56( unsigned char * p, boost::uint64_t v )
{
boost::endian::endian_store<boost::uint64_t, 7, order::big>( p, v );
}
// store 64
inline void store_little_s64( unsigned char * p, boost::int64_t v )
{
boost::endian::endian_store<boost::int64_t, 8, order::little>( p, v );
}
inline void store_little_u64( unsigned char * p, boost::uint64_t v )
{
boost::endian::endian_store<boost::uint64_t, 8, order::little>( p, v );
}
inline void store_big_s64( unsigned char * p, boost::int64_t v )
{
boost::endian::endian_store<boost::int64_t, 8, order::big>( p, v );
}
inline void store_big_u64( unsigned char * p, boost::uint64_t v )
{
boost::endian::endian_store<boost::uint64_t, 8, order::big>( p, v );
}
} // namespace endian
} // namespace boost

View File

@ -1,62 +0,0 @@
// boost/endian/detail/config.hpp ----------------------------------------------------//
// Copyright Beman Dawes 2003, 2010
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
//--------------------------------------------------------------------------------------//
#ifndef BOOST_ENDIAN_CONFIG_HPP
#define BOOST_ENDIAN_CONFIG_HPP
// This header implements separate compilation features as described in
// http://www.boost.org/more/separate_compilation.html
#include <boost/config.hpp>
#include <boost/system/api_config.hpp> // for BOOST_POSIX_API or BOOST_WINDOWS_API
// throw an exception ----------------------------------------------------------------//
//
// Exceptions were originally thrown via boost::throw_exception().
// As throw_exception() became more complex, it caused user error reporting
// to be harder to interpret, since the exception reported became much more complex.
// The immediate fix was to throw directly, wrapped in a macro to make any later change
// easier.
#define BOOST_ENDIAN_THROW(EX) throw EX
// enable dynamic linking -------------------------------------------------------------//
#if defined(BOOST_ALL_DYN_LINK) || defined(BOOST_ENDIAN_DYN_LINK)
# if defined(BOOST_ENDIAN_SOURCE)
# define BOOST_ENDIAN_DECL BOOST_SYMBOL_EXPORT
# else
# define BOOST_ENDIAN_DECL BOOST_SYMBOL_IMPORT
# endif
#else
# define BOOST_ENDIAN_DECL
#endif
// enable automatic library variant selection ----------------------------------------//
#if !defined(BOOST_ENDIAN_SOURCE) && !defined(BOOST_ALL_NO_LIB) \
&& !defined(BOOST_ENDIAN_NO_LIB)
//
// Set the name of our library, this will get undef'ed by auto_link.hpp
// once it's done with it:
//
#define BOOST_LIB_NAME boost_endian
//
// If we're importing code from a dll, then tell auto_link.hpp about it:
//
#if defined(BOOST_ALL_DYN_LINK) || defined(BOOST_ENDIAN_DYN_LINK)
# define BOOST_DYN_LINK
#endif
//
// And include the header that does the work:
//
#include <boost/config/auto_link.hpp>
#endif // auto-linking disabled
#endif // BOOST_ENDIAN_CONFIG_HPP

View File

@ -1,142 +0,0 @@
// boost/endian/detail/cover_operators.hpp ----------------------------------//
// Copyright Darin Adler 2000
// Copyright Beman Dawes 2008
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_ENDIAN_COVER_OPERATORS_HPP
#define BOOST_ENDIAN_COVER_OPERATORS_HPP
#if defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable:4365) // conversion ... signed/unsigned mismatch
#endif
# ifndef BOOST_ENDIAN_MINIMAL_COVER_OPERATORS
# include <boost/operators.hpp>
# endif
#include <boost/config.hpp>
#include <iosfwd>
namespace boost
{
namespace endian
{
//--------------------------------------------------------------------------------------//
// A class that adds arithmetic operators to an arithmetic cover class
//
// Uses the curiously recurring template pattern (CRTP).
//
// If the class being covered has a non-explicit conversion to an integer type
// then a smaller number of cover operations are needed. Define the macro
// BOOST_ENDIAN_MINIMAL_COVER_OPERATORS to indicate this.
//
// Define BOOST_NO_IO_COVER_OPERATORS if I/O cover operations are not desired.
//--------------------------------------------------------------------------------------//
template <class D, // D is the CRTP derived type, i.e. the cover class
class ArithmeticT>
class cover_operators
# ifndef BOOST_ENDIAN_MINIMAL_COVER_OPERATORS
: boost::operators<D>
# endif
{
// The other operations take advantage of the type conversion that's
// built into unary +.
// Unary operations.
friend ArithmeticT operator+(const D& x) BOOST_NOEXCEPT { return x; }
# ifndef BOOST_ENDIAN_MINIMAL_COVER_OPERATORS
friend ArithmeticT operator-(const D& x) BOOST_NOEXCEPT { return -+x; }
friend ArithmeticT operator~(const D& x) BOOST_NOEXCEPT { return ~+x; }
friend ArithmeticT operator!(const D& x) BOOST_NOEXCEPT { return !+x; }
// The basic ordering operations.
friend bool operator==(const D& x, ArithmeticT y) BOOST_NOEXCEPT { return +x == y; }
friend bool operator<(const D& x, ArithmeticT y) BOOST_NOEXCEPT { return +x < y; }
# endif
// The basic arithmetic operations.
friend D& operator+=(D& x, ArithmeticT y) BOOST_NOEXCEPT
{ return x = static_cast<ArithmeticT>(+x + y); }
friend D& operator-=(D& x, ArithmeticT y) BOOST_NOEXCEPT
{ return x = static_cast<ArithmeticT>(+x - y); }
friend D& operator*=(D& x, ArithmeticT y) BOOST_NOEXCEPT
{ return x = static_cast<ArithmeticT>(+x * y); }
friend D& operator/=(D& x, ArithmeticT y) BOOST_NOEXCEPT
{ return x = static_cast<ArithmeticT>(+x / y); }
friend D& operator%=(D& x, ArithmeticT y) BOOST_NOEXCEPT
{ return x = static_cast<ArithmeticT>(+x % y); }
friend D& operator&=(D& x, ArithmeticT y) BOOST_NOEXCEPT
{ return x = static_cast<ArithmeticT>(+x & y); }
friend D& operator|=(D& x, ArithmeticT y) BOOST_NOEXCEPT
{ return x = static_cast<ArithmeticT>(+x | y); }
friend D& operator^=(D& x, ArithmeticT y) BOOST_NOEXCEPT
{ return x = static_cast<ArithmeticT>(+x ^ y); }
friend D& operator<<=(D& x, ArithmeticT y) BOOST_NOEXCEPT
{ return x = static_cast<ArithmeticT>(+x << y); }
friend D& operator>>=(D& x, ArithmeticT y) BOOST_NOEXCEPT
{ return x = static_cast<ArithmeticT>(+x >> y); }
// A few binary arithmetic operations not covered by operators base class.
friend ArithmeticT operator<<(const D& x, ArithmeticT y) BOOST_NOEXCEPT
{ return static_cast<ArithmeticT>(+x << y); }
friend ArithmeticT operator>>(const D& x, ArithmeticT y) BOOST_NOEXCEPT
{ return static_cast<ArithmeticT>(+x >> y); }
// Auto-increment and auto-decrement can be defined in terms of the
// arithmetic operations.
friend D& operator++(D& x) BOOST_NOEXCEPT { return x += 1; }
friend D& operator--(D& x) BOOST_NOEXCEPT { return x -= 1; }
# ifdef BOOST_ENDIAN_MINIMAL_COVER_OPERATORS
friend D operator++(D& x, int) BOOST_NOEXCEPT
{
D tmp(x);
x += 1;
return tmp;
}
friend D operator--(D& x, int) BOOST_NOEXCEPT
{
D tmp(x);
x -= 1;
return tmp;
}
# endif
# ifndef BOOST_NO_IO_COVER_OPERATORS
// Stream inserter
template <class charT, class traits>
friend std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& os, const D& x)
{
return os << +x;
}
// Stream extractor
template <class charT, class traits>
friend std::basic_istream<charT, traits>&
operator>>(std::basic_istream<charT, traits>& is, D& x)
{
ArithmeticT i;
if (is >> i)
x = i;
return is;
}
# endif
};
} // namespace endian
} // namespace boost
#if defined(_MSC_VER)
# pragma warning(pop)
#endif
#endif // BOOST_ENDIAN_COVER_OPERATORS_HPP

View File

@ -8,5 +8,5 @@
//--------------------------------------------------------------------------------------//
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(pop)
#endif

View File

@ -0,0 +1,589 @@
#ifndef BOOST_ENDIAN_DETAIL_ENDIAN_LOAD_HPP_INCLUDED
#define BOOST_ENDIAN_DETAIL_ENDIAN_LOAD_HPP_INCLUDED
// Copyright 2019 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/endian/detail/endian_reverse.hpp>
#include <boost/endian/detail/order.hpp>
#include <boost/endian/detail/integral_by_size.hpp>
#include <boost/endian/detail/is_trivially_copyable.hpp>
#include <boost/type_traits/is_signed.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/is_enum.hpp>
#include <boost/static_assert.hpp>
#include <cstddef>
#include <cstring>
namespace boost
{
namespace endian
{
namespace detail
{
template<class T, std::size_t N1, BOOST_SCOPED_ENUM(order) O1, std::size_t N2, BOOST_SCOPED_ENUM(order) O2> struct endian_load_impl
{
};
} // namespace detail
// Requires:
//
// sizeof(T) must be 1, 2, 4, or 8
// 1 <= N <= sizeof(T)
// T is TriviallyCopyable
// if N < sizeof(T), T is integral or enum
template<class T, std::size_t N, BOOST_SCOPED_ENUM(order) Order>
inline T endian_load( unsigned char const * p ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8 );
BOOST_STATIC_ASSERT( N >= 1 && N <= sizeof(T) );
return detail::endian_load_impl<T, sizeof(T), order::native, N, Order>()( p );
}
namespace detail
{
// same endianness, same size
template<class T, std::size_t N, BOOST_SCOPED_ENUM(order) O> struct endian_load_impl<T, N, O, N, O>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_trivially_copyable<T>::value );
T t;
std::memcpy( &t, p, N );
return t;
}
};
// same size, reverse endianness
template<class T, std::size_t N, BOOST_SCOPED_ENUM(order) O1, BOOST_SCOPED_ENUM(order) O2> struct endian_load_impl<T, N, O1, N, O2>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_trivially_copyable<T>::value );
typename integral_by_size<N>::type tmp;
std::memcpy( &tmp, p, N );
endian_reverse_inplace( tmp );
T t;
std::memcpy( &t, &tmp, N );
return t;
}
};
// expanding load 1 -> 2
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 2, Order, 1, order::little>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 2 ];
tmp[0] = p[0];
tmp[1] = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
return boost::endian::endian_load<T, 2, order::little>( tmp );
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 2, Order, 1, order::big>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 2 ];
tmp[0] = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[1] = p[0];
return boost::endian::endian_load<T, 2, order::big>( tmp );
}
};
// expanding load 1 -> 4
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 4, Order, 1, order::little>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[0] = p[0];
tmp[1] = fill;
tmp[2] = fill;
tmp[3] = fill;
return boost::endian::endian_load<T, 4, order::little>( tmp );
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 4, Order, 1, order::big>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[0] = fill;
tmp[1] = fill;
tmp[2] = fill;
tmp[3] = p[0];
return boost::endian::endian_load<T, 4, order::big>( tmp );
}
};
// expanding load 2 -> 4
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 4, Order, 2, order::little>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
unsigned char fill = boost::is_signed<T>::value && ( p[1] & 0x80 )? 0xFF: 0x00;
tmp[0] = p[0];
tmp[1] = p[1];
tmp[2] = fill;
tmp[3] = fill;
return boost::endian::endian_load<T, 4, order::little>( tmp );
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 4, Order, 2, order::big>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[0] = fill;
tmp[1] = fill;
tmp[2] = p[0];
tmp[3] = p[1];
return boost::endian::endian_load<T, 4, order::big>( tmp );
}
};
// expanding load 3 -> 4
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 4, Order, 3, order::little>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
tmp[0] = p[0];
tmp[1] = p[1];
tmp[2] = p[2];
tmp[3] = boost::is_signed<T>::value && ( p[2] & 0x80 )? 0xFF: 0x00;
return boost::endian::endian_load<T, 4, order::little>( tmp );
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 4, Order, 3, order::big>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
tmp[0] = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[1] = p[0];
tmp[2] = p[1];
tmp[3] = p[2];
return boost::endian::endian_load<T, 4, order::big>( tmp );
}
};
// expanding load 1 -> 8
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 1, order::little>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[0] = p[0];
tmp[1] = fill;
tmp[2] = fill;
tmp[3] = fill;
tmp[4] = fill;
tmp[5] = fill;
tmp[6] = fill;
tmp[7] = fill;
return boost::endian::endian_load<T, 8, order::little>( tmp );
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 1, order::big>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[0] = fill;
tmp[1] = fill;
tmp[2] = fill;
tmp[3] = fill;
tmp[4] = fill;
tmp[5] = fill;
tmp[6] = fill;
tmp[7] = p[0];
return boost::endian::endian_load<T, 8, order::big>( tmp );
}
};
// expanding load 2 -> 8
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 2, order::little>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[1] & 0x80 )? 0xFF: 0x00;
tmp[0] = p[0];
tmp[1] = p[1];
tmp[2] = fill;
tmp[3] = fill;
tmp[4] = fill;
tmp[5] = fill;
tmp[6] = fill;
tmp[7] = fill;
return boost::endian::endian_load<T, 8, order::little>( tmp );
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 2, order::big>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[0] = fill;
tmp[1] = fill;
tmp[2] = fill;
tmp[3] = fill;
tmp[4] = fill;
tmp[5] = fill;
tmp[6] = p[0];
tmp[7] = p[1];
return boost::endian::endian_load<T, 8, order::big>( tmp );
}
};
// expanding load 3 -> 8
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 3, order::little>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[2] & 0x80 )? 0xFF: 0x00;
tmp[0] = p[0];
tmp[1] = p[1];
tmp[2] = p[2];
tmp[3] = fill;
tmp[4] = fill;
tmp[5] = fill;
tmp[6] = fill;
tmp[7] = fill;
return boost::endian::endian_load<T, 8, order::little>( tmp );
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 3, order::big>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[0] = fill;
tmp[1] = fill;
tmp[2] = fill;
tmp[3] = fill;
tmp[4] = fill;
tmp[5] = p[0];
tmp[6] = p[1];
tmp[7] = p[2];
return boost::endian::endian_load<T, 8, order::big>( tmp );
}
};
// expanding load 4 -> 8
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 4, order::little>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[3] & 0x80 )? 0xFF: 0x00;
tmp[0] = p[0];
tmp[1] = p[1];
tmp[2] = p[2];
tmp[3] = p[3];
tmp[4] = fill;
tmp[5] = fill;
tmp[6] = fill;
tmp[7] = fill;
return boost::endian::endian_load<T, 8, order::little>( tmp );
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 4, order::big>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[0] = fill;
tmp[1] = fill;
tmp[2] = fill;
tmp[3] = fill;
tmp[4] = p[0];
tmp[5] = p[1];
tmp[6] = p[2];
tmp[7] = p[3];
return boost::endian::endian_load<T, 8, order::big>( tmp );
}
};
// expanding load 5 -> 8
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 5, order::little>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[4] & 0x80 )? 0xFF: 0x00;
tmp[0] = p[0];
tmp[1] = p[1];
tmp[2] = p[2];
tmp[3] = p[3];
tmp[4] = p[4];
tmp[5] = fill;
tmp[6] = fill;
tmp[7] = fill;
return boost::endian::endian_load<T, 8, order::little>( tmp );
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 5, order::big>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[0] = fill;
tmp[1] = fill;
tmp[2] = fill;
tmp[3] = p[0];
tmp[4] = p[1];
tmp[5] = p[2];
tmp[6] = p[3];
tmp[7] = p[4];
return boost::endian::endian_load<T, 8, order::big>( tmp );
}
};
// expanding load 6 -> 8
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 6, order::little>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[5] & 0x80 )? 0xFF: 0x00;
tmp[0] = p[0];
tmp[1] = p[1];
tmp[2] = p[2];
tmp[3] = p[3];
tmp[4] = p[4];
tmp[5] = p[5];
tmp[6] = fill;
tmp[7] = fill;
return boost::endian::endian_load<T, 8, order::little>( tmp );
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 6, order::big>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[0] = fill;
tmp[1] = fill;
tmp[2] = p[0];
tmp[3] = p[1];
tmp[4] = p[2];
tmp[5] = p[3];
tmp[6] = p[4];
tmp[7] = p[5];
return boost::endian::endian_load<T, 8, order::big>( tmp );
}
};
// expanding load 7 -> 8
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 7, order::little>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[6] & 0x80 )? 0xFF: 0x00;
tmp[0] = p[0];
tmp[1] = p[1];
tmp[2] = p[2];
tmp[3] = p[3];
tmp[4] = p[4];
tmp[5] = p[5];
tmp[6] = p[6];
tmp[7] = fill;
return boost::endian::endian_load<T, 8, order::little>( tmp );
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 7, order::big>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[0] = fill;
tmp[1] = p[0];
tmp[2] = p[1];
tmp[3] = p[2];
tmp[4] = p[3];
tmp[5] = p[4];
tmp[6] = p[5];
tmp[7] = p[6];
return boost::endian::endian_load<T, 8, order::big>( tmp );
}
};
} // namespace detail
} // namespace endian
} // namespace boost
#endif // BOOST_ENDIAN_DETAIL_ENDIAN_LOAD_HPP_INCLUDED

View File

@ -0,0 +1,117 @@
#ifndef BOOST_ENDIAN_DETAIL_ENDIAN_REVERSE_HPP_INCLUDED
#define BOOST_ENDIAN_DETAIL_ENDIAN_REVERSE_HPP_INCLUDED
// Copyright 2019 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/endian/detail/integral_by_size.hpp>
#include <boost/endian/detail/intrinsic.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/static_assert.hpp>
#include <boost/cstdint.hpp>
#include <boost/config.hpp>
#include <cstddef>
#include <cstring>
#if defined(BOOST_ENDIAN_NO_INTRINSICS)
# if defined(BOOST_NO_CXX14_CONSTEXPR)
# define BOOST_ENDIAN_CONSTEXPR
# else
# define BOOST_ENDIAN_CONSTEXPR constexpr
# endif
#else
# if defined(BOOST_ENDIAN_CONSTEXPR_INTRINSICS)
# define BOOST_ENDIAN_CONSTEXPR BOOST_CONSTEXPR
# else
# define BOOST_ENDIAN_CONSTEXPR
# endif
#endif
namespace boost
{
namespace endian
{
namespace detail
{
// -- portable approach suggested by tymofey, with avoidance of undefined behavior
// as suggested by Giovanni Piero Deretta, with a further refinement suggested
// by Pyry Jahkola.
// -- intrinsic approach suggested by reviewers, and by David Stone, who provided
// his Boost licensed macro implementation (detail/intrinsic.hpp)
inline uint8_t BOOST_CONSTEXPR endian_reverse_impl( uint8_t x ) BOOST_NOEXCEPT
{
return x;
}
inline uint16_t BOOST_ENDIAN_CONSTEXPR endian_reverse_impl( uint16_t x ) BOOST_NOEXCEPT
{
#ifdef BOOST_ENDIAN_NO_INTRINSICS
return (x << 8) | (x >> 8);
#else
return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x);
#endif
}
inline uint32_t BOOST_ENDIAN_CONSTEXPR endian_reverse_impl(uint32_t x) BOOST_NOEXCEPT
{
#ifdef BOOST_ENDIAN_NO_INTRINSICS
uint32_t step16 = x << 16 | x >> 16;
return ((step16 << 8) & 0xff00ff00) | ((step16 >> 8) & 0x00ff00ff);
#else
return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x);
#endif
}
inline uint64_t BOOST_ENDIAN_CONSTEXPR endian_reverse_impl(uint64_t x) BOOST_NOEXCEPT
{
#ifdef BOOST_ENDIAN_NO_INTRINSICS
uint64_t step32 = x << 32 | x >> 32;
uint64_t step16 = (step32 & 0x0000FFFF0000FFFFULL) << 16 | (step32 & 0xFFFF0000FFFF0000ULL) >> 16;
return (step16 & 0x00FF00FF00FF00FFULL) << 8 | (step16 & 0xFF00FF00FF00FF00ULL) >> 8;
#else
return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x);
# endif
}
} // namespace detail
// Requires:
// T is non-bool integral
template<class T> inline BOOST_CONSTEXPR T endian_reverse( T x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value && !is_same<T, bool>::value );
typedef typename detail::integral_by_size< sizeof(T) >::type uintN_t;
return static_cast<T>( detail::endian_reverse_impl( static_cast<uintN_t>( x ) ) );
}
template <class EndianReversible>
inline void endian_reverse_inplace(EndianReversible& x) BOOST_NOEXCEPT
{
x = endian_reverse( x );
}
} // namespace endian
} // namespace boost
#endif // BOOST_ENDIAN_DETAIL_ENDIAN_REVERSE_HPP_INCLUDED

View File

@ -0,0 +1,442 @@
#ifndef BOOST_ENDIAN_DETAIL_ENDIAN_STORE_HPP_INCLUDED
#define BOOST_ENDIAN_DETAIL_ENDIAN_STORE_HPP_INCLUDED
// Copyright 2019 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/endian/detail/endian_reverse.hpp>
#include <boost/endian/detail/order.hpp>
#include <boost/endian/detail/integral_by_size.hpp>
#include <boost/endian/detail/is_trivially_copyable.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/is_enum.hpp>
#include <boost/static_assert.hpp>
#include <cstddef>
#include <cstring>
namespace boost
{
namespace endian
{
namespace detail
{
template<class T, std::size_t N1, BOOST_SCOPED_ENUM(order) O1, std::size_t N2, BOOST_SCOPED_ENUM(order) O2> struct endian_store_impl
{
};
} // namespace detail
// Requires:
//
// sizeof(T) must be 1, 2, 4, or 8
// 1 <= N <= sizeof(T)
// T is TriviallyCopyable
// if N < sizeof(T), T is integral or enum
template<class T, std::size_t N, BOOST_SCOPED_ENUM(order) Order>
inline void endian_store( unsigned char * p, T const & v ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8 );
BOOST_STATIC_ASSERT( N >= 1 && N <= sizeof(T) );
return detail::endian_store_impl<T, sizeof(T), order::native, N, Order>()( p, v );
}
namespace detail
{
// same endianness, same size
template<class T, std::size_t N, BOOST_SCOPED_ENUM(order) O> struct endian_store_impl<T, N, O, N, O>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_trivially_copyable<T>::value );
std::memcpy( p, &v, N );
}
};
// same size, reverse endianness
template<class T, std::size_t N, BOOST_SCOPED_ENUM(order) O1, BOOST_SCOPED_ENUM(order) O2> struct endian_store_impl<T, N, O1, N, O2>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_trivially_copyable<T>::value );
typename integral_by_size<N>::type tmp;
std::memcpy( &tmp, &v, N );
endian_reverse_inplace( tmp );
std::memcpy( p, &tmp, N );
}
};
// truncating store 2 -> 1
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 2, Order, 1, order::little>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 2 ];
boost::endian::endian_store<T, 2, order::little>( tmp, v );
p[0] = tmp[0];
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 2, Order, 1, order::big>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 2 ];
boost::endian::endian_store<T, 2, order::big>( tmp, v );
p[0] = tmp[1];
}
};
// truncating store 4 -> 1
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 4, Order, 1, order::little>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
boost::endian::endian_store<T, 4, order::little>( tmp, v );
p[0] = tmp[0];
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 4, Order, 1, order::big>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
boost::endian::endian_store<T, 4, order::big>( tmp, v );
p[0] = tmp[3];
}
};
// truncating store 4 -> 2
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 4, Order, 2, order::little>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
boost::endian::endian_store<T, 4, order::little>( tmp, v );
p[0] = tmp[0];
p[1] = tmp[1];
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 4, Order, 2, order::big>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
boost::endian::endian_store<T, 4, order::big>( tmp, v );
p[0] = tmp[2];
p[1] = tmp[3];
}
};
// truncating store 4 -> 3
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 4, Order, 3, order::little>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
boost::endian::endian_store<T, 4, order::little>( tmp, v );
p[0] = tmp[0];
p[1] = tmp[1];
p[2] = tmp[2];
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 4, Order, 3, order::big>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
boost::endian::endian_store<T, 4, order::big>( tmp, v );
p[0] = tmp[1];
p[1] = tmp[2];
p[2] = tmp[3];
}
};
// truncating store 8 -> 1
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 1, order::little>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::little>( tmp, v );
p[0] = tmp[0];
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 1, order::big>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::big>( tmp, v );
p[0] = tmp[7];
}
};
// truncating store 8 -> 2
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 2, order::little>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::little>( tmp, v );
p[0] = tmp[0];
p[1] = tmp[1];
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 2, order::big>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::big>( tmp, v );
p[0] = tmp[6];
p[1] = tmp[7];
}
};
// truncating store 8 -> 3
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 3, order::little>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::little>( tmp, v );
p[0] = tmp[0];
p[1] = tmp[1];
p[2] = tmp[2];
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 3, order::big>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::big>( tmp, v );
p[0] = tmp[5];
p[1] = tmp[6];
p[2] = tmp[7];
}
};
// truncating store 8 -> 4
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 4, order::little>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::little>( tmp, v );
p[0] = tmp[0];
p[1] = tmp[1];
p[2] = tmp[2];
p[3] = tmp[3];
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 4, order::big>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::big>( tmp, v );
p[0] = tmp[4];
p[1] = tmp[5];
p[2] = tmp[6];
p[3] = tmp[7];
}
};
// truncating store 8 -> 5
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 5, order::little>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::little>( tmp, v );
p[0] = tmp[0];
p[1] = tmp[1];
p[2] = tmp[2];
p[3] = tmp[3];
p[4] = tmp[4];
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 5, order::big>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::big>( tmp, v );
p[0] = tmp[3];
p[1] = tmp[4];
p[2] = tmp[5];
p[3] = tmp[6];
p[4] = tmp[7];
}
};
// truncating store 8 -> 6
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 6, order::little>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::little>( tmp, v );
p[0] = tmp[0];
p[1] = tmp[1];
p[2] = tmp[2];
p[3] = tmp[3];
p[4] = tmp[4];
p[5] = tmp[5];
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 6, order::big>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::big>( tmp, v );
p[0] = tmp[2];
p[1] = tmp[3];
p[2] = tmp[4];
p[3] = tmp[5];
p[4] = tmp[6];
p[5] = tmp[7];
}
};
// truncating store 8 -> 7
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 7, order::little>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::little>( tmp, v );
p[0] = tmp[0];
p[1] = tmp[1];
p[2] = tmp[2];
p[3] = tmp[3];
p[4] = tmp[4];
p[5] = tmp[5];
p[6] = tmp[6];
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 7, order::big>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::big>( tmp, v );
p[0] = tmp[1];
p[1] = tmp[2];
p[2] = tmp[3];
p[3] = tmp[4];
p[4] = tmp[5];
p[5] = tmp[6];
p[6] = tmp[7];
}
};
} // namespace detail
} // namespace endian
} // namespace boost
#endif // BOOST_ENDIAN_DETAIL_ENDIAN_STORE_HPP_INCLUDED

View File

@ -0,0 +1,47 @@
#ifndef BOOST_ENDIAN_DETAIL_INTEGRAL_BY_SIZE_HPP_INCLUDED
#define BOOST_ENDIAN_DETAIL_INTEGRAL_BY_SIZE_HPP_INCLUDED
// Copyright 2019 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/cstdint.hpp>
#include <cstddef>
namespace boost
{
namespace endian
{
namespace detail
{
template<std::size_t N> struct integral_by_size
{
};
template<> struct integral_by_size<1>
{
typedef uint8_t type;
};
template<> struct integral_by_size<2>
{
typedef uint16_t type;
};
template<> struct integral_by_size<4>
{
typedef uint32_t type;
};
template<> struct integral_by_size<8>
{
typedef uint64_t type;
};
} // namespace detail
} // namespace endian
} // namespace boost
#endif // BOOST_ENDIAN_DETAIL_INTEGRAL_BY_SIZE_HPP_INCLUDED

View File

@ -18,7 +18,7 @@
#define __has_builtin(x) 0 // Compatibility with non-clang compilers
#endif
#if defined(_MSC_VER)
#if defined(_MSC_VER) && ( !defined(__clang__) || defined(__c2__) )
// Microsoft documents these as being compatible since Windows 95 and specifically
// lists runtime library support since Visual Studio 2003 (aka 7.1).
// Clang/c2 uses the Microsoft rather than GCC intrinsics, so we check for
@ -46,7 +46,9 @@
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x) __builtin_bswap32(x)
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x) __builtin_bswap64(x)
// Linux systems provide the byteswap.h header, with
# define BOOST_ENDIAN_CONSTEXPR_INTRINSICS
// Linux systems provide the byteswap.h header, with
#elif defined(__linux__)
// don't check for obsolete forms defined(linux) and defined(__linux) on the theory that
// compilers that predefine only these are so old that byteswap.h probably isn't present.

View File

@ -0,0 +1,39 @@
#ifndef BOOST_ENDIAN_DETAIL_IS_TRIVIALLY_COPYABLE_HPP_INCLUDED
#define BOOST_ENDIAN_DETAIL_IS_TRIVIALLY_COPYABLE_HPP_INCLUDED
// Copyright 2019 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/config.hpp>
#include <boost/type_traits/has_trivial_copy.hpp>
#include <boost/type_traits/has_trivial_assign.hpp>
#if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
# include <type_traits>
#endif
namespace boost
{
namespace endian
{
namespace detail
{
#if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
using std::is_trivially_copyable;
#else
template<class T> struct is_trivially_copyable: boost::integral_constant<bool,
boost::has_trivial_copy<T>::value && boost::has_trivial_assign<T>::value> {};
#endif
} // namespace detail
} // namespace endian
} // namespace boost
#endif // BOOST_ENDIAN_DETAIL_IS_TRIVIALLY_COPYABLE_HPP_INCLUDED

View File

@ -1,223 +0,0 @@
// boost/endian/detail/lightweight_test.hpp --------------------------------------------//
#ifndef BOOST_ENDIAN_LIGHTWEIGHT_TEST_HPP
#define BOOST_ENDIAN_LIGHTWEIGHT_TEST_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER)
# pragma once
#endif
//
// Copyright (c) 2002, 2009, 2014 Peter Dimov
// Copyright (2) Beman Dawes 2010, 2011, 2015
// Copyright (3) Ion Gaztanaga 2013
//
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
//
#include <boost/assert.hpp>
#include <boost/current_function.hpp>
#include <boost/core/no_exceptions_support.hpp>
#include <cstring> // for memcmp
#include <iostream>
// IDE's like Visual Studio perform better if output goes to std::cout or
// some other stream, so allow user to configure output stream:
#ifndef BOOST_LIGHTWEIGHT_TEST_OSTREAM
# define BOOST_LIGHTWEIGHT_TEST_OSTREAM std::cerr
#endif
namespace boost
{
namespace endian
{
namespace detail
{
struct report_errors_reminder
{
bool called_report_errors_function;
report_errors_reminder() : called_report_errors_function(false) {}
~report_errors_reminder()
{
BOOST_ASSERT(called_report_errors_function); // verify report_errors() was called
}
};
inline report_errors_reminder& report_errors_remind()
{
static report_errors_reminder r;
return r;
}
inline int & test_errors()
{
static int x = 0;
report_errors_remind();
return x;
}
inline void test_failed_impl(char const * expr, char const * file, int line, char const * function)
{
BOOST_LIGHTWEIGHT_TEST_OSTREAM
<< file << "(" << line << "): test '" << expr << "' failed in function '"
<< function << "'" << std::endl;
++test_errors();
}
inline void error_impl(char const * msg, char const * file, int line, char const * function)
{
BOOST_LIGHTWEIGHT_TEST_OSTREAM
<< file << "(" << line << "): " << msg << " in function '"
<< function << "'" << std::endl;
++test_errors();
}
inline void throw_failed_impl(char const * excep, char const * file, int line, char const * function)
{
BOOST_LIGHTWEIGHT_TEST_OSTREAM
<< file << "(" << line << "): Exception '" << excep << "' not thrown in function '"
<< function << "'" << std::endl;
++test_errors();
}
template<class T, class U> inline void test_eq_impl( char const * expr1, char const * expr2,
char const * file, int line, char const * function, T const & t, U const & u )
{
if( t == u )
{
report_errors_remind();
}
else
{
BOOST_LIGHTWEIGHT_TEST_OSTREAM
<< file << "(" << line << "): test '" << expr1 << " == " << expr2
<< "' failed in function '" << function << "': "
<< "'" << t << "' != '" << u << "'" << std::endl;
++test_errors();
}
}
template<class T, class U> inline void test_ne_impl( char const * expr1, char const * expr2,
char const * file, int line, char const * function, T const & t, U const & u )
{
if( t != u )
{
report_errors_remind();
}
else
{
BOOST_LIGHTWEIGHT_TEST_OSTREAM
<< file << "(" << line << "): test '" << expr1 << " != " << expr2
<< "' failed in function '" << function << "': "
<< "'" << t << "' == '" << u << "'" << std::endl;
++test_errors();
}
}
template <class T>
std::string to_hex(const T& x)
{
const char hex[] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
std::string tmp;
const unsigned char* p = reinterpret_cast<const unsigned char*>(&x);
const unsigned char* e = p + sizeof(T);
for (; p < e; ++p)
{
tmp += hex[*p >> 4]; // high-order nibble
tmp += hex[*p & 0x0f]; // low-order nibble
}
return tmp;
}
template<class T, class U> inline bool test_memcmp_eq_impl(char const * expr1,
char const * expr2, char const * file, int line, char const * function, T const & t,
U const & u)
{
BOOST_ASSERT(sizeof(T) == sizeof(U));
if (sizeof(T) == sizeof(U)
&& std::memcmp(&t, &u, sizeof(T)) == 0)
{
report_errors_remind();
return true;
}
else
{
BOOST_LIGHTWEIGHT_TEST_OSTREAM
<< file << "(" << line << "): test 'std::memcmp(" << expr1 << ", " << expr2
<< ") == 0' fails in function '" << function << "': "
<< " with values '" << to_hex(t) << "' and '" << to_hex(u) << "'" << std::endl;
++test_errors();
return false;
}
}
} // namespace detail
inline int report_errors()
{
boost::endian::detail::report_errors_remind().called_report_errors_function = true;
int errors = boost::endian::detail::test_errors();
if( errors == 0 )
{
BOOST_LIGHTWEIGHT_TEST_OSTREAM
<< "No errors detected." << std::endl;
return 0;
}
else
{
BOOST_LIGHTWEIGHT_TEST_OSTREAM
<< errors << " error" << (errors == 1? "": "s") << " detected." << std::endl;
return 1;
}
}
} // namespace endian
} // namespace boost
//////////////////////////////////////////////////////////////////////////////////////////
// TODO: Should all test macros return bool? See BOOST_TEST_MEM_EQ usage in fp_exaustive_test,cpp
//////////////////////////////////////////////////////////////////////////////////////////
#define BOOST_TEST(expr) \
((expr)? (void)0: ::boost::endian::detail::test_failed_impl(#expr, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION))
#define BOOST_ERROR(msg) \
( ::boost::endian::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )
#define BOOST_TEST_EQ(expr1,expr2) \
( ::boost::endian::detail::test_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
#define BOOST_TEST_NE(expr1,expr2) \
( ::boost::endian::detail::test_ne_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
#define BOOST_TEST_MEM_EQ(expr1,expr2) \
(::boost::endian::detail::test_memcmp_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2))
#ifndef BOOST_NO_EXCEPTIONS
#define BOOST_TEST_THROWS( EXPR, EXCEP ) \
try { \
EXPR; \
::boost::detail::throw_failed_impl \
(#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
} \
catch(EXCEP const&) { \
} \
catch(...) { \
::boost::detail::throw_failed_impl \
(#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
} \
//
#else
#define BOOST_TEST_THROWS( EXPR, EXCEP )
#endif
#endif // #ifndef BOOST_ENDIAN_LIGHTWEIGHT_TEST_HPP

View File

@ -0,0 +1,35 @@
#ifndef BOOST_ENDIAN_DETAIL_ORDER_HPP_INCLUDED
#define BOOST_ENDIAN_DETAIL_ORDER_HPP_INCLUDED
// Copyright 2019 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/core/scoped_enum.hpp>
#include <boost/predef/other/endian.h>
namespace boost
{
namespace endian
{
BOOST_SCOPED_ENUM_START(order)
{
big, little,
# if BOOST_ENDIAN_BIG_BYTE
native = big
# else
native = little
# endif
}; BOOST_SCOPED_ENUM_END
} // namespace endian
} // namespace boost
#endif // BOOST_ENDIAN_DETAIL_ORDER_HPP_INCLUDED

View File

@ -14,6 +14,105 @@
# error "<boost/endian/endian.hpp> is deprecated. Define BOOST_ENDIAN_DEPRECATED_NAMES to use."
#endif
#include <boost/config/header_deprecated.hpp>
BOOST_HEADER_DEPRECATED( "<boost/endian/arithmetic.hpp>" )
#include <boost/endian/arithmetic.hpp>
#include <boost/config.hpp>
namespace boost
{
namespace endian
{
typedef order endianness;
typedef align alignment;
# ifndef BOOST_NO_CXX11_TEMPLATE_ALIASES
template <BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits,
BOOST_SCOPED_ENUM(align) Align = align::no>
using endian = endian_arithmetic<Order, T, n_bits, Align>;
# endif
// unaligned big endian signed integer types
typedef endian_arithmetic< order::big, int_least8_t, 8 > big8_t;
typedef endian_arithmetic< order::big, int_least16_t, 16 > big16_t;
typedef endian_arithmetic< order::big, int_least32_t, 24 > big24_t;
typedef endian_arithmetic< order::big, int_least32_t, 32 > big32_t;
typedef endian_arithmetic< order::big, int_least64_t, 40 > big40_t;
typedef endian_arithmetic< order::big, int_least64_t, 48 > big48_t;
typedef endian_arithmetic< order::big, int_least64_t, 56 > big56_t;
typedef endian_arithmetic< order::big, int_least64_t, 64 > big64_t;
// unaligned big endian_arithmetic unsigned integer types
typedef endian_arithmetic< order::big, uint_least8_t, 8 > ubig8_t;
typedef endian_arithmetic< order::big, uint_least16_t, 16 > ubig16_t;
typedef endian_arithmetic< order::big, uint_least32_t, 24 > ubig24_t;
typedef endian_arithmetic< order::big, uint_least32_t, 32 > ubig32_t;
typedef endian_arithmetic< order::big, uint_least64_t, 40 > ubig40_t;
typedef endian_arithmetic< order::big, uint_least64_t, 48 > ubig48_t;
typedef endian_arithmetic< order::big, uint_least64_t, 56 > ubig56_t;
typedef endian_arithmetic< order::big, uint_least64_t, 64 > ubig64_t;
// unaligned little endian_arithmetic signed integer types
typedef endian_arithmetic< order::little, int_least8_t, 8 > little8_t;
typedef endian_arithmetic< order::little, int_least16_t, 16 > little16_t;
typedef endian_arithmetic< order::little, int_least32_t, 24 > little24_t;
typedef endian_arithmetic< order::little, int_least32_t, 32 > little32_t;
typedef endian_arithmetic< order::little, int_least64_t, 40 > little40_t;
typedef endian_arithmetic< order::little, int_least64_t, 48 > little48_t;
typedef endian_arithmetic< order::little, int_least64_t, 56 > little56_t;
typedef endian_arithmetic< order::little, int_least64_t, 64 > little64_t;
// unaligned little endian_arithmetic unsigned integer types
typedef endian_arithmetic< order::little, uint_least8_t, 8 > ulittle8_t;
typedef endian_arithmetic< order::little, uint_least16_t, 16 > ulittle16_t;
typedef endian_arithmetic< order::little, uint_least32_t, 24 > ulittle24_t;
typedef endian_arithmetic< order::little, uint_least32_t, 32 > ulittle32_t;
typedef endian_arithmetic< order::little, uint_least64_t, 40 > ulittle40_t;
typedef endian_arithmetic< order::little, uint_least64_t, 48 > ulittle48_t;
typedef endian_arithmetic< order::little, uint_least64_t, 56 > ulittle56_t;
typedef endian_arithmetic< order::little, uint_least64_t, 64 > ulittle64_t;
// unaligned native endian_arithmetic signed integer types
typedef endian_arithmetic< order::native, int_least8_t, 8 > native8_t;
typedef endian_arithmetic< order::native, int_least16_t, 16 > native16_t;
typedef endian_arithmetic< order::native, int_least32_t, 24 > native24_t;
typedef endian_arithmetic< order::native, int_least32_t, 32 > native32_t;
typedef endian_arithmetic< order::native, int_least64_t, 40 > native40_t;
typedef endian_arithmetic< order::native, int_least64_t, 48 > native48_t;
typedef endian_arithmetic< order::native, int_least64_t, 56 > native56_t;
typedef endian_arithmetic< order::native, int_least64_t, 64 > native64_t;
// unaligned native endian_arithmetic unsigned integer types
typedef endian_arithmetic< order::native, uint_least8_t, 8 > unative8_t;
typedef endian_arithmetic< order::native, uint_least16_t, 16 > unative16_t;
typedef endian_arithmetic< order::native, uint_least32_t, 24 > unative24_t;
typedef endian_arithmetic< order::native, uint_least32_t, 32 > unative32_t;
typedef endian_arithmetic< order::native, uint_least64_t, 40 > unative40_t;
typedef endian_arithmetic< order::native, uint_least64_t, 48 > unative48_t;
typedef endian_arithmetic< order::native, uint_least64_t, 56 > unative56_t;
typedef endian_arithmetic< order::native, uint_least64_t, 64 > unative64_t;
// aligned native endian_arithmetic typedefs are not provided because
// <cstdint> types are superior for this use case
typedef endian_arithmetic< order::big, int16_t, 16, align::yes > aligned_big16_t;
typedef endian_arithmetic< order::big, uint16_t, 16, align::yes > aligned_ubig16_t;
typedef endian_arithmetic< order::little, int16_t, 16, align::yes > aligned_little16_t;
typedef endian_arithmetic< order::little, uint16_t, 16, align::yes > aligned_ulittle16_t;
typedef endian_arithmetic< order::big, int32_t, 32, align::yes > aligned_big32_t;
typedef endian_arithmetic< order::big, uint32_t, 32, align::yes > aligned_ubig32_t;
typedef endian_arithmetic< order::little, int32_t, 32, align::yes > aligned_little32_t;
typedef endian_arithmetic< order::little, uint32_t, 32, align::yes > aligned_ulittle32_t;
typedef endian_arithmetic< order::big, int64_t, 64, align::yes > aligned_big64_t;
typedef endian_arithmetic< order::big, uint64_t, 64, align::yes > aligned_ubig64_t;
typedef endian_arithmetic< order::little, int64_t, 64, align::yes > aligned_little64_t;
typedef endian_arithmetic< order::little, uint64_t, 64, align::yes > aligned_ulittle64_t;
} // namespace endian
} // namespace boost
#endif //BOOST_ENDIAN_ENDIAN_HPP

View File

@ -1,38 +0,0 @@
// boost/endian/std_pair.hpp ---------------------------------------------------------//
// Copyright Beman Dawes 2013
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
//--------------------------------------------------------------------------------------//
#ifndef BOOST_ENDIAN_STD_PAIR_HPP
#define BOOST_ENDIAN_STD_PAIR_HPP
#include <boost/endian/conversion.hpp>
#include <utility>
namespace boost
{
namespace endian
{
template <class ReversibleValueT, class ReversibleValueU>
std::pair<ReversibleValueT, ReversibleValueU>
reverse_value(std::pair<ReversibleValueT, ReversibleValueU> x)
{
return std::pair<ReversibleValueT, ReversibleValueU>(reverse_value(x.first),
reverse_value(x.second));
}
template <class ReversibleT, class ReversibleU>
void reverse(std::pair<ReversibleT, ReversibleU>& x)
{
reverse(x.first);
reverse(x.second);
}
}
}
#endif // BOOST_ENDIAN_STD_PAIR_HPP

View File

@ -1,14 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="0; URL=doc/index.html">
<meta http-equiv="refresh" content="0; URL=doc/html/endian.html">
</head>
<body>
<p>
Automatic redirection failed, please go to
<a href="doc/index.html">doc/index.html</a>.&nbsp;<hr>
<p><font size="2"><EFBFBD> Copyright Beman Dawes, 2001</font></p>
<p><font size="2">Distributed under the Boost Software License, Version 1.0.
(See <a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a>)
</font>
<a href="doc/html/endian.html">doc/html/endian.html</a>.
</p>
</body>
</html>
</html>
<!--
Copyright 2001 Beman Dawes
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
-->

View File

@ -1,28 +1,84 @@
# Boost Endian Library test Jamfile
# Copyright Beman Dawes 2006, 2013
# Copyright 2018, 2019 Peter Dimov
# Distributed under the Boost Software License, Version 1.0.
# See http://www.boost.org/LICENSE_1_0.txt
# See library home page at http://www.boost.org/libs/endian
import testing ;
project
: requirements
<toolset>msvc:<asynch-exceptions>on
;
test-suite "endian"
:
[ run buffer_test.cpp # sources
: # command line
: # input files
: # requirements
: # target name
]
[ run endian_test.cpp ]
[ run endian_operations_test.cpp ]
[ run endian_in_union_test.cpp ]
[ run conversion_test.cpp ]
# [ run floating_point_test.cpp : : : <test-info>always_show_run_output ]
;
: default-build
<warnings>all
: requirements
<toolset>msvc:<warnings-as-errors>on
<toolset>gcc:<cxxflags>-Wno-long-long
<toolset>gcc-4.4.7:<cxxflags>-Wno-strict-aliasing
<toolset>gcc-4.4.7:<cxxflags>-Wno-sign-compare
<toolset>gcc:<warnings-as-errors>on
<toolset>clang:<cxxflags>-Wno-long-long
<toolset>clang:<warnings-as-errors>on
;
run buffer_test.cpp ;
run buffer_test.cpp : : : <define>BOOST_ENDIAN_NO_INTRINSICS : buffer_test_ni ;
run endian_test.cpp ;
run endian_test.cpp : : : <define>BOOST_ENDIAN_NO_INTRINSICS : endian_test_ni ;
run endian_operations_test.cpp ;
run endian_operations_test.cpp : : : <define>BOOST_ENDIAN_NO_INTRINSICS : endian_operations_test_ni ;
run endian_in_union_test.cpp ;
run conversion_test.cpp ;
run conversion_test.cpp : : : <define>BOOST_ENDIAN_NO_INTRINSICS : conversion_test_ni ;
run intrinsic_test.cpp ;
run quick.cpp ;
local allow-warnings =
"-<toolset>msvc:<warnings-as-errors>on"
"-<toolset>gcc:<warnings-as-errors>on"
"-<toolset>clang:<warnings-as-errors>on" ;
compile spirit_conflict_test.cpp
: $(allow-warnings) ;
run endian_reverse_test.cpp ;
run endian_reverse_test.cpp : : : <define>BOOST_ENDIAN_NO_INTRINSICS : endian_reverse_test_ni ;
run endian_load_test.cpp ;
run endian_load_test.cpp : : : <define>BOOST_ENDIAN_NO_INTRINSICS : endian_load_test_ni ;
run endian_store_test.cpp ;
run endian_store_test.cpp : : : <define>BOOST_ENDIAN_NO_INTRINSICS : endian_store_test_ni ;
run endian_ld_st_roundtrip_test.cpp ;
run endian_ld_st_roundtrip_test.cpp : : : <define>BOOST_ENDIAN_NO_INTRINSICS : endian_ld_st_roundtrip_test_ni ;
run endian_arithmetic_test.cpp ;
run endian_arithmetic_test.cpp : : : <define>BOOST_ENDIAN_NO_INTRINSICS : endian_arithmetic_test_ni ;
run deprecated_test.cpp ;
compile endian_reverse_cx_test.cpp ;
compile endian_reverse_cx_test.cpp : <define>BOOST_ENDIAN_NO_INTRINSICS : endian_reverse_cx_test_ni ;
run load_convenience_test.cpp ;
run load_convenience_test.cpp : : : <define>BOOST_ENDIAN_NO_INTRINSICS : load_convenience_test_ni ;
run store_convenience_test.cpp ;
run store_convenience_test.cpp : : : <define>BOOST_ENDIAN_NO_INTRINSICS : store_convenience_test_ni ;
run float_typedef_test.cpp ;
run float_typedef_test.cpp : : : <define>BOOST_ENDIAN_NO_INTRINSICS : float_typedef_test_ni ;

View File

@ -41,24 +41,24 @@ namespace
#endif
typedef boost::timer::nanosecond_type nanosecond_t;
//--------------------------------------------------------------------------------------//
nanosecond_t benchmark(timee_func timee, const char* msg,
nanosecond_t overhead = 0)
// Returns: total cpu time (i.e. system time + user time)
{
if (verbose)
cout << "\nRunning benchmark..." << endl;
int64_t sum = 0;
{
if (verbose)
cout << "\nRunning benchmark..." << endl;
int64_t sum = 0;
boost::timer::cpu_times times;
nanosecond_t cpu_time;
boost::timer::auto_cpu_timer t(places);
for (long long i = n_cases; i; --i)
{
boost::timer::auto_cpu_timer t(places);
for (long long i = n_cases; i; --i)
{
# ifndef BOOST_TWO_ARG
sum += timee(static_cast<int32_t>(i)) ;
sum += timee(static_cast<int32_t>(i)) ;
# else
int32_t y;
timee(static_cast<int32_t>(i), y);
@ -71,13 +71,13 @@ namespace
const long double sec = 1000000000.0L;
cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
cout.precision(places);
cout << msg << " " << cpu_time / sec << endl;
if (verbose)
{
cout << msg << " " << cpu_time / sec << endl;
if (verbose)
{
t.report();
cout << " Benchmark complete\n"
" sum is " << sum << endl;
cout << " Benchmark complete\n"
" sum is " << sum << endl;
}
return cpu_time;
}
@ -100,7 +100,7 @@ namespace
n_cases = _atoi64(argv[1]);
#endif
for (; argc > 2; ++argv, --argc)
for (; argc > 2; ++argv, --argc)
{
if ( *(argv[2]+1) == 'p' )
places = atoi( argv[2]+2 );
@ -114,7 +114,7 @@ namespace
}
}
if (argc < 2)
if (argc < 2)
{
cout << "Usage: benchmark n [Options]\n"
" The argument n specifies the number of test cases to run\n"
@ -211,7 +211,7 @@ namespace
//-------------------------------------- main() ---------------------------------------//
int main(int argc, char * argv[])
int main(int argc, char * argv[])
{
process_command_line(argc, argv);

View File

@ -11,13 +11,13 @@
#include <boost/endian/detail/disable_warnings.hpp>
//#define BOOST_ENDIAN_LOG
#include <boost/endian/buffers.hpp>
#include <boost/detail/lightweight_main.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/cstdint.hpp>
#include <iostream>
#include <sstream>
#include <limits>
using namespace boost::endian;
using std::cout;
@ -49,6 +49,9 @@ namespace
BOOST_TEST_EQ(sizeof(big_uint56_buf_t), 7u);
BOOST_TEST_EQ(sizeof(big_uint64_buf_t), 8u);
BOOST_TEST_EQ(sizeof(big_float32_buf_t), 4u);
BOOST_TEST_EQ(sizeof(big_float64_buf_t), 8u);
BOOST_TEST_EQ(sizeof(little_int8_buf_t), 1u);
BOOST_TEST_EQ(sizeof(little_int16_buf_t), 2u);
BOOST_TEST_EQ(sizeof(little_int24_buf_t), 3u);
@ -67,6 +70,9 @@ namespace
BOOST_TEST_EQ(sizeof(little_uint56_buf_t), 7u);
BOOST_TEST_EQ(sizeof(little_uint64_buf_t), 8u);
BOOST_TEST_EQ(sizeof(little_float32_buf_t), 4u);
BOOST_TEST_EQ(sizeof(little_float64_buf_t), 8u);
BOOST_TEST_EQ(sizeof(native_int8_buf_t), 1u);
BOOST_TEST_EQ(sizeof(native_int16_buf_t), 2u);
BOOST_TEST_EQ(sizeof(native_int24_buf_t), 3u);
@ -85,6 +91,9 @@ namespace
BOOST_TEST_EQ(sizeof(native_uint56_buf_t), 7u);
BOOST_TEST_EQ(sizeof(native_uint64_buf_t), 8u);
BOOST_TEST_EQ(sizeof(native_float32_buf_t), 4u);
BOOST_TEST_EQ(sizeof(native_float64_buf_t), 8u);
BOOST_TEST_EQ(sizeof(big_int8_buf_at), 1u);
BOOST_TEST_EQ(sizeof(big_int16_buf_at), 2u);
BOOST_TEST_EQ(sizeof(big_int32_buf_at), 4u);
@ -95,6 +104,9 @@ namespace
BOOST_TEST_EQ(sizeof(big_uint32_buf_at), 4u);
BOOST_TEST_EQ(sizeof(big_uint64_buf_at), 8u);
BOOST_TEST_EQ(sizeof(big_float32_buf_at), 4u);
BOOST_TEST_EQ(sizeof(big_float64_buf_at), 8u);
BOOST_TEST_EQ(sizeof(little_int8_buf_at), 1u);
BOOST_TEST_EQ(sizeof(little_int16_buf_at), 2u);
BOOST_TEST_EQ(sizeof(little_int32_buf_at), 4u);
@ -104,6 +116,10 @@ namespace
BOOST_TEST_EQ(sizeof(little_uint16_buf_at), 2u);
BOOST_TEST_EQ(sizeof(little_uint32_buf_at), 4u);
BOOST_TEST_EQ(sizeof(little_uint64_buf_at), 8u);
BOOST_TEST_EQ(sizeof(little_float32_buf_at), 4u);
BOOST_TEST_EQ(sizeof(little_float64_buf_at), 8u);
} // check_size
// test_inserter_and_extractor -----------------------------------------------------//
@ -144,6 +160,130 @@ namespace
}
template<class T> struct unaligned
{
char x;
T y;
};
template<class T> void test_buffer_type( typename T::value_type v1, typename T::value_type v2 )
{
T buffer( v1 );
BOOST_TEST_EQ( buffer.value(), v1 );
buffer = v2;
BOOST_TEST_EQ( buffer.value(), v2 );
unaligned<T> buffer2 = { 0, T( v1 ) };
BOOST_TEST_EQ( buffer2.y.value(), v1 );
buffer2.y = v2;
BOOST_TEST_EQ( buffer2.y.value(), v2 );
}
void test_construction_and_assignment()
{
std::cout << "test construction and assignment..." << std::endl;
test_buffer_type< big_int8_buf_at>( 0x01, -0x01 );
test_buffer_type<big_int16_buf_at>( 0x0102, -0x0102 );
test_buffer_type<big_int32_buf_at>( 0x01020304, -0x01020304 );
test_buffer_type<big_int64_buf_at>( 0x0102030405060708LL, -0x0102030405060708LL );
test_buffer_type< big_uint8_buf_at>( 0x01, 0xFE );
test_buffer_type<big_uint16_buf_at>( 0x0102, 0xFE02 );
test_buffer_type<big_uint32_buf_at>( 0x01020304, 0xFE020304 );
test_buffer_type<big_uint64_buf_at>( 0x0102030405060708ULL, 0xFE02030405060708ULL );
test_buffer_type<big_float32_buf_at>( +1.5f, -3.14f );
test_buffer_type<big_float64_buf_at>( +1.5, -3.14 );
test_buffer_type< little_int8_buf_at>( 0x01, -0x01 );
test_buffer_type<little_int16_buf_at>( 0x0102, -0x0102 );
test_buffer_type<little_int32_buf_at>( 0x01020304, -0x01020304 );
test_buffer_type<little_int64_buf_at>( 0x0102030405060708LL, -0x0102030405060708LL );
test_buffer_type< little_uint8_buf_at>( 0x01, 0xFE );
test_buffer_type<little_uint16_buf_at>( 0x0102, 0xFE02 );
test_buffer_type<little_uint32_buf_at>( 0x01020304, 0xFE020304 );
test_buffer_type<little_uint64_buf_at>( 0x0102030405060708ULL, 0xFE02030405060708ULL );
test_buffer_type<little_float32_buf_at>( +1.5f, -3.14f );
test_buffer_type<little_float64_buf_at>( +1.5, -3.14 );
test_buffer_type< big_int8_buf_t>( 0x01, -0x01 );
test_buffer_type<big_int16_buf_t>( 0x0102, -0x0102 );
test_buffer_type<big_int24_buf_t>( 0x010203, -0x010203 );
test_buffer_type<big_int32_buf_t>( 0x01020304, -0x01020304 );
test_buffer_type<big_int40_buf_t>( 0x0102030405LL, -0x0102030405LL );
test_buffer_type<big_int48_buf_t>( 0x010203040506LL, -0x010203040506LL );
test_buffer_type<big_int56_buf_t>( 0x01020304050607LL, -0x01020304050607LL );
test_buffer_type<big_int64_buf_t>( 0x0102030405060708LL, -0x0102030405060708LL );
test_buffer_type<big_float32_buf_t>( +1.5f, -3.14f );
test_buffer_type<big_float64_buf_t>( +1.5, -3.14 );
test_buffer_type< little_uint8_buf_t>( 0x01, 0xFE );
test_buffer_type<little_uint16_buf_t>( 0x0102, 0xFE02 );
test_buffer_type<little_uint24_buf_t>( 0x010203, 0xFE0203 );
test_buffer_type<little_uint32_buf_t>( 0x01020304, 0xFE020304 );
test_buffer_type<little_uint40_buf_t>( 0x0102030405ULL, 0xFE02030405ULL );
test_buffer_type<little_uint48_buf_t>( 0x010203040506ULL, 0xFE0203040506ULL );
test_buffer_type<little_uint56_buf_t>( 0x01020304050607ULL, 0xFE020304050607ULL );
test_buffer_type<little_uint64_buf_t>( 0x0102030405060708ULL, 0xFE02030405060708ULL );
test_buffer_type<little_float32_buf_t>( +1.5f, -3.14f );
test_buffer_type<little_float64_buf_t>( +1.5, -3.14 );
std::cout << "test construction and assignment complete" << std::endl;
}
template <typename T>
void test_boundary_values_()
{
test_buffer_type< endian_buffer<order::big, T, sizeof(T) * CHAR_BIT, align::no > >( std::numeric_limits<T>::min(), std::numeric_limits<T>::max() );
test_buffer_type< endian_buffer<order::little, T, sizeof(T) * CHAR_BIT, align::no > >( std::numeric_limits<T>::min(), std::numeric_limits<T>::max() );
test_buffer_type< endian_buffer<order::big, T, sizeof(T) * CHAR_BIT, align::yes> >( std::numeric_limits<T>::min(), std::numeric_limits<T>::max() );
test_buffer_type< endian_buffer<order::little, T, sizeof(T) * CHAR_BIT, align::yes> >( std::numeric_limits<T>::min(), std::numeric_limits<T>::max() );
}
void test_boundary_values()
{
std::cout << "test boundary values..." << std::endl;
// integer types
test_boundary_values_<signed char>();
test_boundary_values_<unsigned char>();
test_boundary_values_<signed short>();
test_boundary_values_<unsigned short>();
test_boundary_values_<signed int>();
test_boundary_values_<unsigned int>();
test_boundary_values_<signed long>();
test_boundary_values_<unsigned long>();
test_boundary_values_<signed long long>();
test_boundary_values_<unsigned long long>();
// character types
test_boundary_values_<char>();
#if !defined(BOOST_NO_CXX11_CHAR16_T)
test_boundary_values_<char16_t>();
#endif
#if !defined(BOOST_NO_CXX11_CHAR32_T)
test_boundary_values_<char32_t>();
#endif
// floating-point types
test_boundary_values_<float>();
test_boundary_values_<double>();
std::cout << "test boundary values complete" << std::endl;
}
} // unnamed namespace
//--------------------------------------------------------------------------------------//
@ -174,6 +314,8 @@ int cpp_main(int, char *[])
check_size();
test_inserter_and_extractor();
test_construction_and_assignment();
test_boundary_values();
cout << " done" << endl;

View File

@ -0,0 +1,39 @@
# Copyright 2018, 2019 Peter Dimov
# Distributed under the Boost Software License, Version 1.0.
# See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt
cmake_minimum_required(VERSION 3.5)
project(cmake_subdir_test LANGUAGES CXX)
add_subdirectory(../.. boostorg/endian)
# boost_add_subdir
function(boost_add_subdir name)
add_subdirectory(../../../${name} boostorg/${name})
endfunction()
# primary dependencies
boost_add_subdir(config)
boost_add_subdir(core)
boost_add_subdir(predef)
boost_add_subdir(static_assert)
boost_add_subdir(type_traits)
# secondary dependencies
boost_add_subdir(assert)
# --target check
add_executable(quick ../quick.cpp)
target_link_libraries(quick Boost::endian Boost::core)
enable_testing()
add_test(quick quick)
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -C $<CONFIG>)

View File

@ -14,6 +14,7 @@
#include <boost/core/lightweight_test.hpp>
#include <iostream>
#include <cstring>
#include <algorithm>
namespace be = boost::endian;
using std::cout;
@ -27,6 +28,13 @@ using boost::uint32_t;
using boost::int64_t;
using boost::uint64_t;
template <class T> inline T std_endian_reverse(T x) BOOST_NOEXCEPT
{
T tmp(x);
std::reverse( reinterpret_cast<unsigned char*>(&tmp), reinterpret_cast<unsigned char*>(&tmp) + sizeof(T) );
return tmp;
}
namespace
{
@ -34,7 +42,7 @@ namespace
void native_value(int8_t& x) {x = static_cast<int8_t>(0xF0U);}
void native_value(uint8_t& x) {x = static_cast<uint8_t>(0xF0U);}
# ifdef BOOST_BIG_ENDIAN
# if BOOST_ENDIAN_BIG_BYTE
void big_value(int8_t& x) {x = static_cast<int8_t>(0xF0U);}
void big_value(uint8_t& x) {x = static_cast<uint8_t>(0xF0U);}
void little_value(int8_t& x) {x = static_cast<int8_t>(0xF0U);}
@ -48,7 +56,7 @@ namespace
void native_value(int16_t& x) {x = static_cast<int16_t>(0xF102U);}
void native_value(uint16_t& x) {x = static_cast<uint16_t>(0xF102U);}
# ifdef BOOST_BIG_ENDIAN
# if BOOST_ENDIAN_BIG_BYTE
void big_value(int16_t& x) {x = static_cast<int16_t>(0xF102U);}
void big_value(uint16_t& x) {x = static_cast<uint16_t>(0xF102U);}
void little_value(int16_t& x) {x = static_cast<int16_t>(0x02F1U);}
@ -62,7 +70,7 @@ namespace
void native_value(int32_t& x) {x = static_cast<int32_t>(0xF1E21304UL);}
void native_value(uint32_t& x) {x = static_cast<uint32_t>(0xF1E21304UL);}
# ifdef BOOST_BIG_ENDIAN
# if BOOST_ENDIAN_BIG_BYTE
void big_value(int32_t& x) {x = static_cast<int32_t>(0xF1E21304UL);}
void big_value(uint32_t& x) {x = static_cast<uint32_t>(0xF1E21304UL);}
void little_value(int32_t& x) {x = static_cast<int32_t>(0x0413E2F1UL);}
@ -76,7 +84,7 @@ namespace
void native_value(int64_t& x) {x = static_cast<int64_t>(0xF1E2D3C444231201ULL);}
void native_value(uint64_t& x) {x = static_cast<uint64_t>(0xF1E2D3C444231201ULL);}
# ifdef BOOST_BIG_ENDIAN
# if BOOST_ENDIAN_BIG_BYTE
void big_value(int64_t& x) {x = static_cast<int64_t>(0xF1E2D3C444231201ULL);}
void big_value(uint64_t& x) {x = static_cast<uint64_t>(0xF1E2D3C444231201ULL);}
void little_value(int64_t& x) {x = static_cast<int64_t>(0x01122344C4D3E2F1ULL);}
@ -100,11 +108,11 @@ namespace
// validate the values used by the tests below
# ifdef BOOST_BIG_ENDIAN
# if BOOST_ENDIAN_BIG_BYTE
BOOST_TEST_EQ(native, big);
BOOST_TEST_EQ(be::detail::std_endian_reverse(native), little);
BOOST_TEST_EQ(::std_endian_reverse(native), little);
# else
BOOST_TEST_EQ(be::detail::std_endian_reverse(native), big);
BOOST_TEST_EQ(::std_endian_reverse(native), big);
BOOST_TEST_EQ(native, little);
# endif
@ -210,16 +218,16 @@ namespace
be::conditional_reverse_inplace(x, be::order::big, be::order::native);
BOOST_TEST_EQ(x, native);
x = little;
be::conditional_reverse_inplace(x, be::order::little, be::order::big);
be::conditional_reverse_inplace(x, be::order::little, be::order::big);
BOOST_TEST_EQ(x, big);
x = little;
be::conditional_reverse_inplace(x, be::order::little, be::order::native);
be::conditional_reverse_inplace(x, be::order::little, be::order::native);
BOOST_TEST_EQ(x, native);
x = native;
be::conditional_reverse_inplace(x, be::order::native, be::order::big);
be::conditional_reverse_inplace(x, be::order::native, be::order::big);
BOOST_TEST_EQ(x, big);
x = native;
be::conditional_reverse_inplace(x, be::order::native, be::order::little);
be::conditional_reverse_inplace(x, be::order::native, be::order::little);
BOOST_TEST_EQ(x, little);
}
@ -270,8 +278,8 @@ namespace
//--------------------------------------------------------------------------------------//
// User-defined types
// User-defined types
namespace user
{
// UDT1 supplies both endian_reverse and endian_reverse_inplace
@ -331,7 +339,7 @@ namespace
int cpp_main(int, char * [])
{
cout << "byte swap intrinsics: " BOOST_ENDIAN_INTRINSIC_MSG << endl;
//std::cerr << std::hex;
cout << "int8_t" << endl;

View File

@ -15,6 +15,7 @@
#include <boost/endian/endian.hpp>
#include <boost/detail/lightweight_main.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/cstdint.hpp>
#include <iostream>
#include <sstream>
@ -115,7 +116,7 @@ namespace
ubig64_t bu64(0x010203040506070ULL);
ulittle64_t lu64(0x010203040506070ULL);
uint64_t x;
boost::uint64_t x;
std::stringstream ss;

View File

@ -0,0 +1,164 @@
// Copyright 2019 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/endian/arithmetic.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/config.hpp>
#include <boost/cstdint.hpp>
#include <cstddef>
template<BOOST_SCOPED_ENUM(boost::endian::order) Order, BOOST_SCOPED_ENUM(boost::endian::align) Align, class T> void test_arithmetic_( T const& x )
{
boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y( x );
BOOST_TEST_EQ( +x, +y );
BOOST_TEST_EQ( x + x, y + y );
BOOST_TEST_EQ( x - x, y - y );
BOOST_TEST_EQ( x * x, y * y );
BOOST_TEST_EQ( x / x, y / y );
{
T x2( x );
boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
BOOST_TEST_EQ( x2 += x, y2 += y );
}
{
T x2( x );
boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
BOOST_TEST_EQ( x2 -= x, y2 -= y );
}
{
T x2( x );
boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
BOOST_TEST_EQ( x2 *= x, y2 *= y );
}
{
T x2( x );
boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
BOOST_TEST_EQ( x2 /= x, y2 /= y );
}
{
T x2( x );
boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
BOOST_TEST_EQ( ++x2, ++y2 );
}
{
T x2( x );
boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
BOOST_TEST_EQ( --x2, --y2 );
}
{
T x2( x );
boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
BOOST_TEST_EQ( x2++, y2++ );
}
{
T x2( x );
boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
BOOST_TEST_EQ( x2--, y2-- );
}
}
template<BOOST_SCOPED_ENUM(boost::endian::order) Order, BOOST_SCOPED_ENUM(boost::endian::align) Align, class T> void test_integral_( T const& x )
{
boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y( x );
BOOST_TEST_EQ( x % x, y % y );
BOOST_TEST_EQ( x & x, y & y );
BOOST_TEST_EQ( x | x, y | y );
BOOST_TEST_EQ( x ^ x, y ^ y );
BOOST_TEST_EQ( x << 1, y << 1 );
BOOST_TEST_EQ( x >> 1, y >> 1 );
{
T x2( x );
boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
BOOST_TEST_EQ( x2 %= x, y2 %= y );
}
{
T x2( x );
boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
BOOST_TEST_EQ( x2 &= x, y2 &= y );
}
{
T x2( x );
boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
BOOST_TEST_EQ( x2 |= x, y2 |= y );
}
{
T x2( x );
boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
BOOST_TEST_EQ( x2 ^= x, y2 ^= y );
}
{
T x2( x );
boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
BOOST_TEST_EQ( x2 <<= 1, y2 <<= 1 );
}
{
T x2( x );
boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
BOOST_TEST_EQ( x2 >>= 1, y2 >>= 1 );
}
}
template<class T> void test_arithmetic( T const& x )
{
test_arithmetic_<boost::endian::order::little, boost::endian::align::no>( x );
test_arithmetic_<boost::endian::order::little, boost::endian::align::yes>( x );
test_arithmetic_<boost::endian::order::big, boost::endian::align::no>( x );
test_arithmetic_<boost::endian::order::big, boost::endian::align::yes>( x );
}
template<class T> void test_integral( T const& x )
{
test_arithmetic( x );
test_integral_<boost::endian::order::little, boost::endian::align::no>( x );
test_integral_<boost::endian::order::little, boost::endian::align::yes>( x );
test_integral_<boost::endian::order::big, boost::endian::align::no>( x );
test_integral_<boost::endian::order::big, boost::endian::align::yes>( x );
}
int main()
{
test_integral( 0x7EF2 );
test_integral( 0x01020304u );
test_arithmetic( 3.1416f );
test_arithmetic( 3.14159 );
return boost::report_errors();
}

View File

@ -14,8 +14,6 @@
#include <boost/endian/detail/disable_warnings.hpp>
#include <boost/endian/arithmetic.hpp>
#include <boost/detail/lightweight_main.hpp>
#include <cassert>
using namespace boost::endian;
@ -29,7 +27,7 @@ union U
big_int48_t big_48;
big_int56_t big_56;
big_int64_t big_64;
big_uint8_t big_u8;
big_uint16_t big_u16;
big_uint24_t big_u24;
@ -38,7 +36,7 @@ union U
big_uint48_t big_u48;
big_uint56_t big_u56;
big_uint64_t big_u64;
little_int8_t little_8;
little_int16_t little_16;
little_int24_t little_24;
@ -47,7 +45,7 @@ union U
little_int48_t little_48;
little_int56_t little_56;
little_int64_t little_64;
little_uint8_t little_u8;
little_uint16_t little_u16;
little_uint24_t little_u24;
@ -56,7 +54,7 @@ union U
little_uint48_t little_u48;
little_uint56_t little_u56;
little_uint64_t little_u64;
native_int8_t native_8;
native_int16_t native_16;
native_int24_t native_24;
@ -65,7 +63,7 @@ union U
native_int48_t native_48;
native_int56_t native_56;
native_int64_t native_64;
native_uint8_t native_u8;
native_uint16_t native_u16;
native_uint24_t native_u24;
@ -78,9 +76,6 @@ union U
U foo;
int cpp_main(int, char * [])
int main()
{
return 0;
}

View File

@ -0,0 +1,45 @@
// Copyright 2019 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/endian/conversion.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/config.hpp>
#include <boost/cstdint.hpp>
#include <cstddef>
template<class T> void test( T const& x )
{
{
unsigned char buffer[ sizeof(T) ];
boost::endian::endian_store<T, sizeof(T), boost::endian::order::little>( buffer, x );
T x2 = boost::endian::endian_load<T, sizeof(T), boost::endian::order::little>( buffer );
BOOST_TEST_EQ( x, x2 );
}
{
unsigned char buffer[ sizeof(T) ];
boost::endian::endian_store<T, sizeof(T), boost::endian::order::big>( buffer, x );
T x2 = boost::endian::endian_load<T, sizeof(T), boost::endian::order::big>( buffer );
BOOST_TEST_EQ( x, x2 );
}
}
enum E
{
e = 0xF1F2F3
};
int main()
{
test( 1.2e+34f );
test( -1.234e+56 );
test( e );
return boost::report_errors();
}

263
test/endian_load_test.cpp Normal file
View File

@ -0,0 +1,263 @@
// Copyright 2019 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/endian/conversion.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/config.hpp>
#include <boost/cstdint.hpp>
#include <cstddef>
int main()
{
{
unsigned char v[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
// 1 -> 1
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int8_t, 1, boost::endian::order::little>( v )), 0x01 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint8_t, 1, boost::endian::order::little>( v )), 0x01 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int8_t, 1, boost::endian::order::big>( v )), 0x01 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint8_t, 1, boost::endian::order::big>( v )), 0x01 );
// 1 -> 2
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int16_t, 1, boost::endian::order::little>( v )), 0x01 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint16_t, 1, boost::endian::order::little>( v )), 0x01 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int16_t, 1, boost::endian::order::big>( v )), 0x01 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint16_t, 1, boost::endian::order::big>( v )), 0x01 );
// 2 -> 2
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int16_t, 2, boost::endian::order::little>( v )), 0x0201 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint16_t, 2, boost::endian::order::little>( v )), 0x0201 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int16_t, 2, boost::endian::order::big>( v )), 0x0102 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint16_t, 2, boost::endian::order::big>( v )), 0x0102 );
// 1 -> 4
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int32_t, 1, boost::endian::order::little>( v )), 0x01 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint32_t, 1, boost::endian::order::little>( v )), 0x01 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int32_t, 1, boost::endian::order::big>( v )), 0x01 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint32_t, 1, boost::endian::order::big>( v )), 0x01 );
// 2 -> 4
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int32_t, 2, boost::endian::order::little>( v )), 0x0201 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint32_t, 2, boost::endian::order::little>( v )), 0x0201 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int32_t, 2, boost::endian::order::big>( v )), 0x0102 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint32_t, 2, boost::endian::order::big>( v )), 0x0102 );
// 3 -> 4
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int32_t, 3, boost::endian::order::little>( v )), 0x030201 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint32_t, 3, boost::endian::order::little>( v )), 0x030201 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int32_t, 3, boost::endian::order::big>( v )), 0x010203 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint32_t, 3, boost::endian::order::big>( v )), 0x010203 );
// 4 -> 4
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int32_t, 4, boost::endian::order::little>( v )), 0x04030201 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint32_t, 4, boost::endian::order::little>( v )), 0x04030201 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int32_t, 4, boost::endian::order::big>( v )), 0x01020304 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint32_t, 4, boost::endian::order::big>( v )), 0x01020304 );
// 1 -> 8
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 1, boost::endian::order::little>( v )), 0x01 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 1, boost::endian::order::little>( v )), 0x01 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 1, boost::endian::order::big>( v )), 0x01 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 1, boost::endian::order::big>( v )), 0x01 );
// 2 -> 8
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 2, boost::endian::order::little>( v )), 0x0201 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 2, boost::endian::order::little>( v )), 0x0201 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 2, boost::endian::order::big>( v )), 0x0102 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 2, boost::endian::order::big>( v )), 0x0102 );
// 3 -> 8
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 3, boost::endian::order::little>( v )), 0x030201 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 3, boost::endian::order::little>( v )), 0x030201 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 3, boost::endian::order::big>( v )), 0x010203 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 3, boost::endian::order::big>( v )), 0x010203 );
// 4 -> 8
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 4, boost::endian::order::little>( v )), 0x04030201 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 4, boost::endian::order::little>( v )), 0x04030201 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 4, boost::endian::order::big>( v )), 0x01020304 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 4, boost::endian::order::big>( v )), 0x01020304 );
// 5 -> 8
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 5, boost::endian::order::little>( v )), 0x0504030201 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 5, boost::endian::order::little>( v )), 0x0504030201 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 5, boost::endian::order::big>( v )), 0x0102030405 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 5, boost::endian::order::big>( v )), 0x0102030405 );
// 6 -> 8
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 6, boost::endian::order::little>( v )), 0x060504030201 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 6, boost::endian::order::little>( v )), 0x060504030201 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 6, boost::endian::order::big>( v )), 0x010203040506 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 6, boost::endian::order::big>( v )), 0x010203040506 );
// 7 -> 8
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 7, boost::endian::order::little>( v )), 0x07060504030201 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 7, boost::endian::order::little>( v )), 0x07060504030201 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 7, boost::endian::order::big>( v )), 0x01020304050607 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 7, boost::endian::order::big>( v )), 0x01020304050607 );
// 8 -> 8
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 8, boost::endian::order::little>( v )), 0x0807060504030201 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 8, boost::endian::order::little>( v )), 0x0807060504030201 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 8, boost::endian::order::big>( v )), 0x0102030405060708 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 8, boost::endian::order::big>( v )), 0x0102030405060708 );
}
{
unsigned char v[] = { 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8 };
// 1 -> 1
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int8_t, 1, boost::endian::order::little>( v )), -15 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint8_t, 1, boost::endian::order::little>( v )), 0xF1 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int8_t, 1, boost::endian::order::big>( v )), -15 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint8_t, 1, boost::endian::order::big>( v )), 0xF1 );
// 1 -> 2
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int16_t, 1, boost::endian::order::little>( v )), -15 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint16_t, 1, boost::endian::order::little>( v )), 0xF1 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int16_t, 1, boost::endian::order::big>( v )), -15 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint16_t, 1, boost::endian::order::big>( v )), 0xF1 );
// 2 -> 2
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int16_t, 2, boost::endian::order::little>( v )), -3343 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint16_t, 2, boost::endian::order::little>( v )), 0xF2F1 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int16_t, 2, boost::endian::order::big>( v )), -3598 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint16_t, 2, boost::endian::order::big>( v )), 0xF1F2 );
// 1 -> 4
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int32_t, 1, boost::endian::order::little>( v )), -15 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint32_t, 1, boost::endian::order::little>( v )), 0xF1 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int32_t, 1, boost::endian::order::big>( v )), -15 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint32_t, 1, boost::endian::order::big>( v )), 0xF1 );
// 2 -> 4
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int32_t, 2, boost::endian::order::little>( v )), -3343 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint32_t, 2, boost::endian::order::little>( v )), 0xF2F1 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int32_t, 2, boost::endian::order::big>( v )), -3598 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint32_t, 2, boost::endian::order::big>( v )), 0xF1F2 );
// 3 -> 4
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int32_t, 3, boost::endian::order::little>( v )), -789775 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint32_t, 3, boost::endian::order::little>( v )), 0xF3F2F1 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int32_t, 3, boost::endian::order::big>( v )), -920845 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint32_t, 3, boost::endian::order::big>( v )), 0xF1F2F3 );
// 4 -> 4
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int32_t, 4, boost::endian::order::little>( v )), 0xF4F3F2F1 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint32_t, 4, boost::endian::order::little>( v )), 0xF4F3F2F1 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int32_t, 4, boost::endian::order::big>( v )), 0xF1F2F3F4 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint32_t, 4, boost::endian::order::big>( v )), 0xF1F2F3F4 );
// 1 -> 8
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 1, boost::endian::order::little>( v )), -15 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 1, boost::endian::order::little>( v )), 0xF1 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 1, boost::endian::order::big>( v )), -15 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 1, boost::endian::order::big>( v )), 0xF1 );
// 2 -> 8
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 2, boost::endian::order::little>( v )), -3343 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 2, boost::endian::order::little>( v )), 0xF2F1 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 2, boost::endian::order::big>( v )), -3598 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 2, boost::endian::order::big>( v )), 0xF1F2 );
// 3 -> 8
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 3, boost::endian::order::little>( v )), -789775 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 3, boost::endian::order::little>( v )), 0xF3F2F1 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 3, boost::endian::order::big>( v )), -920845 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 3, boost::endian::order::big>( v )), 0xF1F2F3 );
// 4 -> 8
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 4, boost::endian::order::little>( v )), -185339151 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 4, boost::endian::order::little>( v )), 0xF4F3F2F1 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 4, boost::endian::order::big>( v )), -235736076 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 4, boost::endian::order::big>( v )), 0xF1F2F3F4 );
// 5 -> 8
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 5, boost::endian::order::little>( v )), -43135012111 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 5, boost::endian::order::little>( v )), 0xF5F4F3F2F1 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 5, boost::endian::order::big>( v )), -60348435211 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 5, boost::endian::order::big>( v )), 0xF1F2F3F4F5 );
// 6 -> 8
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 6, boost::endian::order::little>( v )), -9938739662095 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 6, boost::endian::order::little>( v )), 0xF6F5F4F3F2F1 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 6, boost::endian::order::big>( v )), -15449199413770 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 6, boost::endian::order::big>( v )), 0xF1F2F3F4F5F6 );
// 7 -> 8
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 7, boost::endian::order::little>( v )), -2261738553347343 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 7, boost::endian::order::little>( v )), 0xF7F6F5F4F3F2F1 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 7, boost::endian::order::big>( v )), -3954995049924873 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 7, boost::endian::order::big>( v )), 0xF1F2F3F4F5F6F7 );
// 8 -> 8
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 8, boost::endian::order::little>( v )), 0xF8F7F6F5F4F3F2F1 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 8, boost::endian::order::little>( v )), 0xF8F7F6F5F4F3F2F1 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::int64_t, 8, boost::endian::order::big>( v )), 0xF1F2F3F4F5F6F7F8 );
BOOST_TEST_EQ( (boost::endian::endian_load<boost::uint64_t, 8, boost::endian::order::big>( v )), 0xF1F2F3F4F5F6F7F8 );
}
return boost::report_errors();
}

View File

@ -14,8 +14,6 @@
// See endian_test for tests of endianness correctness, size, and value.
#define BOOST_ENDIAN_LOG
#include <boost/endian/detail/disable_warnings.hpp>
#ifdef _MSC_VER
@ -28,8 +26,6 @@
# pragma GCC diagnostic ignored "-Wconversion"
#endif
#define BOOST_ENDIAN_LOG
#include <boost/endian/arithmetic.hpp>
#include <boost/type_traits/is_signed.hpp>
#include <boost/core/lightweight_test.hpp>
@ -372,8 +368,6 @@ void f_big_int32_ut(be::big_int32_t) {}
int cpp_main(int, char * [])
{
be::endian_log = false;
// make sure some simple things work
be::big_int32_t o1(1);
@ -381,11 +375,6 @@ int cpp_main(int, char * [])
be::big_int32_t o3(3LL);
be::big_int64_t o4(1);
// use cases; if BOOST_ENDIAN_LOG is defined, will output to clog info on
// what overloads and conversions are actually being performed.
be::endian_log = true;
std::clog << "set up test values\n";
be::big_int32_t big(12345);
be::little_uint16_t little_u(10);
@ -486,10 +475,8 @@ int cpp_main(int, char * [])
u2 = u1 + u4;
std::clog << "\n";
be::endian_log = false;
test_inserter_and_extractor();
// perform the indicated test on ~60*60 operand types
op_test<default_construct>();

View File

@ -0,0 +1,40 @@
// Copyright 2019 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/endian/conversion.hpp>
#include <boost/config/pragma_message.hpp>
#include <boost/config.hpp>
#include <boost/cstdint.hpp>
#if defined(BOOST_NO_CXX11_CONSTEXPR)
BOOST_PRAGMA_MESSAGE("Test skipped because BOOST_NO_CXX11_CONSTEXPR is defined")
#elif defined(BOOST_ENDIAN_NO_INTRINSICS) && defined(BOOST_NO_CXX14_CONSTEXPR)
BOOST_PRAGMA_MESSAGE("Test skipped because BOOST_ENDIAN_NO_INTRINSICS and BOOST_NO_CXX14_CONSTEXPR are defined")
#elif !defined(BOOST_ENDIAN_NO_INTRINSICS) && !defined(BOOST_ENDIAN_CONSTEXPR_INTRINSICS)
BOOST_PRAGMA_MESSAGE("Test skipped because BOOST_ENDIAN_NO_INTRINSICS and BOOST_ENDIAN_CONSTEXPR_INTRINSICS are not defined")
#else
using namespace boost::endian;
#define STATIC_ASSERT(expr) static_assert(expr, #expr)
STATIC_ASSERT( endian_reverse( static_cast<boost::uint8_t>( 0x01 ) ) == 0x01 );
STATIC_ASSERT( endian_reverse( static_cast<boost::uint16_t>( 0x0102 ) ) == 0x0201 );
STATIC_ASSERT( endian_reverse( static_cast<boost::uint32_t>( 0x01020304 ) ) == 0x04030201 );
STATIC_ASSERT( endian_reverse( static_cast<boost::uint64_t>( 0x0102030405060708 ) ) == 0x0807060504030201 );
STATIC_ASSERT( big_to_native( native_to_big( 0x01020304 ) ) == 0x01020304 );
STATIC_ASSERT( little_to_native( native_to_little( 0x01020304 ) ) == 0x01020304 );
STATIC_ASSERT( native_to_big( 0x01020304 ) == (conditional_reverse<order::native, order::big>( 0x01020304 )) );
STATIC_ASSERT( native_to_big( 0x01020304 ) == conditional_reverse( 0x01020304, order::native, order::big ) );
#endif

View File

@ -0,0 +1,156 @@
// Copyright 2019 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#if defined(_MSC_VER)
# pragma warning( disable: 4309 ) // static_cast: truncation of constant value
#endif
#include <boost/endian/conversion.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/config.hpp>
#include <cstddef>
template<class T, std::size_t N = sizeof(T)> struct test_value
{
};
template<class T> struct test_value<T, 1>
{
static const T v1 = static_cast<T>( 0x1F );
static const T w1 = static_cast<T>( 0x1F );
static const T v2 = static_cast<T>( 0xF1 );
static const T w2 = static_cast<T>( 0xF1 );
};
template<class T> T const test_value<T, 1>::v1;
template<class T> T const test_value<T, 1>::w1;
template<class T> T const test_value<T, 1>::v2;
template<class T> T const test_value<T, 1>::w2;
template<class T> struct test_value<T, 2>
{
static const T v1 = static_cast<T>( 0x1F2E );
static const T w1 = static_cast<T>( 0x2E1F );
static const T v2 = static_cast<T>( 0xF1E2 );
static const T w2 = static_cast<T>( 0xE2F1 );
};
template<class T> T const test_value<T, 2>::v1;
template<class T> T const test_value<T, 2>::w1;
template<class T> T const test_value<T, 2>::v2;
template<class T> T const test_value<T, 2>::w2;
template<class T> struct test_value<T, 4>
{
static const T v1 = static_cast<T>( 0x1F2E3D4C );
static const T w1 = static_cast<T>( 0x4C3D2E1F );
static const T v2 = static_cast<T>( 0xF1E2D3C4 );
static const T w2 = static_cast<T>( 0xC4D3E2F1 );
};
template<class T> T const test_value<T, 4>::v1;
template<class T> T const test_value<T, 4>::w1;
template<class T> T const test_value<T, 4>::v2;
template<class T> T const test_value<T, 4>::w2;
template<class T> struct test_value<T, 8>
{
static const T v1 = static_cast<T>( 0x1F2E3D4C5B6A7988ull );
static const T w1 = static_cast<T>( 0x88796A5B4C3D2E1Full );
static const T v2 = static_cast<T>( 0xF1E2D3C4B5A69788ull );
static const T w2 = static_cast<T>( 0x8897A6B5C4D3E2F1ull );
};
template<class T> T const test_value<T, 8>::v1;
template<class T> T const test_value<T, 8>::w1;
template<class T> T const test_value<T, 8>::v2;
template<class T> T const test_value<T, 8>::w2;
template<class T> void test()
{
using boost::endian::endian_reverse;
using boost::endian::endian_reverse_inplace;
{
T t1 = test_value<T>::v1;
T t2 = endian_reverse( t1 );
BOOST_TEST_EQ( t2, test_value<T>::w1 );
T t3 = endian_reverse( t2 );
BOOST_TEST_EQ( t3, t1 );
T t4 = t1;
endian_reverse_inplace( t4 );
BOOST_TEST_EQ( t4, test_value<T>::w1 );
endian_reverse_inplace( t4 );
BOOST_TEST_EQ( t4, t1 );
}
{
T t1 = test_value<T>::v2;
T t2 = endian_reverse( t1 );
BOOST_TEST_EQ( t2, test_value<T>::w2 );
T t3 = endian_reverse( t2 );
BOOST_TEST_EQ( t3, t1 );
T t4 = t1;
endian_reverse_inplace( t4 );
BOOST_TEST_EQ( t4, test_value<T>::w2 );
endian_reverse_inplace( t4 );
BOOST_TEST_EQ( t4, t1 );
}
}
int main()
{
test<boost::int8_t>();
test<boost::uint8_t>();
test<boost::int16_t>();
test<boost::uint16_t>();
test<boost::int32_t>();
test<boost::uint32_t>();
test<boost::int64_t>();
test<boost::uint64_t>();
test<char>();
test<unsigned char>();
test<signed char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
#if !defined(BOOST_NO_CXX11_CHAR16_T)
test<char16_t>();
#endif
#if !defined(BOOST_NO_CXX11_CHAR32_T)
test<char32_t>();
#endif
return boost::report_errors();
}

310
test/endian_store_test.cpp Normal file
View File

@ -0,0 +1,310 @@
// Copyright 2019 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/endian/conversion.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/config.hpp>
#include <boost/cstdint.hpp>
#include <cstddef>
#include <ostream>
#include <iomanip>
class byte_span
{
private:
unsigned char const * p_;
std::size_t n_;
public:
byte_span( unsigned char const * p, std::size_t n ): p_( p ), n_( n )
{
}
template<std::size_t N> explicit byte_span( unsigned char const (&a)[ N ] ): p_( a ), n_( N )
{
}
bool operator==( byte_span const& r ) const
{
if( n_ != r.n_ ) return false;
for( std::size_t i = 0; i < n_; ++i )
{
if( p_[ i ] != r.p_[ i ] ) return false;
}
return true;
}
friend std::ostream& operator<<( std::ostream& os, byte_span s )
{
if( s.n_ == 0 ) return os;
os << std::hex << std::setfill( '0' ) << std::uppercase;
os << std::setw( 2 ) << +s.p_[ 0 ];
for( std::size_t i = 1; i < s.n_; ++i )
{
os << ':' << std::setw( 2 ) << +s.p_[ i ];
}
os << std::dec << std::setfill( ' ' ) << std::nouppercase;;
return os;
}
};
template<class T> void test_1()
{
{
unsigned char v[] = { 0xAA, 0xAA };
boost::endian::endian_store<T, 1, boost::endian::order::little>( v, 0x01 );
unsigned char w[] = { 0x01, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
}
{
unsigned char v[] = { 0xAA, 0xAA };
boost::endian::endian_store<T, 1, boost::endian::order::big>( v, 0x01 );
unsigned char w[] = { 0x01, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
}
}
template<class T> void test_2()
{
{
unsigned char v[] = { 0xAA, 0xAA, 0xAA };
boost::endian::endian_store<T, 2, boost::endian::order::little>( v, 0x0102 );
unsigned char w[] = { 0x02, 0x01, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
}
{
unsigned char v[] = { 0xAA, 0xAA, 0xAA };
boost::endian::endian_store<T, 2, boost::endian::order::big>( v, 0x0102 );
unsigned char w[] = { 0x01, 0x02, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
}
}
template<class T> void test_3()
{
{
unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA };
boost::endian::endian_store<T, 3, boost::endian::order::little>( v, 0x010203 );
unsigned char w[] = { 0x03, 0x02, 0x01, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
}
{
unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA };
boost::endian::endian_store<T, 3, boost::endian::order::big>( v, 0x010203 );
unsigned char w[] = { 0x01, 0x02, 0x03, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
}
}
template<class T> void test_4()
{
{
unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
boost::endian::endian_store<T, 4, boost::endian::order::little>( v, 0x01020304 );
unsigned char w[] = { 0x04, 0x03, 0x02, 0x01, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
}
{
unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
boost::endian::endian_store<T, 4, boost::endian::order::big>( v, 0x01020304 );
unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
}
}
template<class T> void test_5()
{
{
unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
boost::endian::endian_store<T, 5, boost::endian::order::little>( v, 0x0102030405 );
unsigned char w[] = { 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
}
{
unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
boost::endian::endian_store<T, 5, boost::endian::order::big>( v, 0x0102030405 );
unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
}
}
template<class T> void test_6()
{
{
unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
boost::endian::endian_store<T, 6, boost::endian::order::little>( v, 0x010203040506 );
unsigned char w[] = { 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
}
{
unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
boost::endian::endian_store<T, 6, boost::endian::order::big>( v, 0x010203040506 );
unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
}
}
template<class T> void test_7()
{
{
unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
boost::endian::endian_store<T, 7, boost::endian::order::little>( v, 0x01020304050607 );
unsigned char w[] = { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
}
{
unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
boost::endian::endian_store<T, 7, boost::endian::order::big>( v, 0x01020304050607 );
unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
}
}
template<class T> void test_8()
{
{
unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
boost::endian::endian_store<T, 8, boost::endian::order::little>( v, 0x0102030405060708 );
unsigned char w[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
}
{
unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
boost::endian::endian_store<T, 8, boost::endian::order::big>( v, 0x0102030405060708 );
unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
}
}
int main()
{
// 1
test_1<boost::int8_t>();
test_1<boost::uint8_t>();
test_1<boost::int16_t>();
test_1<boost::uint16_t>();
test_1<boost::int32_t>();
test_1<boost::uint32_t>();
test_1<boost::int64_t>();
test_1<boost::uint64_t>();
// 2
test_2<boost::int16_t>();
test_2<boost::uint16_t>();
test_2<boost::int32_t>();
test_2<boost::uint32_t>();
test_2<boost::int64_t>();
test_2<boost::uint64_t>();
// 3
test_3<boost::int32_t>();
test_3<boost::uint32_t>();
test_3<boost::int64_t>();
test_3<boost::uint64_t>();
// 4
test_4<boost::int32_t>();
test_4<boost::uint32_t>();
test_4<boost::int64_t>();
test_4<boost::uint64_t>();
// 5
test_5<boost::int64_t>();
test_5<boost::uint64_t>();
// 6
test_6<boost::int64_t>();
test_6<boost::uint64_t>();
// 7
test_7<boost::int64_t>();
test_7<boost::uint64_t>();
// 8
test_8<boost::int64_t>();
test_8<boost::uint64_t>();
return boost::report_errors();
}

View File

@ -59,7 +59,7 @@ namespace
cout << " A structure with an expected sizeof() " << expected
<< " had an actual sizeof() " << actual
<< "\n This will cause uses of endian types to fail\n";
}
}
template <class Endian, class Base>
void verify_value_and_ops( const Base & expected, int line )
@ -74,7 +74,9 @@ namespace
++v; // verify integer_cover_operators being applied to this type -
// will fail to compile if no endian<> specialization is present
Endian x(static_cast<typename Endian::value_type>(v+v));
Endian v3( static_cast<Base>( 1 ) );
Endian x(static_cast<typename Endian::value_type>(v2+v3));
if ( x == x ) // silence warning
return;
}
@ -100,7 +102,7 @@ namespace
template <class Endian>
inline void verify_native_representation( int line )
{
# ifdef BOOST_BIG_ENDIAN
# if BOOST_ENDIAN_BIG_BYTE
verify_representation<Endian>( true, line );
# else
verify_representation<Endian>( false, line );
@ -112,35 +114,35 @@ namespace
void detect_order()
{
union View
{
{
long long i;
unsigned char c[8];
};
View v = { 0x0102030405060708LL }; // initialize v.i
if ( memcmp( v.c, "\x8\7\6\5\4\3\2\1", 8) == 0 )
{
cout << "This machine is little-endian.\n";
# ifndef BOOST_LITTLE_ENDIAN
cout << "yet boost/detail/endian.hpp does not define BOOST_LITTLE_ENDIAN.\n"
# if !BOOST_ENDIAN_LITTLE_BYTE
cout << "yet boost/predef/other/endian.h does not define BOOST_ENDIAN_LITTLE_BYTE.\n"
"The Boost Endian library must be revised to work correctly on this system.\n"
"Please report this problem to the Boost mailing list.\n";
exit(1);
# endif
}
}
else if ( memcmp( v.c, "\1\2\3\4\5\6\7\x8", 8) == 0 )
{
cout << "This machine is big-endian.\n";
# ifndef BOOST_BIG_ENDIAN
cout << "yet boost/detail/endian.hpp does not define BOOST_BIG_ENDIAN.\n"
# if !BOOST_ENDIAN_BIG_BYTE
cout << "yet boost/predef/other/endian.h does not define BOOST_ENDIAN_BIG_BYTE.\n"
"The Boost Endian library must be revised to work correctly on this system.\n"
"Please report this problem to the Boost mailing list.\n";
exit(1);
# endif
}
else
{
{
cout << "This machine is neither strict big-endian nor strict little-endian\n"
"The Boost Endian library must be revised to work correctly on this system.\n"
"Please report this problem to the Boost mailing list.\n";
@ -292,7 +294,7 @@ namespace
VERIFY(little_align_uint16.data() == reinterpret_cast<const char *>(&little_align_uint16));
VERIFY(little_align_uint32.data() == reinterpret_cast<const char *>(&little_align_uint32));
VERIFY(little_align_uint64.data() == reinterpret_cast<const char *>(&little_align_uint64));
}
// check_size ------------------------------------------------------------//
@ -475,7 +477,7 @@ namespace
};
// aligned test cases
struct big_aligned_struct
{
big_int16_at v0;
@ -484,7 +486,7 @@ namespace
// on a 32-bit system, the padding here may be 3 rather than 7 bytes
big_int64_at v4;
};
struct little_aligned_struct
{
little_int16_at v0;
@ -506,7 +508,7 @@ namespace
VERIFY( sizeof(little_aligned_struct) <= 24 );
if ( saved_err_count == err_count )
{
{
cout <<
"Size and alignment for structures of endian types are as expected.\n";
}
@ -518,35 +520,35 @@ namespace
{
// unaligned integer types
VERIFY_BIG_REPRESENTATION( big_int8_t );
VERIFY_VALUE_AND_OPS( big_int8_t, int_least8_t, 0x7f );
VERIFY_VALUE_AND_OPS( big_int8_t, int_least8_t, 0x7e );
VERIFY_VALUE_AND_OPS( big_int8_t, int_least8_t, -0x80 );
VERIFY_BIG_REPRESENTATION( big_int16_t );
VERIFY_VALUE_AND_OPS( big_int16_t, int_least16_t, 0x7fff );
VERIFY_VALUE_AND_OPS( big_int16_t, int_least16_t, 0x7ffe );
VERIFY_VALUE_AND_OPS( big_int16_t, int_least16_t, -0x8000 );
VERIFY_BIG_REPRESENTATION( big_int24_t );
VERIFY_VALUE_AND_OPS( big_int24_t, int_least32_t, 0x7fffff );
VERIFY_VALUE_AND_OPS( big_int24_t, int_least32_t, 0x7ffffe );
VERIFY_VALUE_AND_OPS( big_int24_t, int_least32_t, -0x800000 );
VERIFY_BIG_REPRESENTATION( big_int32_t );
VERIFY_VALUE_AND_OPS( big_int32_t, int_least32_t, 0x7fffffff );
VERIFY_VALUE_AND_OPS( big_int32_t, int_least32_t, 0x7ffffffe );
VERIFY_VALUE_AND_OPS( big_int32_t, int_least32_t, -0x7fffffff-1 );
VERIFY_BIG_REPRESENTATION( big_int40_t );
VERIFY_VALUE_AND_OPS( big_int40_t, int_least64_t, 0x7fffffffffLL );
VERIFY_VALUE_AND_OPS( big_int40_t, int_least64_t, 0x7ffffffffeLL );
VERIFY_VALUE_AND_OPS( big_int40_t, int_least64_t, -0x8000000000LL );
VERIFY_BIG_REPRESENTATION( big_int48_t );
VERIFY_VALUE_AND_OPS( big_int48_t, int_least64_t, 0x7fffffffffffLL );
VERIFY_VALUE_AND_OPS( big_int48_t, int_least64_t, 0x7ffffffffffeLL );
VERIFY_VALUE_AND_OPS( big_int48_t, int_least64_t, -0x800000000000LL );
VERIFY_BIG_REPRESENTATION( big_int56_t );
VERIFY_VALUE_AND_OPS( big_int56_t, int_least64_t, 0x7fffffffffffffLL );
VERIFY_VALUE_AND_OPS( big_int56_t, int_least64_t, 0x7ffffffffffffeLL );
VERIFY_VALUE_AND_OPS( big_int56_t, int_least64_t, -0x80000000000000LL );
VERIFY_BIG_REPRESENTATION( big_int64_t );
VERIFY_VALUE_AND_OPS( big_int64_t, int_least64_t, 0x7fffffffffffffffLL );
VERIFY_VALUE_AND_OPS( big_int64_t, int_least64_t, 0x7ffffffffffffffeLL );
VERIFY_VALUE_AND_OPS( big_int64_t, int_least64_t, -0x7fffffffffffffffLL-1 );
VERIFY_BIG_REPRESENTATION( big_uint8_t );
@ -574,35 +576,35 @@ namespace
VERIFY_VALUE_AND_OPS( big_uint64_t, uint_least64_t, 0xffffffffffffffffULL );
VERIFY_LITTLE_REPRESENTATION( little_int8_t );
VERIFY_VALUE_AND_OPS( little_int8_t, int_least8_t, 0x7f );
VERIFY_VALUE_AND_OPS( little_int8_t, int_least8_t, 0x7e );
VERIFY_VALUE_AND_OPS( little_int8_t, int_least8_t, -0x80 );
VERIFY_LITTLE_REPRESENTATION( little_int16_t );
VERIFY_VALUE_AND_OPS( little_int16_t, int_least16_t, 0x7fff );
VERIFY_VALUE_AND_OPS( little_int16_t, int_least16_t, 0x7ffe );
VERIFY_VALUE_AND_OPS( little_int16_t, int_least16_t, -0x8000 );
VERIFY_LITTLE_REPRESENTATION( little_int24_t );
VERIFY_VALUE_AND_OPS( little_int24_t, int_least32_t, 0x7fffff );
VERIFY_VALUE_AND_OPS( little_int24_t, int_least32_t, 0x7ffffe );
VERIFY_VALUE_AND_OPS( little_int24_t, int_least32_t, -0x800000 );
VERIFY_LITTLE_REPRESENTATION( little_int32_t );
VERIFY_VALUE_AND_OPS( little_int32_t, int_least32_t, 0x7fffffff );
VERIFY_VALUE_AND_OPS( little_int32_t, int_least32_t, 0x7ffffffe );
VERIFY_VALUE_AND_OPS( little_int32_t, int_least32_t, -0x7fffffff-1 );
VERIFY_LITTLE_REPRESENTATION( little_int40_t );
VERIFY_VALUE_AND_OPS( little_int40_t, int_least64_t, 0x7fffffffffLL );
VERIFY_VALUE_AND_OPS( little_int40_t, int_least64_t, 0x7ffffffffeLL );
VERIFY_VALUE_AND_OPS( little_int40_t, int_least64_t, -0x8000000000LL );
VERIFY_LITTLE_REPRESENTATION( little_int48_t );
VERIFY_VALUE_AND_OPS( little_int48_t, int_least64_t, 0x7fffffffffffLL );
VERIFY_VALUE_AND_OPS( little_int48_t, int_least64_t, 0x7ffffffffffeLL );
VERIFY_VALUE_AND_OPS( little_int48_t, int_least64_t, -0x800000000000LL );
VERIFY_LITTLE_REPRESENTATION( little_int56_t );
VERIFY_VALUE_AND_OPS( little_int56_t, int_least64_t, 0x7fffffffffffffLL );
VERIFY_VALUE_AND_OPS( little_int56_t, int_least64_t, 0x7ffffffffffffeLL );
VERIFY_VALUE_AND_OPS( little_int56_t, int_least64_t, -0x80000000000000LL );
VERIFY_LITTLE_REPRESENTATION( little_int64_t );
VERIFY_VALUE_AND_OPS( little_int64_t, int_least64_t, 0x7fffffffffffffffLL );
VERIFY_VALUE_AND_OPS( little_int64_t, int_least64_t, 0x7ffffffffffffffeLL );
VERIFY_VALUE_AND_OPS( little_int64_t, int_least64_t, -0x7fffffffffffffffLL-1 );
VERIFY_LITTLE_REPRESENTATION( little_uint8_t );
@ -630,35 +632,35 @@ namespace
VERIFY_VALUE_AND_OPS( little_uint64_t, uint_least64_t, 0xffffffffffffffffULL );
VERIFY_NATIVE_REPRESENTATION( native_int8_t );
VERIFY_VALUE_AND_OPS( native_int8_t, int_least8_t, 0x7f );
VERIFY_VALUE_AND_OPS( native_int8_t, int_least8_t, 0x7e );
VERIFY_VALUE_AND_OPS( native_int8_t, int_least8_t, -0x80 );
VERIFY_NATIVE_REPRESENTATION( native_int16_t );
VERIFY_VALUE_AND_OPS( native_int16_t, int_least16_t, 0x7fff );
VERIFY_VALUE_AND_OPS( native_int16_t, int_least16_t, 0x7ffe );
VERIFY_VALUE_AND_OPS( native_int16_t, int_least16_t, -0x8000 );
VERIFY_NATIVE_REPRESENTATION( native_int24_t );
VERIFY_VALUE_AND_OPS( native_int24_t, int_least32_t, 0x7fffff );
VERIFY_VALUE_AND_OPS( native_int24_t, int_least32_t, 0x7ffffe );
VERIFY_VALUE_AND_OPS( native_int24_t, int_least32_t, -0x800000 );
VERIFY_NATIVE_REPRESENTATION( native_int32_t );
VERIFY_VALUE_AND_OPS( native_int32_t, int_least32_t, 0x7fffffff );
VERIFY_VALUE_AND_OPS( native_int32_t, int_least32_t, 0x7ffffffe );
VERIFY_VALUE_AND_OPS( native_int32_t, int_least32_t, -0x7fffffff-1 );
VERIFY_NATIVE_REPRESENTATION( native_int40_t );
VERIFY_VALUE_AND_OPS( native_int40_t, int_least64_t, 0x7fffffffffLL );
VERIFY_VALUE_AND_OPS( native_int40_t, int_least64_t, 0x7ffffffffeLL );
VERIFY_VALUE_AND_OPS( native_int40_t, int_least64_t, -0x8000000000LL );
VERIFY_NATIVE_REPRESENTATION( native_int48_t );
VERIFY_VALUE_AND_OPS( native_int48_t, int_least64_t, 0x7fffffffffffLL );
VERIFY_VALUE_AND_OPS( native_int48_t, int_least64_t, 0x7ffffffffffeLL );
VERIFY_VALUE_AND_OPS( native_int48_t, int_least64_t, -0x800000000000LL );
VERIFY_NATIVE_REPRESENTATION( native_int56_t );
VERIFY_VALUE_AND_OPS( native_int56_t, int_least64_t, 0x7fffffffffffffLL );
VERIFY_VALUE_AND_OPS( native_int56_t, int_least64_t, 0x7ffffffffffffeLL );
VERIFY_VALUE_AND_OPS( native_int56_t, int_least64_t, -0x80000000000000LL );
VERIFY_NATIVE_REPRESENTATION( native_int64_t );
VERIFY_VALUE_AND_OPS( native_int64_t, int_least64_t, 0x7fffffffffffffffLL );
VERIFY_VALUE_AND_OPS( native_int64_t, int_least64_t, 0x7ffffffffffffffeLL );
VERIFY_VALUE_AND_OPS( native_int64_t, int_least64_t, -0x7fffffffffffffffLL-1 );
VERIFY_NATIVE_REPRESENTATION( native_uint8_t );
@ -687,15 +689,15 @@ namespace
// aligned integer types
VERIFY_BIG_REPRESENTATION( big_int16_at );
VERIFY_VALUE_AND_OPS( big_int16_at, int_least16_t, 0x7fff );
VERIFY_VALUE_AND_OPS( big_int16_at, int_least16_t, 0x7ffe );
VERIFY_VALUE_AND_OPS( big_int16_at, int_least16_t, -0x8000 );
VERIFY_BIG_REPRESENTATION( big_int32_at );
VERIFY_VALUE_AND_OPS( big_int32_at, int_least32_t, 0x7fffffff );
VERIFY_VALUE_AND_OPS( big_int32_at, int_least32_t, 0x7ffffffe );
VERIFY_VALUE_AND_OPS( big_int32_at, int_least32_t, -0x7fffffff-1 );
VERIFY_BIG_REPRESENTATION( big_int64_at );
VERIFY_VALUE_AND_OPS( big_int64_at, int_least64_t, 0x7fffffffffffffffLL );
VERIFY_VALUE_AND_OPS( big_int64_at, int_least64_t, 0x7ffffffffffffffeLL );
VERIFY_VALUE_AND_OPS( big_int64_at, int_least64_t, -0x7fffffffffffffffLL-1 );
VERIFY_BIG_REPRESENTATION( big_uint16_at );
@ -708,15 +710,15 @@ namespace
VERIFY_VALUE_AND_OPS( big_uint64_at, uint_least64_t, 0xffffffffffffffffULL );
VERIFY_LITTLE_REPRESENTATION( little_int16_at );
VERIFY_VALUE_AND_OPS( little_int16_at, int_least16_t, 0x7fff );
VERIFY_VALUE_AND_OPS( little_int16_at, int_least16_t, 0x7ffe );
VERIFY_VALUE_AND_OPS( little_int16_at, int_least16_t, -0x8000 );
VERIFY_LITTLE_REPRESENTATION( little_int32_at );
VERIFY_VALUE_AND_OPS( little_int32_at, int_least32_t, 0x7fffffff );
VERIFY_VALUE_AND_OPS( little_int32_at, int_least32_t, 0x7ffffffe );
VERIFY_VALUE_AND_OPS( little_int32_at, int_least32_t, -0x7fffffff-1 );
VERIFY_LITTLE_REPRESENTATION( little_int64_at );
VERIFY_VALUE_AND_OPS( little_int64_at, int_least64_t, 0x7fffffffffffffffLL );
VERIFY_VALUE_AND_OPS( little_int64_at, int_least64_t, 0x7ffffffffffffffeLL );
VERIFY_VALUE_AND_OPS( little_int64_at, int_least64_t, -0x7fffffffffffffffLL-1 );
VERIFY_LITTLE_REPRESENTATION( little_uint16_at );
@ -730,11 +732,13 @@ namespace
} // check_representation_and_range
/*
class MyInt
{
int32_t mx;
public:
MyInt(int32_t x) : mx(x) {}
MyInt(int32_t x = 0) : mx(x) {}
operator int32_t() const {return mx;}
//friend int32_t operator+(const MyInt& x) {return x;}
@ -759,8 +763,29 @@ namespace
// cout << "v+v is " << +(v+v) << endl;
}
void check_udt_le()
{
typedef boost::endian::endian_arithmetic< order::little, MyInt, 32 > mylittle_int32_ut;
mylittle_int32_ut v(10);
cout << "+v is " << +v << endl;
v += 1;
cout << "v is " << +v << endl;
v -= 2;
cout << "v is " << +v << endl;
v *= 2;
cout << "v is " << +v << endl;
++v;
cout << "v is " << +v << endl;
--v;
cout << "v is " << +v << endl;
// cout << "v+v is " << +(v+v) << endl;
}
*/
long iterations = 10000;
template< class Endian >
Endian timing_test( const char * s)
{
@ -798,7 +823,8 @@ int cpp_main( int argc, char * argv[] )
check_alignment();
check_representation_and_range_and_ops();
check_data();
check_udt();
//check_udt();
//check_udt_le();
//timing_test<big_int32_t> ( "big_int32_t" );
//timing_test<big_int32_at>( "big_int32_at" );

View File

@ -0,0 +1,77 @@
// Copyright 2019 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/endian/arithmetic.hpp>
#include <boost/endian/buffers.hpp>
#include <boost/core/lightweight_test.hpp>
template<class T> struct align
{
char _;
T v;
explicit align( typename T::value_type y ): _(), v( y )
{
}
};
template<class T, class U> void test_buffer( U const & y, bool aligned )
{
align<T> x( y );
BOOST_TEST_EQ( sizeof(x), aligned? 2 * sizeof(U): 1 + sizeof(U) );
BOOST_TEST_EQ( x.v.value(), y );
}
template<class T, class U> void test_arithmetic( U const & y, bool aligned )
{
test_buffer<T>( y, aligned );
align<T> x( y );
BOOST_TEST_EQ( x.v + 7, y + 7 );
}
int main()
{
using namespace boost::endian;
// buffers
test_buffer<big_float32_buf_t>( 3.1416f, false );
test_buffer<big_float64_buf_t>( 3.14159, false );
test_buffer<little_float32_buf_t>( 3.1416f, false );
test_buffer<little_float64_buf_t>( 3.14159, false );
test_buffer<native_float32_buf_t>( 3.1416f, false );
test_buffer<native_float64_buf_t>( 3.14159, false );
test_buffer<big_float32_buf_at>( 3.1416f, true );
test_buffer<big_float64_buf_at>( 3.14159, true );
test_buffer<little_float32_buf_at>( 3.1416f, true );
test_buffer<little_float64_buf_at>( 3.14159, true );
// arithmetic
test_arithmetic<big_float32_t>( 3.1416f, false );
test_arithmetic<big_float64_t>( 3.14159, false );
test_arithmetic<little_float32_t>( 3.1416f, false );
test_arithmetic<little_float64_t>( 3.14159, false );
test_arithmetic<native_float32_t>( 3.1416f, false );
test_arithmetic<native_float64_t>( 3.14159, false );
test_arithmetic<big_float32_at>( 3.1416f, true );
test_arithmetic<big_float64_at>( 3.14159, true );
test_arithmetic<little_float32_at>( 3.1416f, true );
test_arithmetic<little_float64_at>( 3.14159, true );
return boost::report_errors();
}

View File

@ -1,27 +1,35 @@
// Copyright Beman Dawes 2013
// Copyright 2018 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#include "test.hpp"
#include <iostream>
#include <boost/assert.hpp>
#include <boost/endian/detail/intrinsic.hpp>
#include <boost/core/lightweight_test.hpp>
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned long long uint64;
int main()
{
std::cout << "BOOST_ENDIAN_INTRINSIC_MSG: " BOOST_ENDIAN_INTRINSIC_MSG << std::endl;
std::cout << "BOOST_ENDIAN_INTRINSIC_MSG: " BOOST_ENDIAN_INTRINSIC_MSG << std::endl;
#ifndef BOOST_ENDIAN_NO_INTRINSICS
uint16_t x2 = 0x1122U;
BOOST_ASSERT(BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x2) == 0x2211U);
uint32_t x4 = 0x11223344UL;
BOOST_ASSERT(BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x4) == 0x44332211UL);
uint64_t x8 = 0x1122334455667788U;
BOOST_ASSERT(BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x8) == 0x8877665544332211ULL);
uint16 x2 = 0x1122U;
uint16 y2 = BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x2);
BOOST_TEST_EQ( y2, 0x2211U );
uint32 x4 = 0x11223344UL;
uint32 y4 = BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x4);
BOOST_TEST_EQ( y4, 0x44332211UL );
uint64 x8 = 0x1122334455667788U;
uint64 y8 = BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x8);
BOOST_TEST_EQ( y8, 0x8877665544332211ULL );
#endif
return 0;
return boost::report_errors();
}

View File

@ -0,0 +1,75 @@
// Copyright 2019 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/endian/conversion.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/config.hpp>
#include <boost/cstdint.hpp>
#include <cstddef>
int main()
{
using namespace boost::endian;
unsigned char v[] = { 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8 };
// 16
BOOST_TEST_EQ( load_little_s16( v ), -3343 );
BOOST_TEST_EQ( load_little_u16( v ), 0xF2F1 );
BOOST_TEST_EQ( load_big_s16( v ), -3598 );
BOOST_TEST_EQ( load_big_u16( v ), 0xF1F2 );
// 24
BOOST_TEST_EQ( load_little_s24( v ), -789775 );
BOOST_TEST_EQ( load_little_u24( v ), 0xF3F2F1 );
BOOST_TEST_EQ( load_big_s24( v ), -920845 );
BOOST_TEST_EQ( load_big_u24( v ), 0xF1F2F3 );
// 32
BOOST_TEST_EQ( load_little_s32( v ), 0xF4F3F2F1 );
BOOST_TEST_EQ( load_little_u32( v ), 0xF4F3F2F1 );
BOOST_TEST_EQ( load_big_s32( v ), 0xF1F2F3F4 );
BOOST_TEST_EQ( load_big_u32( v ), 0xF1F2F3F4 );
// 40
BOOST_TEST_EQ( load_little_s40( v ), -43135012111 );
BOOST_TEST_EQ( load_little_u40( v ), 0xF5F4F3F2F1 );
BOOST_TEST_EQ( load_big_s40( v ), -60348435211 );
BOOST_TEST_EQ( load_big_u40( v ), 0xF1F2F3F4F5 );
// 48
BOOST_TEST_EQ( load_little_s48( v ), -9938739662095 );
BOOST_TEST_EQ( load_little_u48( v ), 0xF6F5F4F3F2F1 );
BOOST_TEST_EQ( load_big_s48( v ), -15449199413770 );
BOOST_TEST_EQ( load_big_u48( v ), 0xF1F2F3F4F5F6 );
// 56
BOOST_TEST_EQ( load_little_s56( v ), -2261738553347343 );
BOOST_TEST_EQ( load_little_u56( v ), 0xF7F6F5F4F3F2F1 );
BOOST_TEST_EQ( load_big_s56( v ), -3954995049924873 );
BOOST_TEST_EQ( load_big_u56( v ), 0xF1F2F3F4F5F6F7 );
// 64
BOOST_TEST_EQ( load_little_s64( v ), 0xF8F7F6F5F4F3F2F1 );
BOOST_TEST_EQ( load_little_u64( v ), 0xF8F7F6F5F4F3F2F1 );
BOOST_TEST_EQ( load_big_s64( v ), 0xF1F2F3F4F5F6F7F8 );
BOOST_TEST_EQ( load_big_u64( v ), 0xF1F2F3F4F5F6F7F8 );
return boost::report_errors();
}

View File

@ -8,7 +8,6 @@
//--------------------------------------------------------------------------------------//
//#define BOOST_ENDIAN_NO_INTRINSICS
//#define BOOST_ENDIAN_LOG
#include <boost/endian/detail/disable_warnings.hpp>
@ -67,7 +66,7 @@ namespace
n = _atoi64(argv[1]);
#endif
for (; argc > 2; ++argv, --argc)
for (; argc > 2; ++argv, --argc)
{
if ( *(argv[2]+1) == 'p' )
places = atoi( argv[2]+2 );
@ -91,7 +90,7 @@ namespace
}
}
if (argc < 2)
if (argc < 2)
{
cout << "Usage: loop_time_test n [Options]\n"
" The argument n specifies the number of test cases to run\n"
@ -128,7 +127,7 @@ namespace
void time()
{
T total = 0;
{
{
// cout << "*************Endian integer approach...\n";
EndianT x(0);
boost::timer::cpu_timer t;
@ -140,7 +139,7 @@ namespace
total += x;
cout << "<td align=\"right\">" << t.format(places, "%t") << " s</td>";
}
{
{
// cout << "***************Endian conversion approach...\n";
T x(0);
boost::timer::cpu_timer t;
@ -158,7 +157,7 @@ namespace
}
}
void test_big_align_int16()
{
cout << "<tr><td>16-bit aligned big endian</td>";
@ -186,7 +185,7 @@ namespace
time<int16_t, little_int16_t>();
cout << "</tr>\n";
}
void test_big_align_int32()
{
cout << "<tr><td>32-bit aligned big endian</td>";
@ -214,7 +213,7 @@ namespace
time<int32_t, little_int32_t>();
cout << "</tr>\n";
}
void test_big_align_int64()
{
cout << "<tr><td>64-bit aligned big endian</td>";
@ -250,10 +249,10 @@ namespace
int cpp_main(int argc, char* argv[])
{
process_command_line(argc, argv);
cout
<< "<html>\n<head>\n<title>Endian Loop Time Test</title>\n</head>\n<body>\n"
<< "<!-- boost-no-inspect -->\n"
<< "<!-- boost-no-inspect -->\n"
<< "<div align=\"center\"> <center>\n"
<< "<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\""
<< "style=\"border-collapse: collapse\" bordercolor=\"#111111\">\n"
@ -268,7 +267,7 @@ int cpp_main(int argc, char* argv[])
"<td align=\"center\"><b>Endian<br>conversion<br>function</b></td>\n"
"</tr>\n"
;
if (time_aligned)
{
if (time_16)

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
@ -21,31 +21,32 @@
<PropertyGroup Label="Globals">
<ProjectGuid>{D9C80FE0-20A6-4711-A3F4-676019BD5A06}</ProjectGuid>
<RootNamespace>associatedfiles</RootNamespace>
<WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
@ -22,33 +22,34 @@
<ProjectGuid>{C9FEAE75-4DD9-44F5-B302-9910559A91BE}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>benchmark</RootNamespace>
<WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
@ -22,31 +22,32 @@
<ProjectGuid>{BFB68CF4-EB92-4E5C-9694-A939496C5CDE}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>buffer_test</RootNamespace>
<WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
@ -22,31 +22,32 @@
<ProjectGuid>{1139E765-DE0F-497A-A7D9-EB2683521DF1}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>conversion_use_case</RootNamespace>
<WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
@ -25,33 +25,34 @@
<ProjectGuid>{EAE18F4D-AAF2-4C19-86FB-1144B5BD5993}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>converter_test</RootNamespace>
<WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
@ -14,18 +14,19 @@
<ProjectGuid>{DA4BC67F-9284-4D2C-81D5-407312C31BD7}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>deprecated_test</RootNamespace>
<WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
@ -22,29 +22,30 @@
<ProjectGuid>{8638A3D8-D121-40BF-82E5-127F1B1B2CB2}</ProjectGuid>
<RootNamespace>endian_example</RootNamespace>
<Keyword>Win32Proj</Keyword>
<WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
@ -22,29 +22,30 @@
<ProjectGuid>{3926C6DC-9D1E-4227-BEF5-81F5EC621A75}</ProjectGuid>
<RootNamespace>endian_in_union_test</RootNamespace>
<Keyword>Win32Proj</Keyword>
<WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
@ -22,29 +22,30 @@
<ProjectGuid>{A0060A5B-673C-4AD8-BD08-A5C643B1A1CB}</ProjectGuid>
<RootNamespace>endian_operations_test</RootNamespace>
<Keyword>Win32Proj</Keyword>
<WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
@ -22,29 +22,30 @@
<ProjectGuid>{74C201F3-8308-40BE-BC0F-24974DEAF405}</ProjectGuid>
<RootNamespace>endian_test</RootNamespace>
<Keyword>Win32Proj</Keyword>
<WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
@ -22,31 +22,32 @@
<ProjectGuid>{541A2D06-B34E-4592-BE47-F87DF47E73D8}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>loop_time_test</RootNamespace>
<WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
@ -22,29 +22,30 @@
<ProjectGuid>{8420E151-B23B-4651-B526-6AB11EF1E278}</ProjectGuid>
<RootNamespace>scoped_enum_emulation_test</RootNamespace>
<Keyword>Win32Proj</Keyword>
<WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
@ -22,31 +22,32 @@
<ProjectGuid>{5407AF29-59E9-4DE2-9939-F067576F7868}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>speed_test</RootNamespace>
<WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
@ -22,31 +22,32 @@
<ProjectGuid>{27A53564-D32B-4A32-8A6E-2F3BD252EEBA}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>udt_conversion_example</RootNamespace>
<WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>

31
test/quick.cpp Normal file
View File

@ -0,0 +1,31 @@
// Copyright 2019 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/endian/arithmetic.hpp>
#include <boost/core/lightweight_test.hpp>
int main()
{
using namespace boost::endian;
{
little_uint32_t v( 0x01020304 );
BOOST_TEST_EQ( v.data()[ 0 ], 0x04 );
BOOST_TEST_EQ( v.data()[ 1 ], 0x03 );
BOOST_TEST_EQ( v.data()[ 2 ], 0x02 );
BOOST_TEST_EQ( v.data()[ 3 ], 0x01 );
}
{
big_uint32_t v( 0x01020304 );
BOOST_TEST_EQ( v.data()[ 0 ], 0x01 );
BOOST_TEST_EQ( v.data()[ 1 ], 0x02 );
BOOST_TEST_EQ( v.data()[ 2 ], 0x03 );
BOOST_TEST_EQ( v.data()[ 3 ], 0x04 );
}
return boost::report_errors();
}

View File

@ -8,7 +8,6 @@
//--------------------------------------------------------------------------------------//
//#define BOOST_ENDIAN_NO_INTRINSICS
//#define BOOST_ENDIAN_LOG
#include <boost/endian/detail/disable_warnings.hpp>
@ -53,7 +52,7 @@ namespace
n = _atoi64(argv[1]);
#endif
for (; argc > 2; ++argv, --argc)
for (; argc > 2; ++argv, --argc)
{
if ( *(argv[2]+1) == 'p' )
places = atoi( argv[2]+2 );
@ -67,7 +66,7 @@ namespace
}
}
if (argc < 2)
if (argc < 2)
{
cout << "Usage: speed_test n [Options]\n"
" The argument n specifies the number of test cases to run\n"
@ -85,7 +84,7 @@ namespace
{
T x(0);
EndianT y(0);
boost::timer::cpu_timer t;
boost::timer::cpu_timer t;
for (uint64_t i = 0; i < n; ++i)
{
f(x, y);
@ -161,7 +160,7 @@ namespace
int cpp_main(int argc, char* argv[])
{
process_command_line(argc, argv);
cout
<< "<html>\n<head>\n<title>Endian Speed Test</title>\n</head>\n<body>\n"
<< "<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\""

View File

@ -1,5 +1,5 @@
// speed_test_functions.cpp ----------------------------------------------------------//
// Copyright Beman Dawes 2013
// Distributed under the Boost Software License, Version 1.0.

View File

@ -0,0 +1,23 @@
// Copyright 2019 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#if defined(_MSC_VER)
# pragma warning( disable: 4510 ) // default constructor not generated
# pragma warning( disable: 4512 ) // assignment operator not generated
# pragma warning( disable: 4610 ) // class can never be instantiated
#endif
#include <boost/spirit/include/qi.hpp>
#include <boost/endian/arithmetic.hpp>
struct record
{
boost::endian::big_int16_t type;
record( boost::int16_t t )
{
type = t;
}
};

View File

@ -0,0 +1,277 @@
// Copyright 2019 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/endian/conversion.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/config.hpp>
#include <boost/cstdint.hpp>
#include <cstddef>
#include <ostream>
#include <iomanip>
class byte_span
{
private:
unsigned char const * p_;
std::size_t n_;
public:
byte_span( unsigned char const * p, std::size_t n ): p_( p ), n_( n )
{
}
template<std::size_t N> explicit byte_span( unsigned char const (&a)[ N ] ): p_( a ), n_( N )
{
}
bool operator==( byte_span const& r ) const
{
if( n_ != r.n_ ) return false;
for( std::size_t i = 0; i < n_; ++i )
{
if( p_[ i ] != r.p_[ i ] ) return false;
}
return true;
}
friend std::ostream& operator<<( std::ostream& os, byte_span s )
{
if( s.n_ == 0 ) return os;
os << std::hex << std::setfill( '0' ) << std::uppercase;
os << std::setw( 2 ) << +s.p_[ 0 ];
for( std::size_t i = 1; i < s.n_; ++i )
{
os << ':' << std::setw( 2 ) << +s.p_[ i ];
}
os << std::dec << std::setfill( ' ' ) << std::nouppercase;;
return os;
}
};
int main()
{
using namespace boost::endian;
// 16
{
unsigned char v[] = { 0xAA, 0xAA, 0xAA };
store_little_s16( v, -3343 );
unsigned char w1[] = { 0xF1, 0xF2, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w1 ) );
store_little_u16( v, 0x0201 );
unsigned char w2[] = { 0x01, 0x02, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w2 ) );
store_big_s16( v, -3343 );
unsigned char w3[] = { 0xF2, 0xF1, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w3 ) );
store_big_u16( v, 0x0201 );
unsigned char w4[] = { 0x02, 0x01, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w4 ) );
}
// 24
{
unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA };
store_little_s24( v, -789775 );
unsigned char w1[] = { 0xF1, 0xF2, 0xF3, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w1 ) );
store_little_u24( v, 0x030201 );
unsigned char w2[] = { 0x01, 0x02, 0x03, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w2 ) );
store_big_s24( v, -789775 );
unsigned char w3[] = { 0xF3, 0xF2, 0xF1, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w3 ) );
store_big_u24( v, 0x030201 );
unsigned char w4[] = { 0x03, 0x02, 0x01, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w4 ) );
}
// 32
{
unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
store_little_s32( v, 0xF4F3F2F1 );
unsigned char w1[] = { 0xF1, 0xF2, 0xF3, 0xF4, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w1 ) );
store_little_u32( v, 0x04030201 );
unsigned char w2[] = { 0x01, 0x02, 0x03, 0x04, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w2 ) );
store_big_s32( v, 0xF4F3F2F1 );
unsigned char w3[] = { 0xF4, 0xF3, 0xF2, 0xF1, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w3 ) );
store_big_u32( v, 0x04030201 );
unsigned char w4[] = { 0x04, 0x03, 0x02, 0x01, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w4 ) );
}
// 40
{
unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
store_little_s40( v, -43135012111 );
unsigned char w1[] = { 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w1 ) );
store_little_u40( v, 0x0504030201 );
unsigned char w2[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w2 ) );
store_big_s40( v, -43135012111 );
unsigned char w3[] = { 0xF5, 0xF4, 0xF3, 0xF2, 0xF1, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w3 ) );
store_big_u40( v, 0x0504030201 );
unsigned char w4[] = { 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w4 ) );
}
// 48
{
unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
store_little_s48( v, -9938739662095 );
unsigned char w1[] = { 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w1 ) );
store_little_u48( v, 0x060504030201 );
unsigned char w2[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w2 ) );
store_big_s48( v, -9938739662095 );
unsigned char w3[] = { 0xF6, 0xF5, 0xF4, 0xF3, 0xF2, 0xF1, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w3 ) );
store_big_u48( v, 0x060504030201 );
unsigned char w4[] = { 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w4 ) );
}
// 56
{
unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
store_little_s56( v, -2261738553347343 );
unsigned char w1[] = { 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w1 ) );
store_little_u56( v, 0x07060504030201 );
unsigned char w2[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w2 ) );
store_big_s56( v, -2261738553347343 );
unsigned char w3[] = { 0xF7, 0xF6, 0xF5, 0xF4, 0xF3, 0xF2, 0xF1, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w3 ) );
store_big_u56( v, 0x07060504030201 );
unsigned char w4[] = { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w4 ) );
}
// 64
{
unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
store_little_s64( v, 0xF8F7F6F5F4F3F2F1 );
unsigned char w1[] = { 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w1 ) );
store_little_u64( v, 0x0807060504030201 );
unsigned char w2[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w2 ) );
store_big_s64( v, 0xF8F7F6F5F4F3F2F1 );
unsigned char w3[] = { 0xF8, 0xF7, 0xF6, 0xF5, 0xF4, 0xF3, 0xF2, 0xF1, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w3 ) );
store_big_u64( v, 0x0807060504030201 );
unsigned char w4[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA };
BOOST_TEST_EQ( byte_span( v ), byte_span( w4 ) );
}
return boost::report_errors();
}