2 * LIBOIL - Library of Optimized Inner Loops
3 * Copyright (c) 2003,2004 David A. Schleef <ds@schleef.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
28 #ifndef _LIBOIL_FUNCTION_H_
29 #define _LIBOIL_FUNCTION_H_
31 #include <liboil/liboilutils.h>
32 #include <liboil/liboiltypes.h>
35 * OIL_CHECK_PROTOTYPE:
38 * Macro used internally to implement the --enable-prototype-checking
41 #ifdef LIBOIL_STRICT_PROTOTYPES
42 #include <liboil/liboilfuncs.h>
43 #define OIL_CHECK_PROTOTYPE(a) a
45 #define OIL_CHECK_PROTOTYPE(a)
53 * Implementation flags.
55 * @OIL_IMPL_FLAG_REF: is the reference implementation for the class.
57 * @OIL_IMPL_FLAG_OPT: was compiled with alternate CFLAGS as specified
58 * by --enable-alternate-optimization.
60 * @OIL_IMPL_FLAG_ASM: is written in assembly code.
62 * @OIL_IMPL_FLAG_DISABLED: is disabled. This can be set either in the
63 * source code or during library initialization.
65 * @OIL_IMPL_FLAG_CMOV: uses the i386 instruction cmov or its variants.
67 * @OIL_IMPL_FLAG_MMX: uses MMX instructions.
69 * @OIL_IMPL_FLAG_SSE: uses SSE instructions.
71 * @OIL_IMPL_FLAG_MMXEXT: uses AMD's extended MMX instructions. These
72 * are a subset of what Intel calls SSE2. If an implementation uses
73 * only AMD's extended MMX instructions, it should set this flag, and
74 * not @OIL_IMPL_FLAG_SSE2.
76 * @OIL_IMPL_FLAG_SSE2: uses SSE2 instructions. This flag implies
77 * @OIL_IMPL_FLAG_SSE and @OIL_IMPL_FLAG_MMXEXT.
79 * @OIL_IMPL_FLAG_3DNOW: uses 3DNow! instructions.
81 * @OIL_IMPL_FLAG_3DNOWEXT: uses extended 3DNow! instructions.
83 * @OIL_IMPL_FLAG_SSE3: uses SSE3 instructions. This flag implies
84 * @OIL_IMPL_FLAG_SSE2.
86 * @OIL_IMPL_FLAG_SSSE3: uses SSSE3 instructions. This flag implies
87 * @OIL_IMPL_FLAG_SSE3.
89 * @OIL_IMPL_FLAG_ALTIVEC: uses Altivec instructions.
93 OIL_IMPL_FLAG_REF = (1<<0),
94 OIL_IMPL_FLAG_OPT = (1<<1),
95 OIL_IMPL_FLAG_ASM = (1<<2),
96 OIL_IMPL_FLAG_DISABLED = (1<<3),
97 OIL_IMPL_FLAG_CMOV = (1<<16),
98 OIL_IMPL_FLAG_MMX = (1<<17),
99 OIL_IMPL_FLAG_SSE = (1<<18),
100 OIL_IMPL_FLAG_MMXEXT = (1<<19),
101 OIL_IMPL_FLAG_SSE2 = (1<<20),
102 OIL_IMPL_FLAG_3DNOW = (1<<21),
103 OIL_IMPL_FLAG_3DNOWEXT = (1<<22),
104 OIL_IMPL_FLAG_SSE3 = (1<<23),
105 OIL_IMPL_FLAG_ALTIVEC = (1<<24),
106 OIL_IMPL_FLAG_EDSP = (1<<25),
107 OIL_IMPL_FLAG_ARM6 = (1<<26),
108 OIL_IMPL_FLAG_VFP = (1<<27),
109 OIL_IMPL_FLAG_SSSE3 = (1<<28)
112 #ifdef OIL_ENABLE_UNSTABLE_API
117 * Used internally to implement the --enable-alternate-optimizations
121 * OIL_OPT_FLAG_MANGLE:
123 * Used internally to implement the --enable-alternate-optimizations
129 * Used internally to implement the --enable-alternate-optimizations
135 * Used internally to implement the --enable-alternate-optimizations
138 #ifndef OIL_OPT_MANGLE
139 #define OIL_OPT_MANGLE(a) a
140 #define OIL_OPT_FLAG_MANGLE(a) a
142 #define OIL_NO_CLASSES
143 #define OIL_OPT_FLAG_MANGLE(a) (((a)&(~OIL_IMPL_FLAG_REF)) | OIL_IMPL_FLAG_OPT)
145 #ifndef OIL_OPT_SUFFIX
146 #define OIL_OPT_SUFFIX
152 * An opaque structure representing a function class.
155 struct _OilFunctionClass {
160 OilTestFunction test_func;
162 OilFunctionImpl *first_impl;
163 OilFunctionImpl *reference_impl;
165 OilFunctionImpl *chosen_impl;
167 const char *prototype;
173 * An opaque structure representing a function implementation.
176 struct _OilFunctionImpl {
179 OilFunctionClass *klass;
193 * Offsets @ptr by @offset number of bytes, and dereferences it
194 * as type @type. Note that the offset is in bytes, and not in
195 * the size of the pointer type.
197 #define OIL_GET(ptr, offset, type) (*(type *)((uint8_t *)(ptr) + (offset)) )
203 * Add @offset bytes to the pointer @ptr.
205 #define OIL_OFFSET(ptr, offset) ((void *)((uint8_t *)(ptr) + (offset)) )
211 * Increments the pointer @ptr by @offset number of bytes.
213 #define OIL_INCREMENT(ptr, offset) (ptr = (void *)((uint8_t *)ptr + (offset)) )
218 * Mask describing which bits in #OilImplFlag depend on the current
221 #define OIL_CPU_FLAG_MASK 0xffff0000
225 * @klass: the name of a function class (without the oil_ prefix)
227 * Declares the Liboil function class @klass.
229 #define OIL_DECLARE_CLASS(klass) \
230 extern OilFunctionClass _oil_function_class_ ## klass
233 * SECTION:liboilmacros
235 * @short_description: Macros
238 #ifndef OIL_NO_CLASSES
240 * OIL_DEFINE_CLASS_FULL:
241 * @klass: name of class to declare (without oil_ prefix)
242 * @string: prototype of class
243 * @test: test function
245 * Defines a #OilFunctionClass structure for @klass. Classes
246 * defined this way will be automatically at Liboil initialization
249 #define OIL_DEFINE_CLASS_FULL(klass, string, test) \
250 OilFunctionClass _oil_function_class_ ## klass = { \
260 OilFunctionClass *oil_function_class_ptr_ ## klass = \
261 &_oil_function_class_ ## klass
263 #define OIL_DEFINE_CLASS_FULL(klass, string, test) \
264 OIL_DECLARE_CLASS(klass)
269 * @klass: name of class to declare (without oil_ prefix)
270 * @string: prototype of class
272 * Defines a #OilFunctionClass structure for @klass. Classes
273 * defined this way will be automatically at Liboil initialization
276 #define OIL_DEFINE_CLASS(klass, string) \
277 OIL_DEFINE_CLASS_FULL (klass, string, NULL)
280 * OIL_DEFINE_IMPL_FULL:
281 * @function: name of function
282 * @klass: name of class to declare (without oil_ prefix)
283 * @flags: implementation flags and CPU requirements
285 * Defines a #OilFunctionImpl structure for the function @function
286 * and class @klass. CPU-dependent flags in @flags will indicate
287 * that this implementation requires the given CPU flags.
289 #define OIL_DEFINE_IMPL_FULL(function,klass,flags) \
290 OilFunctionImpl OIL_OPT_MANGLE(_oil_function_impl_ ## function) = { \
292 &_oil_function_class_ ## klass , \
293 (void (*)(void)) function, \
294 OIL_OPT_FLAG_MANGLE(flags), \
295 #function OIL_OPT_SUFFIX \
297 OIL_CHECK_PROTOTYPE(;_oil_type_ ## klass _ignore_me_ ## function = function)
301 * @function: name of function
302 * @klass: name of class to declare (without oil_ prefix)
304 * Shorthand for defining a C implementation. See OIL_DEFINE_IMPL_FULL().
306 #define OIL_DEFINE_IMPL(function,klass) \
307 OIL_DEFINE_IMPL_FULL(function,klass,0)
309 * OIL_DEFINE_IMPL_REF:
310 * @function: name of function
311 * @klass: name of class to declare (without oil_ prefix)
313 * Shorthand for defining a reference implementation. See OIL_DEFINE_IMPL_FULL().
315 #define OIL_DEFINE_IMPL_REF(function,klass) \
316 OIL_DEFINE_IMPL_FULL(function,klass,OIL_IMPL_FLAG_REF)
318 * OIL_DEFINE_IMPL_ASM:
319 * @function: name of function
320 * @klass: name of class to declare (without oil_ prefix)
322 * Shorthand for defining an implementation written in inline
323 * assembly code. See OIL_DEFINE_IMPL_FULL().
325 #define OIL_DEFINE_IMPL_ASM(function,klass) \
326 OIL_DEFINE_IMPL_FULL(function,klass,OIL_IMPL_FLAG_ASM)
328 * OIL_DEFINE_IMPL_DEPENDS
329 * @function: name of function
330 * @klass: name of class to declare (without oil_ prefix)
331 * @...: other classes this implementation uses
333 * Shorthand for defining an implementation that uses another Liboil
334 * function class. This is not currently used. See
335 * OIL_DEFINE_IMPL_FULL().
337 #define OIL_DEFINE_IMPL_DEPENDS(function,klass,...) \
338 OIL_DEFINE_IMPL_FULL(function,klass,0)
340 void oil_optimize_all (void);
341 void oil_optimize (const char *class_name);
343 OilFunctionClass * oil_class_get_by_index (int i);
344 OilFunctionClass *oil_class_get (const char *class_name);
345 void oil_class_optimize (OilFunctionClass *klass);
346 int oil_class_get_n_classes (void);
348 OilFunctionImpl * oil_impl_get_by_index (int i);
349 int oil_impl_is_runnable (OilFunctionImpl *impl);
350 int oil_impl_is_usable (OilFunctionImpl *impl);
352 void oil_class_choose_by_name (OilFunctionClass * klass, const char *name);
354 void oil_class_register_impl_full (OilFunctionClass * klass,
355 void (*func)(void), const char *name, unsigned int flags);
356 void oil_class_register_impl (OilFunctionClass * klass, OilFunctionImpl *impl);
357 void oil_class_register_impl_by_name (const char *klass_name,
358 OilFunctionImpl *impl);
360 void oil_init_no_optimize(void);