2 * wm-FPU-emu an FPU emulator for 80386 and 80486SX microprocessors.
5 * Copyright (C) 1992,1993,1994
6 * W. Metzenthen, 22 Parker St, Ormond, Vic 3163,
7 * Australia. E-mail billm@vaxc.cc.monash.edu.au
10 * This copyright notice covers the redistribution and use of the
11 * FPU emulator developed by W. Metzenthen. It covers only its use
12 * in the 386BSD, FreeBSD and NetBSD operating systems. Any other
13 * use is not permitted under this copyright.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must include information specifying
21 * that source code for the emulator is freely available and include
23 * a) an offer to provide the source code for a nominal distribution
25 * b) list at least two alternative methods whereby the source
26 * can be obtained, e.g. a publically accessible bulletin board
27 * and an anonymous ftp site from which the software can be
29 * 3. All advertising materials specifically mentioning features or use of
30 * this emulator must acknowledge that it was developed by W. Metzenthen.
31 * 4. The name of W. Metzenthen may not be used to endorse or promote
32 * products derived from this software without specific prior written
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
36 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
37 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
38 * W. METZENTHEN BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
40 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
41 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
42 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
43 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 * The purpose of this copyright, based upon the Berkeley copyright, is to
48 * ensure that the covered software remains freely available to everyone.
50 * The software (with necessary differences) is also available, but under
51 * the terms of the GNU copyleft, for the Linux operating system and for
52 * the djgpp ms-dos extender.
54 * W. Metzenthen June 1994.
56 * $FreeBSD: src/sys/gnu/i386/fpemul/README,v 1.2.20.1 2000/08/03 00:53:55 peter Exp $
59 wm-FPU-emu is an FPU emulator for Linux. It is derived from wm-emu387
60 which is my 80387 emulator for djgpp (gcc under msdos); wm-emu387 was
61 in turn based upon emu387 which was written by DJ Delorie for djgpp.
62 The interface to the Linux kernel is based upon the original Linux
63 math emulator by Linus Torvalds.
65 My target FPU for wm-FPU-emu is that described in the Intel486
66 Programmer's Reference Manual (1992 edition). Numerous facets of the
67 functioning of the FPU are not well covered in the Reference Manual;
68 in the absence of clear details I have made guesses about the most
69 reasonable behaviour. Recently, this situation has improved because
70 I now have some access to the results produced by a real 80486 FPU.
72 wm-FPU-emu does not implement all of the behaviour of the 80486 FPU.
73 See "Limitations" later in this file for a partial list of some
74 differences. I believe that the missing features are never used by
75 normal C or FORTRAN programs.
78 Please report bugs, etc to me at:
79 apm233m@vaxc.cc.monash.edu.au
86 ----------------------- Internals of wm-FPU-emu -----------------------
89 (1) Add, subtract, and multiply. Nothing remarkable in these.
90 (2) Divide has been tuned to get reasonable performance. The algorithm
91 is not the obvious one which most people seem to use, but is designed
92 to take advantage of the characteristics of the 80386. I expect that
93 it has been invented many times before I discovered it, but I have not
94 seen it. It is based upon one of those ideas which one carries around
95 for years without ever bothering to check it out.
96 (3) The sqrt function has been tuned to get good performance. It is based
97 upon Newton's classic method. Performance was improved by capitalizing
98 upon the properties of Newton's method, and the code is once again
99 structured taking account of the 80386 characteristics.
100 (4) The trig, log, and exp functions are based in each case upon quasi-
101 "optimal" polynomial approximations. My definition of "optimal" was
102 based upon getting good accuracy with reasonable speed.
104 The code of the emulator is complicated slightly by the need to
105 account for a limited form of re-entrancy. Normally, the emulator will
106 emulate each FPU instruction to completion without interruption.
107 However, it may happen that when the emulator is accessing the user
108 memory space, swapping may be needed. In this case the emulator may be
109 temporarily suspended while disk i/o takes place. During this time
110 another process may use the emulator, thereby changing some static
111 variables (eg FPU_st0_ptr, etc). The code which accesses user memory
112 is confined to five files:
119 ----------------------- Limitations of wm-FPU-emu -----------------------
121 There are a number of differences between the current wm-FPU-emu
122 (version beta 1.4) and the 80486 FPU (apart from bugs). Some of the
123 more important differences are listed below:
125 All internal computations are performed at 64 bit or higher precision
126 and rounded etc as required by the PC bits of the FPU control word.
127 Under the crt0 version for Linux current at March 1993, the FPU PC
128 bits specify 53 bits precision.
130 The precision flag (PE of the FPU status word) and the Roundup flag
131 (C1 of the status word) are now partially implemented. Does anyone
132 write code which uses these features?
134 The functions which load/store the FPU state are partially implemented,
135 but the implementation should be sufficient for handling FPU errors etc
136 in 32 bit protected mode.
138 The implementation of the exception mechanism is flawed for unmasked
141 Detection of certain conditions, such as denormal operands, is not yet
144 ----------------------- Performance of wm-FPU-emu -----------------------
149 The speed of floating point computation with the emulator will depend
150 upon instruction mix. Relative performance is best for the instructions
151 which require most computation. The simple instructions are adversely
152 affected by the fpu instruction trap overhead.
155 Timing: Some simple timing tests have been made on the emulator functions.
156 The times include load/store instructions. All times are in microseconds
157 measured on a 33MHz 386 with 64k cache. The Turbo C tests were under
158 ms-dos, the next two columns are for emulators running with the djgpp
159 ms-dos extender. The final column is for wm-FPU-emu in Linux 0.97,
160 using libm4.0 (hard).
162 function Turbo C djgpp 1.06 WM-emu387 wm-FPU-emu
164 + 60.5 154.8 76.5 139.4
165 - 61.1-65.5 157.3-160.8 76.2-79.5 142.9-144.7
166 * 71.0 190.8 79.6 146.6
167 / 61.2-75.0 261.4-266.9 75.3-91.6 142.2-158.1
169 sin() 310.8 4692.0 319.0 398.5
170 cos() 284.4 4855.2 308.0 388.7
171 tan() 495.0 8807.1 394.9 504.7
172 atan() 328.9 4866.4 601.1 419.5-491.9
174 sqrt() 128.7 crashed 145.2 227.0
175 log() 413.1-419.1 5103.4-5354.21 254.7-282.2 409.4-437.1
176 exp() 479.1 6619.2 469.1 850.8
179 The performance under Linux is improved by the use of look-ahead code.
180 The following results show the improvement which is obtained under
181 Linux due to the look-ahead code. Also given are the times for the
182 original Linux emulator with the 4.1 'soft' lib.
184 [ Linus' note: I changed look-ahead to be the default under linux, as
185 there was no reason not to use it after I had edited it to be
186 disabled during tracing ]
188 wm-FPU-emu w original w
189 look-ahead 'soft' lib
191 - 108.6-111.6 192.4-216.2
193 / 108.8-124.4 700.1-706.2
198 atan() 367.2-435.5 2439.4-3396.8
201 log() 358.0-387.5 3359.2-3390.3
205 These figures are now somewhat out-of-date. The emulator has become
206 progressively slower for most functions as more of the 80486 features
207 have been implemented.
210 ----------------------- Accuracy of wm-FPU-emu -----------------------
213 Accuracy: The following table gives the accuracy of the sqrt(), trig
214 and log functions. Each function was tested at about 400 points. Ideal
215 results would be 64 bits. The reduced accuracy of cos() and tan() for
216 arguments greater than pi/4 can be thought of as being due to the
217 precision of the argument x; e.g. an argument of pi/2-(1e-10) which is
218 accurate to 64 bits can result in a relative accuracy in cos() of about
219 64 + log2(cos(x)) = 31 bits. Results for the Turbo C emulator are given
223 Function Tested x range Worst result (bits) Turbo C
225 sqrt(x) 1 .. 2 64.1 63.2
226 atan(x) 1e-10 .. 200 62.6 62.8
227 cos(x) 0 .. pi/2-(1e-10) 63.2 (x <= pi/4) 62.4
228 35.2 (x = pi/2-(1e-10)) 31.9
229 sin(x) 1e-10 .. pi/2 63.0 62.8
230 tan(x) 1e-10 .. pi/2-(1e-10) 62.4 (x <= pi/4) 62.1
231 35.2 (x = pi/2-(1e-10)) 31.9
232 exp(x) 0 .. 1 63.1 62.9
233 log(x) 1+1e-6 .. 2 62.4 62.1
236 As of version 1.3 of the emulator, the accuracy of the basic
237 arithmetic has been improved (by a small fraction of a bit). Care has
238 been taken to ensure full accuracy of the rounding of the basic
239 arithmetic functions (+,-,*,/,and fsqrt), and they all now produce
240 results which are exact to the 64th bit (unless there are any bugs
241 left). To ensure this, it was necessary to effectively get information
242 of up to about 128 bits precision. The emulator now passes the
243 "paranoia" tests (compiled with gcc 2.3.3) for 'float' variables (24
244 bit precision numbers) when precision control is set to 24, 53 or 64
245 bits, and for 'double' variables (53 bit precision numbers) when
246 precision control is set to 53 bits (a properly performing FPU cannot
247 pass the 'paranoia' tests for 'double' variables when precision
248 control is set to 64 bits).
250 ------------------------- Contributors -------------------------------
252 A number of people have contributed to the development of the
253 emulator, often by just reporting bugs, sometimes with a suggested
254 fix, and a few kind people have provided me with access in one way or
255 another to an 80486 machine. Contributors include (to those people who
256 I have forgotten, please excuse me):
259 Tommy.Thorn@daimi.aau.dk
260 Andrew.Tridgell@anu.edu.au
261 Nick Holloway alfie@dcs.warwick.ac.uk
262 Hermano Moura moura@dcs.gla.ac.uk
263 Jon Jagger J.Jagger@scp.ac.uk
265 Brian Gallew geek+@CMU.EDU
266 Thomas Staniszewski ts3v+@andrew.cmu.edu
267 Martin Howell mph@plasma.apana.org.au
268 M Saggaf alsaggaf@athena.mit.edu
269 Peter Barker PETER@socpsy.sci.fau.edu
270 tom@vlsivie.tuwien.ac.at
271 Dan Russel russed@rpi.edu
272 Daniel Carosone danielce@ee.mu.oz.au
274 Hamish Coleman t933093@minyos.xx.rmit.oz.au
276 ...and numerous others who responded to my request for help with