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 2006 Sun Microsystems, Inc.  All rights reserved.
2425c28e8Piotr Jasiukajtis * Use is subject to license terms.
2525c28e8Piotr Jasiukajtis */
2625c28e8Piotr Jasiukajtis
2725c28e8Piotr Jasiukajtis/*
2825c28e8Piotr Jasiukajtis * Copyright 2011, Richard Lowe
2925c28e8Piotr Jasiukajtis */
3025c28e8Piotr Jasiukajtis
3125c28e8Piotr Jasiukajtis/* Functions in this file are duplicated in locallibm.il.  Keep them in sync */
3225c28e8Piotr Jasiukajtis
3325c28e8Piotr Jasiukajtis#ifndef _LIBM_INLINES_H
3425c28e8Piotr Jasiukajtis#define	_LIBM_INLINES_H
3525c28e8Piotr Jasiukajtis
3625c28e8Piotr Jasiukajtis#ifdef __GNUC__
3725c28e8Piotr Jasiukajtis
3825c28e8Piotr Jasiukajtis#ifdef __cplusplus
3925c28e8Piotr Jasiukajtisextern "C" {
4025c28e8Piotr Jasiukajtis#endif
4125c28e8Piotr Jasiukajtis
4225c28e8Piotr Jasiukajtis#include <sys/types.h>
4325c28e8Piotr Jasiukajtis#include <sys/ieeefp.h>
4425c28e8Piotr Jasiukajtis
4525c28e8Piotr Jasiukajtis#define	_LO_WORD(x)	((uint32_t *)&x)[0]
4625c28e8Piotr Jasiukajtis#define	_HI_WORD(x)	((uint32_t *)&x)[1]
4725c28e8Piotr Jasiukajtis#define	_HIER_WORD(x)	((uint32_t *)&x)[2]
4825c28e8Piotr Jasiukajtis
497f11fd0Richard Loweextern __GNU_INLINE double
5025c28e8Piotr Jasiukajtis__inline_sqrt(double a)
5125c28e8Piotr Jasiukajtis{
5225c28e8Piotr Jasiukajtis	double ret;
5325c28e8Piotr Jasiukajtis
5425c28e8Piotr Jasiukajtis	__asm__ __volatile__("fsqrt\n\t" : "=t" (ret) : "0" (a) : "cc");
5525c28e8Piotr Jasiukajtis	return (ret);
5625c28e8Piotr Jasiukajtis}
5725c28e8Piotr Jasiukajtis
587f11fd0Richard Loweextern __GNU_INLINE double
5925c28e8Piotr Jasiukajtis__ieee754_sqrt(double a)
6025c28e8Piotr Jasiukajtis{
6125c28e8Piotr Jasiukajtis	return (__inline_sqrt(a));
6225c28e8Piotr Jasiukajtis}
6325c28e8Piotr Jasiukajtis
647f11fd0Richard Loweextern __GNU_INLINE float
6525c28e8Piotr Jasiukajtis__inline_sqrtf(float a)
6625c28e8Piotr Jasiukajtis{
6725c28e8Piotr Jasiukajtis	float ret;
6825c28e8Piotr Jasiukajtis
6925c28e8Piotr Jasiukajtis	__asm__ __volatile__("fsqrt\n\t" : "=t" (ret) : "0" (a) : "cc");
7025c28e8Piotr Jasiukajtis	return (ret);
7125c28e8Piotr Jasiukajtis}
7225c28e8Piotr Jasiukajtis
737f11fd0Richard Loweextern __GNU_INLINE double
7425c28e8Piotr Jasiukajtis__inline_rint(double a)
7525c28e8Piotr Jasiukajtis{
7625c28e8Piotr Jasiukajtis	__asm__ __volatile__(
7725c28e8Piotr Jasiukajtis	    "andl $0x7fffffff,%1\n\t"
7825c28e8Piotr Jasiukajtis	    "cmpl $0x43300000,%1\n\t"
7925c28e8Piotr Jasiukajtis	    "jae  1f\n\t"
8025c28e8Piotr Jasiukajtis	    "frndint\n\t"
8125c28e8Piotr Jasiukajtis	    "1: fwait\n\t"
8225c28e8Piotr Jasiukajtis	    : "+t" (a), "+&r" (_HI_WORD(a))
8325c28e8Piotr Jasiukajtis	    :
8425c28e8Piotr Jasiukajtis	    : "cc");
8525c28e8Piotr Jasiukajtis
8625c28e8Piotr Jasiukajtis	return (a);
8725c28e8Piotr Jasiukajtis}
8825c28e8Piotr Jasiukajtis
8925c28e8Piotr Jasiukajtis/*
9025c28e8Piotr Jasiukajtis * 00 - 24 bits
9125c28e8Piotr Jasiukajtis * 01 - reserved
9225c28e8Piotr Jasiukajtis * 10 - 53 bits
9325c28e8Piotr Jasiukajtis * 11 - 64 bits
9425c28e8Piotr Jasiukajtis */
957f11fd0Richard Loweextern __GNU_INLINE int
9625c28e8Piotr Jasiukajtis__swapRP(int i)
9725c28e8Piotr Jasiukajtis{
9825c28e8Piotr Jasiukajtis	int ret;
9925c28e8Piotr Jasiukajtis	uint16_t cw;
10025c28e8Piotr Jasiukajtis
10125c28e8Piotr Jasiukajtis	__asm__ __volatile__("fstcw %0\n\t" : "=m" (cw));
10225c28e8Piotr Jasiukajtis
10325c28e8Piotr Jasiukajtis	ret = (cw >> 8) & 0x3;
10425c28e8Piotr Jasiukajtis	cw = (cw & 0xfcff) | ((i & 0x3) << 8);
10525c28e8Piotr Jasiukajtis
10625c28e8Piotr Jasiukajtis	__asm__ __volatile__("fldcw %0\n\t" : : "m" (cw));
10725c28e8Piotr Jasiukajtis
10825c28e8Piotr Jasiukajtis	return (ret);
10925c28e8Piotr Jasiukajtis}
11025c28e8Piotr Jasiukajtis
11125c28e8Piotr Jasiukajtis/*
11225c28e8Piotr Jasiukajtis * 00 - Round to nearest, with even preferred
11325c28e8Piotr Jasiukajtis * 01 - Round down
11425c28e8Piotr Jasiukajtis * 10 - Round up
11525c28e8Piotr Jasiukajtis * 11 - Chop
11625c28e8Piotr Jasiukajtis */
1177f11fd0Richard Loweextern __GNU_INLINE enum fp_direction_type
11825c28e8Piotr Jasiukajtis__swap87RD(enum fp_direction_type i)
11925c28e8Piotr Jasiukajtis{
12025c28e8Piotr Jasiukajtis	int ret;
12125c28e8Piotr Jasiukajtis	uint16_t cw;
12225c28e8Piotr Jasiukajtis
12325c28e8Piotr Jasiukajtis	__asm__ __volatile__("fstcw %0\n\t" : "=m" (cw));
12425c28e8Piotr Jasiukajtis
12525c28e8Piotr Jasiukajtis	ret = (cw >> 10) & 0x3;
12625c28e8Piotr Jasiukajtis	cw = (cw & 0xf3ff) | ((i & 0x3) << 10);
12725c28e8Piotr Jasiukajtis
12825c28e8Piotr Jasiukajtis	__asm__ __volatile__("fldcw %0\n\t" : : "m" (cw));
12925c28e8Piotr Jasiukajtis
13025c28e8Piotr Jasiukajtis	return (ret);
13125c28e8Piotr Jasiukajtis}
13225c28e8Piotr Jasiukajtis
1337f11fd0Richard Loweextern __GNU_INLINE double
13425c28e8Piotr Jasiukajtisceil(double d)
13525c28e8Piotr Jasiukajtis{
13625c28e8Piotr Jasiukajtis	/*
13725c28e8Piotr Jasiukajtis	 * Let's set a Rounding Control (RC) bits from x87 FPU Control Word
13825c28e8Piotr Jasiukajtis	 * to fp_positive and save old bits in rd.
13925c28e8Piotr Jasiukajtis	 */
14025c28e8Piotr Jasiukajtis	short rd = __swap87RD(fp_positive);
14125c28e8Piotr Jasiukajtis
14225c28e8Piotr Jasiukajtis	/*
14325c28e8Piotr Jasiukajtis	 * The FRNDINT instruction returns a floating-point value that is the
14425c28e8Piotr Jasiukajtis	 * integral value closest to the source value in the direction of the
14525c28e8Piotr Jasiukajtis	 * rounding mode specified in the RC field of the x87 FPU control word.
14625c28e8Piotr Jasiukajtis	 *
14725c28e8Piotr Jasiukajtis	 * Rounds the source value in the ST(0) register to the nearest
14825c28e8Piotr Jasiukajtis	 * integral value, depending on the current rounding mode
14925c28e8Piotr Jasiukajtis	 * (setting of the RC field of the FPU control word),
15025c28e8Piotr Jasiukajtis	 * and stores the result in ST(0).
15125c28e8Piotr Jasiukajtis	 */
15225c28e8Piotr Jasiukajtis	__asm__ __volatile__("frndint" : "+t" (d) : : "cc");
15325c28e8Piotr Jasiukajtis
15425c28e8Piotr Jasiukajtis	/* restore old RC bits */
15525c28e8Piotr Jasiukajtis	__swap87RD(rd);
15625c28e8Piotr Jasiukajtis
15725c28e8Piotr Jasiukajtis	return (d);
15825c28e8Piotr Jasiukajtis}
15925c28e8Piotr Jasiukajtis
1607f11fd0Richard Loweextern __GNU_INLINE double
16125c28e8Piotr Jasiukajtiscopysign(double d1, double d2)
16225c28e8Piotr Jasiukajtis{
16325c28e8Piotr Jasiukajtis	__asm__ __volatile__(
16425c28e8Piotr Jasiukajtis	    "andl $0x7fffffff,%0\n\t"	/* %0 <-- hi_32(abs(d)) */
16525c28e8Piotr Jasiukajtis	    "andl $0x80000000,%1\n\t"	/* %1[31] <-- sign_bit(d2) */
16625c28e8Piotr Jasiukajtis	    "orl  %1,%0\n\t"		/* %0 <-- hi_32(copysign(x,y)) */
16725c28e8Piotr Jasiukajtis	    : "+&r" (_HI_WORD(d1)), "+r" (_HI_WORD(d2))
16825c28e8Piotr Jasiukajtis	    :
16925c28e8Piotr Jasiukajtis	    : "cc");
17025c28e8Piotr Jasiukajtis
17125c28e8Piotr Jasiukajtis	return (d1);
17225c28e8Piotr Jasiukajtis}
17325c28e8Piotr Jasiukajtis
1747f11fd0Richard Loweextern __GNU_INLINE double
17525c28e8Piotr Jasiukajtisfabs(double d)
17625c28e8Piotr Jasiukajtis{
17725c28e8Piotr Jasiukajtis	__asm__ __volatile__("fabs\n\t" : "+t" (d) : : "cc");
17825c28e8Piotr Jasiukajtis	return (d);
17925c28e8Piotr Jasiukajtis}
18025c28e8Piotr Jasiukajtis
1817f11fd0Richard Loweextern __GNU_INLINE float
18225c28e8Piotr Jasiukajtisfabsf(float d)
18325c28e8Piotr Jasiukajtis{
18425c28e8Piotr Jasiukajtis	__asm__ __volatile__("fabs\n\t" : "+t" (d) : : "cc");
18525c28e8Piotr Jasiukajtis	return (d);
18625c28e8Piotr Jasiukajtis}
18725c28e8Piotr Jasiukajtis
1887f11fd0Richard Loweextern __GNU_INLINE long double
18925c28e8Piotr Jasiukajtisfabsl(long double d)
19025c28e8Piotr Jasiukajtis{
19125c28e8Piotr Jasiukajtis	__asm__ __volatile__("fabs\n\t" : "+t" (d) : : "cc");
19225c28e8Piotr Jasiukajtis	return (d);
19325c28e8Piotr Jasiukajtis}
19425c28e8Piotr Jasiukajtis
1957f11fd0Richard Loweextern __GNU_INLINE int
19625c28e8Piotr Jasiukajtisfinite(double d)
19725c28e8Piotr Jasiukajtis{
19825c28e8Piotr Jasiukajtis	int ret = _HI_WORD(d);
19925c28e8Piotr Jasiukajtis
20025c28e8Piotr Jasiukajtis	__asm__ __volatile__(
20125c28e8Piotr Jasiukajtis	    "notl %0\n\t"
20225c28e8Piotr Jasiukajtis	    "andl $0x7ff00000,%0\n\t"
20325c28e8Piotr Jasiukajtis	    "negl %0\n\t"
20425c28e8Piotr Jasiukajtis	    "shrl $31,%0\n\t"
20525c28e8Piotr Jasiukajtis	    : "+r" (ret)
20625c28e8Piotr Jasiukajtis	    :
20725c28e8Piotr Jasiukajtis	    : "cc");
20825c28e8Piotr Jasiukajtis	return (ret);
20925c28e8Piotr Jasiukajtis}
21025c28e8Piotr Jasiukajtis
2117f11fd0Richard Loweextern __GNU_INLINE double
21225c28e8Piotr Jasiukajtisfloor(double d)
21325c28e8Piotr Jasiukajtis{
21425c28e8Piotr Jasiukajtis	short rd = __swap87RD(fp_negative);
21525c28e8Piotr Jasiukajtis
21625c28e8Piotr Jasiukajtis	__asm__ __volatile__("frndint" : "+t" (d), "+r" (rd) : : "cc");
21725c28e8Piotr Jasiukajtis	__swap87RD(rd);
21825c28e8Piotr Jasiukajtis
21925c28e8Piotr Jasiukajtis	return (d);
22025c28e8Piotr Jasiukajtis}
22125c28e8Piotr Jasiukajtis
22225c28e8Piotr Jasiukajtis/*
22325c28e8Piotr Jasiukajtis *      branchless __isnan
22425c28e8Piotr Jasiukajtis *      ((0x7ff00000-[((lx|-lx)>>31)&1]|ahx)>>31)&1 = 1 iff x is NaN
22525c28e8Piotr Jasiukajtis */
2267f11fd0Richard Loweextern __GNU_INLINE int
22725c28e8Piotr Jasiukajtisisnan(double d)
22825c28e8Piotr Jasiukajtis{
22925c28e8Piotr Jasiukajtis	int ret;
23025c28e8Piotr Jasiukajtis
23125c28e8Piotr Jasiukajtis	__asm__ __volatile__(
2327f11fd0Richard Lowe	    "movl %1,%%ecx\n\t"
2337f11fd0Richard Lowe	    "negl %%ecx\n\t"			/* ecx <-- -lo_32(x) */
2347f11fd0Richard Lowe	    "orl  %%ecx,%1\n\t"
2357f11fd0Richard Lowe	    "shrl $31,%1\n\t"			/* 1 iff lx != 0 */
2367f11fd0Richard Lowe	    "andl $0x7fffffff,%2\n\t"	/* ecx <-- hi_32(abs(x)) */
2377f11fd0Richard Lowe	    "orl  %2,%1\n\t"
2387f11fd0Richard Lowe	    "subl $0x7ff00000,%1\n\t"
2397f11fd0Richard Lowe	    "negl %1\n\t"
2407f11fd0Richard Lowe	    "shrl $31,%1\n\t"
2417f11fd0Richard Lowe	    : "=r" (ret)
2427f11fd0Richard Lowe	    : "0" (_HI_WORD(d)), "r" (_LO_WORD(d))
2437f11fd0Richard Lowe	    : "ecx");
24425c28e8Piotr Jasiukajtis
24525c28e8Piotr Jasiukajtis	return (ret);
24625c28e8Piotr Jasiukajtis}
24725c28e8Piotr Jasiukajtis
2487f11fd0Richard Loweextern __GNU_INLINE int
24925c28e8Piotr Jasiukajtisisnanf(float f)
25025c28e8Piotr Jasiukajtis{
25125c28e8Piotr Jasiukajtis	__asm__ __volatile__(
25225c28e8Piotr Jasiukajtis	    "andl $0x7fffffff,%0\n\t"
25325c28e8Piotr Jasiukajtis	    "negl %0\n\t"
25425c28e8Piotr Jasiukajtis	    "addl $0x7f800000,%0\n\t"
25525c28e8Piotr Jasiukajtis	    "shrl $31,%0\n\t"
25625c28e8Piotr Jasiukajtis	    : "+r" (f)
25725c28e8Piotr Jasiukajtis	    :
25825c28e8Piotr Jasiukajtis	    : "cc");
25925c28e8Piotr Jasiukajtis
26025c28e8Piotr Jasiukajtis	return (f);
26125c28e8Piotr Jasiukajtis}
26225c28e8Piotr Jasiukajtis
2637f11fd0Richard Loweextern __GNU_INLINE double
26425c28e8Piotr Jasiukajtisrint(double a) {
26525c28e8Piotr Jasiukajtis    return (__inline_rint(a));
26625c28e8Piotr Jasiukajtis}
26725c28e8Piotr Jasiukajtis
2687f11fd0Richard Loweextern __GNU_INLINE double
26925c28e8Piotr Jasiukajtisscalbn(double d, int n)
27025c28e8Piotr Jasiukajtis{
27125c28e8Piotr Jasiukajtis	double dummy;
27225c28e8Piotr Jasiukajtis
27325c28e8Piotr Jasiukajtis	__asm__ __volatile__(
27425c28e8Piotr Jasiukajtis	    "fildl %2\n\t"	/* Convert N to extended */
27525c28e8Piotr Jasiukajtis	    "fxch\n\t"
27625c28e8Piotr Jasiukajtis	    "fscale\n\t"
27725c28e8Piotr Jasiukajtis	    : "+t" (d), "=u" (dummy)
27825c28e8Piotr Jasiukajtis	    : "m" (n)
27925c28e8Piotr Jasiukajtis	    : "cc");
28025c28e8Piotr Jasiukajtis
28125c28e8Piotr Jasiukajtis	return (d);
28225c28e8Piotr Jasiukajtis}
28325c28e8Piotr Jasiukajtis
2847f11fd0Richard Loweextern __GNU_INLINE int
28525c28e8Piotr Jasiukajtissignbit(double d)
28625c28e8Piotr Jasiukajtis{
28725c28e8Piotr Jasiukajtis	return (_HI_WORD(d) >> 31);
28825c28e8Piotr Jasiukajtis}
28925c28e8Piotr Jasiukajtis
2907f11fd0Richard Loweextern __GNU_INLINE int
29125c28e8Piotr Jasiukajtissignbitf(float f)
29225c28e8Piotr Jasiukajtis{
29325c28e8Piotr Jasiukajtis	return ((*(uint32_t *)&f) >> 31);
29425c28e8Piotr Jasiukajtis}
29525c28e8Piotr Jasiukajtis
2967f11fd0Richard Loweextern __GNU_INLINE double
29725c28e8Piotr Jasiukajtissqrt(double d)
29825c28e8Piotr Jasiukajtis{
29925c28e8Piotr Jasiukajtis	return (__inline_sqrt(d));
30025c28e8Piotr Jasiukajtis}
30125c28e8Piotr Jasiukajtis
3027f11fd0Richard Loweextern __GNU_INLINE float
30325c28e8Piotr Jasiukajtissqrtf(float f)
30425c28e8Piotr Jasiukajtis{
30525c28e8Piotr Jasiukajtis	return (__inline_sqrtf(f));
30625c28e8Piotr Jasiukajtis}
30725c28e8Piotr Jasiukajtis
3087f11fd0Richard Loweextern __GNU_INLINE long double
30925c28e8Piotr Jasiukajtissqrtl(long double ld)
31025c28e8Piotr Jasiukajtis{
31125c28e8Piotr Jasiukajtis	__asm__ __volatile__("fsqrt" : "+t" (ld) : : "cc");
31225c28e8Piotr Jasiukajtis	return (ld);
31325c28e8Piotr Jasiukajtis}
31425c28e8Piotr Jasiukajtis
3157f11fd0Richard Loweextern __GNU_INLINE int
31625c28e8Piotr Jasiukajtisisnanl(long double ld)
31725c28e8Piotr Jasiukajtis{
31825c28e8Piotr Jasiukajtis	int ret = _HIER_WORD(ld);
31925c28e8Piotr Jasiukajtis
32025c28e8Piotr Jasiukajtis	__asm__ __volatile__(
32125c28e8Piotr Jasiukajtis	    "andl  $0x00007fff,%0\n\t"
32225c28e8Piotr Jasiukajtis	    "jz	   1f\n\t"		/* jump if exp is all 0 */
32325c28e8Piotr Jasiukajtis	    "xorl  $0x00007fff,%0\n\t"
32425c28e8Piotr Jasiukajtis	    "jz	   2f\n\t"		/* jump if exp is all 1 */
32525c28e8Piotr Jasiukajtis	    "testl $0x80000000,%1\n\t"
32625c28e8Piotr Jasiukajtis	    "jz	   3f\n\t"		/* jump if leading bit is 0 */
32725c28e8Piotr Jasiukajtis	    "xorl  %0,%0\n\t"
32825c28e8Piotr Jasiukajtis	    "jmp   1f\n\t"
32925c28e8Piotr Jasiukajtis	    "2:\n\t"			/* note that %0 = 0 from before */
33025c28e8Piotr Jasiukajtis	    "cmpl  $0x80000000,%1\n\t"	/* what is first half of significand? */
33125c28e8Piotr Jasiukajtis	    "jnz   3f\n\t"		/* jump if not equal to 0x80000000 */
33225c28e8Piotr Jasiukajtis	    "testl $0xffffffff,%2\n\t"	/* is second half of significand 0? */
33325c28e8Piotr Jasiukajtis	    "jnz   3f\n\t"		/* jump if not equal to 0 */
33425c28e8Piotr Jasiukajtis	    "jmp   1f\n\t"
33525c28e8Piotr Jasiukajtis	    "3:\n\t"
33625c28e8Piotr Jasiukajtis	    "movl  $1,%0\n\t"
33725c28e8Piotr Jasiukajtis	    "1:\n\t"
33825c28e8Piotr Jasiukajtis	    : "+&r" (ret)
33925c28e8Piotr Jasiukajtis	    : "r" (_HI_WORD(ld)), "r" (_LO_WORD(ld))
34025c28e8Piotr Jasiukajtis	    : "cc");
34125c28e8Piotr Jasiukajtis
34225c28e8Piotr Jasiukajtis	return (ret);
34325c28e8Piotr Jasiukajtis}
34425c28e8Piotr Jasiukajtis
34525c28e8Piotr Jasiukajtis#ifdef __cplusplus
34625c28e8Piotr Jasiukajtis}
34725c28e8Piotr Jasiukajtis#endif
34825c28e8Piotr Jasiukajtis
34925c28e8Piotr Jasiukajtis#endif  /* __GNUC__ */
35025c28e8Piotr Jasiukajtis
35125c28e8Piotr Jasiukajtis#endif /* _LIBM_INLINES_H */
352