{"id":16842628,"url":"https://github.com/rofl0r/endianness.h","last_synced_at":"2026-03-08T19:36:55.739Z","repository":{"id":47084355,"uuid":"109829519","full_name":"rofl0r/endianness.h","owner":"rofl0r","description":"simple public domain header to get endianess at compile time on a variety of platforms","archived":false,"fork":false,"pushed_at":"2021-09-15T22:05:52.000Z","size":21,"stargazers_count":31,"open_issues_count":2,"forks_count":15,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-11T05:52:25.578Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rofl0r.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-07T11:57:00.000Z","updated_at":"2025-03-25T04:31:37.000Z","dependencies_parsed_at":"2022-09-17T05:23:43.316Z","dependency_job_id":null,"html_url":"https://github.com/rofl0r/endianness.h","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rofl0r%2Fendianness.h","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rofl0r%2Fendianness.h/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rofl0r%2Fendianness.h/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rofl0r%2Fendianness.h/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rofl0r","download_url":"https://codeload.github.com/rofl0r/endianness.h/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248351409,"owners_count":21089271,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T12:47:25.736Z","updated_at":"2026-03-08T19:36:55.711Z","avatar_url":"https://github.com/rofl0r.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Description\n\nA one-file public-domain library to determine endianness at compile time.\nJust drop a copy of the header in your source tree, use it and forget about all\nthe platform-specific differences to get that endianess definition!\n\nThe usual conversion functions are also provided.\n\n# Rationale\n\nIf someone needs to know the endianess at compile time, there are 2 different\nuse cases:\n\n- in some rare cases one needs to know the endianess to lay out structs and the\n  like, for example an RGB union; or directly in the code.\n\n- in 99% of the cases, just a conversion from one endian to the other is needed,\n  e.g. when a protocol defines that a value is stored in a specific endianness.\n\n# Use case 1 - compile time endianness detecion macro\n\nThe header tries its best to determine the endianess from a number of possible\nsources, and if successful, defines both `ENDIANNESS_LE` and `ENDIANNESS_BE`\nmacros to 0 or 1.\n\n```C\n#include \"endianness.h\"\n#if ENDIANNESS_LE +0 == 1\n/* Little Endian code here */\n#elif ENDIANNESS_BE +0 == 1\n/* Big Endian code here */\n#endif\n```\n\nor if the macro is to be used directly from the code, you can even use the nicer\nform (taking advantage of dead code elimination done by the compiler):\n\n```C\nif (ENDIANNESS_LE) {\n    ...\n} else {\n    ...\n}\n```\n\nNote that on very exotic platforms, the detection may fail. The header then\nthrows an error and tells the user to define the macros by hand (and report the\nsystem here so we can get it fixed).\nHowever, if your use case is just conversion, there's a way to avoid erroring\nout. Read on!\n\n# Use case 2 - endian conversion\n\nIn the majority of use cases, one wants just to convert from little to big\nendian, and vice versa (or actually to and from host endianness!).\n\nThe provided endian conversion functions/macros start with the `end_` prefix.\n\n```\nend_htobe16(x)  // Host to Big Endian 16\nend_be16toh(x)\nend_htobe32(x)\nend_be32toh(x)  // Big Endian 32 to Host\nend_htobe64(x)\nend_be64toh(x)\nend_htole16(x)\nend_le16toh(x)\nend_htole32(x)  // Host to Little Endian 32\nend_le32toh(x)\nend_htole64(x)\nend_le64toh(x)  // Little Endian 64 to Host\n```\n\nFor conversion of network byte order (big endian) to host order:\n```\nend_ntoh16(x)  // equivalent of ntohs(x) from \u003cnetinet/in.h\u003e\nend_hton16(x)  //               htons(x)\nend_ntoh32(x)  //               ntohl(x)\nend_hton32(x)  //               htonl(x)\nend_ntoh64(x)\nend_hton64(x)\n```\n\nGeneral purpose byteswap functions:\n```\nend_bswap16(x)\nend_bswap32(x)\nend_bswap64(x)\n```\n\nIn case the endianness detection failed and you need only those conversions,\nyou can avoid erroring out by defining `ENDIANNESS_PORTABLE_CONVERSION` prior to\nincluding the header:\n\n```C\n#define ENDIANNESS_PORTABLE_CONVERSION\n#include \"endianness.h\"\n```\n\nThat way, the code will fallback to a slightly slower, but portable version of\nthe conversion functions. On modern compilers like GCC 8.0 these compile into\na single move and a byteswap instruction.\n\nHowever, if `ENDIANNESS_PORTABLE_CONVERSION` is in use, there's no guarantee\nthat the macros for use case 1 will be defined!\n\n# License\n\nThis file is published under the public domain. In case the concept of\npublic domain does not exist under your jurisdiction, you can consider it\nto be dual licensed under the [MIT](https://opensource.org/licenses/MIT),\n[Apache](https://www.apache.org/licenses/LICENSE-2.0) and\n[WTFPL](http://www.wtfpl.net/about/) licenses.\n\n# Contribution\n\nIf you notice an issue on a platform you're using, feel free to open a PR or\nissue on the Github repository https://github.com/rofl0r/endianness.h .\n\n# Appendix A: Methodology used to detect endianness\n\nThe header first tries to use the macro `__BYTE_ORDER__` which is built into\nall GCC versions \u003e= 4.6 and all clang versions \u003e= 3.1. Recent ICC versions\nalso support it.\nAs GCC 4.6.0 was released in 2011, this should already cover the vast majority\nof available toolchains for UNIX platforms and other platforms targetted by\nGCC/Clang, such as mingw.\n\nIf that fails, a number of CPU-specific macros is tested.\nThe list was assembled carefully by looking at built-in macros for compilers\ntargeting all platforms supported by musl libc (which includes e.g.\n`powerpc`, `mips`, `arm`, `aarch64`, `microblaze` and many others), but also\nby looking at predefined macros for compilers like MSVC and others.\nFor architectures that support both little and big endian configuration, we\nonly test for them when a macro to detect the subarch, e.g. `mipsel` exists,\nin order to not produce wrong results.\nOlder compilers targeting one such architecture, `avr`, don't provide a macro\nthat could be used to determine the endianness, and even though 99% of users\nuse avr in little-endian configuration, we do not hardcode that avr equals\nlittle endian.\n\nIf even this does not yield detection, we next try to open the header `endian.h`\nwhich is unfortunately not standardized and available in different locations\nin different Operating Systems. Here we use a list of OS detection macros to\nderive the correct location of endian.h and then use the macros it provides.\nNote that getting at this point requires that either a really old or a really\nexotic compiler is used, and that one targetting a really exotic architecture.\nIf even this pass fails, we're dealing with an exotic compiler, an exotic\narchitecture, and an exotic OS - most likely a bare-metal toolchain for\nembedded development.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frofl0r%2Fendianness.h","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frofl0r%2Fendianness.h","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frofl0r%2Fendianness.h/lists"}