| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* sse2.d - SSE2 vector type support |
| 6 |
* |
| 7 |
* Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya |
| 8 |
* Copyright (c) 1990 Giuseppe Attardi |
| 9 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 10 |
* |
| 11 |
* See file 'LICENSE' for the copyright details. |
| 12 |
* |
| 13 |
*/ |
| 14 |
|
| 15 |
#include <limits.h> |
| 16 |
#include <string.h> |
| 17 |
#include <ecl/ecl.h> |
| 18 |
#define ECL_DEFINE_AET_SIZE |
| 19 |
#include <ecl/internal.h> |
| 20 |
|
| 21 |
#ifdef ECL_SSE2 |
| 22 |
|
| 23 |
/* Predicates */ |
| 24 |
|
| 25 |
cl_object |
| 26 |
si_sse_pack_p(cl_object x) |
| 27 |
{ |
| 28 |
@(return (ECL_SSE_PACK_P(x) ? ECL_T : ECL_NIL)); |
| 29 |
} |
| 30 |
|
| 31 |
/* Element type substitution */ |
| 32 |
|
| 33 |
static void verify_sse_elttype(cl_elttype eltt) { |
| 34 |
switch (eltt) { |
| 35 |
case ecl_aet_sf: |
| 36 |
case ecl_aet_df: |
| 37 |
case ecl_aet_b8: |
| 38 |
case ecl_aet_i8: |
| 39 |
#ifdef ecl_uint16_t |
| 40 |
case ecl_aet_b16: |
| 41 |
case ecl_aet_i16: |
| 42 |
#endif |
| 43 |
#ifdef ecl_uint32_t |
| 44 |
case ecl_aet_b32: |
| 45 |
case ecl_aet_i32: |
| 46 |
#endif |
| 47 |
#ifdef ecl_uint64_t |
| 48 |
case ecl_aet_b64: |
| 49 |
case ecl_aet_i64: |
| 50 |
#endif |
| 51 |
break; /* OK */ |
| 52 |
default: |
| 53 |
FEerror("Invalid element type for an SSE pack: ~S", 1, ecl_elttype_to_symbol(eltt)); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
static |
| 58 |
cl_elttype symbol_to_sse_elttype(cl_object type) { |
| 59 |
cl_elttype eltt = ecl_symbol_to_elttype(type); |
| 60 |
verify_sse_elttype(eltt); |
| 61 |
return eltt; |
| 62 |
} |
| 63 |
|
| 64 |
cl_object |
| 65 |
si_sse_pack_as_elt_type(cl_object x, cl_object type) |
| 66 |
{ |
| 67 |
cl_elttype rtype; |
| 68 |
|
| 69 |
if (ecl_unlikely(!ECL_SSE_PACK_P(x))) { |
| 70 |
FEwrong_type_nth_arg(@[ext::sse-pack-as-elt-type], 1, x, @[ext::sse-pack]); |
| 71 |
} |
| 72 |
|
| 73 |
rtype = symbol_to_sse_elttype(type); |
| 74 |
|
| 75 |
if (x->sse.elttype != rtype) { |
| 76 |
cl_object new = ecl_alloc_object(t_sse_pack); |
| 77 |
new->sse.elttype = rtype; |
| 78 |
new->sse.data.vi = x->sse.data.vi; |
| 79 |
x = new; |
| 80 |
} |
| 81 |
|
| 82 |
@(return x); |
| 83 |
} |
| 84 |
|
| 85 |
cl_object |
| 86 |
si_sse_pack_element_type(cl_object x) |
| 87 |
{ |
| 88 |
if (ecl_unlikely(!ECL_SSE_PACK_P(x))) { |
| 89 |
FEwrong_type_nth_arg(@[ext::sse-pack-element-type], 1, x, @[ext::sse-pack]); |
| 90 |
} |
| 91 |
|
| 92 |
@(return ecl_elttype_to_symbol(x->sse.elttype) ecl_make_fixnum(x->sse.elttype)); |
| 93 |
} |
| 94 |
|
| 95 |
/* Conversion to and from specialized vectors */ |
| 96 |
|
| 97 |
cl_object |
| 98 |
si_sse_pack_to_vector(cl_object x, cl_object elt_type) |
| 99 |
{ |
| 100 |
cl_elttype etype; |
| 101 |
cl_object vec; |
| 102 |
|
| 103 |
if (ecl_unlikely(!ECL_SSE_PACK_P(x))) { |
| 104 |
FEwrong_type_nth_arg(@[ext::sse-pack-to-vector], 1, x, @[ext::sse-pack]); |
| 105 |
} |
| 106 |
|
| 107 |
etype = x->sse.elttype; |
| 108 |
if (elt_type != ECL_NIL) |
| 109 |
etype = symbol_to_sse_elttype(elt_type); |
| 110 |
|
| 111 |
vec = ecl_alloc_simple_vector(16/ecl_aet_size[etype], etype); |
| 112 |
memcpy(vec->vector.self.b8, x->sse.data.b8, 16); |
| 113 |
|
| 114 |
@(return vec); |
| 115 |
} |
| 116 |
|
| 117 |
cl_object |
| 118 |
si_vector_to_sse_pack(cl_object x) |
| 119 |
{ |
| 120 |
cl_object ssev; |
| 121 |
|
| 122 |
if (ecl_unlikely(!ECL_ARRAYP(x))) { |
| 123 |
FEwrong_type_nth_arg(@[ext::vector-to-sse-pack], 1, x, @[array]); |
| 124 |
} |
| 125 |
|
| 126 |
verify_sse_elttype(x->vector.elttype); |
| 127 |
|
| 128 |
if (ecl_unlikely(x->vector.dim * ecl_aet_size[x->vector.elttype] != 16)) |
| 129 |
FEerror("Wrong vector size in VECTOR-TO-SSE-PACK: ~S",1,ecl_make_fixnum(x->vector.dim)); |
| 130 |
|
| 131 |
ssev = ecl_alloc_object(t_sse_pack); |
| 132 |
ssev->sse.elttype = x->vector.elttype; |
| 133 |
memcpy(ssev->sse.data.b8, x->vector.self.b8, 16); |
| 134 |
|
| 135 |
@(return ssev); |
| 136 |
} |
| 137 |
|
| 138 |
/* Boxing and unboxing. |
| 139 |
|
| 140 |
The unboxing primitives accept any kind of sse-pack on purpose. */ |
| 141 |
|
| 142 |
cl_object |
| 143 |
ecl_make_int_sse_pack(__m128i value) |
| 144 |
{ |
| 145 |
cl_object obj = ecl_alloc_object(t_sse_pack); |
| 146 |
obj->sse.elttype = ecl_aet_b8; |
| 147 |
obj->sse.data.vi = value; |
| 148 |
@(return obj); |
| 149 |
} |
| 150 |
|
| 151 |
__m128i |
| 152 |
ecl_unbox_int_sse_pack(cl_object x) |
| 153 |
{ |
| 154 |
do { |
| 155 |
if (ECL_SSE_PACK_P(x)) |
| 156 |
return x->sse.data.vi; |
| 157 |
x = ecl_type_error(@'coerce', "variable", x, @'ext::sse-pack'); |
| 158 |
} while(1); |
| 159 |
} |
| 160 |
|
| 161 |
cl_object |
| 162 |
ecl_make_float_sse_pack(__m128 value) |
| 163 |
{ |
| 164 |
cl_object obj = ecl_alloc_object(t_sse_pack); |
| 165 |
obj->sse.elttype = ecl_aet_sf; |
| 166 |
obj->sse.data.vf = value; |
| 167 |
@(return obj); |
| 168 |
} |
| 169 |
|
| 170 |
__m128 |
| 171 |
ecl_unbox_float_sse_pack(cl_object x) |
| 172 |
{ |
| 173 |
do { |
| 174 |
if (ECL_SSE_PACK_P(x)) |
| 175 |
return x->sse.data.vf; |
| 176 |
x = ecl_type_error(@'coerce', "variable", x, @'ext::sse-pack'); |
| 177 |
} while(1); |
| 178 |
} |
| 179 |
|
| 180 |
cl_object |
| 181 |
ecl_make_double_sse_pack(__m128d value) |
| 182 |
{ |
| 183 |
cl_object obj = ecl_alloc_object(t_sse_pack); |
| 184 |
obj->sse.elttype = ecl_aet_df; |
| 185 |
obj->sse.data.vd = value; |
| 186 |
@(return obj); |
| 187 |
} |
| 188 |
|
| 189 |
__m128d |
| 190 |
ecl_unbox_double_sse_pack(cl_object x) |
| 191 |
{ |
| 192 |
do { |
| 193 |
if (ECL_SSE_PACK_P(x)) |
| 194 |
return x->sse.data.vd; |
| 195 |
x = ecl_type_error(@'coerce', "variable", x, @'ext::sse-pack'); |
| 196 |
} while(1); |
| 197 |
} |
| 198 |
|
| 199 |
#endif /* ECL_SSE2 */ |