summaryrefslogtreecommitdiff
path: root/usr/src/lib/libm/sparc/src/libm_inlines.h
blob: bd4a463e7ae5a714cecf1ec89e69e0b53e795b62 (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Copyright 2011, Richard Lowe.
 */

#ifndef _LIBM_INLINES_H
#define	_LIBM_INLINES_H

#ifdef __GNUC__

#include <sys/types.h>
#include <sys/ieeefp.h>

#ifdef __cplusplus
extern "C" {
#endif

extern __GNU_INLINE double
__inline_sqrt(double d)
{
	double ret;

	__asm__ __volatile__("fsqrtd %1,%0\n\t" : "=e" (ret) : "e" (d));
	return (ret);
}

extern __GNU_INLINE float
__inline_sqrtf(float f)
{
	float ret;

	__asm__ __volatile__("fsqrts %1,%0\n\t" : "=f" (ret) : "f" (f));
	return (ret);
}

extern __GNU_INLINE enum fp_class_type
fp_classf(float f)
{
	enum fp_class_type ret;
	uint32_t tmp;

	/* XXX: Separate input and output */
	__asm__ __volatile__(
	    "sethi  %%hi(0x80000000),%1\n\t"
	    "andncc %2,%1,%0\n\t"
	    "bne    1f\n\t"
	    "nop\n\t"
	    "mov    0,%0\n\t"
	    "ba	2f\n\t"		/* x is 0 */
	    "nop\n\t"
	    "1:\n\t"
	    "sethi  %%hi(0x7f800000),%1\n\t"
	    "andcc  %0,%1,%%g0\n\t"
	    "bne    1f\n\t"
	    "nop\n\t"
	    "mov    1,%0\n\t"
	    "ba	    2f\n\t"	/* x is subnormal */
	    "nop\n\t"
	    "1:\n\t"
	    "cmp    %0,%1\n\t"
	    "bge    1f\n\t"
	    "nop\n\t"
	    "mov    2,%0\n\t"
	    "ba	    2f\n\t"	/* x is normal */
	    "nop\n\t"
	    "1:\n\t"
	    "bg	    1f\n\t"
	    "nop\n\t"
	    "mov    3,%0\n\t"
	    "ba	    2f\n\t"	/* x is __infinity */
	    "nop\n\t"
	    "1:\n\t"
	    "sethi  %%hi(0x00400000),%1\n\t"
	    "andcc  %0,%1,%%g0\n\t"
	    "mov    4,%0\n\t"	/* x is quiet NaN */
	    "bne    2f\n\t"
	    "nop\n\t"
	    "mov    5,%0\n\t"	/* x is signaling NaN */
	    "2:\n\t"
	    : "=r" (ret), "=&r" (tmp)
	    : "r" (f)
	    : "cc");
	return (ret);
}

#define	_HI_WORD(x)	((uint32_t *)&x)[0]
#define	_LO_WORD(x)	((uint32_t *)&x)[1]

extern __GNU_INLINE enum fp_class_type
fp_class(double d)
{
	enum fp_class_type ret;
	uint32_t tmp;

	/* BEGIN CSTYLED */
	__asm__ __volatile__(
	    "sethi %%hi(0x80000000),%1\n\t"	/* %1 gets 80000000 */
	    "andn  %2,%1,%0\n\t"		/* %2-%0 gets abs(x) */
	    "orcc  %0,%3,%%g0\n\t"		/* set cc as x is zero/nonzero */
	    "bne   1f\n\t"			/* branch if x is nonzero */
	    "nop\n\t"
	    "mov   0,%0\n\t"
	    "ba	   2f\n\t"			/* x is 0 */
	    "nop\n\t"
	    "1:\n\t"
	    "sethi %%hi(0x7ff00000),%1\n\t"	/* %1 gets 7ff00000 */
	    "andcc %0,%1,%%g0\n\t"		/* cc set by __exp field of x */
	    "bne   1f\n\t"			/* branch if normal or max __exp */
	    "nop\n\t"
	    "mov   1,%0\n\t"
	    "ba	   2f\n\t"			/* x is subnormal */
	    "nop\n\t"
	    "1:\n\t"
	    "cmp   %0,%1\n\t"
	    "bge   1f\n\t"			/* branch if x is max __exp */
	    "nop\n\t"
	    "mov   2,%0\n\t"
	    "ba	   2f\n\t"			/* x is normal */
	    "nop\n\t"
	    "1:\n\t"
	    "andn  %0,%1,%0\n\t"		/* o0 gets msw __significand field */
	    "orcc  %0,%3,%%g0\n\t"		/* set cc by OR __significand */
	    "bne   1f\n\t"			/* Branch if __nan */
	    "nop\n\t"
	    "mov   3,%0\n\t"
	    "ba	   2f\n\t"			/* x is __infinity */
	    "nop\n\t"
	    "1:\n\t"
	    "sethi %%hi(0x00080000),%1\n\t"
	    "andcc %0,%1,%%g0\n\t"		/* set cc by quiet/sig bit */
	    "be	   1f\n\t"			/* Branch if signaling */
	    "nop\n\t"
	    "mov   4,%0\n\t"			/* x is quiet NaN */
	    "ba	   2f\n\t"
	    "nop\n\t"
	    "1:\n\t"
	    "mov   5,%0\n\t"			/* x is signaling NaN */
	    "2:\n\t"
	    : "=&r" (ret), "=&r" (tmp)
	    : "r" (_HI_WORD(d)), "r" (_LO_WORD(d))
	    : "cc");
	/* END CSTYLED */

	return (ret);
}

extern __GNU_INLINE int
__swapEX(int i)
{
	int ret;
	uint32_t fsr;
	uint32_t tmp1, tmp2;

	__asm__ __volatile__(
	    "and  %4,0x1f,%2\n\t" /* tmp1 = %2 = %o1 */
	    "sll  %2,5,%2\n\t"	/* shift input to aexc bit location */
	    ".volatile\n\t"
	    "st   %%fsr,%1\n\t"
	    "ld   %1,%0\n\t"	/* %0 = fsr */
	    "andn %0,0x3e0,%3\n\t" /* tmp2 = %3 = %o2 */
	    "or   %2,%3,%2\n\t"	/* %2 = new fsr */
	    "st	  %2,%1\n\t"
	    "ld	  %1,%%fsr\n\t"
	    "srl  %0,5,%0\n\t"
	    "and  %0,0x1f,%0\n\t" /* %0 = ret = %o0 */
	    ".nonvolatile\n\t"
	    : "=r" (ret), "=m" (fsr), "=r" (tmp1), "=r" (tmp2)
	    : "r" (i)
	    : "cc");

	return (ret);
}

/*
 * On the SPARC, __swapRP is a no-op; always return 0 for backward
 * compatibility
 */
/* ARGSUSED */
extern __GNU_INLINE enum fp_precision_type
__swapRP(enum fp_precision_type i)
{
	return (0);
}

extern __GNU_INLINE enum fp_direction_type
__swapRD(enum fp_direction_type d)
{
	enum fp_direction_type ret;
	uint32_t fsr;
	uint32_t tmp1, tmp2, tmp3;

	__asm__ __volatile__(
	    "and  %5,0x3,%0\n\t"
	    "sll  %0,30,%2\n\t"		/* shift input to RD bit location */
	    ".volatile\n\t"
	    "st   %%fsr,%1\n\t"
	    "ld	  %1,%0\n\t"		/* %0 = fsr */
	    "set  0xc0000000,%4\n\t"	/* mask of rounding direction bits */
	    "andn %0,%4,%3\n\t"
	    "or   %2,%3,%2\n\t"		/* %2 = new fsr */
	    "st	  %2,%1\n\t"
	    "ld	  %1,%%fsr\n\t"
	    "srl  %0,30,%0\n\t"
	    "and  %0,0x3,%0\n\t"
	    ".nonvolatile\n\t"
	    : "=r" (ret), "=m" (fsr), "=r" (tmp1), "=r" (tmp2), "=r" (tmp3)
	    : "r" (d)
	    : "cc");

	return (ret);
}

extern __GNU_INLINE int
__swapTE(int i)
{
	int ret;
	uint32_t fsr, tmp1, tmp2;

	/* BEGIN CSTYLED */
	__asm__ __volatile__(
	    "and  %4,0x1f,%0\n\t"
	    "sll  %0,23,%2\n\t"		/* shift input to TEM bit location */
	    ".volatile\n\t"
	    "st   %%fsr,%1\n\t"
	    "ld	  %1,%0\n\t"		/* %0 = fsr */
	    "set  0x0f800000,%3\n\t"	/* mask of TEM (Trap Enable Mode bits) */
	    "andn %0,%3,%3\n\t"
	    "or   %2,%3,%2\n\t"		/* %2 = new fsr */
	    "st	  %2,%1\n\t"
	    "ld	  %1,%%fsr\n\t"
	    "srl  %0,23,%0\n\t"
	    "and  %0,0x1f,%0\n\t"
	    ".nonvolatile\n\t"
	    : "=r" (ret), "=m" (fsr), "=r" (tmp1), "=r" (tmp2)
	    : "r" (i)
	    : "cc");
	/* END CSTYLED */

	return (ret);
}

extern __GNU_INLINE double
sqrt(double d)
{
	return (__inline_sqrt(d));
}

extern __GNU_INLINE float
sqrtf(float f)
{
	return (__inline_sqrtf(f));
}

extern __GNU_INLINE double
fabs(double d)
{
	double ret;

	__asm__ __volatile__("fabsd %1,%0\n\t" : "=e" (ret) : "e" (d));
	return (ret);
}

extern __GNU_INLINE float
fabsf(float f)
{
	float ret;

	__asm__ __volatile__("fabss %1,%0\n\t" : "=f" (ret) : "f" (f));
	return (ret);
}

#ifdef __cplusplus
}
#endif

#endif	/* __GNUC */

#endif /* _LIBM_INLINES_H */