162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * fp_emu.h
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright Roman Zippel, 1997.  All rights reserved.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
762306a36Sopenharmony_ci * modification, are permitted provided that the following conditions
862306a36Sopenharmony_ci * are met:
962306a36Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright
1062306a36Sopenharmony_ci *    notice, and the entire permission notice in its entirety,
1162306a36Sopenharmony_ci *    including the disclaimer of warranties.
1262306a36Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
1362306a36Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in the
1462306a36Sopenharmony_ci *    documentation and/or other materials provided with the distribution.
1562306a36Sopenharmony_ci * 3. The name of the author may not be used to endorse or promote
1662306a36Sopenharmony_ci *    products derived from this software without specific prior
1762306a36Sopenharmony_ci *    written permission.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci * ALTERNATIVELY, this product may be distributed under the terms of
2062306a36Sopenharmony_ci * the GNU General Public License, in which case the provisions of the GPL are
2162306a36Sopenharmony_ci * required INSTEAD OF the above restrictions.  (This clause is
2262306a36Sopenharmony_ci * necessary due to a potential bad interaction between the GPL and
2362306a36Sopenharmony_ci * the restrictions contained in a BSD-style copyright.)
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
2662306a36Sopenharmony_ci * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2762306a36Sopenharmony_ci * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2862306a36Sopenharmony_ci * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
2962306a36Sopenharmony_ci * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
3062306a36Sopenharmony_ci * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
3162306a36Sopenharmony_ci * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3262306a36Sopenharmony_ci * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3362306a36Sopenharmony_ci * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3462306a36Sopenharmony_ci * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
3562306a36Sopenharmony_ci * OF THE POSSIBILITY OF SUCH DAMAGE.
3662306a36Sopenharmony_ci */
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci#ifndef _FP_EMU_H
3962306a36Sopenharmony_ci#define _FP_EMU_H
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci#ifdef __ASSEMBLY__
4262306a36Sopenharmony_ci#include <asm/asm-offsets.h>
4362306a36Sopenharmony_ci#endif
4462306a36Sopenharmony_ci#include <asm/math-emu.h>
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci#ifndef __ASSEMBLY__
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci#define IS_INF(a) ((a)->exp == 0x7fff)
4962306a36Sopenharmony_ci#define IS_ZERO(a) ((a)->mant.m64 == 0)
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci#define fp_set_sr(bit) ({					\
5362306a36Sopenharmony_ci	FPDATA->fpsr |= 1 << (bit);				\
5462306a36Sopenharmony_ci})
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci#define fp_set_quotient(quotient) ({				\
5762306a36Sopenharmony_ci	FPDATA->fpsr &= 0xff00ffff;				\
5862306a36Sopenharmony_ci	FPDATA->fpsr |= ((quotient) & 0xff) << 16;		\
5962306a36Sopenharmony_ci})
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci/* linkage for several useful functions */
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci/* Normalize the extended struct, return 0 for a NaN */
6462306a36Sopenharmony_ci#define fp_normalize_ext(fpreg) ({				\
6562306a36Sopenharmony_ci	register struct fp_ext *reg asm ("a0") = fpreg;		\
6662306a36Sopenharmony_ci	register int res asm ("d0");				\
6762306a36Sopenharmony_ci								\
6862306a36Sopenharmony_ci	asm volatile ("jsr fp_conv_ext2ext"			\
6962306a36Sopenharmony_ci			: "=d" (res) : "a" (reg)		\
7062306a36Sopenharmony_ci			: "a1", "d1", "d2", "memory");		\
7162306a36Sopenharmony_ci	res;							\
7262306a36Sopenharmony_ci})
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci#define fp_copy_ext(dest, src) ({				\
7562306a36Sopenharmony_ci	*dest = *src;						\
7662306a36Sopenharmony_ci})
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci#define fp_monadic_check(dest, src) ({				\
7962306a36Sopenharmony_ci	fp_copy_ext(dest, src);					\
8062306a36Sopenharmony_ci	if (!fp_normalize_ext(dest))				\
8162306a36Sopenharmony_ci		return dest;					\
8262306a36Sopenharmony_ci})
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci#define fp_dyadic_check(dest, src) ({				\
8562306a36Sopenharmony_ci	if (!fp_normalize_ext(dest))				\
8662306a36Sopenharmony_ci		return dest;					\
8762306a36Sopenharmony_ci	if (!fp_normalize_ext(src)) {				\
8862306a36Sopenharmony_ci		fp_copy_ext(dest, src);				\
8962306a36Sopenharmony_ci		return dest;					\
9062306a36Sopenharmony_ci	}							\
9162306a36Sopenharmony_ci})
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ciextern const struct fp_ext fp_QNaN;
9462306a36Sopenharmony_ciextern const struct fp_ext fp_Inf;
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci#define fp_set_nan(dest) ({					\
9762306a36Sopenharmony_ci	fp_set_sr(FPSR_EXC_OPERR);				\
9862306a36Sopenharmony_ci	*dest = fp_QNaN;					\
9962306a36Sopenharmony_ci})
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci/* TODO check rounding mode? */
10262306a36Sopenharmony_ci#define fp_set_ovrflw(dest) ({					\
10362306a36Sopenharmony_ci	fp_set_sr(FPSR_EXC_OVFL);				\
10462306a36Sopenharmony_ci	dest->exp = 0x7fff;					\
10562306a36Sopenharmony_ci	dest->mant.m64 = 0;					\
10662306a36Sopenharmony_ci})
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci#define fp_conv_ext2long(src) ({				\
10962306a36Sopenharmony_ci	register struct fp_ext *__src asm ("a0") = src;		\
11062306a36Sopenharmony_ci	register int __res asm ("d0");				\
11162306a36Sopenharmony_ci								\
11262306a36Sopenharmony_ci	asm volatile ("jsr fp_conv_ext2long"			\
11362306a36Sopenharmony_ci			: "=d" (__res) : "a" (__src)		\
11462306a36Sopenharmony_ci			: "a1", "d1", "d2", "memory");		\
11562306a36Sopenharmony_ci	__res;							\
11662306a36Sopenharmony_ci})
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci#define fp_conv_long2ext(dest, src) ({				\
11962306a36Sopenharmony_ci	register struct fp_ext *__dest asm ("a0") = dest;	\
12062306a36Sopenharmony_ci	register int __src asm ("d0") = src;			\
12162306a36Sopenharmony_ci								\
12262306a36Sopenharmony_ci	asm volatile ("jsr fp_conv_ext2long"			\
12362306a36Sopenharmony_ci			: : "d" (__src), "a" (__dest)		\
12462306a36Sopenharmony_ci			: "a1", "d1", "d2", "memory");		\
12562306a36Sopenharmony_ci})
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci#else /* __ASSEMBLY__ */
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci/*
13062306a36Sopenharmony_ci * set, reset or clear a bit in the fp status register
13162306a36Sopenharmony_ci */
13262306a36Sopenharmony_ci.macro	fp_set_sr	bit
13362306a36Sopenharmony_ci	bset	#(\bit&7),(FPD_FPSR+3-(\bit/8),FPDATA)
13462306a36Sopenharmony_ci.endm
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci.macro	fp_clr_sr	bit
13762306a36Sopenharmony_ci	bclr	#(\bit&7),(FPD_FPSR+3-(\bit/8),FPDATA)
13862306a36Sopenharmony_ci.endm
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci.macro	fp_tst_sr	bit
14162306a36Sopenharmony_ci	btst	#(\bit&7),(FPD_FPSR+3-(\bit/8),FPDATA)
14262306a36Sopenharmony_ci.endm
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci#endif /* __ASSEMBLY__ */
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci#endif /* _FP_EMU_H */
147