David Neto | db901b6 | 2015-10-27 16:14:40 -0400 | [diff] [blame] | 1 | // Copyright (c) 2015 The Khronos Group Inc. |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | // copy of this software and/or associated documentation files (the |
| 5 | // "Materials"), to deal in the Materials without restriction, including |
| 6 | // without limitation the rights to use, copy, modify, merge, publish, |
| 7 | // distribute, sublicense, and/or sell copies of the Materials, and to |
| 8 | // permit persons to whom the Materials are furnished to do so, subject to |
| 9 | // the following conditions: |
| 10 | // |
| 11 | // The above copyright notice and this permission notice shall be included |
| 12 | // in all copies or substantial portions of the Materials. |
| 13 | // |
| 14 | // MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS |
| 15 | // KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS |
| 16 | // SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT |
| 17 | // https://www.khronos.org/registry/ |
| 18 | // |
| 19 | // THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 22 | // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 23 | // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 24 | // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 25 | // MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
| 26 | |
| 27 | #include "endian.h" |
| 28 | |
| 29 | #include <cstring> |
| 30 | |
| 31 | enum { |
| 32 | I32_ENDIAN_LITTLE = 0x03020100ul, |
| 33 | I32_ENDIAN_BIG = 0x00010203ul, |
| 34 | }; |
| 35 | |
| 36 | // TODO(dneto): This relies on undefined behaviour. Fix that. |
| 37 | static const union { |
| 38 | unsigned char bytes[4]; |
| 39 | uint32_t value; |
| 40 | } o32_host_order = {{0, 1, 2, 3}}; |
| 41 | |
| 42 | #define I32_ENDIAN_HOST (o32_host_order.value) |
| 43 | |
| 44 | uint32_t spvFixWord(const uint32_t word, const spv_endianness_t endian) { |
| 45 | if ((SPV_ENDIANNESS_LITTLE == endian && I32_ENDIAN_HOST == I32_ENDIAN_BIG) || |
| 46 | (SPV_ENDIANNESS_BIG == endian && I32_ENDIAN_HOST == I32_ENDIAN_LITTLE)) { |
| 47 | return (word & 0x000000ff) << 24 | (word & 0x0000ff00) << 8 | |
| 48 | (word & 0x00ff0000) >> 8 | (word & 0xff000000) >> 24; |
| 49 | } |
| 50 | |
| 51 | return word; |
| 52 | } |
| 53 | |
| 54 | uint64_t spvFixDoubleWord(const uint32_t low, const uint32_t high, |
| 55 | const spv_endianness_t endian) { |
| 56 | return (uint64_t(spvFixWord(high, endian)) << 32) | spvFixWord(low, endian); |
| 57 | } |
| 58 | |
Andrew Woloszyn | 55ecc2e | 2015-11-11 11:05:07 -0500 | [diff] [blame] | 59 | spv_result_t spvBinaryEndianness(spv_const_binary binary, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 60 | spv_endianness_t* pEndian) { |
David Neto | db901b6 | 2015-10-27 16:14:40 -0400 | [diff] [blame] | 61 | if (!binary->code || !binary->wordCount) return SPV_ERROR_INVALID_BINARY; |
| 62 | if (!pEndian) return SPV_ERROR_INVALID_POINTER; |
| 63 | |
| 64 | uint8_t bytes[4]; |
| 65 | memcpy(bytes, binary->code, sizeof(uint32_t)); |
| 66 | |
| 67 | if (0x03 == bytes[0] && 0x02 == bytes[1] && 0x23 == bytes[2] && |
| 68 | 0x07 == bytes[3]) { |
| 69 | *pEndian = SPV_ENDIANNESS_LITTLE; |
| 70 | return SPV_SUCCESS; |
| 71 | } |
| 72 | |
| 73 | if (0x07 == bytes[0] && 0x23 == bytes[1] && 0x02 == bytes[2] && |
| 74 | 0x03 == bytes[3]) { |
| 75 | *pEndian = SPV_ENDIANNESS_BIG; |
| 76 | return SPV_SUCCESS; |
| 77 | } |
| 78 | |
| 79 | return SPV_ERROR_INVALID_BINARY; |
| 80 | } |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 81 | |
| 82 | bool spvIsHostEndian(spv_endianness_t endian) { |
| 83 | return ((SPV_ENDIANNESS_LITTLE == endian) && |
| 84 | (I32_ENDIAN_LITTLE == I32_ENDIAN_HOST)) || |
| 85 | ((SPV_ENDIANNESS_BIG == endian) && |
| 86 | (I32_ENDIAN_BIG == I32_ENDIAN_HOST)); |
| 87 | } |