00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef CUDYN_TYPETPL_H
00019 #define CUDYN_TYPETPL_H
00020
00021 #include <cudyn/fwd.h>
00022 #include <cufo/fwd.h>
00023 #include <cufo/tagdefs.h>
00024
00025 CU_BEGIN_DECLARATIONS
00026
00027
00028
00029 #ifdef CUCONF_HAVE_LIBFFI
00030 # define CUDYN_LIBFFI_ARG(x) , x
00031 #else
00032 # define CUDYN_LIBFFI_ARG(x)
00033 #endif
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 #define CUDYN_ETYPE_DCLN(NAME, type_t) \
00059 extern cudyn_elmtype_t cudynP_##NAME##_type; \
00060 \
00061 CU_SINLINE cuoo_type_t \
00062 cudyn_##NAME##_type() \
00063 { \
00064 return cudyn_elmtype_to_type(cudynP_##NAME##_type); \
00065 } \
00066 \
00067 CU_SINLINE cu_bool_t \
00068 cudyn_is_##NAME(cuex_t e) \
00069 { \
00070 return cuex_meta(e) == cuoo_type_to_meta(cudyn_##NAME##_type());\
00071 } \
00072 \
00073 CU_SINLINE cuex_t \
00074 cudyn_##NAME(type_t x) \
00075 { \
00076 if (sizeof(type_t) >= sizeof(cu_word_t)) \
00077 return cuoo_halloc(cudyn_##NAME##_type(), sizeof(type_t), &x);\
00078 else { \
00079 struct { \
00080 type_t xp; \
00081 char padding[sizeof(cu_word_t) > sizeof(type_t) \
00082 ? sizeof(cu_word_t) - sizeof(type_t) : 1]; \
00083 } tpl = { x }; \
00084 return cuoo_halloc(cudyn_##NAME##_type(), \
00085 sizeof(cu_word_t), &tpl); \
00086 } \
00087 } \
00088 \
00089 CU_SINLINE type_t \
00090 cudyn_to_##NAME(cuex_t e) \
00091 { \
00092 return *(type_t *)(cu_ptr_add(e, CUOO_HCOBJ_SHIFT)); \
00093 }
00094
00095 #define CUDYN_ETYPE_DEFN(NAME, type_t) \
00096 cudyn_elmtype_t cudynP_##NAME##_type; \
00097 struct cudynP_##NAME##_aligntest { char ch; type_t x; };
00098
00099 #define CUDYN_ETYPE_INIT(NAME, UCNAME, type_t, kind, ffitype) \
00100 cudynP_##NAME##_type = \
00101 cudyn_elmtype_new(CUOO_SHAPE_SCALAR_##UCNAME, \
00102 cudynP_##NAME##_impl, sizeof(type_t), \
00103 offsetof(struct cudynP_##NAME##_aligntest, x) \
00104 CUDYN_LIBFFI_ARG(ffitype)); \
00105 cuoo_prop_define_ptr(cuoo_raw_c_name_prop(), \
00106 cudynP_##NAME##_type, \
00107 cu_idr_by_cstr(#type_t));
00108
00109 #define CUDYN_ETYPE_DEFN_PRINT(NAME, type_t, format) \
00110 CUDYN_ETYPE_DEFN(NAME, type_t) \
00111 \
00112 static void cudynP_##NAME##_print(cuex_t e, FILE *out) \
00113 { fprintf(out, "%"format, cudyn_to_##NAME(e)); } \
00114 \
00115 static void \
00116 cudynP_##NAME##_foprint(cufo_stream_t fos, cufo_prispec_t spec, void *e) \
00117 { cufo_printf(fos, "%<%"format"%>", cufoT_literal, cudyn_to_##NAME(e)); }
00118
00119 #define CUDYN_ETYPE_DEFN_PRINTIND(NAME, type_t, PRI, IND) \
00120 CUDYN_ETYPE_DEFN(NAME, type_t) \
00121 \
00122 static void cudynP_##NAME##_print(cuex_t e, FILE *out) \
00123 { fprintf(out, "%"PRI""IND, cudyn_to_##NAME(e)); } \
00124 \
00125 static void \
00126 cudynP_##NAME##_foprint(cufo_stream_t fos, cufo_prispec_t spec, cuex_t e) \
00127 { \
00128 cufo_printf(fos, "%<%"PRI"%<"IND"%>%>", \
00129 cufoT_literal, cudyn_to_##NAME(e), cufoT_special); \
00130 }
00131
00132
00133
00134
00135 #define CUDYN_ETYPE_ALIAS(NAME, type_t, IMPLNAME) \
00136 CU_SINLINE cuoo_type_t cudyn_##NAME##_type() \
00137 { return cudyn_##IMPLNAME##_type(); } \
00138 CU_SINLINE cu_bool_t cudyn_is_##NAME(cuex_t e) \
00139 { return cudyn_is_##IMPLNAME(e); } \
00140 CU_SINLINE cuex_t cudyn_##NAME(type_t x) \
00141 { return cudyn_##IMPLNAME(x); } \
00142 CU_SINLINE type_t cudyn_to_##NAME(cuex_t e) \
00143 { return (type_t)cudyn_to_##IMPLNAME(e); }
00144
00145 #define cudyn_condsize_none ((size_t)-1)
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 #define CUDYN_ARRTYPE_DCLN(NAME, type_t) \
00172 CU_SINLINE cuoo_type_t \
00173 cudyn_##NAME##arr_type(size_t size) \
00174 { \
00175 return cudyn_arrtype_to_type( \
00176 cudyn_arrtype(cudyn_##NAME##_type(), size)); \
00177 } \
00178 \
00179 size_t cudyn_##NAME##arr_condsize(cuex_t); \
00180 \
00181 CU_SINLINE size_t \
00182 cudyn_##NAME##arr_size(cuex_t ex) \
00183 { \
00184 return cudyn_arrtype_elt_cnt(cudyn_arrtype_from_meta(cuex_meta(ex)));\
00185 } \
00186 \
00187 CU_SINLINE cuex_t \
00188 cudyn_##NAME##arr(size_t cnt, type_t *arr) \
00189 { \
00190 if (sizeof(type_t) >= sizeof(cu_word_t)) \
00191 return cuoo_halloc(cudyn_##NAME##arr_type(cnt), \
00192 sizeof(type_t)*cnt, arr); \
00193 else \
00194 cu_bugf("unimplemented"); \
00195 } \
00196 \
00197 CU_SINLINE type_t * \
00198 cudyn_##NAME##arr_arr(cuex_t e) \
00199 { return (type_t *)(cu_ptr_add(e, CUOO_HCOBJ_SHIFT)); } \
00200 \
00201 CU_SINLINE type_t \
00202 cudyn_##NAME##arr_at(cuex_t e, size_t i) \
00203 { return cudyn_##NAME##arr_arr(e)[i]; }
00204
00205 #define CUDYN_ARRTYPE_DEFN(NAME, type_t) \
00206 size_t \
00207 cudyn_##NAME##arr_condsize(cuex_t e) \
00208 { \
00209 cuex_meta_t meta = cuex_meta(e); \
00210 cudyn_arrtype_t t; \
00211 if (!cuex_meta_is_type(meta) \
00212 ||!cuoo_type_is_arrtype(cuoo_type_from_meta(meta))) \
00213 return cudyn_condsize_none; \
00214 t = cudyn_arrtype_from_meta(meta); \
00215 if (cudyn_arrtype_elt_type(t) != cudyn_##NAME##_type()) \
00216 return cudyn_condsize_none; \
00217 return cudyn_arrtype_elt_cnt(t); \
00218 }
00219
00220 #define CUDYN_ETYPEARR_DCLN(NAME, type_t) \
00221 CUDYN_ETYPE_DCLN(NAME, type_t) \
00222 CUDYN_ARRTYPE_DCLN(NAME, type_t)
00223
00224 #define CUDYN_ETYPEARR_DEFN(NAME, type_t) \
00225 CUDYN_ETYPE_DEFN(NAME, type_t) \
00226 CUDYN_ARRTYPE_DEFN(NAME, type_t)
00227
00228 #define CUDYN_ETYPEARR_INIT CUDYN_ETYPE_INIT
00229
00230 #define CUDYN_ETYPEARR_DEFN_PRINT CUDYN_ETYPE_DEFN_PRINT
00231 #define CUDYN_ETYPEARR_DEFN_PRINTIND CUDYN_ETYPE_DEFN_PRINTIND
00232
00233
00234
00235
00236
00237 #define CUDYN_ETYPEARR_ALIAS(NAME, type_t, IMPLNAME) \
00238 CUDYN_ETYPE_ALIAS(NAME, type_t, IMPLNAME) \
00239 CU_SINLINE cuoo_type_t cudyn_##NAME##arr_type(size_t cnt) \
00240 { return cudyn_##IMPLNAME##arr_type(cnt); } \
00241 CU_SINLINE int cudyn_##NAME##_condsize(cuex_t e) \
00242 { return cudyn_##IMPLNAME##arr_condsize(e); } \
00243 CU_SINLINE cuex_t cudyn_##NAME##arr(size_t cnt, type_t *arr) \
00244 { return cudyn_##IMPLNAME##arr(cnt, (IMPLNAME##_t *)arr); } \
00245 CU_SINLINE type_t *cudyn_##NAME##arr_arr(cuex_t e) \
00246 { return (type_t *)cudyn_##IMPLNAME##arr_arr(e); }
00247
00248
00249 CU_END_DECLARATIONS
00250
00251 #endif