|
1 | | -/* |
| 1 | +/** |
2 | 2 | * ----------------------------------------------------------------------------- |
3 | 3 | * Project: Fossil Logic |
4 | 4 | * |
5 | | - * This file is part of the Fossil Logic project, which aims to develop high- |
6 | | - * performance, cross-platform applications and libraries. The code contained |
7 | | - * herein is subject to the terms and conditions defined in the project license. |
| 5 | + * This file is part of the Fossil Logic project, which aims to develop |
| 6 | + * high-performance, cross-platform applications and libraries. The code |
| 7 | + * contained herein is licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. You may obtain |
| 9 | + * a copy of the License at: |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | + * License for the specific language governing permissions and limitations |
| 17 | + * under the License. |
8 | 18 | * |
9 | 19 | * Author: Michael Gene Brockus (Dreamer) |
10 | 20 | * Date: 04/05/2014 |
@@ -923,8 +933,36 @@ int pizza_sys_hostinfo_get_memory(pizza_sys_hostinfo_memory_t *info) { |
923 | 933 |
|
924 | 934 | int pizza_sys_hostinfo_get_endianness(pizza_sys_hostinfo_endianness_t *info) { |
925 | 935 | if (!info) return -1; |
926 | | - uint16_t test = 0x0001; |
927 | | - info->is_little_endian = (*(uint8_t*)&test) ? 1 : 0; |
| 936 | + |
| 937 | + // --- Compile-time detection if supported --- |
| 938 | +#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__) |
| 939 | +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 940 | + info->is_little_endian = 1; |
| 941 | + info->is_big_endian = 0; |
| 942 | +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 943 | + info->is_little_endian = 0; |
| 944 | + info->is_big_endian = 1; |
| 945 | +#else |
| 946 | + #error "Unknown byte order at compile-time" |
| 947 | +#endif |
| 948 | +#else |
| 949 | + // --- Runtime fallback using union --- |
| 950 | + union { |
| 951 | + uint16_t value; |
| 952 | + uint8_t bytes[2]; |
| 953 | + } test; |
| 954 | + |
| 955 | + test.value = 0x0102; |
| 956 | + |
| 957 | + if (test.bytes[0] == 0x02) { |
| 958 | + info->is_little_endian = 1; |
| 959 | + info->is_big_endian = 0; |
| 960 | + } else { |
| 961 | + info->is_little_endian = 0; |
| 962 | + info->is_big_endian = 1; |
| 963 | + } |
| 964 | +#endif |
| 965 | + |
928 | 966 | return 0; |
929 | 967 | } |
930 | 968 |
|
|
0 commit comments