2 * Copyright 2008, 2009 David Hilvert <dhilvert@gmail.com>
4 * This file is part of libale.
6 * libale is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU Affero General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
11 * libale is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
16 * You should have received a copy of the GNU Affero General Public License
17 * along with libale. If not, see <http://www.gnu.org/licenses/>.
25 * Abstract types used internally by host code (cf. ale_context_get_*_type for
26 * preferred computational types).
29 typedef double ale_pos
;
30 typedef double ale_accum
;
31 typedef double ale_real
;
33 typedef struct { ale_pos x
[3]; } point
;
35 static point
point2(ale_pos x0
, ale_pos x1
) {
43 static point
point3(ale_pos x0
, ale_pos x1
, ale_pos x2
) {
51 static point
point_posinf(int dim
) {
55 /* Division logic from ALE d2::point::posinf */
62 for (d
= 0; d
< 3; d
++)
63 p
.x
[d
] = (d
< dim
) ? a
: 0;
68 static point
point_neginf(int dim
) {
70 point p
= point_posinf(dim
);
72 for (d
= 0; d
< dim
&& d
< 3; d
++)
79 * Macro for obtaining a pointer from ALE_POINTER type.
85 * Macro for obtaining a pointer to host-mapped memory from an ALE_POINTER
86 * type to device memory.
89 #warning map macro not yet defined
90 #define Q(x) /* not yet defined */
93 * Macro for unmapping a previously mapped ALE_POINTER region
96 #warning unmap macro not yet defined
97 #define R(x) /* not yet defined */
100 * Macro for NULL ALE_POINTER.
103 #define N(TYPEP) TYPEP ## _NULL()
106 * Macros for common API type elements and functions.
110 * We do our own reference counting, since there's no obvious
111 * way to perform destruction operations in the case of CL
112 * reference counts. (E.g., the spec explicitly states that
113 * obtained reference count values are unsuitable for uses
114 * other than debugging.
117 #define TYPE_COMMON_ELEMENTS \
118 unsigned int refcount;
120 #define TYPE_STRUCTURE(TYPENAME, TYPE_ELEMENTS) \
121 struct _ ## TYPENAME { \
122 TYPE_COMMON_ELEMENTS \
126 #define TYPE_HOST_COMMON_FUNCTIONS(TYPENAME, DESTRUCTOR) \
127 int TYPENAME ## _valid(TYPENAME xarg) { \
128 if (P(xarg) == NULL || P(xarg)->refcount == 0) \
133 int TYPENAME ## _retain(TYPENAME xarg) { \
134 if (!TYPENAME ## _valid(xarg)) \
135 return ALE_UNSPECIFIED_FAILURE; \
136 if (!(P(xarg)->refcount + 1 > 0)) \
137 return ALE_UNSPECIFIED_FAILURE; \
138 P(xarg)->refcount++; \
139 return ALE_SUCCESS; \
142 static TYPENAME TYPENAME ## _alloc() { \
144 P(retval) = (struct _ ## TYPENAME *) malloc(sizeof(struct _ ## TYPENAME)); \
146 P(retval)->refcount = 1; \
150 static void TYPENAME ## _free(TYPENAME this) { \
154 int TYPENAME ## _release(TYPENAME xarg) { \
155 if (!TYPENAME ## _valid(xarg)) \
156 return ALE_UNSPECIFIED_FAILURE; \
157 P(xarg)->refcount--; \
158 if (P(xarg)->refcount == 0) {\
159 TYPENAME ## _free(xarg); \
162 return ALE_SUCCESS; \
165 #warning TYPE_COMMON_FUNCTIONS not yet differentiated from TYPE_HOST_COMMON_FUNCTIONS
166 #define TYPE_COMMON_FUNCTIONS(TYPENAME, DESTRUCTOR) \
167 int TYPENAME ## _valid(TYPENAME xarg) { \
168 if (P(xarg) == NULL || P(xarg)->refcount == 0) \
173 int TYPENAME ## _retain(TYPENAME xarg) { \
174 if (!TYPENAME ## _valid(xarg)) \
175 return ALE_UNSPECIFIED_FAILURE; \
176 if (!(P(xarg)->refcount + 1 > 0)) \
177 return ALE_UNSPECIFIED_FAILURE; \
178 P(xarg)->refcount++; \
179 return ALE_SUCCESS; \
182 static TYPENAME TYPENAME ## _alloc() { \
184 P(retval) = (struct _ ## TYPENAME *) malloc(sizeof(struct _ ## TYPENAME)); \
186 P(retval)->refcount = 1; \
190 static void TYPENAME ## _free(TYPENAME this) { \
194 int TYPENAME ## _release(TYPENAME xarg) { \
195 if (!TYPENAME ## _valid(xarg)) \
196 return ALE_UNSPECIFIED_FAILURE; \
197 P(xarg)->refcount--; \
198 if (P(xarg)->refcount == 0) {\
199 TYPENAME ## _free(xarg); \
202 return ALE_SUCCESS; \
205 #define TYPE_HOST(TYPENAME, TYPE_ELEMENTS, DESTRUCTOR) \
206 TYPE_STRUCTURE(TYPENAME, TYPE_ELEMENTS) \
207 TYPE_HOST_COMMON_FUNCTIONS(TYPENAME, DESTRUCTOR)
209 #define TYPE(TYPENAME, TYPE_ELEMENTS, DESTRUCTOR) \
210 ALE_DATA_TYPE(_ ## TYPENAME ## _def, TYPE_STRUCTURE(TYPENAME, TYPE_ELEMENTS) ) \
211 TYPE_COMMON_FUNCTIONS(TYPENAME, DESTRUCTOR)
214 * Macros for API parameter operations.
217 #define PARAMETER_R(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
218 PARAMETER_TYPE OBJECT_TYPE ## _get_ ## PARAMETER_NAME (OBJECT_TYPE object) { \
219 if (!OBJECT_TYPE ## _valid(object)) \
220 return ((PARAMETER_TYPE) 0); \
221 return object->PARAMETER_NAME; \
224 #define PARAMETER_W(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
225 int OBJECT_TYPE ## _set_ ## PARAMETER_NAME (OBJECT_TYPE object, PARAMETER_TYPE value){ \
226 if (!OBJECT_TYPE ## _valid(object)) \
227 return ALE_UNSPECIFIED_FAILURE; \
228 object->PARAMETER_NAME = value; \
229 return ALE_SUCCESS; \
232 #define PARAMETER_RW(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
233 PARAMETER_R(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
234 PARAMETER_W(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE)
237 * Detemine type size.
240 static int type_size(int type
) {
241 if (type
== ALE_TYPE_UINT_8
)
243 if (type
== ALE_TYPE_UINT_16
)
245 if (type
== ALE_TYPE_FLOAT_32
|| type
== ALE_TYPE_UINT_32
)
247 if (type
== ALE_TYPE_FLOAT_64
|| type
== ALE_TYPE_UINT_64
)