summaryrefslogtreecommitdiffstats
path: root/src/vppinfra/vector_altivec.h
blob: 0e9de820ed8924fe6456bbf113ea7c689843cbcc (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * Copyright (c) 2015 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
  Copyright (c) 2009 Eliot Dresselhaus

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef included_vector_altivec_h
#define included_vector_altivec_h

/* Splats. */
#define _(t,n,ti,fi,tr,fr)						\
  always_inline t##x##n t##x##n##_splat (t v)				\
  { return (t##x##n) __builtin_altivec_##fi ((ti) v); }			\
									\
  always_inline t##x##n t##x##n##_splat_word (t##x##n x, int word_index) \
  { return (t##x##n) __builtin_altivec_##fr ((tr) x, word_index); }

#define u16x8_splat(i) ((u16x8) __builtin_altivec_vspltish (i))
#define i16x8_splat(i) ((i16x8) __builtin_altivec_vspltish (i))
#define u32x4_splat(i) ((u32x4) __builtin_altivec_vspltisw (i))
#define i32x4_splat(i) ((i32x4) __builtin_altivec_vspltisw (i))

#define u16x8_splat_word(x,i) ((u16x8) __builtin_altivec_vsplth ((i16x8) (x), (i)))
#define i16x8_splat_word(x,i) ((i16x8) __builtin_altivec_vsplth ((i16x8) (x), (i)))
#define u32x4_splat_word(x,i) ((u32x4) __builtin_altivec_vspltw ((i32x4) (x), (i)))
#define i32x4_splat_word(x,i) ((i32x4) __builtin_altivec_vspltw ((i32x4) (x), (i)))

#undef _

/* 128 bit shifts. */
#define _(t,ti,lr,f)						\
  always_inline t t##_##lr (t x, t y)				\
  { return (t) __builtin_altivec_##f ((ti) x, (ti) y); }	\
								\
  always_inline t t##_i##lr (t x, int i)			\
  {								\
    t j = {i,i,i,i}; \
    return t##_##lr (x, j);					\
  }

_(u16x8, i16x8, shift_left, vslh);
_(u32x4, i32x4, shift_left, vslw);
_(u16x8, i16x8, shift_right, vsrh);
_(u32x4, i32x4, shift_right, vsrw);
_(i16x8, i16x8, shift_right, vsrah);
_(i32x4, i32x4, shift_right, vsraw);
_(u16x8, i16x8, rotate_left, vrlh);
_(i16x8, i16x8, rotate_left, vrlh);
_(u32x4, i32x4, rotate_left, vrlw);
_(i32x4, i32x4, rotate_left, vrlw);

#undef _

#define _(t,it,lr,f)						\
  always_inline t t##_word_shift_##lr (t x, int n_words)	\
  {								\
    i32x4 n_bits = {0,0,0,n_words * BITS (it)};			\
    return (t) __builtin_altivec_##f ((i32x4) x, n_bits);	\
  }

_(u32x4, u32, left, vslo)
_(i32x4, i32, left, vslo)
_(u32x4, u32, right, vsro)
_(i32x4, i32, right, vsro)
_(u16x8, u16, left, vslo)
_(i16x8, i16, left, vslo)
_(u16x8, u16, right, vsro) _(i16x8, i16, right, vsro)
#undef _
     always_inline
       u32
     u32x4_get0 (u32x4 x)
{
  u32x4_union_t y;
  y.as_u32x4 = x;
  return y.as_u32[3];
}

/* Interleave. */
#define _(t,it,lh,f)						\
  always_inline t t##_interleave_##lh (t x, t y)		\
  { return (t) __builtin_altivec_##f ((it) x, (it) y); }

_(u32x4, i32x4, lo, vmrglw)
_(i32x4, i32x4, lo, vmrglw)
_(u16x8, i16x8, lo, vmrglh)
_(i16x8, i16x8, lo, vmrglh)
_(u32x4, i32x4, hi, vmrghw)
_(i32x4, i32x4, hi, vmrghw)
_(u16x8, i16x8, hi, vmrghh) _(i16x8, i16x8, hi, vmrghh)
#undef _
/* Unaligned loads/stores. */
#ifndef __cplusplus
#define _(t)						\
  always_inline void t##_store_unaligned (t x, t * a)	\
  { clib_mem_unaligned (a, t) = x; }			\
  always_inline t t##_load_unaligned (t * a)		\
  { return clib_mem_unaligned (a, t); }
  _(u8x16) _(u16x8) _(u32x4) _(u64x2) _(i8x16) _(i16x8) _(i32x4) _(i64x2)
#undef _
#endif
#define _signed_binop(n,m,f,g)						\
  /* Unsigned */							\
  always_inline u##n##x##m						\
  u##n##x##m##_##f (u##n##x##m x, u##n##x##m y)				\
  { return (u##n##x##m) __builtin_altivec_##g ((i##n##x##m) x, (i##n##x##m) y); } \
									\
  /* Signed */								\
  always_inline i##n##x##m						\
  i##n##x##m##_##f (i##n##x##m x, i##n##x##m y)				\
  { return (i##n##x##m) __builtin_altivec_##g ((i##n##x##m) x, (i##n##x##m) y); }
/* Compare operations. */
  _signed_binop (16, 8, is_equal, vcmpequh)
_signed_binop (32, 4, is_equal, vcmpequw)
#undef _signed_binop
     always_inline u16x8 u16x8_is_zero (u16x8 x)
{
  u16x8 zero = { 0 };
  return u16x8_is_equal (x, zero);
}

always_inline u32x4
u32x4_is_zero (u32x4 x)
{
  u32x4 zero = { 0 };
  return u32x4_is_equal (x, zero);
}

always_inline u32
u32x4_zero_byte_mask (u32x4 x)
{
  u32x4 cmp = u32x4_is_zero (x);
  u32x4 tmp = { 0x000f, 0x00f0, 0x0f00, 0xf000, };
  cmp &= tmp;
  cmp |= u32x4_word_shift_right (cmp, 2);
  cmp |= u32x4_word_shift_right (cmp, 1);
  return u32x4_get0 (cmp);
}

#endif /* included_vector_altivec_h */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
an> cd0 = classify_dpo_get (cd_index0); table_index0 = cd0->cd_table_index; cd_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX]; cd1 = classify_dpo_get (cd_index1); table_index1 = cd1->cd_table_index; t0 = pool_elt_at_index (vcm->tables, table_index0); t1 = pool_elt_at_index (vcm->tables, table_index1); vnet_buffer (b0)->l2_classify.hash = vnet_classify_hash_packet (t0, h0); vnet_classify_prefetch_bucket (t0, vnet_buffer (b0)->l2_classify.hash); vnet_buffer (b1)->l2_classify.hash = vnet_classify_hash_packet (t1, h1); vnet_classify_prefetch_bucket (t1, vnet_buffer (b1)->l2_classify.hash); vnet_buffer (b0)->l2_classify.table_index = table_index0; vnet_buffer (b1)->l2_classify.table_index = table_index1; from += 2; n_left_from -= 2; } while (n_left_from > 0) { vlib_buffer_t *b0; u32 bi0; u8 *h0; u32 cd_index0; classify_dpo_t *cd0; u32 table_index0; vnet_classify_table_t *t0; bi0 = from[0]; b0 = vlib_get_buffer (vm, bi0); h0 = vlib_buffer_get_current (b0) - ethernet_buffer_header_size (b0); cd_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX]; cd0 = classify_dpo_get (cd_index0); table_index0 = cd0->cd_table_index; t0 = pool_elt_at_index (vcm->tables, table_index0); vnet_buffer (b0)->l2_classify.hash = vnet_classify_hash_packet (t0, h0); vnet_buffer (b0)->l2_classify.table_index = table_index0; vnet_classify_prefetch_bucket (t0, vnet_buffer (b0)->l2_classify.hash); from++; n_left_from--; } next_index = node->cached_next_index; from = vlib_frame_vector_args (frame); n_left_from = frame->n_vectors; while (n_left_from > 0) { u32 n_left_to_next; vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); /* Not enough load/store slots to dual loop... */ while (n_left_from > 0 && n_left_to_next > 0) { u32 bi0; vlib_buffer_t *b0; u32 next0 = IP_LOOKUP_NEXT_DROP; u32 table_index0; vnet_classify_table_t *t0; vnet_classify_entry_t *e0; u64 hash0; u8 *h0; /* Stride 3 seems to work best */ if (PREDICT_TRUE (n_left_from > 3)) { vlib_buffer_t *p1 = vlib_get_buffer (vm, from[3]); vnet_classify_table_t *tp1; u32 table_index1; u64 phash1; table_index1 = vnet_buffer (p1)->l2_classify.table_index; if (PREDICT_TRUE (table_index1 != ~0)) { tp1 = pool_elt_at_index (vcm->tables, table_index1); phash1 = vnet_buffer (p1)->l2_classify.hash; vnet_classify_prefetch_entry (tp1, phash1); } } /* speculatively enqueue b0 to the current next frame */ bi0 = from[0]; to_next[0] = bi0; from += 1; to_next += 1; n_left_from -= 1; n_left_to_next -= 1; b0 = vlib_get_buffer (vm, bi0); h0 = b0->data; table_index0 = vnet_buffer (b0)->l2_classify.table_index; e0 = 0; t0 = 0; vnet_buffer (b0)->l2_classify.opaque_index = ~0; if (PREDICT_TRUE (table_index0 != ~0)) { hash0 = vnet_buffer (b0)->l2_classify.hash; t0 = pool_elt_at_index (vcm->tables, table_index0); e0 = vnet_classify_find_entry (t0, h0, hash0, now); if (e0) { vnet_buffer (b0)->l2_classify.opaque_index = e0->opaque_index; vlib_buffer_advance (b0, e0->advance); next0 = (e0->next_index < node->n_next_nodes) ? e0->next_index : next0; hits++; } else { while (1) { if (t0->next_table_index != ~0) t0 = pool_elt_at_index (vcm->tables, t0->next_table_index); else { next0 = (t0->miss_next_index < n_next) ? t0->miss_next_index : next0; misses++; break; } hash0 = vnet_classify_hash_packet (t0, h0); e0 = vnet_classify_find_entry (t0, h0, hash0, now); if (e0) { vnet_buffer (b0)->l2_classify.opaque_index = e0->opaque_index; vlib_buffer_advance (b0, e0->advance); next0 = (e0->next_index < node->n_next_nodes) ? e0->next_index : next0; hits++; chain_hits++; break; } } } } if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) && (b0->flags & VLIB_BUFFER_IS_TRACED))) { ip_classify_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t)); t->next_index = next0; t->table_index = t0 ? t0 - vcm->tables : ~0; t->entry_index = e0 ? e0 - t0->entries : ~0; } /* verify speculative enqueue, maybe switch current next frame */ vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, n_left_to_next, bi0, next0); } vlib_put_next_frame (vm, node, next_index, n_left_to_next); } vlib_node_increment_counter (vm, node->node_index, IP_CLASSIFY_ERROR_MISS, misses); vlib_node_increment_counter (vm, node->node_index, IP_CLASSIFY_ERROR_HIT, hits); vlib_node_increment_counter (vm, node->node_index, IP_CLASSIFY_ERROR_CHAIN_HIT, chain_hits); return frame->n_vectors; } VLIB_NODE_FN (ip4_classify_node) (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { return ip_classify_inline (vm, node, frame, 1 /* is_ip4 */ ); } /* *INDENT-OFF* */ VLIB_REGISTER_NODE (ip4_classify_node) = { .name = "ip4-classify", .vector_size = sizeof (u32), .sibling_of = "ip4-lookup", .format_trace = format_ip_classify_trace, .n_errors = ARRAY_LEN(ip_classify_error_strings), .error_strings = ip_classify_error_strings, .n_next_nodes = 0, }; /* *INDENT-ON* */ VLIB_NODE_FN (ip6_classify_node) (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { return ip_classify_inline (vm, node, frame, 0 /* is_ip4 */ ); } /* *INDENT-OFF* */ VLIB_REGISTER_NODE (ip6_classify_node) = { .name = "ip6-classify", .vector_size = sizeof (u32), .sibling_of = "ip6-lookup", .format_trace = format_ip_classify_trace, .n_errors = ARRAY_LEN(ip_classify_error_strings), .error_strings = ip_classify_error_strings, .n_next_nodes = 0, }; /* *INDENT-ON* */ #ifndef CLIB_MARCH_VARIANT static clib_error_t * ip_classify_init (vlib_main_t * vm) { return 0; } VLIB_INIT_FUNCTION (ip_classify_init); #endif /* CLIB_MARCH_VARIANT */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */