18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * fp_emu.h
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright Roman Zippel, 1997.  All rights reserved.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
78c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions
88c2ecf20Sopenharmony_ci * are met:
98c2ecf20Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright
108c2ecf20Sopenharmony_ci *    notice, and the entire permission notice in its entirety,
118c2ecf20Sopenharmony_ci *    including the disclaimer of warranties.
128c2ecf20Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
138c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in the
148c2ecf20Sopenharmony_ci *    documentation and/or other materials provided with the distribution.
158c2ecf20Sopenharmony_ci * 3. The name of the author may not be used to endorse or promote
168c2ecf20Sopenharmony_ci *    products derived from this software without specific prior
178c2ecf20Sopenharmony_ci *    written permission.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * ALTERNATIVELY, this product may be distributed under the terms of
208c2ecf20Sopenharmony_ci * the GNU General Public License, in which case the provisions of the GPL are
218c2ecf20Sopenharmony_ci * required INSTEAD OF the above restrictions.  (This clause is
228c2ecf20Sopenharmony_ci * necessary due to a potential bad interaction between the GPL and
238c2ecf20Sopenharmony_ci * the restrictions contained in a BSD-style copyright.)
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
268c2ecf20Sopenharmony_ci * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
278c2ecf20Sopenharmony_ci * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
288c2ecf20Sopenharmony_ci * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
298c2ecf20Sopenharmony_ci * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
308c2ecf20Sopenharmony_ci * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
318c2ecf20Sopenharmony_ci * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
328c2ecf20Sopenharmony_ci * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
338c2ecf20Sopenharmony_ci * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
348c2ecf20Sopenharmony_ci * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
358c2ecf20Sopenharmony_ci * OF THE POSSIBILITY OF SUCH DAMAGE.
368c2ecf20Sopenharmony_ci */
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#ifndef _FP_EMU_H
398c2ecf20Sopenharmony_ci#define _FP_EMU_H
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#ifdef __ASSEMBLY__
428c2ecf20Sopenharmony_ci#include <asm/asm-offsets.h>
438c2ecf20Sopenharmony_ci#endif
448c2ecf20Sopenharmony_ci#include <asm/math-emu.h>
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define IS_INF(a) ((a)->exp == 0x7fff)
498c2ecf20Sopenharmony_ci#define IS_ZERO(a) ((a)->mant.m64 == 0)
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#define fp_set_sr(bit) ({					\
538c2ecf20Sopenharmony_ci	FPDATA->fpsr |= 1 << (bit);				\
548c2ecf20Sopenharmony_ci})
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#define fp_set_quotient(quotient) ({				\
578c2ecf20Sopenharmony_ci	FPDATA->fpsr &= 0xff00ffff;				\
588c2ecf20Sopenharmony_ci	FPDATA->fpsr |= ((quotient) & 0xff) << 16;		\
598c2ecf20Sopenharmony_ci})
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci/* linkage for several useful functions */
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci/* Normalize the extended struct, return 0 for a NaN */
648c2ecf20Sopenharmony_ci#define fp_normalize_ext(fpreg) ({				\
658c2ecf20Sopenharmony_ci	register struct fp_ext *reg asm ("a0") = fpreg;		\
668c2ecf20Sopenharmony_ci	register int res asm ("d0");				\
678c2ecf20Sopenharmony_ci								\
688c2ecf20Sopenharmony_ci	asm volatile ("jsr fp_conv_ext2ext"			\
698c2ecf20Sopenharmony_ci			: "=d" (res) : "a" (reg)		\
708c2ecf20Sopenharmony_ci			: "a1", "d1", "d2", "memory");		\
718c2ecf20Sopenharmony_ci	res;							\
728c2ecf20Sopenharmony_ci})
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci#define fp_copy_ext(dest, src) ({				\
758c2ecf20Sopenharmony_ci	*dest = *src;						\
768c2ecf20Sopenharmony_ci})
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci#define fp_monadic_check(dest, src) ({				\
798c2ecf20Sopenharmony_ci	fp_copy_ext(dest, src);					\
808c2ecf20Sopenharmony_ci	if (!fp_normalize_ext(dest))				\
818c2ecf20Sopenharmony_ci		return dest;					\
828c2ecf20Sopenharmony_ci})
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#define fp_dyadic_check(dest, src) ({				\
858c2ecf20Sopenharmony_ci	if (!fp_normalize_ext(dest))				\
868c2ecf20Sopenharmony_ci		return dest;					\
878c2ecf20Sopenharmony_ci	if (!fp_normalize_ext(src)) {				\
888c2ecf20Sopenharmony_ci		fp_copy_ext(dest, src);				\
898c2ecf20Sopenharmony_ci		return dest;					\
908c2ecf20Sopenharmony_ci	}							\
918c2ecf20Sopenharmony_ci})
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ciextern const struct fp_ext fp_QNaN;
948c2ecf20Sopenharmony_ciextern const struct fp_ext fp_Inf;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci#define fp_set_nan(dest) ({					\
978c2ecf20Sopenharmony_ci	fp_set_sr(FPSR_EXC_OPERR);				\
988c2ecf20Sopenharmony_ci	*dest = fp_QNaN;					\
998c2ecf20Sopenharmony_ci})
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/* TODO check rounding mode? */
1028c2ecf20Sopenharmony_ci#define fp_set_ovrflw(dest) ({					\
1038c2ecf20Sopenharmony_ci	fp_set_sr(FPSR_EXC_OVFL);				\
1048c2ecf20Sopenharmony_ci	dest->exp = 0x7fff;					\
1058c2ecf20Sopenharmony_ci	dest->mant.m64 = 0;					\
1068c2ecf20Sopenharmony_ci})
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci#define fp_conv_ext2long(src) ({				\
1098c2ecf20Sopenharmony_ci	register struct fp_ext *__src asm ("a0") = src;		\
1108c2ecf20Sopenharmony_ci	register int __res asm ("d0");				\
1118c2ecf20Sopenharmony_ci								\
1128c2ecf20Sopenharmony_ci	asm volatile ("jsr fp_conv_ext2long"			\
1138c2ecf20Sopenharmony_ci			: "=d" (__res) : "a" (__src)		\
1148c2ecf20Sopenharmony_ci			: "a1", "d1", "d2", "memory");		\
1158c2ecf20Sopenharmony_ci	__res;							\
1168c2ecf20Sopenharmony_ci})
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci#define fp_conv_long2ext(dest, src) ({				\
1198c2ecf20Sopenharmony_ci	register struct fp_ext *__dest asm ("a0") = dest;	\
1208c2ecf20Sopenharmony_ci	register int __src asm ("d0") = src;			\
1218c2ecf20Sopenharmony_ci								\
1228c2ecf20Sopenharmony_ci	asm volatile ("jsr fp_conv_ext2long"			\
1238c2ecf20Sopenharmony_ci			: : "d" (__src), "a" (__dest)		\
1248c2ecf20Sopenharmony_ci			: "a1", "d1", "d2", "memory");		\
1258c2ecf20Sopenharmony_ci})
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci#else /* __ASSEMBLY__ */
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci/*
1308c2ecf20Sopenharmony_ci * set, reset or clear a bit in the fp status register
1318c2ecf20Sopenharmony_ci */
1328c2ecf20Sopenharmony_ci.macro	fp_set_sr	bit
1338c2ecf20Sopenharmony_ci	bset	#(\bit&7),(FPD_FPSR+3-(\bit/8),FPDATA)
1348c2ecf20Sopenharmony_ci.endm
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci.macro	fp_clr_sr	bit
1378c2ecf20Sopenharmony_ci	bclr	#(\bit&7),(FPD_FPSR+3-(\bit/8),FPDATA)
1388c2ecf20Sopenharmony_ci.endm
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci.macro	fp_tst_sr	bit
1418c2ecf20Sopenharmony_ci	btst	#(\bit&7),(FPD_FPSR+3-(\bit/8),FPDATA)
1428c2ecf20Sopenharmony_ci.endm
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci#endif /* __ASSEMBLY__ */
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci#endif /* _FP_EMU_H */
147