I found a solution which is semi _elegant_ only but does the job. It was
trickier (for me at least) than originally anticipated:
proc `!`(n: int64): int {.compileTime.} =
when int.sizeof == int64.sizeof:
(n and 0xffffffff).int
else:
if (n and 0x80000000) != 0:
(n or (not 0xffffffff)).int
else:
n.int
This allows me to write something like
proc tryLayers(v: int): MpgWhat {.inline.} =
block dontKnow:
case v and !0xfffe0000:
of !0xfffa0000, # MP3, M1A (layer III), v1
!0xfffc0000, # MP2, M1A (layer II), v1
# MPA, M1A starts with fffe => utf16le BOM
!0xfff20000, # MP3, M2A (layer III), v2
!0xfff40000, # MP2, M2A (layer II), v2
!0xfff60000, # MPA, M2A (layer I), v2
!0xffe20000: # MP3, M25A (layer III), v2.5
[...]
which works regardless of architecture for 32bit values loaded into int:
when int.sizeof == int64.sizeof:
doAssert !0xffffffffff == 0xffffffff
else:
doAssert !0xffffffff == -1
doAssert !0x7fffffff == int.high