Move endian utilities to their own file.
diff --git a/source/binary.cpp b/source/binary.cpp
index 4213bea..128d278 100644
--- a/source/binary.cpp
+++ b/source/binary.cpp
@@ -34,6 +34,7 @@
#include <libspirv/libspirv.h>
#include "assembly_grammar.h"
#include "diagnostic.h"
+#include "endian.h"
#include "ext_inst.h"
#include "instruction.h"
#include "opcode.h"
@@ -42,59 +43,9 @@
// Binary API
-enum {
- I32_ENDIAN_LITTLE = 0x03020100ul,
- I32_ENDIAN_BIG = 0x00010203ul,
-};
-
-static const union {
- unsigned char bytes[4];
- uint32_t value;
-} o32_host_order = {{0, 1, 2, 3}};
-
using id_to_type_id_map = std::unordered_map<uint32_t, uint32_t>;
using type_id_to_type_map = std::unordered_map<uint32_t, libspirv::IdType>;
-#define I32_ENDIAN_HOST (o32_host_order.value)
-
-spv_result_t spvBinaryEndianness(const spv_binary binary,
- spv_endianness_t *pEndian) {
- if (!binary->code || !binary->wordCount) return SPV_ERROR_INVALID_BINARY;
- if (!pEndian) return SPV_ERROR_INVALID_POINTER;
-
- uint8_t bytes[4];
- memcpy(bytes, binary->code, sizeof(uint32_t));
-
- if (0x03 == bytes[0] && 0x02 == bytes[1] && 0x23 == bytes[2] &&
- 0x07 == bytes[3]) {
- *pEndian = SPV_ENDIANNESS_LITTLE;
- return SPV_SUCCESS;
- }
-
- if (0x07 == bytes[0] && 0x23 == bytes[1] && 0x02 == bytes[2] &&
- 0x03 == bytes[3]) {
- *pEndian = SPV_ENDIANNESS_BIG;
- return SPV_SUCCESS;
- }
-
- return SPV_ERROR_INVALID_BINARY;
-}
-
-uint32_t spvFixWord(const uint32_t word, const spv_endianness_t endian) {
- if ((SPV_ENDIANNESS_LITTLE == endian && I32_ENDIAN_HOST == I32_ENDIAN_BIG) ||
- (SPV_ENDIANNESS_BIG == endian && I32_ENDIAN_HOST == I32_ENDIAN_LITTLE)) {
- return (word & 0x000000ff) << 24 | (word & 0x0000ff00) << 8 |
- (word & 0x00ff0000) >> 8 | (word & 0xff000000) >> 24;
- }
-
- return word;
-}
-
-uint64_t spvFixDoubleWord(const uint32_t low, const uint32_t high,
- const spv_endianness_t endian) {
- return (uint64_t(spvFixWord(high, endian)) << 32) | spvFixWord(low, endian);
-}
-
spv_result_t spvBinaryHeaderGet(const spv_binary binary,
const spv_endianness_t endian,
spv_header_t *pHeader) {
diff --git a/source/binary.h b/source/binary.h
index ad542cf..0544395 100644
--- a/source/binary.h
+++ b/source/binary.h
@@ -34,37 +34,6 @@
// Functions
-/// @brief Fix the endianness of a word
-///
-/// @param[in] word whos endianness should be fixed
-/// @param[in] endian the desired endianness
-///
-/// @return word with host endianness correction
-uint32_t spvFixWord(const uint32_t word, const spv_endianness_t endian);
-
-/// @brief Fix the endianness of a double word
-///
-/// @param[in] low the lower 32-bit of the double word
-/// @param[in] high the higher 32-bit of the double word
-/// @param[in] endian the desired endianness
-///
-/// @return word with host endianness correction
-uint64_t spvFixDoubleWord(const uint32_t low, const uint32_t high,
- const spv_endianness_t endian);
-
-/// @brief Determine the endianness of the SPV binary
-///
-/// Gets the endianness of the SPV source. Returns SPV_ENDIANNESS_UNKNOWN if
-/// the
-/// SPV magic number is invalid, otherwise the determined endianness.
-///
-/// @param[in] binary the binary module
-/// @param[out] pEndian return the endianness of the SPV module
-///
-/// @return result code
-spv_result_t spvBinaryEndianness(const spv_binary binary,
- spv_endianness_t *pEndian);
-
/// @brief Grab the header from the SPV module
///
/// @param[in] binary the binary module
diff --git a/source/endian.cpp b/source/endian.cpp
new file mode 100644
index 0000000..240718a
--- /dev/null
+++ b/source/endian.cpp
@@ -0,0 +1,80 @@
+// Copyright (c) 2015 The Khronos Group Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and/or associated documentation files (the
+// "Materials"), to deal in the Materials without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Materials, and to
+// permit persons to whom the Materials are furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Materials.
+//
+// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
+// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
+// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
+// https://www.khronos.org/registry/
+//
+// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
+
+#include "endian.h"
+
+#include <cstring>
+
+enum {
+ I32_ENDIAN_LITTLE = 0x03020100ul,
+ I32_ENDIAN_BIG = 0x00010203ul,
+};
+
+// TODO(dneto): This relies on undefined behaviour. Fix that.
+static const union {
+ unsigned char bytes[4];
+ uint32_t value;
+} o32_host_order = {{0, 1, 2, 3}};
+
+#define I32_ENDIAN_HOST (o32_host_order.value)
+
+uint32_t spvFixWord(const uint32_t word, const spv_endianness_t endian) {
+ if ((SPV_ENDIANNESS_LITTLE == endian && I32_ENDIAN_HOST == I32_ENDIAN_BIG) ||
+ (SPV_ENDIANNESS_BIG == endian && I32_ENDIAN_HOST == I32_ENDIAN_LITTLE)) {
+ return (word & 0x000000ff) << 24 | (word & 0x0000ff00) << 8 |
+ (word & 0x00ff0000) >> 8 | (word & 0xff000000) >> 24;
+ }
+
+ return word;
+}
+
+uint64_t spvFixDoubleWord(const uint32_t low, const uint32_t high,
+ const spv_endianness_t endian) {
+ return (uint64_t(spvFixWord(high, endian)) << 32) | spvFixWord(low, endian);
+}
+
+spv_result_t spvBinaryEndianness(const spv_binary binary,
+ spv_endianness_t *pEndian) {
+ if (!binary->code || !binary->wordCount) return SPV_ERROR_INVALID_BINARY;
+ if (!pEndian) return SPV_ERROR_INVALID_POINTER;
+
+ uint8_t bytes[4];
+ memcpy(bytes, binary->code, sizeof(uint32_t));
+
+ if (0x03 == bytes[0] && 0x02 == bytes[1] && 0x23 == bytes[2] &&
+ 0x07 == bytes[3]) {
+ *pEndian = SPV_ENDIANNESS_LITTLE;
+ return SPV_SUCCESS;
+ }
+
+ if (0x07 == bytes[0] && 0x23 == bytes[1] && 0x02 == bytes[2] &&
+ 0x03 == bytes[3]) {
+ *pEndian = SPV_ENDIANNESS_BIG;
+ return SPV_SUCCESS;
+ }
+
+ return SPV_ERROR_INVALID_BINARY;
+}
diff --git a/source/endian.h b/source/endian.h
new file mode 100644
index 0000000..f786b19
--- /dev/null
+++ b/source/endian.h
@@ -0,0 +1,62 @@
+// Copyright (c) 2015 The Khronos Group Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and/or associated documentation files (the
+// "Materials"), to deal in the Materials without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Materials, and to
+// permit persons to whom the Materials are furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Materials.
+//
+// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
+// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
+// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
+// https://www.khronos.org/registry/
+//
+// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
+
+#ifndef _LIBSPIRV_UTIL_ENDIAN_H_
+#define _LIBSPIRV_UTIL_ENDIAN_H_
+
+#include <libspirv/libspirv.h>
+
+/// @brief Fix the endianness of a word
+///
+/// @param[in] word whos endianness should be fixed
+/// @param[in] endian the desired endianness
+///
+/// @return word with host endianness correction
+uint32_t spvFixWord(const uint32_t word, const spv_endianness_t endian);
+
+/// @brief Fix the endianness of a double word
+///
+/// @param[in] low the lower 32-bit of the double word
+/// @param[in] high the higher 32-bit of the double word
+/// @param[in] endian the desired endianness
+///
+/// @return word with host endianness correction
+uint64_t spvFixDoubleWord(const uint32_t low, const uint32_t high,
+ const spv_endianness_t endian);
+
+/// @brief Determine the endianness of the SPV binary
+///
+/// Gets the endianness of the SPV source. Returns SPV_ENDIANNESS_UNKNOWN if
+/// the
+/// SPV magic number is invalid, otherwise the determined endianness.
+///
+/// @param[in] binary the binary module
+/// @param[out] pEndian return the endianness of the SPV module
+///
+/// @return result code
+spv_result_t spvBinaryEndianness(const spv_binary binary,
+ spv_endianness_t *pEndian);
+#endif
diff --git a/source/opcode.cpp b/source/opcode.cpp
index 32fe7fb..fa0159c 100644
--- a/source/opcode.cpp
+++ b/source/opcode.cpp
@@ -25,7 +25,7 @@
// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
#include <libspirv/libspirv.h>
-#include "binary.h"
+#include "endian.h"
#include "instruction.h"
#include "opcode.h"
diff --git a/source/validate.cpp b/source/validate.cpp
index 341d618..ff66308 100644
--- a/source/validate.cpp
+++ b/source/validate.cpp
@@ -27,6 +27,7 @@
#include <libspirv/libspirv.h>
#include "binary.h"
#include "diagnostic.h"
+#include "endian.h"
#include "instruction.h"
#include "opcode.h"
#include "operand.h"