mirror of
https://github.com/KhronosGroup/SPIRV-Tools
synced 2024-12-25 01:01:04 +00:00
Move endian utilities to their own file.
This commit is contained in:
parent
0b9816813b
commit
db901b622e
@ -106,6 +106,7 @@ set(SPIRV_SOURCES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/assembly_grammar.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/binary.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/diagnostic.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/endian.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/ext_inst.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/instruction.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/opcode.h
|
||||
@ -117,6 +118,7 @@ set(SPIRV_SOURCES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/assembly_grammar.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/binary.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/diagnostic.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/endian.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/ext_inst.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/opcode.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/operand.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) {
|
||||
|
@ -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
|
||||
|
80
source/endian.cpp
Normal file
80
source/endian.cpp
Normal file
@ -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;
|
||||
}
|
62
source/endian.h
Normal file
62
source/endian.h
Normal file
@ -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
|
@ -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"
|
||||
|
||||
|
@ -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"
|
||||
|
@ -31,6 +31,7 @@
|
||||
#include "../source/assembly_grammar.h"
|
||||
#include "../source/binary.h"
|
||||
#include "../source/diagnostic.h"
|
||||
#include "../source/endian.h"
|
||||
#include "../source/opcode.h"
|
||||
#include "../source/text.h"
|
||||
#include "../source/text_handler.h"
|
||||
|
Loading…
Reference in New Issue
Block a user