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