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#include <sys/types.h>
3925c28e8Piotr Jasiukajtis#include <sys/ieeefp.h>
4025c28e8Piotr Jasiukajtis
4125c28e8Piotr Jasiukajtis#ifdef __cplusplus
4225c28e8Piotr Jasiukajtisextern "C" {
4325c28e8Piotr Jasiukajtis#endif
4425c28e8Piotr Jasiukajtis
457f11fd0Richard Loweextern __GNU_INLINE enum fp_class_type
4625c28e8Piotr Jasiukajtisfp_classf(float f)
4725c28e8Piotr Jasiukajtis{
4825c28e8Piotr Jasiukajtis	enum fp_class_type ret;
4925c28e8Piotr Jasiukajtis	int fint;		/* scratch for f as int */
5025c28e8Piotr Jasiukajtis	uint64_t tmp;
5125c28e8Piotr Jasiukajtis
5225c28e8Piotr Jasiukajtis	__asm__ __volatile__(
5325c28e8Piotr Jasiukajtis	    "fabss  %3,%3\n\t"
5425c28e8Piotr Jasiukajtis	    "st	    %3,%1\n\t"
5525c28e8Piotr Jasiukajtis	    "ld	    %1,%0\n\t"
5625c28e8Piotr Jasiukajtis	    "orcc   %%g0,%0,%%g0\n\t"
5725c28e8Piotr Jasiukajtis	    "be,pn  %%icc,2f\n\t"
5825c28e8Piotr Jasiukajtis	    "nop\n\t"
5925c28e8Piotr Jasiukajtis	    "1:\n\t"
6025c28e8Piotr Jasiukajtis	    "sethi  %%hi(0x7f800000),%2\n\t"
6125c28e8Piotr Jasiukajtis	    "andcc  %0,%2,%%g0\n\t"
6225c28e8Piotr Jasiukajtis	    "bne,pt %%icc,1f\n\t"
6325c28e8Piotr Jasiukajtis	    "nop\n\t"
6425c28e8Piotr Jasiukajtis	    "or	    %%g0,1,%0\n\t"
6525c28e8Piotr Jasiukajtis	    "ba	    2f\n\t"	/* subnormal */
6625c28e8Piotr Jasiukajtis	    "nop\n\t"
6725c28e8Piotr Jasiukajtis	    "1:\n\t"
6825c28e8Piotr Jasiukajtis	    "subcc  %0,%2,%%g0\n\t"
6925c28e8Piotr Jasiukajtis	    "bge,pn %%icc,1f\n\t"
7025c28e8Piotr Jasiukajtis	    "nop\n\t"
7125c28e8Piotr Jasiukajtis	    "or	    %%g0,2,%0\n\t"
7225c28e8Piotr Jasiukajtis	    "ba	    2f\n\t"	/* normal */
7325c28e8Piotr Jasiukajtis	    "nop\n\t"
7425c28e8Piotr Jasiukajtis	    "1:\n\t"
7525c28e8Piotr Jasiukajtis	    "bg,pn  %%icc,1f\n\t"
7625c28e8Piotr Jasiukajtis	    "nop\n\t"
7725c28e8Piotr Jasiukajtis	    "or	    %%g0,3,%0\n\t"
7825c28e8Piotr Jasiukajtis	    "ba	    2f\n\t"	/* infinity */
7925c28e8Piotr Jasiukajtis	    "nop\n\t"
8025c28e8Piotr Jasiukajtis	    "1:\n\t"
8125c28e8Piotr Jasiukajtis	    "sethi  %%hi(0x00400000),%2\n\t"
8225c28e8Piotr Jasiukajtis	    "andcc  %0,%2,%%g0\n\t"
8325c28e8Piotr Jasiukajtis	    "or	    %%g0,4,%0\n\t"
8425c28e8Piotr Jasiukajtis	    "bne,pt %%icc,2f\n\t" /* quiet NaN */
8525c28e8Piotr Jasiukajtis	    "nop\n\t"
8625c28e8Piotr Jasiukajtis	    "or	    %%g0,5,%0\n\t" /* signalling NaN */
8725c28e8Piotr Jasiukajtis	    "2:\n\t"
8825c28e8Piotr Jasiukajtis	    : "=r" (ret), "=m" (fint), "=r" (tmp), "+f" (f)
8925c28e8Piotr Jasiukajtis	    :
9025c28e8Piotr Jasiukajtis	    : "cc");
9125c28e8Piotr Jasiukajtis
9225c28e8Piotr Jasiukajtis	return (ret);
9325c28e8Piotr Jasiukajtis}
9425c28e8Piotr Jasiukajtis
957f11fd0Richard Loweextern __GNU_INLINE enum fp_class_type
9625c28e8Piotr Jasiukajtisfp_class(double d)
9725c28e8Piotr Jasiukajtis{
9825c28e8Piotr Jasiukajtis	enum fp_class_type ret;
9925c28e8Piotr Jasiukajtis	uint64_t dint;		/* Scratch for d-as-long */
10025c28e8Piotr Jasiukajtis	uint64_t tmp;
10125c28e8Piotr Jasiukajtis
10225c28e8Piotr Jasiukajtis	__asm__ __volatile__(
10325c28e8Piotr Jasiukajtis	    "fabsd  %3,%3\n\t"
10425c28e8Piotr Jasiukajtis	    "std    %3,%1\n\t"
10525c28e8Piotr Jasiukajtis	    "ldx    %1,%0\n\t"
10625c28e8Piotr Jasiukajtis	    "orcc   %%g0,%0,%%g0\n\t"
10725c28e8Piotr Jasiukajtis	    "be,pn  %%xcc,2f\n\t"
10825c28e8Piotr Jasiukajtis	    "nop\n\t"
10925c28e8Piotr Jasiukajtis	    "sethi  %%hi(0x7ff00000),%2\n\t"
11025c28e8Piotr Jasiukajtis	    "sllx   %2,32,%2\n\t"
11125c28e8Piotr Jasiukajtis	    "andcc  %0,%2,%%g0\n\t"
11225c28e8Piotr Jasiukajtis	    "bne,pt %%xcc,1f\n\t"
11325c28e8Piotr Jasiukajtis	    "nop\n\t"
11425c28e8Piotr Jasiukajtis	    "or	    %%g0,1,%0\n\t"
11525c28e8Piotr Jasiukajtis	    "ba	    2f\n\t"
11625c28e8Piotr Jasiukajtis	    "nop\n\t"
11725c28e8Piotr Jasiukajtis	    "1:\n\t"
11825c28e8Piotr Jasiukajtis	    "subcc  %0,%2,%%g0\n\t"
11925c28e8Piotr Jasiukajtis	    "bge,pn %%xcc,1f\n\t"
12025c28e8Piotr Jasiukajtis	    "nop\n\t"
12125c28e8Piotr Jasiukajtis	    "or	    %%g0,2,%0\n\t"
12225c28e8Piotr Jasiukajtis	    "ba	    2f\n\t"
12325c28e8Piotr Jasiukajtis	    "nop\n\t"
12425c28e8Piotr Jasiukajtis	    "1:\n\t"
12525c28e8Piotr Jasiukajtis	    "andncc %0,%2,%0\n\t"
12625c28e8Piotr Jasiukajtis	    "bne,pn %%xcc,1f\n\t"
12725c28e8Piotr Jasiukajtis	    "nop\n\t"
12825c28e8Piotr Jasiukajtis	    "or	    %%g0,3,%0\n\t"
12925c28e8Piotr Jasiukajtis	    "ba	    2f\n\t"
13025c28e8Piotr Jasiukajtis	    "nop\n\t"
13125c28e8Piotr Jasiukajtis	    "1:\n\t"
13225c28e8Piotr Jasiukajtis	    "sethi  %%hi(0x00080000),%2\n\t"
13325c28e8Piotr Jasiukajtis	    "sllx   %2,32,%2\n\t"
13425c28e8Piotr Jasiukajtis	    "andcc  %0,%2,%%g0\n\t"
13525c28e8Piotr Jasiukajtis	    "or	    %%g0,4,%0\n\t"
13625c28e8Piotr Jasiukajtis	    "bne,pt %%xcc,2f\n\t"
13725c28e8Piotr Jasiukajtis	    "nop\n\t"
13825c28e8Piotr Jasiukajtis	    "or	    %%g0,5,%0\n\t"
13925c28e8Piotr Jasiukajtis	    "2:\n\t"
14025c28e8Piotr Jasiukajtis	    : "=r" (ret), "=m" (dint), "=r" (tmp), "+e" (d)
14125c28e8Piotr Jasiukajtis	    :
14225c28e8Piotr Jasiukajtis	    : "cc");
14325c28e8Piotr Jasiukajtis
14425c28e8Piotr Jasiukajtis	return (ret);
14525c28e8Piotr Jasiukajtis}
14625c28e8Piotr Jasiukajtis
1477f11fd0Richard Loweextern __GNU_INLINE float
14825c28e8Piotr Jasiukajtis__inline_sqrtf(float f)
14925c28e8Piotr Jasiukajtis{
15025c28e8Piotr Jasiukajtis	float ret;
15125c28e8Piotr Jasiukajtis
15225c28e8Piotr Jasiukajtis	__asm__ __volatile__("fsqrts %1,%0\n\t" : "=f" (ret) : "f" (f));
15325c28e8Piotr Jasiukajtis	return (ret);
15425c28e8Piotr Jasiukajtis}
15525c28e8Piotr Jasiukajtis
1567f11fd0Richard Loweextern __GNU_INLINE double
15725c28e8Piotr Jasiukajtis__inline_sqrt(double d)
15825c28e8Piotr Jasiukajtis{
15925c28e8Piotr Jasiukajtis	double ret;
16025c28e8Piotr Jasiukajtis
16125c28e8Piotr Jasiukajtis	__asm__ __volatile__("fsqrtd %1,%0\n\t" : "=f" (ret) : "f" (d));
16225c28e8Piotr Jasiukajtis	return (ret);
16325c28e8Piotr Jasiukajtis}
16425c28e8Piotr Jasiukajtis
1657f11fd0Richard Loweextern __GNU_INLINE int
16625c28e8Piotr Jasiukajtis__swapEX(int i)
16725c28e8Piotr Jasiukajtis{
16825c28e8Piotr Jasiukajtis	int ret;
16925c28e8Piotr Jasiukajtis	uint32_t fsr;
17025c28e8Piotr Jasiukajtis	uint64_t tmp1, tmp2;
17125c28e8Piotr Jasiukajtis
17225c28e8Piotr Jasiukajtis	__asm__ __volatile__(
17325c28e8Piotr Jasiukajtis	    "and  %4,0x1f,%2\n\t"
17425c28e8Piotr Jasiukajtis	    "sll  %2,5,%2\n\t"	/* shift input to aexc bit location */
17525c28e8Piotr Jasiukajtis	    ".volatile\n\t"
17625c28e8Piotr Jasiukajtis	    "st   %%fsr,%1\n\t"
17725c28e8Piotr Jasiukajtis	    "ld	  %1,%0\n\t"	/* %0 = fsr */
17825c28e8Piotr Jasiukajtis	    "andn %0,0x3e0,%3\n\t"
17925c28e8Piotr Jasiukajtis	    "or   %2,%3,%2\n\t"	/* %2 = new fsr */
18025c28e8Piotr Jasiukajtis	    "st	  %2,%1\n\t"
18125c28e8Piotr Jasiukajtis	    "ld	  %1,%%fsr\n\t"
18225c28e8Piotr Jasiukajtis	    "srl  %0,5,%0\n\t"
18325c28e8Piotr Jasiukajtis	    "and  %0,0x1f,%0\n\t"
18425c28e8Piotr Jasiukajtis	    ".nonvolatile\n\t"
18525c28e8Piotr Jasiukajtis	    : "=r" (ret), "=m" (fsr), "=r" (tmp1), "=r" (tmp2)
18625c28e8Piotr Jasiukajtis	    : "r" (i)
18725c28e8Piotr Jasiukajtis	    : "cc");
18825c28e8Piotr Jasiukajtis
18925c28e8Piotr Jasiukajtis	return (ret);
19025c28e8Piotr Jasiukajtis}
19125c28e8Piotr Jasiukajtis
19225c28e8Piotr Jasiukajtis/*
19325c28e8Piotr Jasiukajtis * On the SPARC, __swapRP is a no-op; always return 0 for backward
19425c28e8Piotr Jasiukajtis * compatibility
19525c28e8Piotr Jasiukajtis */
19625c28e8Piotr Jasiukajtis/* ARGSUSED */
1977f11fd0Richard Loweextern __GNU_INLINE enum fp_precision_type
19825c28e8Piotr Jasiukajtis__swapRP(enum fp_precision_type i)
19925c28e8Piotr Jasiukajtis{
20025c28e8Piotr Jasiukajtis	return (0);
20125c28e8Piotr Jasiukajtis}
20225c28e8Piotr Jasiukajtis
2037f11fd0Richard Loweextern __GNU_INLINE enum fp_direction_type
20425c28e8Piotr Jasiukajtis__swapRD(enum fp_direction_type d)
20525c28e8Piotr Jasiukajtis{
20625c28e8Piotr Jasiukajtis	enum fp_direction_type ret;
20725c28e8Piotr Jasiukajtis	uint32_t fsr;
20825c28e8Piotr Jasiukajtis	uint64_t tmp1, tmp2, tmp3;
20925c28e8Piotr Jasiukajtis
21025c28e8Piotr Jasiukajtis	__asm__ __volatile__(
21125c28e8Piotr Jasiukajtis	    "and   %5,0x3,%0\n\t"
21225c28e8Piotr Jasiukajtis	    "sll   %0,30,%2\n\t"	/* shift input to RD bit location */
21325c28e8Piotr Jasiukajtis	    ".volatile\n\t"
21425c28e8Piotr Jasiukajtis	    "st    %%fsr,%1\n\t"
21525c28e8Piotr Jasiukajtis	    "ld	   %1,%0\n\t"		/* %0 = fsr */
21625c28e8Piotr Jasiukajtis	    /* mask of rounding direction bits */
21725c28e8Piotr Jasiukajtis	    "sethi %%hi(0xc0000000),%4\n\t"
21825c28e8Piotr Jasiukajtis	    "andn  %0,%4,%3\n\t"
21925c28e8Piotr Jasiukajtis	    "or    %2,%3,%2\n\t"	/* %2 = new fsr */
22025c28e8Piotr Jasiukajtis	    "st	   %2,%1\n\t"
22125c28e8Piotr Jasiukajtis	    "ld	   %1,%%fsr\n\t"
22225c28e8Piotr Jasiukajtis	    "srl   %0,30,%0\n\t"
22325c28e8Piotr Jasiukajtis	    "and   %0,0x3,%0\n\t"
22425c28e8Piotr Jasiukajtis	    ".nonvolatile\n\t"
22525c28e8Piotr Jasiukajtis	    : "=r" (ret), "=m" (fsr), "=r" (tmp1), "=r" (tmp2), "=r" (tmp3)
22625c28e8Piotr Jasiukajtis	    : "r" (d)
22725c28e8Piotr Jasiukajtis	    : "cc");
22825c28e8Piotr Jasiukajtis
22925c28e8Piotr Jasiukajtis	return (ret);
23025c28e8Piotr Jasiukajtis}
23125c28e8Piotr Jasiukajtis
2327f11fd0Richard Loweextern __GNU_INLINE int
23325c28e8Piotr Jasiukajtis__swapTE(int i)
23425c28e8Piotr Jasiukajtis{
23525c28e8Piotr Jasiukajtis	int ret;
23625c28e8Piotr Jasiukajtis	uint32_t fsr;
23725c28e8Piotr Jasiukajtis	uint64_t tmp1, tmp2, tmp3;
23825c28e8Piotr Jasiukajtis
23925c28e8Piotr Jasiukajtis	__asm__ __volatile__(
24025c28e8Piotr Jasiukajtis	    "and   %5,0x1f,%0\n\t"
24125c28e8Piotr Jasiukajtis	    "sll   %0,23,%2\n\t"	/* shift input to TEM bit location */
24225c28e8Piotr Jasiukajtis	    ".volatile\n\t"
24325c28e8Piotr Jasiukajtis	    "st    %%fsr,%1\n\t"
24425c28e8Piotr Jasiukajtis	    "ld	   %1,%0\n\t"		/* %0 = fsr */
24525c28e8Piotr Jasiukajtis	    /* mask of TEM (Trap Enable Mode bits) */
24625c28e8Piotr Jasiukajtis	    "sethi %%hi(0x0f800000),%4\n\t"
24725c28e8Piotr Jasiukajtis	    "andn  %0,%4,%3\n\t"
24825c28e8Piotr Jasiukajtis	    "or    %2,%3,%2\n\t"	/* %2 = new fsr */
24925c28e8Piotr Jasiukajtis	    "st	   %2,%1\n\t"
25025c28e8Piotr Jasiukajtis	    "ld	   %1,%%fsr\n\t"
25125c28e8Piotr Jasiukajtis	    "srl   %0,23,%0\n\t"
25225c28e8Piotr Jasiukajtis	    "and   %0,0x1f,%0\n\t"
25325c28e8Piotr Jasiukajtis	    ".nonvolatile\n\t"
25425c28e8Piotr Jasiukajtis	    : "=r" (ret), "=m" (fsr), "=r" (tmp1), "=r" (tmp2), "=r" (tmp3)
25525c28e8Piotr Jasiukajtis	    : "r" (i)
25625c28e8Piotr Jasiukajtis	    : "cc");
25725c28e8Piotr Jasiukajtis
25825c28e8Piotr Jasiukajtis	return (ret);
25925c28e8Piotr Jasiukajtis}
26025c28e8Piotr Jasiukajtis
26125c28e8Piotr Jasiukajtis
2627f11fd0Richard Loweextern __GNU_INLINE double
26325c28e8Piotr Jasiukajtissqrt(double d)
26425c28e8Piotr Jasiukajtis{
26525c28e8Piotr Jasiukajtis	return (__inline_sqrt(d));
26625c28e8Piotr Jasiukajtis}
26725c28e8Piotr Jasiukajtis
2687f11fd0Richard Loweextern __GNU_INLINE float
26925c28e8Piotr Jasiukajtissqrtf(float f)
27025c28e8Piotr Jasiukajtis{
27125c28e8Piotr Jasiukajtis	return (__inline_sqrtf(f));
27225c28e8Piotr Jasiukajtis}
27325c28e8Piotr Jasiukajtis
2747f11fd0Richard Loweextern __GNU_INLINE double
27525c28e8Piotr Jasiukajtisfabs(double d)
27625c28e8Piotr Jasiukajtis{
27725c28e8Piotr Jasiukajtis	double ret;
27825c28e8Piotr Jasiukajtis
27925c28e8Piotr Jasiukajtis	__asm__ __volatile__("fabsd %1,%0\n\t" : "=e" (ret) : "e" (d));
28025c28e8Piotr Jasiukajtis	return (ret);
28125c28e8Piotr Jasiukajtis}
28225c28e8Piotr Jasiukajtis
2837f11fd0Richard Loweextern __GNU_INLINE float
28425c28e8Piotr Jasiukajtisfabsf(float f)
28525c28e8Piotr Jasiukajtis{
28625c28e8Piotr Jasiukajtis	float ret;
28725c28e8Piotr Jasiukajtis
28825c28e8Piotr Jasiukajtis	__asm__ __volatile__("fabss %1,%0\n\t" : "=f" (ret) : "f" (f));
28925c28e8Piotr Jasiukajtis	return (ret);
29025c28e8Piotr Jasiukajtis}
29125c28e8Piotr Jasiukajtis
29225c28e8Piotr Jasiukajtis#ifdef __cplusplus
29325c28e8Piotr Jasiukajtis}
29425c28e8Piotr Jasiukajtis#endif
29525c28e8Piotr Jasiukajtis
29625c28e8Piotr Jasiukajtis#endif  /* __GNUC__ */
29725c28e8Piotr Jasiukajtis
29825c28e8Piotr Jasiukajtis#endif /* _LIBM_INLINES_H */