src/align, src/libale: Add functions for pos/neg inf calculation, and use these in...
[libale.git] / src / libale.h
blob0b8bf952389b7bf4bc99c7a68adc1a8e18f2df56
1 /*
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)
9 * any later version.
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
14 * more details.
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/>.
20 #include <ale.h>
22 #include <stdlib.h>
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) {
36 point p;
38 p.x[0] = x0;
39 p.x[1] = x1;
42 static point point3(ale_pos x0, ale_pos x1, ale_pos x2) {
43 point p;
45 p.x[0] = x0;
46 p.x[1] = x1;
47 p.x[2] = x2;
50 static point point_posinf(int dim) {
51 int d;
52 point p;
54 /* Division logic from ALE d2::point::posinf */
56 ale_pos a = +1;
57 ale_pos z = +0;
59 a = a / z;
61 for (d = 0; d < dim && d < 3; d++)
62 p.x[d] = a;
64 return p;
67 static point point_neginf(int dim) {
68 int d;
69 point p = point_posinf(dim);
71 for (d = 0; d < dim && d < 3; d++)
72 p.x[d] = -p.x[d];
74 return p;
78 * Macros for common API type elements and functions.
81 #define TYPE_COMMON_ELEMENTS \
82 unsigned int refcount;
84 #define TYPE_STRUCTURE(TYPENAME, TYPE_ELEMENTS) \
85 struct _ ## TYPENAME { \
86 TYPE_COMMON_ELEMENTS \
87 TYPE_ELEMENTS \
90 #define TYPE_COMMON_FUNCTIONS(TYPENAME, DESTRUCTOR) \
91 int TYPENAME ## _valid(TYPENAME xarg) { \
92 if (xarg == NULL || xarg->refcount == 0) \
93 return 0; \
94 return 1; \
95 } \
97 int TYPENAME ## _retain(TYPENAME xarg) { \
98 if (!TYPENAME ## _valid(xarg)) \
99 return ALE_UNSPECIFIED_FAILURE; \
100 if (!(xarg->refcount + 1 > 0)) \
101 return ALE_UNSPECIFIED_FAILURE; \
102 xarg->refcount++; \
103 return ALE_SUCCESS; \
106 static TYPENAME TYPENAME ## _alloc() { \
107 TYPENAME retval = (TYPENAME) malloc(sizeof(struct _ ## TYPENAME)); \
108 if (retval) \
109 retval->refcount = 1; \
110 return retval; \
113 static void TYPENAME ## _free(TYPENAME this) { \
114 DESTRUCTOR \
117 int TYPENAME ## _release(TYPENAME xarg) { \
118 if (!TYPENAME ## _valid(xarg)) \
119 return ALE_UNSPECIFIED_FAILURE; \
120 xarg->refcount--; \
121 if (xarg->refcount == 0) {\
122 TYPENAME ## _free(xarg); \
123 free(xarg); \
125 return ALE_SUCCESS; \
128 #define TYPE(TYPENAME, TYPE_ELEMENTS, DESTRUCTOR) \
129 TYPE_STRUCTURE(TYPENAME, TYPE_ELEMENTS) \
130 TYPE_COMMON_FUNCTIONS(TYPENAME, DESTRUCTOR)
133 * Macros for API parameter operations.
136 #define PARAMETER_R(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
137 PARAMETER_TYPE OBJECT_TYPE ## _get_ ## PARAMETER_NAME (OBJECT_TYPE object) { \
138 if (!OBJECT_TYPE ## _valid(object)) \
139 return ((PARAMETER_TYPE) 0); \
140 return object->PARAMETER_NAME; \
143 #define PARAMETER_W(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
144 int OBJECT_TYPE ## _set_ ## PARAMETER_NAME (OBJECT_TYPE object, PARAMETER_TYPE value){ \
145 if (!OBJECT_TYPE ## _valid(object)) \
146 return ALE_UNSPECIFIED_FAILURE; \
147 object->PARAMETER_NAME = value; \
148 return ALE_SUCCESS; \
151 #define PARAMETER_RW(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
152 PARAMETER_R(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
153 PARAMETER_W(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE)
156 * Detemine type size.
159 static int type_size(int type) {
160 if (type == ALE_TYPE_UINT_8)
161 return 1;
162 if (type == ALE_TYPE_UINT_16)
163 return 2;
164 if (type == ALE_TYPE_FLOAT_32 || type == ALE_TYPE_UINT_32)
165 return 4;
166 if (type == ALE_TYPE_FLOAT_64 || type == ALE_TYPE_UINT_64)
167 return 8;