@@ -60,24 +60,27 @@ const PyLittleEndian = cpuEndian == littleEndian
60
60
61
61
const bytePerWord = 4
62
62
type U8 = uint8 | char
63
- func fromU32sImpl(res: var seq [U 8]| string ; wordarray: openArray [uint32 ]) {.inline.} =
63
+ func fromU32sImpl[S: seq [U 8]| string ] (res: var S ; wordarray: openArray [uint32 ]){.inline.} =
64
64
when declared(copyMem):
65
65
copyMem(res[0 ].addr , wordarray[0 ].addr , res.len)
66
66
else :
67
- template unpack[T: U8| char ](u: uint32 , a, b, c, d: var T) =
68
- d = cast [T](u)
69
- c = cast[T](u shr 8)
70
- b = cast[T](u shr 16)
71
- a = cast[T](u shr 24)
72
67
when PyLittleEndian:
73
- var i = 0
74
- for u32 in wordarray:
75
- unpack u32, res[i], res[i+ 1], res[i+ 2 ], res[i+ 3 ]
76
- i.inc per
68
+ template rng: untyped = 0 .. wordarray.high
69
+ template `[]=` (res; i, o, v): untyped = res[i + (3 - o)] = v
77
70
else :
78
- for i in countdown(wordarray.high, 0 ):
79
- let ii = i * per
80
- unpack wordarray[i], res[ii+ 3 ], res[ii+ 2 ], res[ii+ 1 ], res[ii]
71
+ template rng: untyped = countdown(wordarray.high, 0 )
72
+ template `[]=` (res; i, o, v): untyped = res[i + o] = v
73
+ for i in rng:
74
+ let ii = i * bytePerWord
75
+ # unpack wordarray[i], res[ii+3], res[ii+2], res[ii+1], res[ii]
76
+ let u = wordarray[i]
77
+ type T = typeof(res[0 ])
78
+ template asgnPart(o) =
79
+ res[ii, o] = cast [T](u shr (8 * o))
80
+ asgnPart 0
81
+ asgnPart 1
82
+ asgnPart 2
83
+ asgnPart 3
81
84
82
85
func fromU32s(res: var seq [U 8]; wordarray: openArray [uint32 ]) =
83
86
res = newSeqMayUninit[U8] wordarray.len * bytePerWord
0 commit comments