9 typedef int v4si
__attribute__ ((vector_size (16)));
10 typedef unsigned int v4ui
__attribute__ ((vector_size (16)));
11 typedef float v4f
__attribute__ ((vector_size (16)));
14 create_vec_fn (gcc_jit_context
*ctxt
, const char *fnname
,
15 gcc_jit_type
*vec_type
,
16 gcc_jit_type
*elem_type
,
17 enum gcc_jit_binary_op op
)
19 /* Create equivalent to:
22 FNNAME (V *dst, const V *lhs, E p, E q, E r, E s)
29 where V is "vec_type" (e.g. v4si)
30 and E is "elem_type" (e.g. int). */
32 gcc_jit_type
*ptr_type
= gcc_jit_type_get_pointer (vec_type
);
34 gcc_jit_type
*const_type
= gcc_jit_type_get_const (vec_type
);
35 gcc_jit_type
*ptr_to_const_type
= gcc_jit_type_get_pointer (const_type
);
38 gcc_jit_context_new_param (ctxt
, NULL
, ptr_type
, "dst");
40 gcc_jit_context_new_param (ctxt
, NULL
, ptr_to_const_type
, "lhs");
42 gcc_jit_context_new_param (ctxt
, NULL
, elem_type
, "p");
44 gcc_jit_context_new_param (ctxt
, NULL
, elem_type
, "q");
46 gcc_jit_context_new_param (ctxt
, NULL
, elem_type
, "r");
48 gcc_jit_context_new_param (ctxt
, NULL
, elem_type
, "s");
50 gcc_jit_type
*return_type
=
51 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_VOID
);
53 gcc_jit_param
*params
[6] = {dst
, lhs
, p
, q
, r
, s
};
54 gcc_jit_function
*func
=
55 gcc_jit_context_new_function (ctxt
, NULL
,
56 GCC_JIT_FUNCTION_EXPORTED
,
60 gcc_jit_block
*initial
=
61 gcc_jit_function_new_block (func
, "initial");
65 = gcc_jit_function_new_local (func
, NULL
,
68 /* pqrs = {p, q, r, s}; */
69 gcc_jit_rvalue
*elems
[4];
70 elems
[0] = gcc_jit_param_as_rvalue (p
);
71 elems
[1] = gcc_jit_param_as_rvalue (q
);
72 elems
[2] = gcc_jit_param_as_rvalue (r
);
73 elems
[3] = gcc_jit_param_as_rvalue (s
);
74 gcc_jit_block_add_assignment (
77 gcc_jit_context_new_rvalue_from_vector (ctxt
, NULL
, vec_type
, 4, elems
));
80 gcc_jit_rvalue
*op_result
=
81 gcc_jit_context_new_binary_op (
85 gcc_jit_lvalue_as_rvalue (gcc_jit_rvalue_dereference (gcc_jit_param_as_rvalue (lhs
),
87 gcc_jit_lvalue_as_rvalue (pqrs
));
88 /* *dst = *lhs OP pqrs; */
89 gcc_jit_block_add_assignment (
91 gcc_jit_rvalue_dereference (gcc_jit_param_as_rvalue (dst
), NULL
),
93 gcc_jit_block_end_with_void_return (initial
, NULL
);
97 create_code (gcc_jit_context
*ctxt
, void *user_data
)
99 gcc_jit_type
*int_type
= gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_INT
);
100 gcc_jit_type
*unsigned_type
101 = gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_UNSIGNED_INT
);
102 gcc_jit_type
*float_type
= gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_FLOAT
);
104 gcc_jit_type
*v4si_type
= gcc_jit_type_get_vector (int_type
, 4);
105 gcc_jit_type
*v4ui_type
= gcc_jit_type_get_vector (unsigned_type
, 4);
106 gcc_jit_type
*v4f_type
= gcc_jit_type_get_vector (float_type
, 4);
108 create_vec_fn (ctxt
, "jit_v4si_add",
109 v4si_type
, int_type
, GCC_JIT_BINARY_OP_PLUS
);
110 create_vec_fn (ctxt
, "jit_v4si_sub",
111 v4si_type
, int_type
, GCC_JIT_BINARY_OP_MINUS
);
112 create_vec_fn (ctxt
, "jit_v4si_mult",
113 v4si_type
, int_type
, GCC_JIT_BINARY_OP_MULT
);
114 create_vec_fn (ctxt
, "jit_v4si_div",
115 v4si_type
, int_type
, GCC_JIT_BINARY_OP_DIVIDE
);
117 create_vec_fn (ctxt
, "jit_v4ui_add",
118 v4ui_type
, unsigned_type
, GCC_JIT_BINARY_OP_PLUS
);
119 create_vec_fn (ctxt
, "jit_v4ui_sub",
120 v4ui_type
, unsigned_type
, GCC_JIT_BINARY_OP_MINUS
);
121 create_vec_fn (ctxt
, "jit_v4ui_mult",
122 v4ui_type
, unsigned_type
, GCC_JIT_BINARY_OP_MULT
);
123 create_vec_fn (ctxt
, "jit_v4ui_div",
124 v4ui_type
, unsigned_type
, GCC_JIT_BINARY_OP_DIVIDE
);
126 create_vec_fn (ctxt
, "jit_v4f_add",
127 v4f_type
, float_type
, GCC_JIT_BINARY_OP_PLUS
);
128 create_vec_fn (ctxt
, "jit_v4f_sub",
129 v4f_type
, float_type
, GCC_JIT_BINARY_OP_MINUS
);
130 create_vec_fn (ctxt
, "jit_v4f_mult",
131 v4f_type
, float_type
, GCC_JIT_BINARY_OP_MULT
);
132 create_vec_fn (ctxt
, "jit_v4f_div",
133 v4f_type
, float_type
, GCC_JIT_BINARY_OP_DIVIDE
);
136 template <typename V
>
138 check_add (const V
&a
, const V
&b
, const V
&c
)
140 for (int i
= 0; i
< 4; i
++)
141 CHECK_VALUE (c
[i
], a
[i
] + b
[i
]);
144 template <typename V
>
146 check_sub (const V
&a
, const V
&b
, const V
&c
)
148 for (int i
= 0; i
< 4; i
++)
149 CHECK_VALUE (c
[i
], a
[i
] - b
[i
]);
152 template <typename V
>
154 check_mult (const V
&a
, const V
&b
, const V
&c
)
156 for (int i
= 0; i
< 4; i
++)
157 CHECK_VALUE (c
[i
], a
[i
] * b
[i
]);
160 template <typename V
>
162 check_div (const V
&a
, const V
&b
, const V
&c
)
164 for (int i
= 0; i
< 4; i
++)
165 CHECK_VALUE (c
[i
], a
[i
] / b
[i
]);
168 template <typename V
, typename E
>
170 verify_vec_code (gcc_jit_context
*ctxt
, gcc_jit_result
*result
,
172 void (*check_cb
) (const V
&a
, const V
&b
, const V
&c
))
174 typedef void (*binop_type
) (const V
*a
, V
*b
, E p
, E q
, E r
, E s
);
175 CHECK_NON_NULL (result
);
177 (binop_type
)gcc_jit_result_get_code (result
, fnname
);
183 for (int i
= 0; i
< 4; i
++)
186 pqrs
[i
] = (i
+ 4) * 3;
189 /* Run jit-compiled code and verify result. */
190 fn (&dst
, &lhs
, pqrs
[0], pqrs
[1], pqrs
[2], pqrs
[3]);
191 check_cb (lhs
, pqrs
, dst
);
195 verify_code (gcc_jit_context
*ctxt
, gcc_jit_result
*result
)
197 verify_vec_code
<v4si
, int> (ctxt
, result
, "jit_v4si_add", check_add
);
198 verify_vec_code
<v4si
, int> (ctxt
, result
, "jit_v4si_sub", check_sub
);
199 verify_vec_code
<v4si
, int> (ctxt
, result
, "jit_v4si_mult", check_mult
);
200 verify_vec_code
<v4si
, int> (ctxt
, result
, "jit_v4si_div", check_div
);
202 verify_vec_code
<v4ui
, unsigned int> (ctxt
, result
, "jit_v4ui_add", check_add
);
203 verify_vec_code
<v4ui
, unsigned int> (ctxt
, result
, "jit_v4ui_sub", check_sub
);
204 verify_vec_code
<v4ui
, unsigned int> (ctxt
, result
, "jit_v4ui_mult", check_mult
);
205 verify_vec_code
<v4ui
, unsigned int> (ctxt
, result
, "jit_v4ui_div", check_div
);
207 verify_vec_code
<v4f
, float> (ctxt
, result
, "jit_v4f_add", check_add
);
208 verify_vec_code
<v4f
, float> (ctxt
, result
, "jit_v4f_sub", check_sub
);
209 verify_vec_code
<v4f
, float> (ctxt
, result
, "jit_v4f_mult", check_mult
);
210 verify_vec_code
<v4f
, float> (ctxt
, result
, "jit_v4f_div", check_div
);