My first commit.
[dragonfly.git] / sys / i386 / gnu / fpemul / README
1 /*
2  *  wm-FPU-emu   an FPU emulator for 80386 and 80486SX microprocessors.
3  *
4  *
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
8  * All rights reserved.
9  *
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.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
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
22  *    either:
23  *      a) an offer to provide the source code for a nominal distribution
24  *         fee, or
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
28  *         downloaded.
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
33  *    permission.
34  *
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.
45  *
46  *
47  * The purpose of this copyright, based upon the Berkeley copyright, is to
48  * ensure that the covered software remains freely available to everyone.
49  *
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.
53  *
54  * W. Metzenthen   June 1994.
55  *
56  * $FreeBSD: src/sys/gnu/i386/fpemul/README,v 1.2.20.1 2000/08/03 00:53:55 peter Exp $
57  * $DragonFly: src/sys/i386/gnu/fpemul/Attic/README,v 1.2 2003/06/17 04:28:34 dillon Exp $
58  */
59
60 wm-FPU-emu is an FPU emulator for Linux. It is derived from wm-emu387
61 which is my 80387 emulator for djgpp (gcc under msdos); wm-emu387 was
62 in turn based upon emu387 which was written by DJ Delorie for djgpp.
63 The interface to the Linux kernel is based upon the original Linux
64 math emulator by Linus Torvalds.
65
66 My target FPU for wm-FPU-emu is that described in the Intel486
67 Programmer's Reference Manual (1992 edition). Numerous facets of the
68 functioning of the FPU are not well covered in the Reference Manual;
69 in the absence of clear details I have made guesses about the most
70 reasonable behaviour. Recently, this situation has improved because
71 I now have some access to the results produced by a real 80486 FPU.
72
73 wm-FPU-emu does not implement all of the behaviour of the 80486 FPU. 
74 See "Limitations" later in this file for a partial list of some
75 differences.  I believe that the missing features are never used by
76 normal C or FORTRAN programs. 
77
78
79 Please report bugs, etc to me at:
80        apm233m@vaxc.cc.monash.edu.au
81
82
83 --Bill Metzenthen
84   May 1993
85
86
87 ----------------------- Internals of wm-FPU-emu -----------------------
88
89 Numeric algorithms:
90 (1) Add, subtract, and multiply. Nothing remarkable in these.
91 (2) Divide has been tuned to get reasonable performance. The algorithm
92     is not the obvious one which most people seem to use, but is designed
93     to take advantage of the characteristics of the 80386. I expect that
94     it has been invented many times before I discovered it, but I have not
95     seen it. It is based upon one of those ideas which one carries around
96     for years without ever bothering to check it out.
97 (3) The sqrt function has been tuned to get good performance. It is based
98     upon Newton's classic method. Performance was improved by capitalizing
99     upon the properties of Newton's method, and the code is once again
100     structured taking account of the 80386 characteristics.
101 (4) The trig, log, and exp functions are based in each case upon quasi-
102     "optimal" polynomial approximations. My definition of "optimal" was
103     based upon getting good accuracy with reasonable speed.
104
105 The code of the emulator is complicated slightly by the need to
106 account for a limited form of re-entrancy. Normally, the emulator will
107 emulate each FPU instruction to completion without interruption.
108 However, it may happen that when the emulator is accessing the user
109 memory space, swapping may be needed. In this case the emulator may be
110 temporarily suspended while disk i/o takes place. During this time
111 another process may use the emulator, thereby changing some static
112 variables (eg FPU_st0_ptr, etc). The code which accesses user memory
113 is confined to five files:
114     fpu_entry.c
115     reg_ld_str.c
116     load_store.c
117     get_address.c
118     errors.c
119
120 ----------------------- Limitations of wm-FPU-emu -----------------------
121
122 There are a number of differences between the current wm-FPU-emu
123 (version beta 1.4) and the 80486 FPU (apart from bugs). Some of the
124 more important differences are listed below:
125
126 All internal computations are performed at 64 bit or higher precision
127 and rounded etc as required by the PC bits of the FPU control word.
128 Under the crt0 version for Linux current at March 1993, the FPU PC
129 bits specify 53 bits precision.
130
131 The precision flag (PE of the FPU status word) and the Roundup flag
132 (C1 of the status word) are now partially implemented. Does anyone
133 write code which uses these features?
134
135 The functions which load/store the FPU state are partially implemented,
136 but the implementation should be sufficient for handling FPU errors etc
137 in 32 bit protected mode.
138
139 The implementation of the exception mechanism is flawed for unmasked
140 interrupts.
141
142 Detection of certain conditions, such as denormal operands, is not yet
143 complete.
144
145 ----------------------- Performance of wm-FPU-emu -----------------------
146
147 Speed.
148 -----
149
150 The speed of floating point computation with the emulator will depend
151 upon instruction mix. Relative performance is best for the instructions
152 which require most computation. The simple instructions are adversely
153 affected by the fpu instruction trap overhead.
154
155
156 Timing: Some simple timing tests have been made on the emulator functions.
157 The times include load/store instructions. All times are in microseconds
158 measured on a 33MHz 386 with 64k cache. The Turbo C tests were under
159 ms-dos, the next two columns are for emulators running with the djgpp
160 ms-dos extender. The final column is for wm-FPU-emu in Linux 0.97,
161 using libm4.0 (hard).
162
163 function      Turbo C        djgpp 1.06        WM-emu387     wm-FPU-emu
164
165    +          60.5           154.8              76.5          139.4
166    -          61.1-65.5      157.3-160.8        76.2-79.5     142.9-144.7
167    *          71.0           190.8              79.6          146.6
168    /          61.2-75.0      261.4-266.9        75.3-91.6     142.2-158.1
169
170  sin()        310.8          4692.0            319.0          398.5
171  cos()        284.4          4855.2            308.0          388.7
172  tan()        495.0          8807.1            394.9          504.7
173  atan()       328.9          4866.4            601.1          419.5-491.9
174
175  sqrt()       128.7          crashed           145.2          227.0
176  log()        413.1-419.1    5103.4-5354.21    254.7-282.2    409.4-437.1
177  exp()        479.1          6619.2            469.1          850.8
178
179
180 The performance under Linux is improved by the use of look-ahead code.
181 The following results show the improvement which is obtained under
182 Linux due to the look-ahead code. Also given are the times for the
183 original Linux emulator with the 4.1 'soft' lib.
184
185  [ Linus' note: I changed look-ahead to be the default under linux, as
186    there was no reason not to use it after I had edited it to be
187    disabled during tracing ]
188
189             wm-FPU-emu w     original w
190             look-ahead       'soft' lib
191    +         106.4             190.2
192    -         108.6-111.6      192.4-216.2
193    *         113.4             193.1
194    /         108.8-124.4      700.1-706.2
195
196  sin()       390.5            2642.0
197  cos()       381.5            2767.4
198  tan()       496.5            3153.3
199  atan()      367.2-435.5     2439.4-3396.8
200
201  sqrt()      195.1            4732.5
202  log()       358.0-387.5     3359.2-3390.3
203  exp()       619.3            4046.4
204
205
206 These figures are now somewhat out-of-date. The emulator has become
207 progressively slower for most functions as more of the 80486 features
208 have been implemented.
209
210
211 ----------------------- Accuracy of wm-FPU-emu -----------------------
212
213
214 Accuracy: The following table gives the accuracy of the sqrt(), trig
215 and log functions. Each function was tested at about 400 points. Ideal
216 results would be 64 bits. The reduced accuracy of cos() and tan() for
217 arguments greater than pi/4 can be thought of as being due to the
218 precision of the argument x; e.g. an argument of pi/2-(1e-10) which is
219 accurate to 64 bits can result in a relative accuracy in cos() of about
220 64 + log2(cos(x)) = 31 bits. Results for the Turbo C emulator are given
221 in the last column.
222
223
224 Function      Tested x range            Worst result (bits)         Turbo C
225
226 sqrt(x)       1 .. 2                    64.1                         63.2
227 atan(x)       1e-10 .. 200              62.6                         62.8
228 cos(x)        0 .. pi/2-(1e-10)         63.2 (x <= pi/4)             62.4
229                                         35.2 (x = pi/2-(1e-10))      31.9
230 sin(x)        1e-10 .. pi/2             63.0                         62.8
231 tan(x)        1e-10 .. pi/2-(1e-10)     62.4 (x <= pi/4)             62.1
232                                         35.2 (x = pi/2-(1e-10))      31.9
233 exp(x)        0 .. 1                    63.1                         62.9
234 log(x)        1+1e-6 .. 2               62.4                         62.1
235
236
237 As of version 1.3 of the emulator, the accuracy of the basic
238 arithmetic has been improved (by a small fraction of a bit). Care has
239 been taken to ensure full accuracy of the rounding of the basic
240 arithmetic functions (+,-,*,/,and fsqrt), and they all now produce
241 results which are exact to the 64th bit (unless there are any bugs
242 left). To ensure this, it was necessary to effectively get information
243 of up to about 128 bits precision. The emulator now passes the
244 "paranoia" tests (compiled with gcc 2.3.3) for 'float' variables (24
245 bit precision numbers) when precision control is set to 24, 53 or 64
246 bits, and for 'double' variables (53 bit precision numbers) when
247 precision control is set to 53 bits (a properly performing FPU cannot
248 pass the 'paranoia' tests for 'double' variables when precision
249 control is set to 64 bits).
250
251 ------------------------- Contributors -------------------------------
252
253 A number of people have contributed to the development of the
254 emulator, often by just reporting bugs, sometimes with a suggested
255 fix, and a few kind people have provided me with access in one way or
256 another to an 80486 machine. Contributors include (to those people who
257 I have forgotten, please excuse me):
258
259 Linus Torvalds
260 Tommy.Thorn@daimi.aau.dk
261 Andrew.Tridgell@anu.edu.au
262 Nick Holloway alfie@dcs.warwick.ac.uk
263 Hermano Moura moura@dcs.gla.ac.uk
264 Jon Jagger J.Jagger@scp.ac.uk
265 Lennart Benschop
266 Brian Gallew geek+@CMU.EDU
267 Thomas Staniszewski ts3v+@andrew.cmu.edu
268 Martin Howell mph@plasma.apana.org.au
269 M Saggaf alsaggaf@athena.mit.edu
270 Peter Barker PETER@socpsy.sci.fau.edu
271 tom@vlsivie.tuwien.ac.at
272 Dan Russel russed@rpi.edu
273 Daniel Carosone danielce@ee.mu.oz.au
274 cae@jpmorgan.com
275 Hamish Coleman t933093@minyos.xx.rmit.oz.au
276
277 ...and numerous others who responded to my request for help with
278 a real 80486.
279