1
2
3
4
5
6
7 package spec
8
9 import "math/bits"
10
11
12
13
14
15
16
17
18
19 func ScaleSaturated[E Ints | Uints, W Width, yE Ints](x Vec[E, W], scale Vec[yE, W]) (z Vec[E, W]) {
20 z = makeVec[E, W]()
21 for i := range z {
22 z[i] = scaleSaturated(x[i], scale[i])
23 }
24 return z
25 }
26
27
28
29
30
31
32
33
34
35
36 func ShiftAllRight[E Ints | Uints, W Width](x Vec[E, W], shift uint64) (z Vec[E, W]) {
37 return map1[E, W, E, W](x, func(v E) E {
38 return v >> shift
39 })
40 }
41
42
43
44
45
46 func ShiftAllLeft[E Ints | Uints, W Width](x Vec[E, W], shift uint64) (z Vec[E, W]) {
47 return map1[E, W, E, W](x, func(v E) E {
48 return v << shift
49 })
50 }
51
52
53
54
55
56
57
58
59
60
61
62
63 func ShiftRight[E Ints | Uints, W Width, yE Uints](x Vec[E, W], shift Vec[yE, W]) (z Vec[E, W]) {
64 z = makeVec[E, W]()
65 for i := range z {
66 z[i] = x[i] >> shift[i]
67 }
68 return z
69 }
70
71
72
73
74
75
76
77 func ShiftLeft[E Ints | Uints, W Width, yE Uints](x Vec[E, W], shift Vec[yE, W]) (z Vec[E, W]) {
78 z = makeVec[E, W]()
79 for i := range z {
80 z[i] = x[i] << shift[i]
81 }
82 return z
83 }
84
85
86
87
88 func RotateAllLeft[E Uints, W Width](x Vec[E, W], shift uint64) (z Vec[E, W]) {
89
90 k := int(shift % uint64(elemBits[E]()))
91 return map1[E, W, E, W](x, func(x E) E {
92 switch any(x).(type) {
93 case int8, uint8:
94 return E(bits.RotateLeft8(uint8(x), k))
95 case int16, uint16:
96 return E(bits.RotateLeft16(uint16(x), k))
97 case int32, uint32:
98 return E(bits.RotateLeft32(uint32(x), k))
99 case int64, uint64:
100 return E(bits.RotateLeft64(uint64(x), k))
101 }
102 panic("unreachable")
103 })
104 }
105
106
107
108
109 func RotateLeft[E Uints, W Width](x, shift Vec[E, W]) (z Vec[E, W]) {
110 return map2[E, W, E, W](x, shift, func(x, shift E) E {
111
112 k := int(shift % E(elemBits[E]()))
113 switch any(x).(type) {
114 case int8, uint8:
115 return E(bits.RotateLeft8(uint8(x), k))
116 case int16, uint16:
117 return E(bits.RotateLeft16(uint16(x), k))
118 case int32, uint32:
119 return E(bits.RotateLeft32(uint32(x), k))
120 case int64, uint64:
121 return E(bits.RotateLeft64(uint64(x), k))
122 }
123 panic("unreachable")
124 })
125 }
126
127
128
129
130 func RotateAllRight[E Uints, W Width](x Vec[E, W], shift uint64) (z Vec[E, W]) {
131
132 k := int(shift % uint64(elemBits[E]()))
133 return map1[E, W, E, W](x, func(x E) E {
134 switch any(x).(type) {
135 case int8, uint8:
136 return E(bits.RotateLeft8(uint8(x), -k))
137 case int16, uint16:
138 return E(bits.RotateLeft16(uint16(x), -k))
139 case int32, uint32:
140 return E(bits.RotateLeft32(uint32(x), -k))
141 case int64, uint64:
142 return E(bits.RotateLeft64(uint64(x), -k))
143 }
144 panic("unreachable")
145 })
146 }
147
148
149
150
151 func RotateRight[E Uints, W Width](x, shift Vec[E, W]) (z Vec[E, W]) {
152 return map2[E, W, E, W](x, shift, func(x, shift E) E {
153
154 k := int(shift % E(elemBits[E]()))
155 switch any(x).(type) {
156 case int8, uint8:
157 return E(bits.RotateLeft8(uint8(x), -k))
158 case int16, uint16:
159 return E(bits.RotateLeft16(uint16(x), -k))
160 case int32, uint32:
161 return E(bits.RotateLeft32(uint32(x), -k))
162 case int64, uint64:
163 return E(bits.RotateLeft64(uint64(x), -k))
164 }
165 panic("unreachable")
166 })
167 }
168
View as plain text