11.2Schristos$NetBSD: softfloat-source.txt,v 1.2 2006/11/24 19:46:58 christos Exp $
21.1Sbjh21
31.1Sbjh21SoftFloat Release 2a Source Documentation
41.1Sbjh21
51.1Sbjh21John R. Hauser
61.1Sbjh211998 December 14
71.1Sbjh21
81.1Sbjh21
91.1Sbjh21-------------------------------------------------------------------------------
101.1Sbjh21Introduction
111.1Sbjh21
121.1Sbjh21SoftFloat is a software implementation of floating-point that conforms to
131.1Sbjh21the IEC/IEEE Standard for Binary Floating-Point Arithmetic.  SoftFloat can
141.1Sbjh21support four floating-point formats:  single precision, double precision,
151.1Sbjh21extended double precision, and quadruple precision.  All operations required
161.1Sbjh21by the IEEE Standard are implemented, except for conversions to and from
171.1Sbjh21decimal.  SoftFloat is distributed in the form of C source code, so a
181.1Sbjh21C compiler is needed to compile the code.  Support for the extended double-
191.1Sbjh21precision and quadruple-precision formats is dependent on the C compiler
201.1Sbjh21implementing a 64-bit integer type.
211.1Sbjh21
221.1Sbjh21This document gives information needed for compiling and/or porting
231.1Sbjh21SoftFloat.
241.1Sbjh21
251.1Sbjh21The source code for SoftFloat is intended to be relatively machine-
261.1Sbjh21independent and should be compilable using any ISO/ANSI C compiler.  At the
271.1Sbjh21time of this writing, SoftFloat has been successfully compiled with the GNU
281.1Sbjh21C Compiler (`gcc') for several platforms.
291.1Sbjh21
301.1Sbjh21
311.1Sbjh21-------------------------------------------------------------------------------
321.1Sbjh21Limitations
331.1Sbjh21
341.1Sbjh21SoftFloat as written requires an ISO/ANSI-style C compiler.  No attempt has
351.2Schristosbeen made to accommodate compilers that are not ISO-conformant.  Older ``K&R-
361.1Sbjh21style'' compilers are not adequate for compiling SoftFloat.  All testing I
371.1Sbjh21have done so far has been with the GNU C Compiler.  Compilation with other
381.1Sbjh21compilers should be possible but has not been tested.
391.1Sbjh21
401.1Sbjh21The SoftFloat sources assume that source code file names can be longer than
411.1Sbjh218 characters.  In order to compile under an MS-DOS-type system, many of the
421.1Sbjh21source files will need to be renamed, and the source and makefiles edited
431.1Sbjh21appropriately.  Once compiled, the SoftFloat binary does not depend on the
441.1Sbjh21existence of long file names.
451.1Sbjh21
461.1Sbjh21The underlying machine is assumed to be binary with a word size that is a
471.1Sbjh21power of 2.  Bytes are 8 bits.  Support for the extended double-precision
481.1Sbjh21and quadruple-precision formats depends on the C compiler implementing
491.1Sbjh21a 64-bit integer type.  If the largest integer type supported by the
501.1Sbjh21C compiler is 32 bits, SoftFloat is limited to the single- and double-
511.1Sbjh21precision formats.
521.1Sbjh21
531.1Sbjh21
541.1Sbjh21-------------------------------------------------------------------------------
551.1Sbjh21Contents
561.1Sbjh21
571.1Sbjh21    Introduction
581.1Sbjh21    Limitations
591.1Sbjh21    Contents
601.1Sbjh21    Legal Notice
611.1Sbjh21    SoftFloat Source Directory Structure
621.1Sbjh21    SoftFloat Source Files
631.1Sbjh21        processors/*.h
641.1Sbjh21        softfloat/bits*/*/softfloat.h
651.1Sbjh21        softfloat/bits*/*/milieu.h
661.1Sbjh21        softfloat/bits*/*/softfloat-specialize
671.1Sbjh21        softfloat/bits*/softfloat-macros
681.1Sbjh21        softfloat/bits*/softfloat.c
691.1Sbjh21    Steps to Creating a `softfloat.o'
701.1Sbjh21    Making `softfloat.o' a Library
711.1Sbjh21    Testing SoftFloat
721.1Sbjh21    Timing SoftFloat
731.1Sbjh21    Compiler Options and Efficiency
741.1Sbjh21    Processor-Specific Optimization of `softfloat.c' Using `softfloat-macros'
751.1Sbjh21    Contact Information
761.1Sbjh21
771.1Sbjh21
781.1Sbjh21
791.1Sbjh21-------------------------------------------------------------------------------
801.1Sbjh21Legal Notice
811.1Sbjh21
821.1Sbjh21SoftFloat was written by John R. Hauser.  This work was made possible in
831.1Sbjh21part by the International Computer Science Institute, located at Suite 600,
841.1Sbjh211947 Center Street, Berkeley, California 94704.  Funding was partially
851.1Sbjh21provided by the National Science Foundation under grant MIP-9311980.  The
861.1Sbjh21original version of this code was written as part of a project to build
871.1Sbjh21a fixed-point vector processor in collaboration with the University of
881.1Sbjh21California at Berkeley, overseen by Profs. Nelson Morgan and John Wawrzynek.
891.1Sbjh21
901.1Sbjh21THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE.  Although reasonable effort
911.1Sbjh21has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
921.1Sbjh21TIMES RESULT IN INCORRECT BEHAVIOR.  USE OF THIS SOFTWARE IS RESTRICTED TO
931.1Sbjh21PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
941.1Sbjh21AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
951.1Sbjh21
961.1Sbjh21
971.1Sbjh21-------------------------------------------------------------------------------
981.1Sbjh21SoftFloat Source Directory Structure
991.1Sbjh21
1001.1Sbjh21Because SoftFloat is targeted to multiple platforms, its source code
1011.1Sbjh21is slightly scattered between target-specific and target-independent
1021.1Sbjh21directories and files.  The directory structure is as follows:
1031.1Sbjh21
1041.1Sbjh21    processors
1051.1Sbjh21    softfloat
1061.1Sbjh21        bits64
1071.1Sbjh21            templates
1081.1Sbjh21            386-Win32-gcc
1091.1Sbjh21            SPARC-Solaris-gcc
1101.1Sbjh21        bits32
1111.1Sbjh21            templates
1121.1Sbjh21            386-Win32-gcc
1131.1Sbjh21            SPARC-Solaris-gcc
1141.1Sbjh21
1151.1Sbjh21The two topmost directories and their contents are:
1161.1Sbjh21
1171.1Sbjh21    softfloat    - Most of the source code needed for SoftFloat.
1181.1Sbjh21    processors   - Target-specific header files that are not specific to
1191.1Sbjh21                       SoftFloat.
1201.1Sbjh21
1211.1Sbjh21The `softfloat' directory is further split into two parts:
1221.1Sbjh21
1231.1Sbjh21    bits64       - SoftFloat implementation using 64-bit integers.
1241.1Sbjh21    bits32       - SoftFloat implementation using only 32-bit integers.
1251.1Sbjh21
1261.1Sbjh21Within these directories are subdirectories for each of the targeted
1271.1Sbjh21platforms.  The SoftFloat source code is distributed with targets
1281.1Sbjh21`386-Win32-gcc' and `SPARC-Solaris-gcc' (and perhaps others) already
1291.1Sbjh21prepared for both the 32-bit and 64-bit implementations.  Source files that
1301.1Sbjh21are not within these target-specific subdirectories are intended to be
1311.1Sbjh21target-independent.
1321.1Sbjh21
1331.1Sbjh21The naming convention used for the target-specific directories is
1341.1Sbjh21`<processor>-<executable-type>-<compiler>'.  The names of the supplied
1351.1Sbjh21target directories should be interpreted as follows:
1361.1Sbjh21
1371.1Sbjh21  <processor>:
1381.1Sbjh21    386          - Intel 386-compatible processor.
1391.1Sbjh21    SPARC        - SPARC processor (as used by Sun machines).
1401.1Sbjh21  <executable-type>:
1411.1Sbjh21    Win32        - Microsoft Win32 executable.
1421.1Sbjh21    Solaris      - Sun Solaris executable.
1431.1Sbjh21  <compiler>:
1441.1Sbjh21    gcc          - GNU C Compiler.
1451.1Sbjh21
1461.1Sbjh21You do not need to maintain this convention if you do not want to.
1471.1Sbjh21
1481.1Sbjh21Alongside the supplied target-specific directories is a `templates'
1491.1Sbjh21directory containing a set of ``generic'' target-specific source files.  A
1501.1Sbjh21new target directory can be created by copying the `templates' directory and
1511.1Sbjh21editing the files inside.  (Complete instructions for porting SoftFloat to a
1521.1Sbjh21new target are in the section _Steps_to_Creating_a_`softfloat.o'_.)  Note
1531.1Sbjh21that the `templates' directory will not work as a target directory without
1541.1Sbjh21some editing.  To avoid confusion, it would be wise to refrain from editing
1551.1Sbjh21the files inside `templates' directly.
1561.1Sbjh21
1571.1Sbjh21
1581.1Sbjh21-------------------------------------------------------------------------------
1591.1Sbjh21SoftFloat Source Files
1601.1Sbjh21
1611.1Sbjh21The purpose of each source file is described below.  In the following,
1621.1Sbjh21the `*' symbol is used in place of the name of a specific target, such as
1631.1Sbjh21`386-Win32-gcc' or `SPARC-Solaris-gcc', or in place of some other text, as
1641.1Sbjh21in `bits*' for either `bits32' or `bits64'.
1651.1Sbjh21
1661.1Sbjh21- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1671.1Sbjh21processors/*.h
1681.1Sbjh21
1691.1Sbjh21The target-specific `processors' header file defines integer types
1701.1Sbjh21of various sizes, and also defines certain C preprocessor macros that
1711.1Sbjh21characterize the target.  The two examples supplied are `386-gcc.h' and
1721.1Sbjh21`SPARC-gcc.h'.  The naming convention used for processor header files is
1731.1Sbjh21`<processor>-<compiler>.h'.
1741.1Sbjh21
1751.1Sbjh21If 64-bit integers are supported by the compiler, the macro name `BITS64'
1761.1Sbjh21should be defined here along with the corresponding 64-bit integer
1771.1Sbjh21types.  In addition, the function-like macro `LIT64' must be defined for
1781.1Sbjh21constructing 64-bit integer literals (constants).  The `LIT64' macro is used
1791.1Sbjh21consistently in the SoftFloat code to annotate 64-bit literals.
1801.1Sbjh21
1811.1Sbjh21If `BITS64' is not defined, only the 32-bit version of SoftFloat can be
1821.1Sbjh21compiled.  If `BITS64' _is_ defined, either can be compiled.
1831.1Sbjh21
1841.1Sbjh21If an inlining attribute (such as an `inline' keyword) is provided by the
1851.1Sbjh21compiler, the macro `INLINE' should be defined to the appropriate keyword.
1861.1Sbjh21If not, `INLINE' can be set to the keyword `static'.  The `INLINE' macro
1871.1Sbjh21appears in the SoftFloat source code before every function that should
1881.1Sbjh21be inlined by the compiler.  SoftFloat depends on inlining to obtain
1891.1Sbjh21good speed.  Even if inlining cannot be forced with a language keyword,
1901.1Sbjh21the compiler may still be able to perform inlining on its own as an
1911.1Sbjh21optimization.  If a command-line option is needed to convince the compiler
1921.1Sbjh21to perform this optimization, this should be assured in the makefile.  (See
1931.1Sbjh21the section _Compiler_Options_and_Efficiency_ below.)
1941.1Sbjh21
1951.1Sbjh21- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1961.1Sbjh21softfloat/bits*/*/softfloat.h
1971.1Sbjh21
1981.1Sbjh21The target-specific `softfloat.h' header file defines the SoftFloat
1991.1Sbjh21interface as seen by clients.
2001.1Sbjh21
2011.1Sbjh21Unlike the actual function definitions in `softfloat.c', the declarations
2021.1Sbjh21in `softfloat.h' do not use any of the types defined by the `processors'
2031.1Sbjh21header file.  This is done so that clients will not have to include the
2041.1Sbjh21`processors' header file in order to use SoftFloat.  Nevertheless, the
2051.1Sbjh21target-specific declarations in `softfloat.h' must match what `softfloat.c'
2061.1Sbjh21expects.  For example, if `int32' is defined as `int' in the `processors'
2071.1Sbjh21header file, then in `softfloat.h' the output of `float32_to_int32' should
2081.1Sbjh21be stated as `int', although in `softfloat.c' it is given in target-
2091.1Sbjh21independent form as `int32'.
2101.1Sbjh21
2111.1Sbjh21For the `bits64' implementation of SoftFloat, the macro names `FLOATX80' and
2121.1Sbjh21`FLOAT128' must be defined in order for the extended double-precision and
2131.1Sbjh21quadruple-precision formats to be enabled in the code.  Conversely, either
2141.1Sbjh21or both of the extended formats can be disabled by simply removing the
2151.1Sbjh21`#define' of the respective macro.  When an extended format is not enabled,
2161.1Sbjh21none of the functions that either input or output the format are defined,
2171.1Sbjh21and no space is taken up in `softfloat.o' by such functions.  There is no
2181.1Sbjh21provision for disabling the usual single- and double-precision formats.
2191.1Sbjh21
2201.1Sbjh21- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2211.1Sbjh21softfloat/bits*/*/milieu.h
2221.1Sbjh21
2231.1Sbjh21The target-specific `milieu.h' header file provides declarations that are
2241.1Sbjh21needed to compile SoftFloat.  In addition, deviations from ISO/ANSI C by
2251.1Sbjh21the compiler (such as names not properly declared in system header files)
2261.1Sbjh21are corrected in this header if possible.
2271.1Sbjh21
2281.1Sbjh21- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2291.1Sbjh21softfloat/bits*/*/softfloat-specialize
2301.1Sbjh21
2311.1Sbjh21This target-specific C source fragment defines:
2321.1Sbjh21
2331.1Sbjh21-- whether tininess for underflow is detected before or after rounding by
2341.1Sbjh21       default;
2351.1Sbjh21-- what (if anything) special happens when exceptions are raised;
2361.1Sbjh21-- how signaling NaNs are distinguished from quiet NaNs;
2371.1Sbjh21-- the default generated quiet NaNs; and
2381.1Sbjh21-- how NaNs are propagated from function inputs to output.
2391.1Sbjh21
2401.1Sbjh21These details are not decided by the IEC/IEEE Standard.  This fragment is
2411.1Sbjh21included verbatim within `softfloat.c' when SoftFloat is compiled.
2421.1Sbjh21
2431.1Sbjh21- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2441.1Sbjh21softfloat/bits*/softfloat-macros
2451.1Sbjh21
2461.1Sbjh21This target-independent C source fragment defines a number of arithmetic
2471.1Sbjh21functions used as primitives within the `softfloat.c' source.  Most of the
2481.1Sbjh21functions defined here are intended to be inlined for efficiency.  This
2491.1Sbjh21fragment is included verbatim within `softfloat.c' when SoftFloat is
2501.1Sbjh21compiled.
2511.1Sbjh21
2521.1Sbjh21Target-specific variations on this file are possible.  See the section
2531.1Sbjh21_Processor-Specific_Optimization_of_`softfloat.c'_Using_`softfloat-macros'_
2541.1Sbjh21below.
2551.1Sbjh21
2561.1Sbjh21- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2571.1Sbjh21softfloat/bits*/softfloat.c
2581.1Sbjh21
2591.1Sbjh21The target-independent `softfloat.c' source file contains the body of the
2601.1Sbjh21SoftFloat implementation.
2611.1Sbjh21
2621.1Sbjh21- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2631.1Sbjh21
2641.1Sbjh21The inclusion of the files above within each other (using `#include') can be
2651.1Sbjh21shown graphically as follows:
2661.1Sbjh21
2671.1Sbjh21    softfloat/bits*/softfloat.c
2681.1Sbjh21        softfloat/bits*/*/milieu.h
2691.1Sbjh21            processors/*.h
2701.1Sbjh21        softfloat/bits*/*/softfloat.h
2711.1Sbjh21        softfloat/bits*/*/softfloat-specialize
2721.1Sbjh21        softfloat/bits*/softfloat-macros
2731.1Sbjh21
2741.1Sbjh21Note in particular that `softfloat.c' does not include the `processors'
2751.1Sbjh21header file directly.  Rather, `softfloat.c' includes the target-specific
2761.1Sbjh21`milieu.h' header file, which in turn includes the processor header file.
2771.1Sbjh21
2781.1Sbjh21
2791.1Sbjh21-------------------------------------------------------------------------------
2801.1Sbjh21Steps to Creating a `softfloat.o'
2811.1Sbjh21
2821.1Sbjh21Porting and/or compiling SoftFloat involves the following steps:
2831.1Sbjh21
2841.1Sbjh211. If one does not already exist, create an appropriate `.h' file in the
2851.1Sbjh21   `processors' directory.
2861.1Sbjh21
2871.1Sbjh212. If `BITS64' is defined in the `processors' header file, choose whether
2881.1Sbjh21   to compile the 32-bit or 64-bit implementation of SoftFloat.  If
2891.1Sbjh21   `BITS64' is not defined, your only choice is the 32-bit implementation.
2901.1Sbjh21   The remaining steps occur within either the `bits32' or `bits64'
2911.1Sbjh21   subdirectories.
2921.1Sbjh21
2931.1Sbjh213. If one does not already exist, create an appropriate target-specific
2941.1Sbjh21   subdirectory by copying the given `templates' directory.
2951.1Sbjh21
2961.1Sbjh214. In the target-specific subdirectory, edit the files `softfloat-specialize'
2971.1Sbjh21   and `softfloat.h' to define the desired exception handling functions
2981.1Sbjh21   and mode control values.  In the `softfloat.h' header file, ensure also
2991.1Sbjh21   that all declarations give the proper target-specific type (such as
3001.1Sbjh21   `int' or `long') corresponding to the target-independent type used in
3011.1Sbjh21   `softfloat.c' (such as `int32').  None of the type names declared in the
3021.1Sbjh21   `processors' header file should appear in `softfloat.h'.
3031.1Sbjh21
3041.1Sbjh215. In the target-specific subdirectory, edit the files `milieu.h' and
3051.1Sbjh21   `Makefile' to reflect the current environment.
3061.1Sbjh21
3071.1Sbjh216. In the target-specific subdirectory, execute `make'.
3081.1Sbjh21
3091.1Sbjh21For the targets that are supplied, if the expected compiler is available
3101.1Sbjh21(usually `gcc'), it should only be necessary to execute `make' in the
3111.1Sbjh21target-specific subdirectory.
3121.1Sbjh21
3131.1Sbjh21
3141.1Sbjh21-------------------------------------------------------------------------------
3151.1Sbjh21Making `softfloat.o' a Library
3161.1Sbjh21
3171.1Sbjh21SoftFloat is not made into a software library by the supplied makefile.
3181.1Sbjh21If desired, `softfloat.o' can easily be put into its own library (in Unix,
3191.1Sbjh21`softfloat.a') using the usual system tool (in Unix, `ar').
3201.1Sbjh21
3211.1Sbjh21
3221.1Sbjh21-------------------------------------------------------------------------------
3231.1Sbjh21Testing SoftFloat
3241.1Sbjh21
3251.1Sbjh21SoftFloat can be tested using the `testsoftfloat' program by the same
3261.1Sbjh21author.  The `testsoftfloat' program is part of the TestFloat package
3271.1Sbjh21available at the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/arithmetic/
3281.1Sbjh21TestFloat.html'.
3291.1Sbjh21
3301.1Sbjh21
3311.1Sbjh21-------------------------------------------------------------------------------
3321.1Sbjh21Timing SoftFloat
3331.1Sbjh21
3341.1Sbjh21A program called `timesoftfloat' for timing the SoftFloat functions is
3351.1Sbjh21included with the SoftFloat source code.  Compiling `timesoftfloat' should
3361.1Sbjh21pose no difficulties once `softfloat.o' exists.  The supplied makefile
3371.1Sbjh21will create a `timesoftfloat' executable by default after generating
3381.1Sbjh21`softfloat.o'.  See `timesoftfloat.txt' for documentation about using
3391.1Sbjh21`timesoftfloat'.
3401.1Sbjh21
3411.1Sbjh21
3421.1Sbjh21-------------------------------------------------------------------------------
3431.1Sbjh21Compiler Options and Efficiency
3441.1Sbjh21
3451.1Sbjh21In order to get good speed with SoftFloat, it is important that the compiler
3461.1Sbjh21inline the routines that have been marked `INLINE' in the code.  Even if
3471.1Sbjh21inlining cannot be forced by an appropriate definition of the `INLINE'
3481.1Sbjh21macro, the compiler may still be able to perform inlining on its own as
3491.1Sbjh21an optimization.  In that case, the makefile should be edited to give the
3501.1Sbjh21compiler whatever option is required to cause it to inline small functions.
3511.1Sbjh21
3521.1Sbjh21The ability of the processor to do fast shifts has been assumed.  Efficiency
3531.1Sbjh21will not be as good on processors for which this is not the case (such as
3541.1Sbjh21the original Motorola 68000 or Intel 8086 processors).
3551.1Sbjh21
3561.1Sbjh21
3571.1Sbjh21-------------------------------------------------------------------------------
3581.1Sbjh21Processor-Specific Optimization of `softfloat.c' Using `softfloat-macros'
3591.1Sbjh21
3601.1Sbjh21The `softfloat-macros' source fragment defines arithmetic functions used
3611.1Sbjh21as primitives by `softfloat.c'.  This file has been written in a target-
3621.1Sbjh21independent form.  For a given target, it may be possible to improve on
3631.1Sbjh21these functions using target-specific and/or non-ISO-C features (such
3641.1Sbjh21as `asm' statements).  For example, one of the ``macro'' functions takes
3651.1Sbjh21two word-size integers and returns their full product in two words.
3661.1Sbjh21This operation can be done directly in hardware on many processors; but
3671.1Sbjh21because it is not available through standard C, the function defined in
3681.1Sbjh21`softfloat-macros' uses four multiplies to achieve the same result.
3691.1Sbjh21
3701.1Sbjh21To address these shortcomings, a customized version of `softfloat-macros'
3711.1Sbjh21can be created in any of the target-specific subdirectories.  A simple
3721.1Sbjh21modification to the target's makefile should be sufficient to ensure that
3731.1Sbjh21the custom version is used instead of the generic one.
3741.1Sbjh21
3751.1Sbjh21
3761.1Sbjh21-------------------------------------------------------------------------------
3771.1Sbjh21Contact Information
3781.1Sbjh21
3791.1Sbjh21At the time of this writing, the most up-to-date information about
3801.1Sbjh21SoftFloat and the latest release can be found at the Web page `http://
3811.1Sbjh21HTTP.CS.Berkeley.EDU/~jhauser/arithmetic/SoftFloat.html'.
3821.1Sbjh21
3831.1Sbjh21
384