home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / reference / amiga_mail_vol1 / math / introieee < prev    next >
Text File  |  1990-01-26  |  15KB  |  447 lines

  1. (c)  Copyright 1989 Commodore-Amiga, Inc.   All rights reserved.
  2. The information contained herein is subject to change without notice, and 
  3. is provided "as is" without warranty of any kind, either expressed or implied.  
  4. The entire risk as to the use of this information is assumed by the user.
  5.  
  6.  
  7.  
  8.  
  9.                     Introduction to 1.3 IEEE 
  10.                    Double Precision Libraries
  11.  
  12.                          by Dale Luck
  13.  
  14.  
  15.  
  16.  
  17.  The basic double precision IEEE library has been rewritten for V1.3.
  18.  The new library is up to 4 times faster than the old one that came with 
  19.  V1.2.  There were also several bugs fixed.  And the routines now produce
  20.  slightly more accurate results.  I've listed some benchmarks comparing
  21.  the two versions of the libraries at the end of this article.
  22.  
  23.  Besides the faster software emulation of floating point, the new IEEE math
  24.  library recognizes and uses the 68020/68881 processor combination and
  25.  will use  the special floating point instructions available.  Also, if
  26.  an auto-configured math resource is available, it will use that as well.
  27.  Typically, this resource would point to the base of a 68881 designed as
  28.  a 16 bit IO port.  But it could be another device as well. 
  29.  
  30.  With the new library, you also have the ability to programmatically trap
  31.  math errors such as overflow and divide by zero.  Your program can now
  32.  ignore them or take suitable action without visiting the GURU.
  33.  
  34.  In addition to a new version of the basic mathieeedoubbas.library, a
  35.  second library supporting transcendental functions has been added. The 
  36.  name of the new library is mathieedoubtrans.library for IEEE double 
  37.  precision transcendental library.  It supports the same functions as the
  38.  transcendantal library for the Motorola fast floating point, such as sine,
  39.  cosine, square root, etc.  This library also can identify and use the 
  40.  68020/68881 combination or other math resources. And it has a very fast 
  41.  software square root routine.
  42.  
  43.  
  44. When Should You Use These Libraries?
  45.  
  46.  These libraries have been benchmarked as the fastest IEEE double precision
  47.  libraries available on the Amiga as well as outperforming almost all other
  48.  software math libraries in the Amiga class personal workstation market.
  49.  
  50.  If you need the precision of IEEE double, and wish to have a transparent
  51.  improvement in speed when your programs run on machines with math
  52.  coprocessors, then you should use these libraries.  All the decision
  53.  making is done by the library when it is first initialized and it will
  54.  use the fastest available resources to do your math.  You only need
  55.  one program to support a standard Amiga, a 68020/68881 Amiga, or a
  56.  external math coprocessor Amiga.  It works automatically.
  57.  
  58.  
  59.  
  60.  
  61.  
  62. When Should You Avoid These Libraries?
  63.  
  64.  If you don't need double precision, use the Motorola fast floating point
  65.  routines.  As you can see from the benchmarks, the Motorola routines are
  66.  still quite a bit faster.
  67.  
  68.  If you want your math to be the fastest possible, you will want to use 
  69.  the new instructions available on the 68020/68881 directly in your code.
  70.  In that case, you would not need the IEEE libraries.  However this would
  71.  prevent your code from running on conventional 68000 based Amigas unless
  72.  you supply different versions of your code for each configuration.
  73.  
  74.  
  75.  
  76. Floating Point Formats
  77.  
  78.  Here's a chart comparing the various methods of representing floating 
  79.  point numbers used by Amiga system software.  The IEEE double precision
  80.  libraries operate on 64 bit quantities.  The Motorola FFP libraries use
  81.  32 bits.
  82.  
  83.  Note that there is a "hidden" bit in the fraction part of IEEE numbers. 
  84.  Since all numbers are normalized, the leading 1 is dropped off.
  85.  
  86.  
  87.  
  88.                      Motorola    Single        Double
  89.     Field Size (bits)        FFP        IEEE        IEEE
  90.  
  91.     Sign            1        1        1
  92.     Exponent            7        7        11
  93.     Fraction                24        23+1        52+1
  94.  
  95.     Total            32        32        64
  96.  
  97.  
  98.  
  99.  
  100.     Minimum (+) number        5.4e-20        1.3e-38        2.2e-308
  101.  
  102.     Largest (+) number        9.2e+19        3.4e+38        1.8e+307
  103.  
  104.     Minimum (+) number           n/a        1.4e-45        4.9e-324
  105.     (denormalized)
  106.  
  107.     Denormalized means reduced in precision so that numbers closer 
  108.     to zero can be represented.
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  Floating Point Representation
  119.  
  120.  
  121.     +--------+--------+--------+--------+
  122.     |ffffffff|ffffffff|ffffffff|Seeeeeee|    Motorola FFP
  123.     +--------+--------+--------+--------+  
  124.   
  125.     +--------+--------+--------+--------+
  126.     |Seeeeeee|ffffffff|ffffffff|ffffffff|    IEEE Single
  127.     +--------+--------+--------+--------+
  128.  
  129.  
  130.                         IEEE Double
  131.  
  132.     +--------+--------+--------+--------+--------+--------+--------+--------+
  133.     |Seeeeeee|eeeeffff|ffffffff|ffffffff|ffffffff|ffffffff|ffffffff|ffffffff|
  134.     +--------+--------+--------+--------+--------+--------+--------+--------+   
  135.  
  136.     S = Sign bit
  137.     f = fraction bits
  138.     e = exponent bits
  139.  
  140.  
  141.  The scheme used in IEEE floating point representation includes a few
  142.  "special" numbers.  Certain patterns of bits are used to represent
  143.  exceptions: 
  144.  
  145.     o  NAN      'Not A Number'    (result of 0/0)
  146.  
  147.     o  INF            'Infinity'    (result of 1/0)
  148.  
  149.  There are other assigned patterns in addition to these two.
  150.  
  151.  
  152.  
  153.  
  154.  
  155. Using the Libraries
  156.  
  157.  The new IEEE libraries should be placed in the :libs directory.  Use
  158.  the mathieeedoubbas.library to replace the old library of that same
  159.  name.  The mathieeedoubtrans.library is an all new addition.
  160.  
  161.  Code that calls routines in these libraries will have to be linked
  162.  to the new .lib files which also have awkward names.  They are
  163.  mathieeedoubbas_lib.lib and mathieeedoubtrans_lib.lib.  And there
  164.  is a new .fd file for the transcendental functions.
  165.  
  166.  Using the IEEE routines is straight forward - they are a standard
  167.  library.  Simply open the library, use the routines and close the
  168.  library when you are done.  For example, to use the Sine routine:
  169.  
  170.  
  171.  
  172.  
  173.  
  174.         /* IEEE Sine Routine                                    */
  175.         /* Compile under Lattice 4.0  by linking with   c.o +   */
  176.         /* mathieeedoubbas_lib.lib + mathieeedoubtrans_lib.lib  */
  177.         /* + lcm.lib + lc.lib + amiga.lib                       */
  178.  
  179.            double IEEEDPSin();
  180.     extern int    MathIeeeDoubBasBase;
  181.                int    MathIeeeDoubTransBase;
  182.  
  183.     void
  184.     main()
  185.     {
  186.  
  187.     double x=0;
  188.  
  189.     MathIeeeDoubBasBase=OpenLibrary("mathieeedoubbas.library",0);
  190.     if(MathIeeeDoubBasBase==0)  exit(0);
  191.  
  192.     MathIeeeDoubTransBase=OpenLibrary("mathieeedoubtrans.library",0);
  193.     if(MathIeeeDoubTransBase==0)
  194.       {
  195.       CloseLibrary(MathIeeeDoubBasBase);
  196.       exit(0);
  197.       }
  198.  
  199.     x=IEEEDPSin( (double) 60 );
  200.     printf("sin 60 = %e\n",x);    
  201.  
  202.     CloseLibrary(MathIeeeDoubBasBase);
  203.     CloseLibrary(MathIeeeDoubTransBase);
  204.  
  205.     }
  206.  
  207.  
  208.  
  209. Hardware Developer Information.
  210.  
  211.  To make use of CBM's standard peripheral support for 68881 you must design 
  212.  your peripheral to autoconfig.  Your autoconfig software must create a 
  213.  resource and add it to the resource list.  The name of this resource is 
  214.  "MathIEEE.resource".  The IEEE library will attempt to open this resource. 
  215.  If it finds it, it will extract the BaseAddr pointer and copy it into its 
  216.  library structure.  If the BaseAddr pointer is non-null it will use a 
  217.  different list of routine entry points when the IEEE library is initialized.
  218.  
  219.  After the IEEE library is initialized, the library again checks the resource 
  220.  for alternate function bits in Flags of the resource. The Basic library only 
  221.  checks the DblBasAlt bit, and the transcendental library only checks the
  222.  DblTransAlt bit.  If they are set, the library routine will call the function
  223.  whose address is in the corresponding Init field.  The arguments passed are 
  224.  a6=sysbase, a1=resource and a2=mathlibrary.
  225.  
  226.  If your device is not a 68881 then you may need to use this.  There are 
  227.  separate bits for different library capabilities in case your math resource 
  228.  is only able to handle a limited set of functions.  This will let you tie a 
  229.  math processor in that may only provide addition, subtraction, multiplication and
  230.  and  division functions.  The rest of software will use it transparently by 
  231.  calling your alternate routines.
  232.  
  233.  Amiga does not provide for arbitrating a math accelerator in a multitasking 
  234.  environment.  Therefore, you must provide your own support for this when your 
  235.  device autoconfigs.  The only exception is the 68020/68881 combination where 
  236.  support for that has been standard since V1.2.  Arbitration usually involves 
  237.  saving and restoring the state of you hardware device between task switches.
  238.  
  239.  We recommend that you look at the tc_Switch and tc_Launch vectors in the task
  240.  data structure. These are called each time control transfers from one task to
  241.  another.  Remember not to assume that you are the only process needing to use
  242.  those vectors.
  243.  
  244.  The resource data structure is as follows:
  245.  
  246.  STRUCTURE  MathIEEE,LN_SIZE
  247.         UWORD   MathIEEE_Flags
  248.         ULONG   MathIEEE_BaseAddr       ; for standard 68881 support
  249.         ULONG   MathIEEE_DblBasInit     ; something else besides 68881
  250.         ULONG   MathIEEE_DblTransInit   ; something else besides 68881
  251.         ULONG   MathIEEE_SnglBasInit    ; something else besides 68881
  252.         ULONG   MathIEEE_SnglTransInit  ; something else besides 68881
  253.  LABEL  MathIEEE_sizeof
  254. *
  255. *       Bits for MathIEEE_flags.  All unassigned bits must be 0
  256. *
  257.         BITDEF  MathIEEE,DblBasAlt,0            ; alternate Basic library
  258.         BITDEF  MathIEEE,DblTransAlt,1          ; alternate Trans library
  259.         BITDEF  MathIEEE,SnglBasAlt,2           ; alternate Basic library
  260.         BITDEF  MathIEEE,SnglTransAlt,3         ; alternate Trans library
  261.  
  262.  
  263.  The MathIEEE resource structure may grow in the future.  Extensions will be 
  264.  added as Commodore-Amiga adds new standards such as 80 bit extended format.
  265.  
  266.  The 'Init' entries in the math resource structure are only used if the 
  267.  corresponding Bit is set in the Flags field.  So if you are just a 68881, 
  268.  you do not need the Init entries.  Make sure you have cleared the Flags field.
  269.  This should allow us to add Extended Precision later.  For Init users, make 
  270.  sure you add yourself into the Open/Close/Expunge vectors for this library.
  271.  
  272.  
  273.  
  274.  
  275.  The library structure that is used is tentatively laid out as shown below.
  276.  I say tentatively because the name of the entries may change yet.  The order 
  277.  of entries, their usage and size will not change.  Naturally we may add new
  278.  fields to the end.
  279.  
  280.  
  281.  
  282.     STRUCTURE  MI,LIB_SIZE      ; Standard library node
  283.         UBYTE   io8_Flags       ; is this 68881?
  284.         UBYTE   io8_pad         ; line up to next 32bit boundary
  285.         ULONG   io8_68881       ; ptr to io68881 base
  286.         ULONG   io8_SysLib      ; ptr to SysBase
  287.         ULONG   io8_SegList     ; ptr to this SegList
  288.         ULONG   io8_Resource    ; ptr to mathIEEE.resource
  289.         ULONG   io8_opentask    ; called when task opens
  290.         ULONG   io8_closetask   ; called when task closes
  291.     LABEL   MI_SIZE
  292.  
  293.  
  294.  Of particular interest to hardware developers are the opentask and closetask 
  295.  entry points.  These functions will be called when a task calls OpenLibrary 
  296.  and CloseLibrary.  This will give the vendor the opportunity to set up any 
  297.  per task initialization necessary.  The Amiga library presently sets them up 
  298.  as NOPs in the case of straight emulation.  It puts the 68881 initialization 
  299.  code in there for the 68020/68881 as well as the peripheral 68881.  That 
  300.  initialization code currently sets up rounding modes and interrupt requests.
  301.  
  302.  If you need to override the defaults, you will have to set the appropriate
  303.  Alt bits in the Resource structure and overwrite the opentask/closetask
  304.  fields when your AltInit function is called.  The OpenLibrary routine checks 
  305.  the return value of opentask for errors.  If a nonzero is in d0.l then 
  306.  OpenLibrary will return 0 to the task trying to OpenLibrary.
  307.  
  308.  
  309.  On the 68020/68881 some new exceptions are generated.  Unfortunately the 
  310.  V1.2 operating system does not properly initialize these. For users of the 
  311.  new ramkick/A2024 system, the fixes have been added to the exec.library.  
  312.  For the rest we provide a program to run during your startup sequence to 
  313.  initialize the vectors and redirect processing back to exec when the new 
  314.  exceptions occur.  This is only necessary on 68020/68881 systems.
  315.  
  316.  
  317.  
  318. Benchmarks
  319.  
  320.  This section contains some benchmarks comparing the performance of the
  321.  various Amiga math libraries.  Use these as a guide when selecting the
  322.  math routines to be used for your application.
  323.  
  324.  All these benchmarks show the reults when compiling under Greenhill's C.
  325.  The results you get with another compiler will vary.
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  How does V1.3 stack up to V1.2?
  340.    A Comparison of Software
  341.  
  342.             V1.2      V1.3       V1.2
  343.                         IEEE      IEEE    MathFFP
  344.    Float        
  345.       10000    (secs)    92.14       45.22      17.64
  346.      256000    (secs) 580.58    282.52   136.78
  347.  
  348.    Calcpi
  349.     (kflops/sec)    2.07       4.93      11.14
  350.          PI error     -5.5e-14    -1.4e-11  6.1e-5
  351.  
  352.    Whetstone
  353.        (kwhets/sec)    12      24      78
  354.  
  355.    Savage
  356.     (secs)        N/A     470      98.2
  357.  
  358.    System tested:  A1000, 512k chip memory, 1 external floppy
  359.  
  360.  
  361.  
  362.  Transparent Increase in Speed
  363.  
  364.             V1.3/000    000/881        020/881
  365.    Float        
  366.       10000    (secs)     45.22        19.18         13.46
  367.      256000    (secs)    282.52            179.98        122.46
  368.  
  369.    BCalcpi
  370.     (kflops/sec)     4.93        7.89        11.78
  371.     PI error    -1.39e-11    -2.78e-11    -2.78e-11
  372.  
  373.    Whetstone
  374.        (kwhets/sec)    24        81        124
  375.  
  376.    Savage
  377.     (secs)           470        20.4        15.2
  378.     error        -6.9e-7        -5.6e-7        -5.6e-7
  379.  
  380.  
  381.  
  382.    Systems tested:
  383.  
  384.       V1.3/000 was an A1000 with 512k.
  385.       000/881  was an A1000 with 512k plus 2M and Microbotic's "881 Starmath
  386.       020/881  was an A2000 with CSA's 68020/68881, 2M memory and a 2090a
  387.  
  388.  Penultimate Speed Tests:
  389.  Comparison of Speed Using 
  390.   Inline F instructions
  391.  
  392.             V1.3/000    020/881
  393.    Float        
  394.       10000    (secs)     45.22         0.26*
  395.      256000    (secs)    282.52          15.86
  396.  
  397.    Calcpi
  398.     (kflops/sec)      4.93        81.3
  399.  
  400.    Whetstone
  401.        (kwhets/sec)       24        459
  402.  
  403.    Savage
  404.     (secs)           470          4.6
  405.  
  406.    Systems tested:
  407.  
  408.      V1.3/000 was an A1000 with 512k and 1 external floppy.
  409.      020/881  was an A2000 with  CSA's 68020/881, 2M memory and a 2090a.
  410.  
  411.      Note:  Under this test, the 020/881 test code will not run on a 
  412.      standard 68000 based system.
  413.  
  414.      * The Greenhill compiler may have optimized this benchmark to nothing.
  415.  
  416.  
  417.  Penultimate Speed Tests, II:
  418.    Inline Results With  
  419.    Fast 32-Bit Memory
  420.                                                            Inline       Inline
  421.                         020        020/881    030/882     020/881      030/882
  422.    Float        
  423.       10000    (secs)    25.6         6.08     5.16       0.24*     0.18*
  424.      256000    (secs)    168.74        54.08    47.52      15.28        13.16
  425.  
  426.    Calcpi
  427.     (kflops/sec)    8.44        25.29    28.8      90.09        114.42
  428.  
  429.    Whetstone        
  430.        (kwhets/sec)    39       263        291     673        889
  431.  
  432.    Savage
  433.     (secs)               320.8         8.4    7.6       4.46        3.98
  434.  
  435.    Systems tested:
  436.  
  437.      020     was an A2000 with CSA's 020 board running at 14 MHz.
  438.      020/881 was an A2000 with CSA's 020/881 board running at 14 MHz.
  439.      030/882 was an A2000 with CSA's 030/882 board running at 14/16 MHz.
  440.  
  441.      * The greenhills compiler may have optimized this benchmark to nothing.
  442.  
  443.  
  444.  
  445.  
  446.  
  447.