summary refs log tree commit diff stats
path: root/tests/misc/tsizeof3.nim
blob: 4f8bdb21ef4c3210c9d0dd8e20b2418cece4cf7b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
discard """
output: '''
@[48, 57]
'''
"""
# bug #7238

type ByteArrayBE*[N: static[int]] = array[N, byte]
  ## A byte array that stores bytes in big-endian order

proc toByteArrayBE*[T: SomeInteger](num: T): ByteArrayBE[sizeof(T)]=
  ## Convert an integer (in native host endianness) to a big-endian byte array
  ## Notice the result type
  const N = T.sizeof
  for i in 0 ..< N:
    result[i] = byte((num shr ((N-1-i) * 8)) and high(int8))

let a = 12345.toByteArrayBE
echo a[^2 .. ^1] # to make it work on both 32-bit and 64-bit


#-----------------------------------------------------------------

# bug #11792
type
  m256d {.importc: "__m256d", header: "immintrin.h".} = object

  MyKind = enum
    k1, k2, k3

  MyTypeObj = object
    kind: MyKind
    x: int
    amount: UncheckedArray[m256d]


# The sizeof(MyTypeObj) is not equal to (sizeof(int) + sizeof(MyKind)) due to
# alignment requirement of m256d, make sure Nim understands that
doAssert(sizeof(MyTypeObj) > sizeof(int) + sizeof(MyKind))

#---------------------------------------------------------------------

type
  Payload = object
    something: int
    vals: UncheckedArray[int]

static:
  doAssert(compiles(offsetOf(Payload, vals)))