blob: 0e9de820ed8924fe6456bbf113ea7c689843cbcc [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 Copyright (c) 2009 Eliot Dresselhaus
17
18 Permission is hereby granted, free of charge, to any person obtaining
19 a copy of this software and associated documentation files (the
20 "Software"), to deal in the Software without restriction, including
21 without limitation the rights to use, copy, modify, merge, publish,
22 distribute, sublicense, and/or sell copies of the Software, and to
23 permit persons to whom the Software is furnished to do so, subject to
24 the following conditions:
25
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
28
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36*/
37
38#ifndef included_vector_altivec_h
39#define included_vector_altivec_h
40
41/* Splats. */
42#define _(t,n,ti,fi,tr,fr) \
43 always_inline t##x##n t##x##n##_splat (t v) \
44 { return (t##x##n) __builtin_altivec_##fi ((ti) v); } \
45 \
46 always_inline t##x##n t##x##n##_splat_word (t##x##n x, int word_index) \
47 { return (t##x##n) __builtin_altivec_##fr ((tr) x, word_index); }
48
49#define u16x8_splat(i) ((u16x8) __builtin_altivec_vspltish (i))
50#define i16x8_splat(i) ((i16x8) __builtin_altivec_vspltish (i))
51#define u32x4_splat(i) ((u32x4) __builtin_altivec_vspltisw (i))
52#define i32x4_splat(i) ((i32x4) __builtin_altivec_vspltisw (i))
53
54#define u16x8_splat_word(x,i) ((u16x8) __builtin_altivec_vsplth ((i16x8) (x), (i)))
55#define i16x8_splat_word(x,i) ((i16x8) __builtin_altivec_vsplth ((i16x8) (x), (i)))
56#define u32x4_splat_word(x,i) ((u32x4) __builtin_altivec_vspltw ((i32x4) (x), (i)))
57#define i32x4_splat_word(x,i) ((i32x4) __builtin_altivec_vspltw ((i32x4) (x), (i)))
58
59#undef _
60
61/* 128 bit shifts. */
62#define _(t,ti,lr,f) \
63 always_inline t t##_##lr (t x, t y) \
64 { return (t) __builtin_altivec_##f ((ti) x, (ti) y); } \
65 \
66 always_inline t t##_i##lr (t x, int i) \
67 { \
68 t j = {i,i,i,i}; \
69 return t##_##lr (x, j); \
70 }
71
Dave Barachc3799992016-08-15 11:12:27 -040072_(u16x8, i16x8, shift_left, vslh);
73_(u32x4, i32x4, shift_left, vslw);
74_(u16x8, i16x8, shift_right, vsrh);
75_(u32x4, i32x4, shift_right, vsrw);
76_(i16x8, i16x8, shift_right, vsrah);
77_(i32x4, i32x4, shift_right, vsraw);
78_(u16x8, i16x8, rotate_left, vrlh);
79_(i16x8, i16x8, rotate_left, vrlh);
80_(u32x4, i32x4, rotate_left, vrlw);
81_(i32x4, i32x4, rotate_left, vrlw);
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
83#undef _
84
85#define _(t,it,lr,f) \
86 always_inline t t##_word_shift_##lr (t x, int n_words) \
87 { \
88 i32x4 n_bits = {0,0,0,n_words * BITS (it)}; \
89 return (t) __builtin_altivec_##f ((i32x4) x, n_bits); \
90 }
91
Dave Barachc3799992016-08-15 11:12:27 -040092_(u32x4, u32, left, vslo)
93_(i32x4, i32, left, vslo)
94_(u32x4, u32, right, vsro)
95_(i32x4, i32, right, vsro)
96_(u16x8, u16, left, vslo)
97_(i16x8, i16, left, vslo)
98_(u16x8, u16, right, vsro) _(i16x8, i16, right, vsro)
Ed Warnickecb9cada2015-12-08 15:45:58 -070099#undef _
Dave Barachc3799992016-08-15 11:12:27 -0400100 always_inline
101 u32
102 u32x4_get0 (u32x4 x)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103{
104 u32x4_union_t y;
105 y.as_u32x4 = x;
106 return y.as_u32[3];
107}
108
109/* Interleave. */
110#define _(t,it,lh,f) \
111 always_inline t t##_interleave_##lh (t x, t y) \
112 { return (t) __builtin_altivec_##f ((it) x, (it) y); }
113
Dave Barachc3799992016-08-15 11:12:27 -0400114_(u32x4, i32x4, lo, vmrglw)
115_(i32x4, i32x4, lo, vmrglw)
116_(u16x8, i16x8, lo, vmrglh)
117_(i16x8, i16x8, lo, vmrglh)
118_(u32x4, i32x4, hi, vmrghw)
119_(i32x4, i32x4, hi, vmrghw)
120_(u16x8, i16x8, hi, vmrghh) _(i16x8, i16x8, hi, vmrghh)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121#undef _
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122/* Unaligned loads/stores. */
123#ifndef __cplusplus
124#define _(t) \
125 always_inline void t##_store_unaligned (t x, t * a) \
126 { clib_mem_unaligned (a, t) = x; } \
127 always_inline t t##_load_unaligned (t * a) \
128 { return clib_mem_unaligned (a, t); }
Dave Barachc3799992016-08-15 11:12:27 -0400129 _(u8x16) _(u16x8) _(u32x4) _(u64x2) _(i8x16) _(i16x8) _(i32x4) _(i64x2)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130#undef _
131#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132#define _signed_binop(n,m,f,g) \
133 /* Unsigned */ \
134 always_inline u##n##x##m \
135 u##n##x##m##_##f (u##n##x##m x, u##n##x##m y) \
136 { return (u##n##x##m) __builtin_altivec_##g ((i##n##x##m) x, (i##n##x##m) y); } \
137 \
138 /* Signed */ \
139 always_inline i##n##x##m \
140 i##n##x##m##_##f (i##n##x##m x, i##n##x##m y) \
141 { return (i##n##x##m) __builtin_altivec_##g ((i##n##x##m) x, (i##n##x##m) y); }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142/* Compare operations. */
Dave Barachc3799992016-08-15 11:12:27 -0400143 _signed_binop (16, 8, is_equal, vcmpequh)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144_signed_binop (32, 4, is_equal, vcmpequw)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145#undef _signed_binop
Dave Barachc3799992016-08-15 11:12:27 -0400146 always_inline u16x8 u16x8_is_zero (u16x8 x)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147{
Dave Barachc3799992016-08-15 11:12:27 -0400148 u16x8 zero = { 0 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 return u16x8_is_equal (x, zero);
150}
151
Dave Barachc3799992016-08-15 11:12:27 -0400152always_inline u32x4
153u32x4_is_zero (u32x4 x)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154{
Dave Barachc3799992016-08-15 11:12:27 -0400155 u32x4 zero = { 0 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156 return u32x4_is_equal (x, zero);
157}
158
Dave Barachc3799992016-08-15 11:12:27 -0400159always_inline u32
160u32x4_zero_byte_mask (u32x4 x)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161{
162 u32x4 cmp = u32x4_is_zero (x);
163 u32x4 tmp = { 0x000f, 0x00f0, 0x0f00, 0xf000, };
164 cmp &= tmp;
165 cmp |= u32x4_word_shift_right (cmp, 2);
166 cmp |= u32x4_word_shift_right (cmp, 1);
167 return u32x4_get0 (cmp);
168}
169
170#endif /* included_vector_altivec_h */
Dave Barachc3799992016-08-15 11:12:27 -0400171
172/*
173 * fd.io coding-style-patch-verification: ON
174 *
175 * Local Variables:
176 * eval: (c-set-style "gnu")
177 * End:
178 */