summaryrefslogtreecommitdiff
path: root/core/minute-ia/include/fpu.h
blob: 9261266080b103f6d061b78e6bbace0ae1b4e63f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* Copyright 2016 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* Math utility functions for minute-IA */

#ifndef __CROS_EC_FPU_H
#define __CROS_EC_FPU_H

#include "config.h"

#ifdef CONFIG_FPU

#define M_PI            3.14159265358979323846
#define M_PI_2          1.57079632679489661923

static inline float sqrtf(float v)
{
	float root;

	/* root = fsqart (v); */
	asm volatile(
		"fsqrt"
		: "=t" (root)
		: "0" (v)
	);
	return root;
}

/* Absolute value of V. */
static inline float fabsf(float v)
{
	float root;

	/* root = fabs (v); */
	asm volatile(
		"fabs"
		: "=t" (root)
		: "0" (v)
	);
	return root;
}

/**
 * Natural logarithm of V.
 *
 * @return ln(v)
 */
static inline float logf(float v)
{
	float res;

	asm volatile(
		"fldln2\n"
		"fxch\n"
		"fyl2x\n"
		: "=t" (res)
		: "0" (v));

	return res;
}

/**
 * Exponential function of V.
 *
 * @return e**v
 */
static inline float expf(float v)
{
	float res;

	asm volatile(
		"fldl2e\n"
		"fmulp\n"
		"fld %%st(0)\n"
		"frndint\n"
		"fsubr %%st(0),%%st(1)\n" /* bug-binutils/19054 */
		"fxch %%st(1)\n"
		"f2xm1\n"
		"fld1\n"
		"faddp\n"
		"fscale\n"
		"fstp %%st(1)\n"
		: "=t" (res)
		: "0" (v));

	return res;
}

/**
 * X to the Y power.
 *
 * @return x**y
 */
static inline float powf(float x, float y)
{
	float res;

	asm volatile(
		"fyl2x\n"
		"fld %%st(0)\n"
		"frndint\n"
		"fsub %%st,%%st(1)\n"
		"fxch\n"
		"fchs\n"
		"f2xm1\n"
		"fld1\n"
		"faddp\n"
		"fxch\n"
		"fld1\n"
		"fscale\n"
		"fstp %%st(1)\n"
		"fmulp\n"
		: "=t" (res)
		: "0" (x), "u" (y)
		: "st(1)");

	return res;
}

/* Smallest integral value not less than V.  */
static inline float ceilf(float v)
{
	float res;
	unsigned short control_word, control_word_tmp;

	asm volatile("fnstcw %0" : "=m" (control_word));
	/* Set Rounding Mode to 10B, round up toward +infinity */
	control_word_tmp = (control_word | 0x0800) & 0xfbff;
	asm volatile(
		"fld %3\n"
		"fldcw %1\n"
		"frndint\n"
		"fldcw %2"
		: "=t" (res)
		: "m" (control_word_tmp), "m"(control_word), "m" (v));

	return res;
}

/* Arc tangent of Y/X.  */
static inline float atan2f(float y, float x)
{
	float res;

	asm volatile("fpatan" : "=t" (res) : "0" (x), "u" (y) : "st(1)");

	return res;
}

/* Arc tangent of V. */
static inline float atanf(float v)
{
	float res;

	asm volatile(
		"fld1\n"
		"fpatan\n"
		: "=t" (res)
		: "0" (v));

	return res;
}

/* Sine of V. */
static inline float sinf(float v)
{
	float res;

	asm volatile("fsin" : "=t" (res) : "0" (v));

	return res;
}

/* Cosine of V. */
static inline float cosf(float v)
{
	float res;

	asm volatile("fcos" : "=t" (res) : "0" (v));

	return res;
}

/* Arc cosine of V. */
static inline float acosf(float v)
{
	return atan2f(sqrtf(1.0 - v * v), v);
}

#define COND_FP_NAN      0x0100
#define COND_FP_SIGNBIT  0x0200
#define COND_FP_NORMAL   0x0400
#define COND_FP_ZERO     0x4000
#define COND_FP_INFINITE (COND_FP_NAN | COND_FP_NORMAL)

/* Check if V is NaN (not-a-number).  */
static inline int __isnanf(float v)
{
	uint16_t stat;

	asm volatile(
		"fxam\n"
		"fnstsw %0\n"
		: "=r" (stat)
		: "0" (v));

	return (stat & (COND_FP_NAN | COND_FP_NORMAL | COND_FP_ZERO))
	       == COND_FP_NAN;
}

/**
 * Check if V is infinite.
 *
 * @return 0 if V is finite or NaN.
 * @return +1 if V is +infinite.
 * @return -1 if V is -infinite.
 */
static inline int __isinff(float v)
{
	uint16_t stat;

	asm volatile(
		"fxam\n"
		"fnstsw %0\n"
		: "=r" (stat)
		: "v" (v));

	if ((stat & (COND_FP_NAN | COND_FP_NORMAL | COND_FP_ZERO)) ==
	    COND_FP_INFINITE) {
		/* Infinite number, check sign */
		return stat & COND_FP_SIGNBIT ? -1 : 1;
	}

	/* Finite or NaN */
	return 0;
}

#endif  /* CONFIG_FPU */
#endif  /* __CROS_EC_FPU_H */