KallistiOS  ##version##
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
fmath_base.h
Go to the documentation of this file.
1 /* KallistiOS ##version##
2 
3  dc/fmath_base.h
4  Copyright (C) 2001 Andrew Kieschnick
5  Copyright (C) 2014 Josh Pearson
6 
7 */
8 
9 #ifndef __DC_FMATH_BASE_H
10 #define __DC_FMATH_BASE_H
11 
12 #include <sys/cdefs.h>
13 __BEGIN_DECLS
14 
15 /**
16  \file dc/fmath_base.h
17  \brief Base definitions for the DC's special math instructions
18  \author Andrew Kieschnick
19  \author Josh Pearson
20 */
21 
22 /** PI constant (if you don't want full math.h) */
23 #define F_PI 3.1415926f
24 
25 /** \cond */
26 #define __fsin(x) \
27  ({ float __value, __arg = (x), __scale = 10430.37835; \
28  __asm__("fmul %2,%1\n\t" \
29  "ftrc %1,fpul\n\t" \
30  "fsca fpul,dr0\n\t" \
31  "fmov fr0,%0" \
32  : "=f" (__value), "+&f" (__scale) \
33  : "f" (__arg) \
34  : "fpul", "fr0", "fr1"); \
35  __value; })
36 
37 #define __fcos(x) \
38  ({ float __value, __arg = (x), __scale = 10430.37835; \
39  __asm__("fmul %2,%1\n\t" \
40  "ftrc %1,fpul\n\t" \
41  "fsca fpul,dr0\n\t" \
42  "fmov fr1,%0" \
43  : "=f" (__value), "+&f" (__scale) \
44  : "f" (__arg) \
45  : "fpul", "fr0", "fr1"); \
46  __value; })
47 
48 #define __ftan(x) \
49  ({ float __value, __arg = (x), __scale = 10430.37835; \
50  __asm__("fmul %2,%1\n\t" \
51  "ftrc %1,fpul\n\t" \
52  "fsca fpul,dr0\n\t" \
53  "fdiv fr1, fr0\n\t" \
54  "fmov fr0,%0" \
55  : "=f" (__value), "+&f" (__scale) \
56  : "f" (__arg) \
57  : "fpul", "fr0", "fr1"); \
58  __value; })
59 
60 
61 #define __fisin(x) \
62  ({ float __value, __arg = (x); \
63  __asm__("lds %1,fpul\n\t" \
64  "fsca fpul,dr0\n\t" \
65  "fmov fr0,%0" \
66  : "=f" (__value) \
67  : "r" (__arg) \
68  : "fpul", "fr0", "fr1"); \
69  __value; })
70 
71 #define __ficos(x) \
72  ({ float __value, __arg = (x); \
73  __asm__("lds %1,fpul\n\t" \
74  "fsca fpul,dr0\n\t" \
75  "fmov fr1,%0" \
76  : "=f" (__value) \
77  : "r" (__arg) \
78  : "fpul", "fr0", "fr1"); \
79  __value; })
80 
81 #define __fitan(x) \
82  ({ float __value, __arg = (x); \
83  __asm__("lds %1,fpul\n\t" \
84  "fsca fpul,dr0\n\t" \
85  "fdiv fr1, fr0\n\t" \
86  "fmov fr0,%0" \
87  : "=f" (__value) \
88  : "r" (__arg) \
89  : "fpul", "fr0", "fr1"); \
90  __value; })
91 
92 #define __fsincos(r, s, c) \
93  ({ register float __r __asm__("fr10") = r; \
94  register float __a __asm__("fr11") = 182.04444443; \
95  __asm__("fmul fr11, fr10\n\t" \
96  "ftrc fr10, fpul\n\t" \
97  "fsca fpul, dr10\n\t" \
98  : "+f" (__r), "+f" (__a) \
99  : "0" (__r), "1" (__a) \
100  : "fpul"); \
101  s = __r; c = __a; })
102 
103 #define __fsincosr(r, s, c) \
104  ({ register float __r __asm__("fr10") = r; \
105  register float __a __asm__("fr11") = 10430.37835; \
106  __asm__("fmul fr11, fr10\n\t" \
107  "ftrc fr10, fpul\n\t" \
108  "fsca fpul, dr10\n\t" \
109  : "+f" (__r), "+f" (__a) \
110  : "0" (__r), "1" (__a) \
111  : "fpul"); \
112  s = __r; c = __a; })
113 
114 #define __fsqrt(x) \
115  ({ float __arg = (x); \
116  __asm__("fsqrt %0\n\t" \
117  : "=f" (__arg) : "0" (__arg)); \
118  __arg; })
119 
120 #define __frsqrt(x) \
121  ({ float __arg = (x); \
122  __asm__("fsrra %0\n\t" \
123  : "=f" (__arg) : "0" (__arg)); \
124  __arg; })
125 
126 /* Floating point inner product (dot product) */
127 #define __fipr(x, y, z, w, a, b, c, d) ({ \
128  register float __x __asm__("fr0") = (x); \
129  register float __y __asm__("fr1") = (y); \
130  register float __z __asm__("fr2") = (z); \
131  register float __w __asm__("fr3") = (w); \
132  register float __a __asm__("fr4") = (a); \
133  register float __b __asm__("fr5") = (b); \
134  register float __c __asm__("fr6") = (c); \
135  register float __d __asm__("fr7") = (d); \
136  __asm__ __volatile__( \
137  "fipr fv4,fv0" \
138  : "+f" (__w) \
139  : "f" (__x), "f" (__y), "f" (__z), "f" (__w), \
140  "f" (__a), "f" (__b), "f" (__c), "f" (__d) \
141  ); \
142  __w; })
143 
144 /* Floating point inner product w/self (square of vector magnitude) */
145 #define __fipr_magnitude_sqr(x, y, z, w) ({ \
146  register float __x __asm__("fr4") = (x); \
147  register float __y __asm__("fr5") = (y); \
148  register float __z __asm__("fr6") = (z); \
149  register float __w __asm__("fr7") = (w); \
150  __asm__ __volatile__( \
151  "fipr fv4,fv4" \
152  : "+f" (__w) \
153  : "f" (__x), "f" (__y), "f" (__z), "f" (__w) \
154  ); \
155  __w; })
156 
157 /** \endcond */
158 __END_DECLS
159 
160 #endif /* !__DC_FMATH_BASE_H */