Path: blob/main/lib/libc/softfloat/softfloat-source.txt
39481 views
$NetBSD: softfloat-source.txt,v 1.2 2006/11/24 19:46:58 christos Exp $12SoftFloat Release 2a Source Documentation34John R. Hauser51998 December 14678-------------------------------------------------------------------------------9Introduction1011SoftFloat is a software implementation of floating-point that conforms to12the IEC/IEEE Standard for Binary Floating-Point Arithmetic. SoftFloat can13support four floating-point formats: single precision, double precision,14extended double precision, and quadruple precision. All operations required15by the IEEE Standard are implemented, except for conversions to and from16decimal. SoftFloat is distributed in the form of C source code, so a17C compiler is needed to compile the code. Support for the extended double-18precision and quadruple-precision formats is dependent on the C compiler19implementing a 64-bit integer type.2021This document gives information needed for compiling and/or porting22SoftFloat.2324The source code for SoftFloat is intended to be relatively machine-25independent and should be compilable using any ISO/ANSI C compiler. At the26time of this writing, SoftFloat has been successfully compiled with the GNU27C Compiler (`gcc') for several platforms.282930-------------------------------------------------------------------------------31Limitations3233SoftFloat as written requires an ISO/ANSI-style C compiler. No attempt has34been made to accommodate compilers that are not ISO-conformant. Older ``K&R-35style'' compilers are not adequate for compiling SoftFloat. All testing I36have done so far has been with the GNU C Compiler. Compilation with other37compilers should be possible but has not been tested.3839The SoftFloat sources assume that source code file names can be longer than408 characters. In order to compile under an MS-DOS-type system, many of the41source files will need to be renamed, and the source and makefiles edited42appropriately. Once compiled, the SoftFloat binary does not depend on the43existence of long file names.4445The underlying machine is assumed to be binary with a word size that is a46power of 2. Bytes are 8 bits. Support for the extended double-precision47and quadruple-precision formats depends on the C compiler implementing48a 64-bit integer type. If the largest integer type supported by the49C compiler is 32 bits, SoftFloat is limited to the single- and double-50precision formats.515253-------------------------------------------------------------------------------54Contents5556Introduction57Limitations58Contents59Legal Notice60SoftFloat Source Directory Structure61SoftFloat Source Files62processors/*.h63softfloat/bits*/*/softfloat.h64softfloat/bits*/*/milieu.h65softfloat/bits*/*/softfloat-specialize66softfloat/bits*/softfloat-macros67softfloat/bits*/softfloat.c68Steps to Creating a `softfloat.o'69Making `softfloat.o' a Library70Testing SoftFloat71Timing SoftFloat72Compiler Options and Efficiency73Processor-Specific Optimization of `softfloat.c' Using `softfloat-macros'74Contact Information75767778-------------------------------------------------------------------------------79Legal Notice8081SoftFloat was written by John R. Hauser. This work was made possible in82part by the International Computer Science Institute, located at Suite 600,831947 Center Street, Berkeley, California 94704. Funding was partially84provided by the National Science Foundation under grant MIP-9311980. The85original version of this code was written as part of a project to build86a fixed-point vector processor in collaboration with the University of87California at Berkeley, overseen by Profs. Nelson Morgan and John Wawrzynek.8889THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort90has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT91TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO92PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY93AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.949596-------------------------------------------------------------------------------97SoftFloat Source Directory Structure9899Because SoftFloat is targeted to multiple platforms, its source code100is slightly scattered between target-specific and target-independent101directories and files. The directory structure is as follows:102103processors104softfloat105bits64106templates107386-Win32-gcc108SPARC-Solaris-gcc109bits32110templates111386-Win32-gcc112SPARC-Solaris-gcc113114The two topmost directories and their contents are:115116softfloat - Most of the source code needed for SoftFloat.117processors - Target-specific header files that are not specific to118SoftFloat.119120The `softfloat' directory is further split into two parts:121122bits64 - SoftFloat implementation using 64-bit integers.123bits32 - SoftFloat implementation using only 32-bit integers.124125Within these directories are subdirectories for each of the targeted126platforms. The SoftFloat source code is distributed with targets127`386-Win32-gcc' and `SPARC-Solaris-gcc' (and perhaps others) already128prepared for both the 32-bit and 64-bit implementations. Source files that129are not within these target-specific subdirectories are intended to be130target-independent.131132The naming convention used for the target-specific directories is133`<processor>-<executable-type>-<compiler>'. The names of the supplied134target directories should be interpreted as follows:135136<processor>:137386 - Intel 386-compatible processor.138SPARC - SPARC processor (as used by Sun machines).139<executable-type>:140Win32 - Microsoft Win32 executable.141Solaris - Sun Solaris executable.142<compiler>:143gcc - GNU C Compiler.144145You do not need to maintain this convention if you do not want to.146147Alongside the supplied target-specific directories is a `templates'148directory containing a set of ``generic'' target-specific source files. A149new target directory can be created by copying the `templates' directory and150editing the files inside. (Complete instructions for porting SoftFloat to a151new target are in the section _Steps_to_Creating_a_`softfloat.o'_.) Note152that the `templates' directory will not work as a target directory without153some editing. To avoid confusion, it would be wise to refrain from editing154the files inside `templates' directly.155156157-------------------------------------------------------------------------------158SoftFloat Source Files159160The purpose of each source file is described below. In the following,161the `*' symbol is used in place of the name of a specific target, such as162`386-Win32-gcc' or `SPARC-Solaris-gcc', or in place of some other text, as163in `bits*' for either `bits32' or `bits64'.164165- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -166processors/*.h167168The target-specific `processors' header file defines integer types169of various sizes, and also defines certain C preprocessor macros that170characterize the target. The two examples supplied are `386-gcc.h' and171`SPARC-gcc.h'. The naming convention used for processor header files is172`<processor>-<compiler>.h'.173174If 64-bit integers are supported by the compiler, the macro name `BITS64'175should be defined here along with the corresponding 64-bit integer176types. In addition, the function-like macro `LIT64' must be defined for177constructing 64-bit integer literals (constants). The `LIT64' macro is used178consistently in the SoftFloat code to annotate 64-bit literals.179180If `BITS64' is not defined, only the 32-bit version of SoftFloat can be181compiled. If `BITS64' _is_ defined, either can be compiled.182183If an inlining attribute (such as an `inline' keyword) is provided by the184compiler, the macro `INLINE' should be defined to the appropriate keyword.185If not, `INLINE' can be set to the keyword `static'. The `INLINE' macro186appears in the SoftFloat source code before every function that should187be inlined by the compiler. SoftFloat depends on inlining to obtain188good speed. Even if inlining cannot be forced with a language keyword,189the compiler may still be able to perform inlining on its own as an190optimization. If a command-line option is needed to convince the compiler191to perform this optimization, this should be assured in the makefile. (See192the section _Compiler_Options_and_Efficiency_ below.)193194- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -195softfloat/bits*/*/softfloat.h196197The target-specific `softfloat.h' header file defines the SoftFloat198interface as seen by clients.199200Unlike the actual function definitions in `softfloat.c', the declarations201in `softfloat.h' do not use any of the types defined by the `processors'202header file. This is done so that clients will not have to include the203`processors' header file in order to use SoftFloat. Nevertheless, the204target-specific declarations in `softfloat.h' must match what `softfloat.c'205expects. For example, if `int32' is defined as `int' in the `processors'206header file, then in `softfloat.h' the output of `float32_to_int32' should207be stated as `int', although in `softfloat.c' it is given in target-208independent form as `int32'.209210For the `bits64' implementation of SoftFloat, the macro names `FLOATX80' and211`FLOAT128' must be defined in order for the extended double-precision and212quadruple-precision formats to be enabled in the code. Conversely, either213or both of the extended formats can be disabled by simply removing the214`#define' of the respective macro. When an extended format is not enabled,215none of the functions that either input or output the format are defined,216and no space is taken up in `softfloat.o' by such functions. There is no217provision for disabling the usual single- and double-precision formats.218219- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -220softfloat/bits*/*/milieu.h221222The target-specific `milieu.h' header file provides declarations that are223needed to compile SoftFloat. In addition, deviations from ISO/ANSI C by224the compiler (such as names not properly declared in system header files)225are corrected in this header if possible.226227- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -228softfloat/bits*/*/softfloat-specialize229230This target-specific C source fragment defines:231232-- whether tininess for underflow is detected before or after rounding by233default;234-- what (if anything) special happens when exceptions are raised;235-- how signaling NaNs are distinguished from quiet NaNs;236-- the default generated quiet NaNs; and237-- how NaNs are propagated from function inputs to output.238239These details are not decided by the IEC/IEEE Standard. This fragment is240included verbatim within `softfloat.c' when SoftFloat is compiled.241242- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -243softfloat/bits*/softfloat-macros244245This target-independent C source fragment defines a number of arithmetic246functions used as primitives within the `softfloat.c' source. Most of the247functions defined here are intended to be inlined for efficiency. This248fragment is included verbatim within `softfloat.c' when SoftFloat is249compiled.250251Target-specific variations on this file are possible. See the section252_Processor-Specific_Optimization_of_`softfloat.c'_Using_`softfloat-macros'_253below.254255- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -256softfloat/bits*/softfloat.c257258The target-independent `softfloat.c' source file contains the body of the259SoftFloat implementation.260261- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -262263The inclusion of the files above within each other (using `#include') can be264shown graphically as follows:265266softfloat/bits*/softfloat.c267softfloat/bits*/*/milieu.h268processors/*.h269softfloat/bits*/*/softfloat.h270softfloat/bits*/*/softfloat-specialize271softfloat/bits*/softfloat-macros272273Note in particular that `softfloat.c' does not include the `processors'274header file directly. Rather, `softfloat.c' includes the target-specific275`milieu.h' header file, which in turn includes the processor header file.276277278-------------------------------------------------------------------------------279Steps to Creating a `softfloat.o'280281Porting and/or compiling SoftFloat involves the following steps:2822831. If one does not already exist, create an appropriate `.h' file in the284`processors' directory.2852862. If `BITS64' is defined in the `processors' header file, choose whether287to compile the 32-bit or 64-bit implementation of SoftFloat. If288`BITS64' is not defined, your only choice is the 32-bit implementation.289The remaining steps occur within either the `bits32' or `bits64'290subdirectories.2912923. If one does not already exist, create an appropriate target-specific293subdirectory by copying the given `templates' directory.2942954. In the target-specific subdirectory, edit the files `softfloat-specialize'296and `softfloat.h' to define the desired exception handling functions297and mode control values. In the `softfloat.h' header file, ensure also298that all declarations give the proper target-specific type (such as299`int' or `long') corresponding to the target-independent type used in300`softfloat.c' (such as `int32'). None of the type names declared in the301`processors' header file should appear in `softfloat.h'.3023035. In the target-specific subdirectory, edit the files `milieu.h' and304`Makefile' to reflect the current environment.3053066. In the target-specific subdirectory, execute `make'.307308For the targets that are supplied, if the expected compiler is available309(usually `gcc'), it should only be necessary to execute `make' in the310target-specific subdirectory.311312313-------------------------------------------------------------------------------314Making `softfloat.o' a Library315316SoftFloat is not made into a software library by the supplied makefile.317If desired, `softfloat.o' can easily be put into its own library (in Unix,318`softfloat.a') using the usual system tool (in Unix, `ar').319320321-------------------------------------------------------------------------------322Testing SoftFloat323324SoftFloat can be tested using the `testsoftfloat' program by the same325author. The `testsoftfloat' program is part of the TestFloat package326available at the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/arithmetic/327TestFloat.html'.328329330-------------------------------------------------------------------------------331Timing SoftFloat332333A program called `timesoftfloat' for timing the SoftFloat functions is334included with the SoftFloat source code. Compiling `timesoftfloat' should335pose no difficulties once `softfloat.o' exists. The supplied makefile336will create a `timesoftfloat' executable by default after generating337`softfloat.o'. See `timesoftfloat.txt' for documentation about using338`timesoftfloat'.339340341-------------------------------------------------------------------------------342Compiler Options and Efficiency343344In order to get good speed with SoftFloat, it is important that the compiler345inline the routines that have been marked `INLINE' in the code. Even if346inlining cannot be forced by an appropriate definition of the `INLINE'347macro, the compiler may still be able to perform inlining on its own as348an optimization. In that case, the makefile should be edited to give the349compiler whatever option is required to cause it to inline small functions.350351The ability of the processor to do fast shifts has been assumed. Efficiency352will not be as good on processors for which this is not the case (such as353the original Motorola 68000 or Intel 8086 processors).354355356-------------------------------------------------------------------------------357Processor-Specific Optimization of `softfloat.c' Using `softfloat-macros'358359The `softfloat-macros' source fragment defines arithmetic functions used360as primitives by `softfloat.c'. This file has been written in a target-361independent form. For a given target, it may be possible to improve on362these functions using target-specific and/or non-ISO-C features (such363as `asm' statements). For example, one of the ``macro'' functions takes364two word-size integers and returns their full product in two words.365This operation can be done directly in hardware on many processors; but366because it is not available through standard C, the function defined in367`softfloat-macros' uses four multiplies to achieve the same result.368369To address these shortcomings, a customized version of `softfloat-macros'370can be created in any of the target-specific subdirectories. A simple371modification to the target's makefile should be sufficient to ensure that372the custom version is used instead of the generic one.373374375-------------------------------------------------------------------------------376Contact Information377378At the time of this writing, the most up-to-date information about379SoftFloat and the latest release can be found at the Web page `http://380HTTP.CS.Berkeley.EDU/~jhauser/arithmetic/SoftFloat.html'.381382383384385