xref: /OK3568_Linux_fs/kernel/arch/m68k/math-emu/fp_emu.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * fp_emu.h
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright Roman Zippel, 1997.  All rights reserved.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
7*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
8*4882a593Smuzhiyun  * are met:
9*4882a593Smuzhiyun  * 1. Redistributions of source code must retain the above copyright
10*4882a593Smuzhiyun  *    notice, and the entire permission notice in its entirety,
11*4882a593Smuzhiyun  *    including the disclaimer of warranties.
12*4882a593Smuzhiyun  * 2. Redistributions in binary form must reproduce the above copyright
13*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer in the
14*4882a593Smuzhiyun  *    documentation and/or other materials provided with the distribution.
15*4882a593Smuzhiyun  * 3. The name of the author may not be used to endorse or promote
16*4882a593Smuzhiyun  *    products derived from this software without specific prior
17*4882a593Smuzhiyun  *    written permission.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * ALTERNATIVELY, this product may be distributed under the terms of
20*4882a593Smuzhiyun  * the GNU General Public License, in which case the provisions of the GPL are
21*4882a593Smuzhiyun  * required INSTEAD OF the above restrictions.  (This clause is
22*4882a593Smuzhiyun  * necessary due to a potential bad interaction between the GPL and
23*4882a593Smuzhiyun  * the restrictions contained in a BSD-style copyright.)
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26*4882a593Smuzhiyun  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27*4882a593Smuzhiyun  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28*4882a593Smuzhiyun  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
29*4882a593Smuzhiyun  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30*4882a593Smuzhiyun  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31*4882a593Smuzhiyun  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32*4882a593Smuzhiyun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33*4882a593Smuzhiyun  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34*4882a593Smuzhiyun  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
35*4882a593Smuzhiyun  * OF THE POSSIBILITY OF SUCH DAMAGE.
36*4882a593Smuzhiyun  */
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #ifndef _FP_EMU_H
39*4882a593Smuzhiyun #define _FP_EMU_H
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun #ifdef __ASSEMBLY__
42*4882a593Smuzhiyun #include <asm/asm-offsets.h>
43*4882a593Smuzhiyun #endif
44*4882a593Smuzhiyun #include <asm/math-emu.h>
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #ifndef __ASSEMBLY__
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #define IS_INF(a) ((a)->exp == 0x7fff)
49*4882a593Smuzhiyun #define IS_ZERO(a) ((a)->mant.m64 == 0)
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun #define fp_set_sr(bit) ({					\
53*4882a593Smuzhiyun 	FPDATA->fpsr |= 1 << (bit);				\
54*4882a593Smuzhiyun })
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #define fp_set_quotient(quotient) ({				\
57*4882a593Smuzhiyun 	FPDATA->fpsr &= 0xff00ffff;				\
58*4882a593Smuzhiyun 	FPDATA->fpsr |= ((quotient) & 0xff) << 16;		\
59*4882a593Smuzhiyun })
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /* linkage for several useful functions */
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun /* Normalize the extended struct, return 0 for a NaN */
64*4882a593Smuzhiyun #define fp_normalize_ext(fpreg) ({				\
65*4882a593Smuzhiyun 	register struct fp_ext *reg asm ("a0") = fpreg;		\
66*4882a593Smuzhiyun 	register int res asm ("d0");				\
67*4882a593Smuzhiyun 								\
68*4882a593Smuzhiyun 	asm volatile ("jsr fp_conv_ext2ext"			\
69*4882a593Smuzhiyun 			: "=d" (res) : "a" (reg)		\
70*4882a593Smuzhiyun 			: "a1", "d1", "d2", "memory");		\
71*4882a593Smuzhiyun 	res;							\
72*4882a593Smuzhiyun })
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun #define fp_copy_ext(dest, src) ({				\
75*4882a593Smuzhiyun 	*dest = *src;						\
76*4882a593Smuzhiyun })
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun #define fp_monadic_check(dest, src) ({				\
79*4882a593Smuzhiyun 	fp_copy_ext(dest, src);					\
80*4882a593Smuzhiyun 	if (!fp_normalize_ext(dest))				\
81*4882a593Smuzhiyun 		return dest;					\
82*4882a593Smuzhiyun })
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun #define fp_dyadic_check(dest, src) ({				\
85*4882a593Smuzhiyun 	if (!fp_normalize_ext(dest))				\
86*4882a593Smuzhiyun 		return dest;					\
87*4882a593Smuzhiyun 	if (!fp_normalize_ext(src)) {				\
88*4882a593Smuzhiyun 		fp_copy_ext(dest, src);				\
89*4882a593Smuzhiyun 		return dest;					\
90*4882a593Smuzhiyun 	}							\
91*4882a593Smuzhiyun })
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun extern const struct fp_ext fp_QNaN;
94*4882a593Smuzhiyun extern const struct fp_ext fp_Inf;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun #define fp_set_nan(dest) ({					\
97*4882a593Smuzhiyun 	fp_set_sr(FPSR_EXC_OPERR);				\
98*4882a593Smuzhiyun 	*dest = fp_QNaN;					\
99*4882a593Smuzhiyun })
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun /* TODO check rounding mode? */
102*4882a593Smuzhiyun #define fp_set_ovrflw(dest) ({					\
103*4882a593Smuzhiyun 	fp_set_sr(FPSR_EXC_OVFL);				\
104*4882a593Smuzhiyun 	dest->exp = 0x7fff;					\
105*4882a593Smuzhiyun 	dest->mant.m64 = 0;					\
106*4882a593Smuzhiyun })
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun #define fp_conv_ext2long(src) ({				\
109*4882a593Smuzhiyun 	register struct fp_ext *__src asm ("a0") = src;		\
110*4882a593Smuzhiyun 	register int __res asm ("d0");				\
111*4882a593Smuzhiyun 								\
112*4882a593Smuzhiyun 	asm volatile ("jsr fp_conv_ext2long"			\
113*4882a593Smuzhiyun 			: "=d" (__res) : "a" (__src)		\
114*4882a593Smuzhiyun 			: "a1", "d1", "d2", "memory");		\
115*4882a593Smuzhiyun 	__res;							\
116*4882a593Smuzhiyun })
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun #define fp_conv_long2ext(dest, src) ({				\
119*4882a593Smuzhiyun 	register struct fp_ext *__dest asm ("a0") = dest;	\
120*4882a593Smuzhiyun 	register int __src asm ("d0") = src;			\
121*4882a593Smuzhiyun 								\
122*4882a593Smuzhiyun 	asm volatile ("jsr fp_conv_ext2long"			\
123*4882a593Smuzhiyun 			: : "d" (__src), "a" (__dest)		\
124*4882a593Smuzhiyun 			: "a1", "d1", "d2", "memory");		\
125*4882a593Smuzhiyun })
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun #else /* __ASSEMBLY__ */
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun /*
130*4882a593Smuzhiyun  * set, reset or clear a bit in the fp status register
131*4882a593Smuzhiyun  */
132*4882a593Smuzhiyun .macro	fp_set_sr	bit
133*4882a593Smuzhiyun 	bset	#(\bit&7),(FPD_FPSR+3-(\bit/8),FPDATA)
134*4882a593Smuzhiyun .endm
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun .macro	fp_clr_sr	bit
137*4882a593Smuzhiyun 	bclr	#(\bit&7),(FPD_FPSR+3-(\bit/8),FPDATA)
138*4882a593Smuzhiyun .endm
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun .macro	fp_tst_sr	bit
141*4882a593Smuzhiyun 	btst	#(\bit&7),(FPD_FPSR+3-(\bit/8),FPDATA)
142*4882a593Smuzhiyun .endm
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun #endif /* __ASSEMBLY__ */
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun #endif /* _FP_EMU_H */
147