edubart / nelua-lang

Minimal, efficient, statically-typed and meta-programmable systems programming language heavily inspired by Lua, which compiles to C and native code.
https://nelua.io
MIT License
1.99k stars 64 forks source link

Easier access to compiler endianness #206

Closed IcedQuinn closed 1 year ago

IcedQuinn commented 1 year ago

Access to whether the compiler is on a big or little endian system is useful for some bit operations.

For example in the xxhash algorithm in Nim:

proc XXH_readLE32(p: pointer): uint32 =
    when cpu_endian == little_endian:
        return XXH_read32(p)
    else:
        return XXH_swap32(XXH_read32(p))

I searched for endian in the documentation search engine and with silver searcher in the codebase. It looks like cdefs.lua does mention some endian checking but the docs don't mention it.

edubart commented 1 year ago

You can check for this like in the example:

## if ccinfo.is_little_endian then
  print 'little endian'
## elseif ccinfo.is_big_endian then
  print 'big endian'
## else
  print 'endianess is unknown'
## end