blob: 240718a5b258b48fe66c189db2f731f14f7f8e28 [file] [log] [blame]
David Netodb901b62015-10-27 16:14:40 -04001// 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
31enum {
32 I32_ENDIAN_LITTLE = 0x03020100ul,
33 I32_ENDIAN_BIG = 0x00010203ul,
34};
35
36// TODO(dneto): This relies on undefined behaviour. Fix that.
37static 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
44uint32_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
54uint64_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
59spv_result_t spvBinaryEndianness(const spv_binary binary,
60 spv_endianness_t *pEndian) {
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}