diff --git a/doc/conversion.html b/doc/conversion.html index b243d02..4f83d2a 100644 --- a/doc/conversion.html +++ b/doc/conversion.html @@ -37,9 +37,12 @@
Please: If you haven't done so already, read + Introduction to endianness! | +
Header boost/endian/conversion.hpp
-provides byte reverse and conversion functions that convert built-in
-integers, float
, and double
between native byte ordering and big or little endian byte
+provides byte order reversal and conversion functions that convert objects of
+the multi-byte built-in
+integer types, and also types float
and double,
+between native, big, or little endian byte
ordering. User defined types are also supported.
Caution: Only big and little endianness are supported; + | Caution: Only big endianness and little endianness is supported; middle endianness is not supported. |
Is the implementation header only?
+ ++ ++ +Yes.
+ +
Does the implementation use compiler intrinsic built-in byte swapping?
+ ++ ++Yes, if available. See Intrinsic built-in support +below.
+ +
Why are reverse()
and reverse_value()
templates
-in a detail namespace? They are unsafe for general use. Consider reversing
-the bytes of a std::pair
- the bytes from first
-would end up in second
and visa versa, and this is totally
+in a detail namespace?
+ ++ +They are unsafe for general use. Consider reversing +the bytes of a
+std::pair
as a whole - the bytes fromfirst
+would end up insecond
and visa versa, and this is totally wrong!
Why are both value returning and modify_in_place functions provided?
+ ++ ++ +Value returning functions are the standard idiom for functions that compute a +value. Modification in place functions allow cleaner code in many real-world +endian use cases and are more efficient for user defined types that have many +members such as string data that do not need to be reversed. Thus both are +provided.
+ +
What are the limitations of floating point support?
+ ++ ++ +The only supported types are four byte
+ +float
and eight byte +double
. Even after endianness has been accounted for, floating +point values will not be portable between systems that use different floating +point formats.
What are the limitations of integer support?
+ ++ ++Only 16-bit, 32-bit, and 64-bit integers are supported. No tests have been +performed on machines that use something other than two's complement arithmetic.
+ +
noexcept
is not present for compilers that do not support it.
-Boost scoped enum emulation is used for compilers that do not support scoped
-enums. Actual functions are inline
if appropriate.
Functions are implemented inline
if appropriate. noexcept
is
+elided for compilers that do not support it.
+Boost scoped enum emulation is used so that the library still works for compilers that do not support scoped enums.
namespace boost +#define BOOST_ENDIAN_INTRINSIC_MSG "message describing presence or absence of intrinsics" + +namespace boost { namespace endian { - enum class order {big, little, native}; + enum class order {big, little, native}; // reverse byte order (i.e. endianness) - int16_t reverse_value(int16_t x) noexcept; - int32_t reverse_value(int32_t x) noexcept; - int64_t reverse_value(int64_t x) noexcept; - uint16_t reverse_value(uint16_t x) noexcept; - uint32_t reverse_value(uint32_t x) noexcept; - uint64_t reverse_value(uint64_t x) noexcept; - float reverse_value(float x) noexcept; - double reverse_value(double x) noexcept; + int16_t reverse_value(int16_t x) noexcept; + int32_t reverse_value(int32_t x) noexcept; + int64_t reverse_value(int64_t x) noexcept; + uint16_t reverse_value(uint16_t x) noexcept; + uint32_t reverse_value(uint32_t x) noexcept; + uint64_t reverse_value(uint64_t x) noexcept; + float reverse_value(float x) noexcept; + double reverse_value(double x) noexcept; - void reverse(int16_t& x) noexcept; - void reverse(int32_t& x) noexcept; - void reverse(int64_t& x) noexcept; - void reverse(uint16_t& x) noexcept; - void reverse(uint32_t& x) noexcept; - void reverse(uint64_t& x) noexcept; - void reverse(float& x) noexcept; - void reverse(double& x) noexcept; + void reverse(int16_t& x) noexcept; + void reverse(int32_t& x) noexcept; + void reverse(int64_t& x) noexcept; + void reverse(uint16_t& x) noexcept; + void reverse(uint32_t& x) noexcept; + void reverse(uint64_t& x) noexcept; + void reverse(float& x) noexcept; + void reverse(double& x) noexcept; // reverse byte order unless native endianness is big template <class ReversibleValue > - ReversibleValue big_endian_value(ReversibleValue x) noexcept; + ReversibleValue big_endian_value(ReversibleValue x) noexcept; template <class Reversible> - void big_endian(Reversible& x) noexcept; + void big_endian(Reversible& x) noexcept; // reverse byte order unless native endianness is little template <class ReversibleValue > - ReversibleValue little_endian_value(ReversibleValue x) noexcept; + ReversibleValue little_endian_value(ReversibleValue x) noexcept; template <class Reversible> - void little_endian(Reversible& x) noexcept; + void little_endian(Reversible& x) noexcept; // synonyms, based on names popularized by BSD (e.g. OS X, Linux) // "h" for "host" (i.e. native), "be" for "big endian", @@ -136,30 +205,32 @@ namespace endian template <class T> void betohm(T& big) noexcept {big_endian(big);} template <class T> void letohm(T& little) noexcept {little_endian(little);} - // compile-time generic byte order conversion + // generic byte order conversion template <order From, order To, class ReversibleValue> - ReversibleValue convert_value(ReversibleValue from) noexcept; + ReversibleValue convert_value(ReversibleValue from) noexcept; template <order From, order To, class Reversible> - void convert(Reversible& x) noexcept; + void convert(Reversible& x) noexcept; // runtime effective byte order determination - order effective_order(order x) noexcept; + order effective_order(order x) noexcept; // runtime byte-order conversion template <class ReversibleValue> - ReversibleValue convert_value(ReversibleValue from, + ReversibleValue convert_value(ReversibleValue from, order from_order, order to_order) noexcept; template <class Reversible> - void convert(Reversible& x, + void convert(Reversible& x, order from_order, order to_order) noexcept; } // namespace endian } // namespace boostRequirements
-The template definitions in this library refer to various named -requirements whose details are set out in this section.
+The template definitions in this header refer to named +requirements whose details are set out in this section. User defined types may +be used in the function templates in this header only if they meet the +function's template parameter requirements.
ReversibleValue requirements
-
ReversibleValue
is an object or reference type to be +
ReversibleValue
is an object type to be supplied by a C++ program instantiating a template;x
is a value of type (possiblyconst
)ReversibleValue
.
Reversible
is an object or reference type to be
+
Reversible
is an object type to be
supplied by a C++ program instantiating a template; x
is a
modifiable lvalue of type Reversible
.
int16_t reverse_value(int16_t x) noexcept; +int16_t reverse_value(int16_t x) noexcept; int32_t reverse_value(int32_t x) noexcept; int64_t reverse_value(int64_t x) noexcept; uint16_t reverse_value(uint16_t x) noexcept; @@ -208,7 +279,7 @@ double reverse_value(double x) noexcept;Returns:
-x
, with the order of its constituent bytes reversed.void reverse(int16_t& x) noexcept; +-void reverse(int16_t& x) noexcept; void reverse(int32_t& x) noexcept; void reverse(int64_t& x) noexcept; void reverse(uint16_t& x) noexcept; @@ -220,9 +291,9 @@ void reverse(double& x) noexcept;Postconditions: The order of the constituent bytes of
-x
are reversed.template <class ReversibleValue > +-template <class ReversibleValue > ReversibleValue big_endian_value(ReversibleValue x) noexcept; -template <class Reversible> +template <class Reversible> void big_endian(Reversible& x) noexcept;Returns (first form):
x
if the native byte order is big @@ -236,9 +307,9 @@ big_endian(x); // reverses the byte order of x, unless // the native byte order is big-endiantemplate <class ReversibleValue > +-template <class ReversibleValue > ReversibleValue little_endian_value(ReversibleValue x) noexcept; -template <class Reversible> +template <class Reversible> void little_endian(Reversible& x) noexcept;Returns (first form):
x
if the native byte order is little @@ -253,9 +324,9 @@ int32_t y(little_endian(x)); // the native byte order is little-endian.template <order From, order To, class ReversibleValue> +-template <order From, order To, class ReversibleValue> ReversibleValue convert_value(ReversibleValue from) noexcept; -template <order From, order To, class Reversible> +template <order From, order To, class Reversible> void convert(Reversible& x) noexcept;@@ -275,13 +346,13 @@ template <order From, order To, class Reversible> convert<order::big, order::native>(x); // more generic equivalent of big_endian(x);order effective_order(order x) noexcept;-Returns:
x
ifx != order::native
, otherwise theorder
constant for the actual native byte order.Example:
effective_order(order::big); // returns order::big +order effective_order(order x) noexcept;Returns:
x
ifx != order::native
, otherwise theorder
constant for the actual native byte order.Example:
effective_order(order::big); // returns order::big effective_order(order::little); // returns order::little effective_order(order::native); // returns order::big if the native order - // is big-endian, otherwise order::littletemplate <class ReversibleValue> + // is big-endian, otherwise order::littletemplate <class ReversibleValue> ReversibleValue convert_value(ReversibleValue from, order from_order, order to_order) noexcept; -template <class Reversible> +template <class Reversible> void convert(Reversible& x, order from_order, order to_order) noexcept;Returns (first form):
from
ifeffect_order(from_order) == effective_order(to_order)
, otherwisereverse_value(from)
.Effects (second form): None if
@@ -291,10 +362,12 @@ template <class Reversible> ... read an external value of an endianness know only at runtime into x convert(x, some_order, order::native); // convert to native byte order if neededeffect_order(from_order) == effective_order(to_order)
, otherwisereverse(x)
.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 floating point types and 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.
+Intrinsic built-in support
+ +Recent compilers, including GCC, Clang, and Microsoft, supply intrinsic built-in support for byte swapping. Such support is automatically detected and used since it results in smaller and much faster generated code for release builds.
Defining BOOST_ENDIAN_NO_INTRINSICS will suppress use of the intrinsics. Please try defining it if you get compiler errors, such as header byteswap.h not being found.
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.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 floating point types and 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.
-Last revised: 18 May, 2013
+Last revised: 19 May, 2013
© Copyright Beman Dawes, 2011
Distributed under the Boost Software License, Version 1.0. See www.boost.org/ LICENSE_1_0.txt
diff --git a/doc/index.html b/doc/index.html index 38a6c00..3e68295 100644 --- a/doc/index.html +++ b/doc/index.html @@ -86,12 +86,12 @@ FILE * file = fopen("test", "wb"); // MUST BE BINARY fwrite(&i, sizeof(int16_t), 1, file); fclose(file);On a modern Apple, Linux, or Windows computer with an Intel CPU, a hex dump +
On an Apple, Linux, or Windows computer with an Intel CPU, a hex dump of the "test" output file produces:
-
0201
On a Oracle/Sun Solaris computer with a SPARC CPU, a hex dump of the "test" +
On an Apple or other computer with a PowerPC CPU, or an Oracle/Sun Solaris computer with a SPARC CPU, a hex dump of the "test" output file produces:
@@ -161,7 +161,7 @@ Tomas Puverle, Vincente Botet, and Yuval Ronen.
0102
Last revised: -04 September, 2011
+19 May, 2013© Copyright Beman Dawes, 2011
Distributed under the Boost Software License, Version 1.0. See www.boost.org/ LICENSE_1_0.txt
diff --git a/include/boost/endian/conversion.hpp b/include/boost/endian/conversion.hpp index bea08fe..2070d4a 100644 --- a/include/boost/endian/conversion.hpp +++ b/include/boost/endian/conversion.hpp @@ -143,7 +143,7 @@ namespace endian // undefined behavior as suggested by Giovanni Piero Deretta, and a further // // refinement suggested by Pyry Jahkola. // // -- reverse_value intrinsic approach suggested by reviewers, and by David Stone, // -// who provided his Boost licensed macro implementation (see detail/intrinsic.hpp // +// who provided his Boost licensed macro implementation (detail/intrinsic.hpp) // // // //--------------------------------------------------------------------------------------//