125c28e83SPiotr Jasiukajtis /*
225c28e83SPiotr Jasiukajtis  * CDDL HEADER START
325c28e83SPiotr Jasiukajtis  *
425c28e83SPiotr Jasiukajtis  * The contents of this file are subject to the terms of the
525c28e83SPiotr Jasiukajtis  * Common Development and Distribution License (the "License").
625c28e83SPiotr Jasiukajtis  * You may not use this file except in compliance with the License.
725c28e83SPiotr Jasiukajtis  *
825c28e83SPiotr Jasiukajtis  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
925c28e83SPiotr Jasiukajtis  * or http://www.opensolaris.org/os/licensing.
1025c28e83SPiotr Jasiukajtis  * See the License for the specific language governing permissions
1125c28e83SPiotr Jasiukajtis  * and limitations under the License.
1225c28e83SPiotr Jasiukajtis  *
1325c28e83SPiotr Jasiukajtis  * When distributing Covered Code, include this CDDL HEADER in each
1425c28e83SPiotr Jasiukajtis  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1525c28e83SPiotr Jasiukajtis  * If applicable, add the following below this CDDL HEADER, with the
1625c28e83SPiotr Jasiukajtis  * fields enclosed by brackets "[]" replaced with your own identifying
1725c28e83SPiotr Jasiukajtis  * information: Portions Copyright [yyyy] [name of copyright owner]
1825c28e83SPiotr Jasiukajtis  *
1925c28e83SPiotr Jasiukajtis  * CDDL HEADER END
2025c28e83SPiotr Jasiukajtis  */
2125c28e83SPiotr Jasiukajtis 
2225c28e83SPiotr Jasiukajtis /*
2325c28e83SPiotr Jasiukajtis  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
2425c28e83SPiotr Jasiukajtis  */
2525c28e83SPiotr Jasiukajtis /*
2625c28e83SPiotr Jasiukajtis  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
2725c28e83SPiotr Jasiukajtis  * Use is subject to license terms.
2825c28e83SPiotr Jasiukajtis  */
2925c28e83SPiotr Jasiukajtis 
3025c28e83SPiotr Jasiukajtis /*
3125c28e83SPiotr Jasiukajtis  * Copyright 2011, Richard Lowe.
3225c28e83SPiotr Jasiukajtis  */
3325c28e83SPiotr Jasiukajtis 
3425c28e83SPiotr Jasiukajtis /* Functions in this file are duplicated in locallibm.il.  Keep them in sync */
3525c28e83SPiotr Jasiukajtis 
3625c28e83SPiotr Jasiukajtis #ifndef _LIBM_INLINES_H
3725c28e83SPiotr Jasiukajtis #define	_LIBM_INLINES_H
3825c28e83SPiotr Jasiukajtis 
3925c28e83SPiotr Jasiukajtis #ifdef __GNUC__
4025c28e83SPiotr Jasiukajtis 
4125c28e83SPiotr Jasiukajtis #ifdef __cplusplus
4225c28e83SPiotr Jasiukajtis extern "C" {
4325c28e83SPiotr Jasiukajtis #endif
4425c28e83SPiotr Jasiukajtis 
4525c28e83SPiotr Jasiukajtis #include <sys/types.h>
4625c28e83SPiotr Jasiukajtis #include <sys/ieeefp.h>
4725c28e83SPiotr Jasiukajtis 
48*7f11fd00SRichard Lowe extern __GNU_INLINE float
__inline_sqrtf(float a)4925c28e83SPiotr Jasiukajtis __inline_sqrtf(float a)
5025c28e83SPiotr Jasiukajtis {
5125c28e83SPiotr Jasiukajtis 	float ret;
5225c28e83SPiotr Jasiukajtis 
5325c28e83SPiotr Jasiukajtis 	__asm__ __volatile__("sqrtss %1, %0\n\t" : "=x" (ret) : "x" (a));
5425c28e83SPiotr Jasiukajtis 	return (ret);
5525c28e83SPiotr Jasiukajtis }
5625c28e83SPiotr Jasiukajtis 
57*7f11fd00SRichard Lowe extern __GNU_INLINE double
__inline_sqrt(double a)5825c28e83SPiotr Jasiukajtis __inline_sqrt(double a)
5925c28e83SPiotr Jasiukajtis {
6025c28e83SPiotr Jasiukajtis 	double ret;
6125c28e83SPiotr Jasiukajtis 
6225c28e83SPiotr Jasiukajtis 	__asm__ __volatile__("sqrtsd %1, %0\n\t" : "=x" (ret) : "x" (a));
6325c28e83SPiotr Jasiukajtis 	return (ret);
6425c28e83SPiotr Jasiukajtis }
6525c28e83SPiotr Jasiukajtis 
66*7f11fd00SRichard Lowe extern __GNU_INLINE double
__ieee754_sqrt(double a)6725c28e83SPiotr Jasiukajtis __ieee754_sqrt(double a)
6825c28e83SPiotr Jasiukajtis {
6925c28e83SPiotr Jasiukajtis 	return (__inline_sqrt(a));
7025c28e83SPiotr Jasiukajtis }
7125c28e83SPiotr Jasiukajtis 
7225c28e83SPiotr Jasiukajtis /*
7325c28e83SPiotr Jasiukajtis  * 00 - 24 bits
7425c28e83SPiotr Jasiukajtis  * 01 - reserved
7525c28e83SPiotr Jasiukajtis  * 10 - 53 bits
7625c28e83SPiotr Jasiukajtis  * 11 - 64 bits
7725c28e83SPiotr Jasiukajtis  */
78*7f11fd00SRichard Lowe extern __GNU_INLINE int
__swapRP(int i)7925c28e83SPiotr Jasiukajtis __swapRP(int i)
8025c28e83SPiotr Jasiukajtis {
8125c28e83SPiotr Jasiukajtis 	int ret;
8225c28e83SPiotr Jasiukajtis 	uint16_t cw;
8325c28e83SPiotr Jasiukajtis 
8425c28e83SPiotr Jasiukajtis 	__asm__ __volatile__("fstcw %0\n\t" : "=m" (cw));
8525c28e83SPiotr Jasiukajtis 
8625c28e83SPiotr Jasiukajtis 	ret = (cw >> 8) & 0x3;
8725c28e83SPiotr Jasiukajtis 	cw = (cw & 0xfcff) | ((i & 0x3) << 8);
8825c28e83SPiotr Jasiukajtis 
8925c28e83SPiotr Jasiukajtis 	__asm__ __volatile__("fldcw %0\n\t" : : "m" (cw));
9025c28e83SPiotr Jasiukajtis 
9125c28e83SPiotr Jasiukajtis 	return (ret);
9225c28e83SPiotr Jasiukajtis }
9325c28e83SPiotr Jasiukajtis 
9425c28e83SPiotr Jasiukajtis /*
9525c28e83SPiotr Jasiukajtis  * 00 - Round to nearest, with even preferred
9625c28e83SPiotr Jasiukajtis  * 01 - Round down
9725c28e83SPiotr Jasiukajtis  * 10 - Round up
9825c28e83SPiotr Jasiukajtis  * 11 - Chop
9925c28e83SPiotr Jasiukajtis  */
100*7f11fd00SRichard Lowe extern __GNU_INLINE enum fp_direction_type
__swap87RD(enum fp_direction_type i)10125c28e83SPiotr Jasiukajtis __swap87RD(enum fp_direction_type i)
10225c28e83SPiotr Jasiukajtis {
10325c28e83SPiotr Jasiukajtis 	int ret;
10425c28e83SPiotr Jasiukajtis 	uint16_t cw;
10525c28e83SPiotr Jasiukajtis 
10625c28e83SPiotr Jasiukajtis 	__asm__ __volatile__("fstcw %0\n\t" : "=m" (cw));
10725c28e83SPiotr Jasiukajtis 
10825c28e83SPiotr Jasiukajtis 	ret = (cw >> 10) & 0x3;
10925c28e83SPiotr Jasiukajtis 	cw = (cw & 0xf3ff) | ((i & 0x3) << 10);
11025c28e83SPiotr Jasiukajtis 
11125c28e83SPiotr Jasiukajtis 	__asm__ __volatile__("fldcw %0\n\t" : : "m" (cw));
11225c28e83SPiotr Jasiukajtis 
11325c28e83SPiotr Jasiukajtis 	return (ret);
11425c28e83SPiotr Jasiukajtis }
11525c28e83SPiotr Jasiukajtis 
116*7f11fd00SRichard Lowe extern __GNU_INLINE int
abs(int i)11725c28e83SPiotr Jasiukajtis abs(int i)
11825c28e83SPiotr Jasiukajtis {
11925c28e83SPiotr Jasiukajtis 	int ret;
12025c28e83SPiotr Jasiukajtis 	__asm__ __volatile__(
12125c28e83SPiotr Jasiukajtis 	    "movl    %1, %0\n\t"
12225c28e83SPiotr Jasiukajtis 	    "negl    %1\n\t"
12325c28e83SPiotr Jasiukajtis 	    "cmovnsl %1, %0\n\t"
12425c28e83SPiotr Jasiukajtis 	    : "=r" (ret), "+r" (i)
12525c28e83SPiotr Jasiukajtis 	    :
12625c28e83SPiotr Jasiukajtis 	    : "cc");
12725c28e83SPiotr Jasiukajtis 	return (ret);
12825c28e83SPiotr Jasiukajtis }
12925c28e83SPiotr Jasiukajtis 
130*7f11fd00SRichard Lowe extern __GNU_INLINE double
copysign(double d1,double d2)13125c28e83SPiotr Jasiukajtis copysign(double d1, double d2)
13225c28e83SPiotr Jasiukajtis {
13325c28e83SPiotr Jasiukajtis 	double tmpd;
13425c28e83SPiotr Jasiukajtis 
13525c28e83SPiotr Jasiukajtis 	__asm__ __volatile__(
13625c28e83SPiotr Jasiukajtis 	    "movd %3, %1\n\t"
13725c28e83SPiotr Jasiukajtis 	    "andpd %1, %0\n\t"
13825c28e83SPiotr Jasiukajtis 	    "andnpd %2, %1\n\t"
13925c28e83SPiotr Jasiukajtis 	    "orpd %1, %0\n\t"
14025c28e83SPiotr Jasiukajtis 	    : "+&x" (d1), "=&x" (tmpd)
14125c28e83SPiotr Jasiukajtis 	    : "x" (d2), "r" (0x7fffffffffffffff));
14225c28e83SPiotr Jasiukajtis 
14325c28e83SPiotr Jasiukajtis 	return (d1);
14425c28e83SPiotr Jasiukajtis }
14525c28e83SPiotr Jasiukajtis 
146*7f11fd00SRichard Lowe extern __GNU_INLINE double
fabs(double d)14725c28e83SPiotr Jasiukajtis fabs(double d)
14825c28e83SPiotr Jasiukajtis {
14925c28e83SPiotr Jasiukajtis 	double tmp;
15025c28e83SPiotr Jasiukajtis 
15125c28e83SPiotr Jasiukajtis 	__asm__ __volatile__(
15225c28e83SPiotr Jasiukajtis 	    "movd  %2, %1\n\t"
15325c28e83SPiotr Jasiukajtis 	    "andpd %1, %0"
15425c28e83SPiotr Jasiukajtis 	    : "+x" (d), "=&x" (tmp)
15525c28e83SPiotr Jasiukajtis 	    : "r" (0x7fffffffffffffff));
15625c28e83SPiotr Jasiukajtis 
15725c28e83SPiotr Jasiukajtis 	return (d);
15825c28e83SPiotr Jasiukajtis }
15925c28e83SPiotr Jasiukajtis 
160*7f11fd00SRichard Lowe extern __GNU_INLINE float
fabsf(float d)16125c28e83SPiotr Jasiukajtis fabsf(float d)
16225c28e83SPiotr Jasiukajtis {
16325c28e83SPiotr Jasiukajtis 	__asm__ __volatile__(
16425c28e83SPiotr Jasiukajtis 	    "andpd %1, %0"
16525c28e83SPiotr Jasiukajtis 	    : "+x" (d)
16625c28e83SPiotr Jasiukajtis 	    : "x" (0x7fffffff));
16725c28e83SPiotr Jasiukajtis 
16825c28e83SPiotr Jasiukajtis 	return (d);
16925c28e83SPiotr Jasiukajtis }
17025c28e83SPiotr Jasiukajtis 
171*7f11fd00SRichard Lowe extern __GNU_INLINE int
finite(double d)17225c28e83SPiotr Jasiukajtis finite(double d)
17325c28e83SPiotr Jasiukajtis {
17425c28e83SPiotr Jasiukajtis 	long ret = 0x7fffffffffffffff;
17525c28e83SPiotr Jasiukajtis 	uint64_t tmp;
17625c28e83SPiotr Jasiukajtis 
17725c28e83SPiotr Jasiukajtis 	__asm__ __volatile__(
17825c28e83SPiotr Jasiukajtis 	    "movq %2, %1\n\t"
17925c28e83SPiotr Jasiukajtis 	    "andq %1, %0\n\t"
18025c28e83SPiotr Jasiukajtis 	    "movq $0x7ff0000000000000, %1\n\t"
18125c28e83SPiotr Jasiukajtis 	    "subq %1, %0\n\t"
18225c28e83SPiotr Jasiukajtis 	    "shrq $63, %0\n\t"
18325c28e83SPiotr Jasiukajtis 	    : "+r" (ret), "=r" (tmp)
18425c28e83SPiotr Jasiukajtis 	    : "x" (d)
18525c28e83SPiotr Jasiukajtis 	    : "cc");
18625c28e83SPiotr Jasiukajtis 
18725c28e83SPiotr Jasiukajtis 	return (ret);
18825c28e83SPiotr Jasiukajtis }
18925c28e83SPiotr Jasiukajtis 
190*7f11fd00SRichard Lowe extern __GNU_INLINE int
signbit(double d)19125c28e83SPiotr Jasiukajtis signbit(double d)
19225c28e83SPiotr Jasiukajtis {
19325c28e83SPiotr Jasiukajtis 	long ret;
19425c28e83SPiotr Jasiukajtis 	__asm__ __volatile__(
19525c28e83SPiotr Jasiukajtis 	    "movmskpd %1, %0\n\t"
19625c28e83SPiotr Jasiukajtis 	    "andq     $1, %0\n\t"
19725c28e83SPiotr Jasiukajtis 	    : "=r" (ret)
19825c28e83SPiotr Jasiukajtis 	    : "x" (d)
19925c28e83SPiotr Jasiukajtis 	    : "cc");
20025c28e83SPiotr Jasiukajtis 	return (ret);
20125c28e83SPiotr Jasiukajtis }
20225c28e83SPiotr Jasiukajtis 
203*7f11fd00SRichard Lowe extern __GNU_INLINE double
sqrt(double d)20425c28e83SPiotr Jasiukajtis sqrt(double d)
20525c28e83SPiotr Jasiukajtis {
20625c28e83SPiotr Jasiukajtis 	return (__inline_sqrt(d));
20725c28e83SPiotr Jasiukajtis }
20825c28e83SPiotr Jasiukajtis 
209*7f11fd00SRichard Lowe extern __GNU_INLINE float
sqrtf(float f)21025c28e83SPiotr Jasiukajtis sqrtf(float f)
21125c28e83SPiotr Jasiukajtis {
21225c28e83SPiotr Jasiukajtis 	return (__inline_sqrtf(f));
21325c28e83SPiotr Jasiukajtis }
21425c28e83SPiotr Jasiukajtis 
21525c28e83SPiotr Jasiukajtis #ifdef __cplusplus
21625c28e83SPiotr Jasiukajtis }
21725c28e83SPiotr Jasiukajtis #endif
21825c28e83SPiotr Jasiukajtis 
21925c28e83SPiotr Jasiukajtis #endif  /* __GNUC__ */
22025c28e83SPiotr Jasiukajtis 
22125c28e83SPiotr Jasiukajtis #endif /* _LIBM_INLINES_H */