1 /*
2  *  fpu/impl.h - extra functions and inline implementations
3  *
4  * Copyright (c) 2001-2004 Milan Jurik of ARAnyM dev team (see AUTHORS)
5  *
6  * Inspired by Christian Bauer's Basilisk II
7  *
8  * This file is part of the ARAnyM project which builds a new and powerful
9  * TOS/FreeMiNT compatible virtual machine running on almost any hardware.
10  *
11  * MC68881/68040 fpu emulation
12  *
13  * Original UAE FPU, copyright 1996 Herman ten Brugge
14  * Rewrite for x86, copyright 1999-2001 Lauri Pesonen
15  * New framework, copyright 2000-2001 Gwenole Beauchesne
16  * Adapted for JIT compilation (c) Bernd Meyer, 2000-2001
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * (at your option) any later version.
22  *
23  * ARAnyM is free software; you can redistribute it and/or modify
24  * it under the terms of the GNU General Public License as published by
25  * the Free Software Foundation; either version 2 of the License, or
26  * (at your option) any later version.
27  *
28  * ARAnyM is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31  * GNU General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public License
34  * along with ARAnyM; if not, write to the Free Software
35  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
36  */
37 
38 #ifndef FPU_IMPL_H
39 #define FPU_IMPL_H
40 
41 /* NOTE: this file shall be included from fpu/core.h */
42 #undef	PUBLIC
43 #define PUBLIC	/**/
44 
45 #undef	PRIVATE
46 #define PRIVATE	/**/
47 
48 #undef	FFPU
49 #define FFPU	/**/
50 
51 #undef	FPU
52 #define	FPU		fpu.
53 
54 /* -------------------------------------------------------------------------- */
55 /* --- X86 assembly fpu specific methods                                  --- */
56 /* -------------------------------------------------------------------------- */
57 
58 #ifdef FPU_X86
59 
60 /* Return the floating-point status register in m68k format */
get_fpsr(void)61 static inline uae_u32 FFPU get_fpsr(void)
62 {
63 	return	to_m68k_fpcond[(x86_status_word & 0x4700) >> 8]
64 		|	FPU fpsr.quotient
65 		|	exception_host2mac[x86_status_word & (SW_FAKE_BSUN|SW_PE|SW_UE|SW_OE|SW_ZE|SW_DE|SW_IE)]
66 		|	accrued_exception_host2mac[x86_status_word_accrued & (SW_PE|SW_UE|SW_OE|SW_ZE|SW_DE|SW_IE)]
67 		;
68 }
69 
70 /* Set the floating-point status register from an m68k format */
set_fpsr(uae_u32 new_fpsr)71 static inline void FFPU set_fpsr(uae_u32 new_fpsr)
72 {
73 	x86_status_word = to_host_fpcond[(new_fpsr & FPSR_CCB) >> 24 ]
74 		| exception_mac2host[(new_fpsr & FPSR_EXCEPTION_STATUS) >> 8];
75 	x86_status_word_accrued	= accrued_exception_mac2host[(new_fpsr & FPSR_ACCRUED_EXCEPTION) >> 3];
76 }
77 
78 #endif
79 
80 /* -------------------------------------------------------------------------- */
81 /* --- Original UAE and IEEE FPU core methods                             --- */
82 /* -------------------------------------------------------------------------- */
83 
84 #ifndef FPU_X86
85 
86 /* Return the floating-point status register in m68k format */
get_fpsr(void)87 static inline uae_u32 FFPU get_fpsr(void)
88 {
89 	uae_u32 condition_codes		= get_fpccr();
90 	uae_u32 exception_status	= get_exception_status();
91 	uae_u32 accrued_exception	= get_accrued_exception();
92 	uae_u32 quotient			= FPU fpsr.quotient;
93 	return (condition_codes | quotient | exception_status | accrued_exception);
94 }
95 
96 /* Set the floating-point status register from an m68k format */
set_fpsr(uae_u32 new_fpsr)97 static inline void FFPU set_fpsr(uae_u32 new_fpsr)
98 {
99 	set_fpccr					( new_fpsr & FPSR_CCB				);
100 	set_exception_status		( new_fpsr & FPSR_EXCEPTION_STATUS	);
101 	set_accrued_exception		( new_fpsr & FPSR_ACCRUED_EXCEPTION	);
102 	FPU fpsr.quotient			= new_fpsr & FPSR_QUOTIENT;
103 }
104 
105 #endif
106 
107 /* -------------------------------------------------------------------------- */
108 /* --- Common routines for control word                                   --- */
109 /* -------------------------------------------------------------------------- */
110 
111 /* Return the floating-point control register in m68k format */
get_fpcr(void)112 static inline uae_u32 FFPU get_fpcr(void)
113 {
114 	// according to the manual, the msb bits are always zero.
115 	// According to Toni Wilen, on '040 the least
116 	// significant 4 bits are not masked out
117 	return FPU fpcr & (CPUType == 4 ? 0xffff : 0xfff0);
118 }
119 
120 /* Set the floating-point control register from an m68k format */
set_fpcr(uae_u32 new_fpcr)121 static inline void FFPU set_fpcr(uae_u32 new_fpcr)
122 {
123 	FPU fpcr = new_fpcr;
124 	set_rounding_precision		( new_fpcr & FPCR_ROUNDING_PRECISION);
125 	set_rounding_mode			( new_fpcr & FPCR_ROUNDING_MODE		);
126 	set_host_control_word();
127 }
128 
129 /* -------------------------------------------------------------------------- */
130 /* --- Specific part to X86 assembly FPU                                  --- */
131 /* -------------------------------------------------------------------------- */
132 
133 #ifdef FPU_X86
134 
135 /* Retrieve a floating-point register value and convert it to double precision */
fpu_get_register(int r)136 static inline double FFPU fpu_get_register(int r)
137 {
138 	/* only used for debug output; no need for any fancy asm here */
139 	return FPU registers[r];
140 }
141 
142 #endif
143 
144 /* -------------------------------------------------------------------------- */
145 /* --- Specific to original UAE or new IEEE-based FPU core                --- */
146 /* -------------------------------------------------------------------------- */
147 
148 #if defined(FPU_UAE) || defined(FPU_IEEE)
149 
150 /* Retrieve a floating-point register value and convert it to double precision */
fpu_get_register(int r)151 static inline double FFPU fpu_get_register(int r)
152 {
153 	return FPU registers[r];
154 }
155 
156 #endif
157 
158 #endif /* FPU_IMPL_H */
159