xref: /illumos-gate/usr/src/lib/libm/amd64/src/libm_inlines.h (revision 564d52366b33ac17bebd5b441ac47b668dfcb6cd)
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 #ifndef _LIBM_INLINES_H
3525c28e83SPiotr Jasiukajtis #define	_LIBM_INLINES_H
3625c28e83SPiotr Jasiukajtis 
3725c28e83SPiotr Jasiukajtis #ifdef __GNUC__
3825c28e83SPiotr Jasiukajtis 
3925c28e83SPiotr Jasiukajtis #ifdef __cplusplus
4025c28e83SPiotr Jasiukajtis extern "C" {
4125c28e83SPiotr Jasiukajtis #endif
4225c28e83SPiotr Jasiukajtis 
4325c28e83SPiotr Jasiukajtis #include <sys/types.h>
4425c28e83SPiotr Jasiukajtis #include <sys/ieeefp.h>
4525c28e83SPiotr Jasiukajtis 
46*7f11fd00SRichard Lowe extern __GNU_INLINE float
__inline_sqrtf(float a)4725c28e83SPiotr Jasiukajtis __inline_sqrtf(float a)
4825c28e83SPiotr Jasiukajtis {
4925c28e83SPiotr Jasiukajtis 	float ret;
5025c28e83SPiotr Jasiukajtis 
5125c28e83SPiotr Jasiukajtis 	__asm__ __volatile__("sqrtss %1, %0\n\t" : "=x" (ret) : "x" (a));
5225c28e83SPiotr Jasiukajtis 	return (ret);
5325c28e83SPiotr Jasiukajtis }
5425c28e83SPiotr Jasiukajtis 
55*7f11fd00SRichard Lowe extern __GNU_INLINE double
__inline_sqrt(double a)5625c28e83SPiotr Jasiukajtis __inline_sqrt(double a)
5725c28e83SPiotr Jasiukajtis {
5825c28e83SPiotr Jasiukajtis 	double ret;
5925c28e83SPiotr Jasiukajtis 
6025c28e83SPiotr Jasiukajtis 	__asm__ __volatile__("sqrtsd %1, %0\n\t" : "=x" (ret) : "x" (a));
6125c28e83SPiotr Jasiukajtis 	return (ret);
6225c28e83SPiotr Jasiukajtis }
6325c28e83SPiotr Jasiukajtis 
64*7f11fd00SRichard Lowe extern __GNU_INLINE double
__ieee754_sqrt(double a)6525c28e83SPiotr Jasiukajtis __ieee754_sqrt(double a)
6625c28e83SPiotr Jasiukajtis {
6725c28e83SPiotr Jasiukajtis 	return (__inline_sqrt(a));
6825c28e83SPiotr Jasiukajtis }
6925c28e83SPiotr Jasiukajtis 
7025c28e83SPiotr Jasiukajtis /*
7125c28e83SPiotr Jasiukajtis  * 00 - 24 bits
7225c28e83SPiotr Jasiukajtis  * 01 - reserved
7325c28e83SPiotr Jasiukajtis  * 10 - 53 bits
7425c28e83SPiotr Jasiukajtis  * 11 - 64 bits
7525c28e83SPiotr Jasiukajtis  */
76*7f11fd00SRichard Lowe extern __GNU_INLINE int
__swapRP(int i)7725c28e83SPiotr Jasiukajtis __swapRP(int i)
7825c28e83SPiotr Jasiukajtis {
7925c28e83SPiotr Jasiukajtis 	int ret;
8025c28e83SPiotr Jasiukajtis 	uint16_t cw;
8125c28e83SPiotr Jasiukajtis 
8225c28e83SPiotr Jasiukajtis 	__asm__ __volatile__("fstcw %0\n\t" : "=m" (cw));
8325c28e83SPiotr Jasiukajtis 
8425c28e83SPiotr Jasiukajtis 	ret = (cw >> 8) & 0x3;
8525c28e83SPiotr Jasiukajtis 	cw = (cw & 0xfcff) | ((i & 0x3) << 8);
8625c28e83SPiotr Jasiukajtis 
8725c28e83SPiotr Jasiukajtis 	__asm__ __volatile__("fldcw %0\n\t" : : "m" (cw));
8825c28e83SPiotr Jasiukajtis 
8925c28e83SPiotr Jasiukajtis 	return (ret);
9025c28e83SPiotr Jasiukajtis }
9125c28e83SPiotr Jasiukajtis 
9225c28e83SPiotr Jasiukajtis /*
9325c28e83SPiotr Jasiukajtis  * 00 - Round to nearest, with even preferred
9425c28e83SPiotr Jasiukajtis  * 01 - Round down
9525c28e83SPiotr Jasiukajtis  * 10 - Round up
9625c28e83SPiotr Jasiukajtis  * 11 - Chop
9725c28e83SPiotr Jasiukajtis  */
98*7f11fd00SRichard Lowe extern __GNU_INLINE enum fp_direction_type
__swap87RD(enum fp_direction_type i)9925c28e83SPiotr Jasiukajtis __swap87RD(enum fp_direction_type i)
10025c28e83SPiotr Jasiukajtis {
10125c28e83SPiotr Jasiukajtis 	int ret;
10225c28e83SPiotr Jasiukajtis 	uint16_t cw;
10325c28e83SPiotr Jasiukajtis 
10425c28e83SPiotr Jasiukajtis 	__asm__ __volatile__("fstcw %0\n\t" : "=m" (cw));
10525c28e83SPiotr Jasiukajtis 
10625c28e83SPiotr Jasiukajtis 	ret = (cw >> 10) & 0x3;
10725c28e83SPiotr Jasiukajtis 	cw = (cw & 0xf3ff) | ((i & 0x3) << 10);
10825c28e83SPiotr Jasiukajtis 
10925c28e83SPiotr Jasiukajtis 	__asm__ __volatile__("fldcw %0\n\t" : : "m" (cw));
11025c28e83SPiotr Jasiukajtis 
11125c28e83SPiotr Jasiukajtis 	return (ret);
11225c28e83SPiotr Jasiukajtis }
11325c28e83SPiotr Jasiukajtis 
114*7f11fd00SRichard Lowe extern __GNU_INLINE int
abs(int i)11525c28e83SPiotr Jasiukajtis abs(int i)
11625c28e83SPiotr Jasiukajtis {
11725c28e83SPiotr Jasiukajtis 	int ret;
11825c28e83SPiotr Jasiukajtis 	__asm__ __volatile__(
11925c28e83SPiotr Jasiukajtis 	    "movl    %1, %0\n\t"
12025c28e83SPiotr Jasiukajtis 	    "negl    %1\n\t"
12125c28e83SPiotr Jasiukajtis 	    "cmovnsl %1, %0\n\t"
12225c28e83SPiotr Jasiukajtis 	    : "=r" (ret), "+r" (i)
12325c28e83SPiotr Jasiukajtis 	    :
12425c28e83SPiotr Jasiukajtis 	    : "cc");
12525c28e83SPiotr Jasiukajtis 	return (ret);
12625c28e83SPiotr Jasiukajtis }
12725c28e83SPiotr Jasiukajtis 
128*7f11fd00SRichard Lowe extern __GNU_INLINE double
copysign(double d1,double d2)12925c28e83SPiotr Jasiukajtis copysign(double d1, double d2)
13025c28e83SPiotr Jasiukajtis {
13125c28e83SPiotr Jasiukajtis 	double tmpd;
13225c28e83SPiotr Jasiukajtis 
13325c28e83SPiotr Jasiukajtis 	__asm__ __volatile__(
13425c28e83SPiotr Jasiukajtis 	    "movd %3, %1\n\t"
13525c28e83SPiotr Jasiukajtis 	    "andpd %1, %0\n\t"
13625c28e83SPiotr Jasiukajtis 	    "andnpd %2, %1\n\t"
13725c28e83SPiotr Jasiukajtis 	    "orpd %1, %0\n\t"
13825c28e83SPiotr Jasiukajtis 	    : "+&x" (d1), "=&x" (tmpd)
13925c28e83SPiotr Jasiukajtis 	    : "x" (d2), "r" (0x7fffffffffffffff));
14025c28e83SPiotr Jasiukajtis 
14125c28e83SPiotr Jasiukajtis 	return (d1);
14225c28e83SPiotr Jasiukajtis }
14325c28e83SPiotr Jasiukajtis 
144*7f11fd00SRichard Lowe extern __GNU_INLINE double
fabs(double d)14525c28e83SPiotr Jasiukajtis fabs(double d)
14625c28e83SPiotr Jasiukajtis {
14725c28e83SPiotr Jasiukajtis 	double tmp;
14825c28e83SPiotr Jasiukajtis 
14925c28e83SPiotr Jasiukajtis 	__asm__ __volatile__(
15025c28e83SPiotr Jasiukajtis 	    "movd  %2, %1\n\t"
15125c28e83SPiotr Jasiukajtis 	    "andpd %1, %0"
15225c28e83SPiotr Jasiukajtis 	    : "+x" (d), "=&x" (tmp)
15325c28e83SPiotr Jasiukajtis 	    : "r" (0x7fffffffffffffff));
15425c28e83SPiotr Jasiukajtis 
15525c28e83SPiotr Jasiukajtis 	return (d);
15625c28e83SPiotr Jasiukajtis }
15725c28e83SPiotr Jasiukajtis 
158*7f11fd00SRichard Lowe extern __GNU_INLINE float
fabsf(float d)15925c28e83SPiotr Jasiukajtis fabsf(float d)
16025c28e83SPiotr Jasiukajtis {
16125c28e83SPiotr Jasiukajtis 	__asm__ __volatile__(
16225c28e83SPiotr Jasiukajtis 	    "andpd %1, %0"
16325c28e83SPiotr Jasiukajtis 	    : "+x" (d)
16425c28e83SPiotr Jasiukajtis 	    : "x" (0x7fffffff));
16525c28e83SPiotr Jasiukajtis 
16625c28e83SPiotr Jasiukajtis 	return (d);
16725c28e83SPiotr Jasiukajtis }
16825c28e83SPiotr Jasiukajtis 
169*7f11fd00SRichard Lowe extern __GNU_INLINE int
finite(double d)17025c28e83SPiotr Jasiukajtis finite(double d)
17125c28e83SPiotr Jasiukajtis {
17225c28e83SPiotr Jasiukajtis 	long ret = 0x7fffffffffffffff;
17325c28e83SPiotr Jasiukajtis 	uint64_t tmp;
17425c28e83SPiotr Jasiukajtis 
17525c28e83SPiotr Jasiukajtis 	__asm__ __volatile__(
17625c28e83SPiotr Jasiukajtis 	    "movq %2, %1\n\t"
17725c28e83SPiotr Jasiukajtis 	    "andq %1, %0\n\t"
17825c28e83SPiotr Jasiukajtis 	    "movq $0x7ff0000000000000, %1\n\t"
17925c28e83SPiotr Jasiukajtis 	    "subq %1, %0\n\t"
18025c28e83SPiotr Jasiukajtis 	    "shrq $63, %0\n\t"
18125c28e83SPiotr Jasiukajtis 	    : "+r" (ret), "=r" (tmp)
18225c28e83SPiotr Jasiukajtis 	    : "x" (d)
18325c28e83SPiotr Jasiukajtis 	    : "cc");
18425c28e83SPiotr Jasiukajtis 
18525c28e83SPiotr Jasiukajtis 	return (ret);
18625c28e83SPiotr Jasiukajtis }
18725c28e83SPiotr Jasiukajtis 
188*7f11fd00SRichard Lowe extern __GNU_INLINE int
signbit(double d)18925c28e83SPiotr Jasiukajtis signbit(double d)
19025c28e83SPiotr Jasiukajtis {
19125c28e83SPiotr Jasiukajtis 	long ret;
19225c28e83SPiotr Jasiukajtis 	__asm__ __volatile__(
19325c28e83SPiotr Jasiukajtis 	    "movmskpd %1, %0\n\t"
19425c28e83SPiotr Jasiukajtis 	    "andq     $1, %0\n\t"
19525c28e83SPiotr Jasiukajtis 	    : "=r" (ret)
19625c28e83SPiotr Jasiukajtis 	    : "x" (d)
19725c28e83SPiotr Jasiukajtis 	    : "cc");
19825c28e83SPiotr Jasiukajtis 	return (ret);
19925c28e83SPiotr Jasiukajtis }
20025c28e83SPiotr Jasiukajtis 
201*7f11fd00SRichard Lowe extern __GNU_INLINE double
sqrt(double d)20225c28e83SPiotr Jasiukajtis sqrt(double d)
20325c28e83SPiotr Jasiukajtis {
20425c28e83SPiotr Jasiukajtis 	return (__inline_sqrt(d));
20525c28e83SPiotr Jasiukajtis }
20625c28e83SPiotr Jasiukajtis 
207*7f11fd00SRichard Lowe extern __GNU_INLINE float
sqrtf(float f)20825c28e83SPiotr Jasiukajtis sqrtf(float f)
20925c28e83SPiotr Jasiukajtis {
21025c28e83SPiotr Jasiukajtis 	return (__inline_sqrtf(f));
21125c28e83SPiotr Jasiukajtis }
21225c28e83SPiotr Jasiukajtis 
21325c28e83SPiotr Jasiukajtis #ifdef __cplusplus
21425c28e83SPiotr Jasiukajtis }
21525c28e83SPiotr Jasiukajtis #endif
21625c28e83SPiotr Jasiukajtis 
21725c28e83SPiotr Jasiukajtis #endif  /* __GNUC__ */
21825c28e83SPiotr Jasiukajtis 
21925c28e83SPiotr Jasiukajtis #endif /* _LIBM_INLINES_H */
220