home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #6 / amigaacscoverdisc1998-061998.iso / games / descent / source / fix / fix.asm next >
Assembly Source File  |  1998-06-08  |  20KB  |  1,336 lines

  1. ;THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  2. ;SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  3. ;END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  4. ;ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  5. ;IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  6. ;SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  7. ;FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  8. ;CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  9. ;AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
  10. ;COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  11. ;
  12. ; $Source: f:/miner/source/fix/rcs/fix.asm $
  13. ; $Revision: 1.16 $
  14. ; $Author: mike $
  15. ; $Date: 1994/11/30 00:59:40 $
  16. ;
  17. ; Fixed-point rotines
  18. ;
  19. ; $Log: fix.asm $
  20. ; Revision 1.16  1994/11/30  00:59:40  mike
  21. ; optimizations.
  22. ; Revision 1.15  1994/11/16  18:05:01  matt
  23. ; Added error checking to atan2
  24. ; Revision 1.14  1994/10/21  12:14:26  matt
  25. ; For acos & asin, saturate at 1 instead of generate error
  26. ; Revision 1.13  1994/05/18  17:15:56  matt
  27. ; Allow sin & cos ptrs in C sincos func to be null
  28. ; Revision 1.12  1994/02/10  21:23:08  matt
  29. ; Changed 'if DEBUG_ON' to 'ifndef NDEBUG'
  30. ; Revision 1.11  1994/01/19  23:11:26  matt
  31. ; Made fix_atan2() left-handed, like our coordinate system
  32. ; Revision 1.10  1993/10/26  18:51:49  matt
  33. ; Fixed register trash in fix_atan2()
  34. ; Revision 1.9  1993/10/20  01:09:01  matt
  35. ; Add fix_asin(), improved fix_atan2()
  36. ; Revision 1.8  1993/10/19  23:53:48  matt
  37. ; Added fix_atan2()
  38. ; Revision 1.7  1993/10/19  22:22:34  matt
  39. ; Added fix_acos()
  40. ; Revision 1.6  1993/09/10  19:13:44  matt
  41. ; Fixed problem with quad_sqrt() when edx==0 and high bit of eax set
  42. ; Revision 1.5  1993/08/24  13:00:17  matt
  43. ; Adopted new standard, and made assembly-callable routines not trash any regs
  44. ; Revision 1.4  1993/08/12  14:43:34  matt
  45. ; Added rcsid
  46. ; Revision 1.3  1993/08/06  15:59:49  matt
  47. ; Added check for high bit of low longword set in quad_sqrt
  48. ; Changed long_sqrt to do longword moves and save the override
  49. ; Revision 1.2  1993/08/04  19:56:03  matt
  50. ; Fixed mistake in quad_sqrt first quess code
  51. ; Revision 1.1  1993/08/03  17:45:26  matt
  52. ; Initial revision
  53. ;
  54. ;
  55.  
  56. .386
  57.     option    oldstructs
  58.  
  59.     .nolist
  60.     include    types.inc
  61.     include    psmacros.inc
  62.     include    fix.inc
  63.     .list
  64.  
  65.     assume    cs:_TEXT,ds:_DATA
  66.  
  67. _DATA    segment    dword public USE32 'DATA'
  68.  
  69. rcsid    db    "$Id: fix.asm 1.16 1994/11/30 00:59:40 mike Exp $"
  70.  
  71. sin_table    dw    0
  72.     dw    402
  73.     dw    804
  74.     dw    1205
  75.     dw    1606
  76.     dw    2006
  77.     dw    2404
  78.     dw    2801
  79.     dw    3196
  80.     dw    3590
  81.     dw    3981
  82.     dw    4370
  83.     dw    4756
  84.     dw    5139
  85.     dw    5520
  86.     dw    5897
  87.     dw    6270
  88.     dw    6639
  89.     dw    7005
  90.     dw    7366
  91.     dw    7723
  92.     dw    8076
  93.     dw    8423
  94.     dw    8765
  95.     dw    9102
  96.     dw    9434
  97.     dw    9760
  98.     dw    10080
  99.     dw    10394
  100.     dw    10702
  101.     dw    11003
  102.     dw    11297
  103.     dw    11585
  104.     dw    11866
  105.     dw    12140
  106.     dw    12406
  107.     dw    12665
  108.     dw    12916
  109.     dw    13160
  110.     dw    13395
  111.     dw    13623
  112.     dw    13842
  113.     dw    14053
  114.     dw    14256
  115.     dw    14449
  116.     dw    14635
  117.     dw    14811
  118.     dw    14978
  119.     dw    15137
  120.     dw    15286
  121.     dw    15426
  122.     dw    15557
  123.     dw    15679
  124.     dw    15791
  125.     dw    15893
  126.     dw    15986
  127.     dw    16069
  128.     dw    16143
  129.     dw    16207
  130.     dw    16261
  131.     dw    16305
  132.     dw    16340
  133.     dw    16364
  134.     dw    16379
  135. cos_table    dw    16384
  136.     dw    16379
  137.     dw    16364
  138.     dw    16340
  139.     dw    16305
  140.     dw    16261
  141.     dw    16207
  142.     dw    16143
  143.     dw    16069
  144.     dw    15986
  145.     dw    15893
  146.     dw    15791
  147.     dw    15679
  148.     dw    15557
  149.     dw    15426
  150.     dw    15286
  151.     dw    15137
  152.     dw    14978
  153.     dw    14811
  154.     dw    14635
  155.     dw    14449
  156.     dw    14256
  157.     dw    14053
  158.     dw    13842
  159.     dw    13623
  160.     dw    13395
  161.     dw    13160
  162.     dw    12916
  163.     dw    12665
  164.     dw    12406
  165.     dw    12140
  166.     dw    11866
  167.     dw    11585
  168.     dw    11297
  169.     dw    11003
  170.     dw    10702
  171.     dw    10394
  172.     dw    10080
  173.     dw    9760
  174.     dw    9434
  175.     dw    9102
  176.     dw    8765
  177.     dw    8423
  178.     dw    8076
  179.     dw    7723
  180.     dw    7366
  181.     dw    7005
  182.     dw    6639
  183.     dw    6270
  184.     dw    5897
  185.     dw    5520
  186.     dw    5139
  187.     dw    4756
  188.     dw    4370
  189.     dw    3981
  190.     dw    3590
  191.     dw    3196
  192.     dw    2801
  193.     dw    2404
  194.     dw    2006
  195.     dw    1606
  196.     dw    1205
  197.     dw    804
  198.     dw    402
  199.     dw    0
  200.     dw    -402
  201.     dw    -804
  202.     dw    -1205
  203.     dw    -1606
  204.     dw    -2006
  205.     dw    -2404
  206.     dw    -2801
  207.     dw    -3196
  208.     dw    -3590
  209.     dw    -3981
  210.     dw    -4370
  211.     dw    -4756
  212.     dw    -5139
  213.     dw    -5520
  214.     dw    -5897
  215.     dw    -6270
  216.     dw    -6639
  217.     dw    -7005
  218.     dw    -7366
  219.     dw    -7723
  220.     dw    -8076
  221.     dw    -8423
  222.     dw    -8765
  223.     dw    -9102
  224.     dw    -9434
  225.     dw    -9760
  226.     dw    -10080
  227.     dw    -10394
  228.     dw    -10702
  229.     dw    -11003
  230.     dw    -11297
  231.     dw    -11585
  232.     dw    -11866
  233.     dw    -12140
  234.     dw    -12406
  235.     dw    -12665
  236.     dw    -12916
  237.     dw    -13160
  238.     dw    -13395
  239.     dw    -13623
  240.     dw    -13842
  241.     dw    -14053
  242.     dw    -14256
  243.     dw    -14449
  244.     dw    -14635
  245.     dw    -14811
  246.     dw    -14978
  247.     dw    -15137
  248.     dw    -15286
  249.     dw    -15426
  250.     dw    -15557
  251.     dw    -15679
  252.     dw    -15791
  253.     dw    -15893
  254.     dw    -15986
  255.     dw    -16069
  256.     dw    -16143
  257.     dw    -16207
  258.     dw    -16261
  259.     dw    -16305
  260.     dw    -16340
  261.     dw    -16364
  262.     dw    -16379
  263.     dw    -16384
  264.     dw    -16379
  265.     dw    -16364
  266.     dw    -16340
  267.     dw    -16305
  268.     dw    -16261
  269.     dw    -16207
  270.     dw    -16143
  271.     dw    -16069
  272.     dw    -15986
  273.     dw    -15893
  274.     dw    -15791
  275.     dw    -15679
  276.     dw    -15557
  277.     dw    -15426
  278.     dw    -15286
  279.     dw    -15137
  280.     dw    -14978
  281.     dw    -14811
  282.     dw    -14635
  283.     dw    -14449
  284.     dw    -14256
  285.     dw    -14053
  286.     dw    -13842
  287.     dw    -13623
  288.     dw    -13395
  289.     dw    -13160
  290.     dw    -12916
  291.     dw    -12665
  292.     dw    -12406
  293.     dw    -12140
  294.     dw    -11866
  295.     dw    -11585
  296.     dw    -11297
  297.     dw    -11003
  298.     dw    -10702
  299.     dw    -10394
  300.     dw    -10080
  301.     dw    -9760
  302.     dw    -9434
  303.     dw    -9102
  304.     dw    -8765
  305.     dw    -8423
  306.     dw    -8076
  307.     dw    -7723
  308.     dw    -7366
  309.     dw    -7005
  310.     dw    -6639
  311.     dw    -6270
  312.     dw    -5897
  313.     dw    -5520
  314.     dw    -5139
  315.     dw    -4756
  316.     dw    -4370
  317.     dw    -3981
  318.     dw    -3590
  319.     dw    -3196
  320.     dw    -2801
  321.     dw    -2404
  322.     dw    -2006
  323.     dw    -1606
  324.     dw    -1205
  325.     dw    -804
  326.     dw    -402
  327.     dw    0
  328.     dw    402
  329.     dw    804
  330.     dw    1205
  331.     dw    1606
  332.     dw    2006
  333.     dw    2404
  334.     dw    2801
  335.     dw    3196
  336.     dw    3590
  337.     dw    3981
  338.     dw    4370
  339.     dw    4756
  340.     dw    5139
  341.     dw    5520
  342.     dw    5897
  343.     dw    6270
  344.     dw    6639
  345.     dw    7005
  346.     dw    7366
  347.     dw    7723
  348.     dw    8076
  349.     dw    8423
  350.     dw    8765
  351.     dw    9102
  352.     dw    9434
  353.     dw    9760
  354.     dw    10080
  355.     dw    10394
  356.     dw    10702
  357.     dw    11003
  358.     dw    11297
  359.     dw    11585
  360.     dw    11866
  361.     dw    12140
  362.     dw    12406
  363.     dw    12665
  364.     dw    12916
  365.     dw    13160
  366.     dw    13395
  367.     dw    13623
  368.     dw    13842
  369.     dw    14053
  370.     dw    14256
  371.     dw    14449
  372.     dw    14635
  373.     dw    14811
  374.     dw    14978
  375.     dw    15137
  376.     dw    15286
  377.     dw    15426
  378.     dw    15557
  379.     dw    15679
  380.     dw    15791
  381.     dw    15893
  382.     dw    15986
  383.     dw    16069
  384.     dw    16143
  385.     dw    16207
  386.     dw    16261
  387.     dw    16305
  388.     dw    16340
  389.     dw    16364
  390.     dw    16379
  391.     dw    16384
  392.  
  393. asin_table    dw    0
  394.     dw    41
  395.     dw    81
  396.     dw    122
  397.     dw    163
  398.     dw    204
  399.     dw    244
  400.     dw    285
  401.     dw    326
  402.     dw    367
  403.     dw    408
  404.     dw    448
  405.     dw    489
  406.     dw    530
  407.     dw    571
  408.     dw    612
  409.     dw    652
  410.     dw    693
  411.     dw    734
  412.     dw    775
  413.     dw    816
  414.     dw    857
  415.     dw    897
  416.     dw    938
  417.     dw    979
  418.     dw    1020
  419.     dw    1061
  420.     dw    1102
  421.     dw    1143
  422.     dw    1184
  423.     dw    1225
  424.     dw    1266
  425.     dw    1307
  426.     dw    1348
  427.     dw    1389
  428.     dw    1431
  429.     dw    1472
  430.     dw    1513
  431.     dw    1554
  432.     dw    1595
  433.     dw    1636
  434.     dw    1678
  435.     dw    1719
  436.     dw    1760
  437.     dw    1802
  438.     dw    1843
  439.     dw    1884
  440.     dw    1926
  441.     dw    1967
  442.     dw    2009
  443.     dw    2050
  444.     dw    2092
  445.     dw    2134
  446.     dw    2175
  447.     dw    2217
  448.     dw    2259
  449.     dw    2300
  450.     dw    2342
  451.     dw    2384
  452.     dw    2426
  453.     dw    2468
  454.     dw    2510
  455.     dw    2551
  456.     dw    2593
  457.     dw    2636
  458.     dw    2678
  459.     dw    2720
  460.     dw    2762
  461.     dw    2804
  462.     dw    2847
  463.     dw    2889
  464.     dw    2931
  465.     dw    2974
  466.     dw    3016
  467.     dw    3059
  468.     dw    3101
  469.     dw    3144
  470.     dw    3187
  471.     dw    3229
  472.     dw    3272
  473.     dw    3315
  474.     dw    3358
  475.     dw    3401
  476.     dw    3444
  477.     dw    3487
  478.     dw    3530
  479.     dw    3573
  480.     dw    3617
  481.     dw    3660
  482.     dw    3704
  483.     dw    3747
  484.     dw    3791
  485.     dw    3834
  486.     dw    3878
  487.     dw    3922
  488.     dw    3965
  489.     dw    4009
  490.     dw    4053
  491.     dw    4097
  492.     dw    4142
  493.     dw    4186
  494.     dw    4230
  495.     dw    4275
  496.     dw    4319
  497.     dw    4364
  498.     dw    4408
  499.     dw    4453
  500.     dw    4498
  501.     dw    4543
  502.     dw    4588
  503.     dw    4633
  504.     dw    4678
  505.     dw    4723
  506.     dw    4768
  507.     dw    4814
  508.     dw    4859
  509.     dw    4905
  510.     dw    4951
  511.     dw    4997
  512.     dw    5043
  513.     dw    5089
  514.     dw    5135
  515.     dw    5181
  516.     dw    5228
  517.     dw    5274
  518.     dw    5321
  519.     dw    5367
  520.     dw    5414
  521.     dw    5461
  522.     dw    5508
  523.     dw    5556
  524.     dw    5603
  525.     dw    5651
  526.     dw    5698
  527.     dw    5746
  528.     dw    5794
  529.     dw    5842
  530.     dw    5890
  531.     dw    5938
  532.     dw    5987
  533.     dw    6035
  534.     dw    6084
  535.     dw    6133
  536.     dw    6182
  537.     dw    6231
  538.     dw    6281
  539.     dw    6330
  540.     dw    6380
  541.     dw    6430
  542.     dw    6480
  543.     dw    6530
  544.     dw    6580
  545.     dw    6631
  546.     dw    6681
  547.     dw    6732
  548.     dw    6783
  549.     dw    6835
  550.     dw    6886
  551.     dw    6938
  552.     dw    6990
  553.     dw    7042
  554.     dw    7094
  555.     dw    7147
  556.     dw    7199
  557.     dw    7252
  558.     dw    7306
  559.     dw    7359
  560.     dw    7413
  561.     dw    7466
  562.     dw    7521
  563.     dw    7575
  564.     dw    7630
  565.     dw    7684
  566.     dw    7740
  567.     dw    7795
  568.     dw    7851
  569.     dw    7907
  570.     dw    7963
  571.     dw    8019
  572.     dw    8076
  573.     dw    8133
  574.     dw    8191
  575.     dw    8249
  576.     dw    8307
  577.     dw    8365
  578.     dw    8424
  579.     dw    8483
  580.     dw    8543
  581.     dw    8602
  582.     dw    8663
  583.     dw    8723
  584.     dw    8784
  585.     dw    8846
  586.     dw    8907
  587.     dw    8970
  588.     dw    9032
  589.     dw    9095
  590.     dw    9159
  591.     dw    9223
  592.     dw    9288
  593.     dw    9353
  594.     dw    9418
  595.     dw    9484
  596.     dw    9551
  597.     dw    9618
  598.     dw    9686
  599.     dw    9754
  600.     dw    9823
  601.     dw    9892
  602.     dw    9963
  603.     dw    10034
  604.     dw    10105
  605.     dw    10177
  606.     dw    10251
  607.     dw    10324
  608.     dw    10399
  609.     dw    10475
  610.     dw    10551
  611.     dw    10628
  612.     dw    10706
  613.     dw    10785
  614.     dw    10866
  615.     dw    10947
  616.     dw    11029
  617.     dw    11113
  618.     dw    11198
  619.     dw    11284
  620.     dw    11371
  621.     dw    11460
  622.     dw    11550
  623.     dw    11642
  624.     dw    11736
  625.     dw    11831
  626.     dw    11929
  627.     dw    12028
  628.     dw    12130
  629.     dw    12234
  630.     dw    12340
  631.     dw    12449
  632.     dw    12561
  633.     dw    12677
  634.     dw    12796
  635.     dw    12919
  636.     dw    13046
  637.     dw    13178
  638.     dw    13315
  639.     dw    13459
  640.     dw    13610
  641.     dw    13770
  642.     dw    13939
  643.     dw    14121
  644.     dw    14319
  645.     dw    14538
  646.     dw    14786
  647.     dw    15079
  648.     dw    15462
  649.     dw    16384
  650.     dw    16384    ;extra for when exacty 1
  651.  
  652.  
  653. acos_table    dw    16384
  654.     dw    16343
  655.     dw    16303
  656.     dw    16262
  657.     dw    16221
  658.     dw    16180
  659.     dw    16140
  660.     dw    16099
  661.     dw    16058
  662.     dw    16017
  663.     dw    15976
  664.     dw    15936
  665.     dw    15895
  666.     dw    15854
  667.     dw    15813
  668.     dw    15772
  669.     dw    15732
  670.     dw    15691
  671.     dw    15650
  672.     dw    15609
  673.     dw    15568
  674.     dw    15527
  675.     dw    15487
  676.     dw    15446
  677.     dw    15405
  678.     dw    15364
  679.     dw    15323
  680.     dw    15282
  681.     dw    15241
  682.     dw    15200
  683.     dw    15159
  684.     dw    15118
  685.     dw    15077
  686.     dw    15036
  687.     dw    14995
  688.     dw    14953
  689.     dw    14912
  690.     dw    14871
  691.     dw    14830
  692.     dw    14789
  693.     dw    14748
  694.     dw    14706
  695.     dw    14665
  696.     dw    14624
  697.     dw    14582
  698.     dw    14541
  699.     dw    14500
  700.     dw    14458
  701.     dw    14417
  702.     dw    14375
  703.     dw    14334
  704.     dw    14292
  705.     dw    14250
  706.     dw    14209
  707.     dw    14167
  708.     dw    14125
  709.     dw    14084
  710.     dw    14042
  711.     dw    14000
  712.     dw    13958
  713.     dw    13916
  714.     dw    13874
  715.     dw    13833
  716.     dw    13791
  717.     dw    13748
  718.     dw    13706
  719.     dw    13664
  720.     dw    13622
  721.     dw    13580
  722.     dw    13537
  723.     dw    13495
  724.     dw    13453
  725.     dw    13410
  726.     dw    13368
  727.     dw    13325
  728.     dw    13283
  729.     dw    13240
  730.     dw    13197
  731.     dw    13155
  732.     dw    13112
  733.     dw    13069
  734.     dw    13026
  735.     dw    12983
  736.     dw    12940
  737.     dw    12897
  738.     dw    12854
  739.     dw    12811
  740.     dw    12767
  741.     dw    12724
  742.     dw    12680
  743.     dw    12637
  744.     dw    12593
  745.     dw    12550
  746.     dw    12506
  747.     dw    12462
  748.     dw    12419
  749.     dw    12375
  750.     dw    12331
  751.     dw    12287
  752.     dw    12242
  753.     dw    12198
  754.     dw    12154
  755.     dw    12109
  756.     dw    12065
  757.     dw    12020
  758.     dw    11976
  759.     dw    11931
  760.     dw    11886
  761.     dw    11841
  762.     dw    11796
  763.     dw    11751
  764.     dw    11706
  765.     dw    11661
  766.     dw    11616
  767.     dw    11570
  768.     dw    11525
  769.     dw    11479
  770.     dw    11433
  771.     dw    11387
  772.     dw    11341
  773.     dw    11295
  774.     dw    11249
  775.     dw    11203
  776.     dw    11156
  777.     dw    11110
  778.     dw    11063
  779.     dw    11017
  780.     dw    10970
  781.     dw    10923
  782.     dw    10876
  783.     dw    10828
  784.     dw    10781
  785.     dw    10733
  786.     dw    10686
  787.     dw    10638
  788.     dw    10590
  789.     dw    10542
  790.     dw    10494
  791.     dw    10446
  792.     dw    10397
  793.     dw    10349
  794.     dw    10300
  795.     dw    10251
  796.     dw    10202
  797.     dw    10153
  798.     dw    10103
  799.     dw    10054
  800.     dw    10004
  801.     dw    9954
  802.     dw    9904
  803.     dw    9854
  804.     dw    9804
  805.     dw    9753
  806.     dw    9703
  807.     dw    9652
  808.     dw    9601
  809.     dw    9549
  810.     dw    9498
  811.     dw    9446
  812.     dw    9394
  813.     dw    9342
  814.     dw    9290
  815.     dw    9237
  816.     dw    9185
  817.     dw    9132
  818.     dw    9078
  819.     dw    9025
  820.     dw    8971
  821.     dw    8918
  822.     dw    8863
  823.     dw    8809
  824.     dw    8754
  825.     dw    8700
  826.     dw    8644
  827.     dw    8589
  828.     dw    8533
  829.     dw    8477
  830.     dw    8421
  831.     dw    8365
  832.     dw    8308
  833.     dw    8251
  834.     dw    8193
  835.     dw    8135
  836.     dw    8077
  837.     dw    8019
  838.     dw    7960
  839.     dw    7901
  840.     dw    7841
  841.     dw    7782
  842.     dw    7721
  843.     dw    7661
  844.     dw    7600
  845.     dw    7538
  846.     dw    7477
  847.     dw    7414
  848.     dw    7352
  849.     dw    7289
  850.     dw    7225
  851.     dw    7161
  852.     dw    7096
  853.     dw    7031
  854.     dw    6966
  855.     dw    6900
  856.     dw    6833
  857.     dw    6766
  858.     dw    6698
  859.     dw    6630
  860.     dw    6561
  861.     dw    6492
  862.     dw    6421
  863.     dw    6350
  864.     dw    6279
  865.     dw    6207
  866.     dw    6133
  867.     dw    6060
  868.     dw    5985
  869.     dw    5909
  870.     dw    5833
  871.     dw    5756
  872.     dw    5678
  873.     dw    5599
  874.     dw    5518
  875.     dw    5437
  876.     dw    5355
  877.     dw    5271
  878.     dw    5186
  879.     dw    5100
  880.     dw    5013
  881.     dw    4924
  882.     dw    4834
  883.     dw    4742
  884.     dw    4648
  885.     dw    4553
  886.     dw    4455
  887.     dw    4356
  888.     dw    4254
  889.     dw    4150
  890.     dw    4044
  891.     dw    3935
  892.     dw    3823
  893.     dw    3707
  894.     dw    3588
  895.     dw    3465
  896.     dw    3338
  897.     dw    3206
  898.     dw    3069
  899.     dw    2925
  900.     dw    2774
  901.     dw    2614
  902.     dw    2445
  903.     dw    2263
  904.     dw    2065
  905.     dw    1846
  906.     dw    1598
  907.     dw    1305
  908.     dw    922
  909.     dw    0
  910.     dw    0    ;extra for when exacty 1
  911.  
  912. ;values for first guess in square root routines.  Note that the first entry
  913. ;is useful in quad_sqrt when edx=0 and high bit of eax is set.
  914. guess_table    db      1 dup (1)    ;0
  915.     db      3 dup (1)    ;1..3
  916.     db      5 dup (2)    ;4..8
  917.     db      7 dup (3)    ;9..15
  918.     db      9 dup (4)    ;16..24
  919.     db      11 dup (5)    ;25..35
  920.     db      13 dup (6)    ;36..48
  921.     db      15 dup (7)    ;49..63
  922.     db      17 dup (8)    ;64..80
  923.     db      19 dup (9)    ;81..99
  924.     db      21 dup (10)    ;100..120
  925.     db      23 dup (11)    ;121..143
  926.     db      25 dup (12)    ;144..168
  927.     db      27 dup (13)    ;169..195
  928.     db      29 dup (14)    ;196..224
  929.     db      31 dup (15)    ;225..255
  930.  
  931. _DATA    ends
  932.  
  933.  
  934.  
  935. _TEXT    segment    para public USE32 'CODE'
  936.  
  937. ;the sincos functions have two varients: the C version is passed pointer
  938. ;to variables for sin & cos, and the assembly version returns the values
  939. ;in two registers
  940.  
  941. ;takes ax=angle, returns eax=sin, ebx=cos.
  942. fix_fastsincos:
  943.     movzx    eax,ah    ;get high byte 
  944.     movsx    ebx,cos_table[eax*2]
  945.     sal    ebx,2    ;make a fix
  946.     movsx    eax,sin_table[eax*2]
  947.     sal    eax,2    ;make a fix
  948.     ret
  949.  
  950. ;takes ax=angle, returns eax=sin, ebx=cos.
  951. fix_sincos:
  952.     pushm    ecx,edx
  953.  
  954.     xor    edx, edx
  955.     xor    ecx, ecx
  956.     mov    dl, ah    ;get high byte
  957.     mov    cl, al    ;save low byte
  958.     shl    edx, 1
  959.  
  960.     movsx    eax,sin_table[edx]
  961.     movsx    ebx,sin_table+2[edx]
  962.     sub    ebx,eax
  963.     imul    ebx,ecx    ;mul by fraction
  964.     sar    ebx,8
  965.     add    eax,ebx    ;add in frac part
  966.     sal    eax,2    ;make a fix
  967.  
  968.     movsx    ebx,cos_table[edx]
  969.     movsx    edx,cos_table+2[edx]
  970.     sub    edx,ebx
  971.     imul    edx,ecx    ;mul by fraction
  972.     sar    edx,8
  973.     add    ebx,edx    ;add in frac part
  974.     sal    ebx,2    ;make a fix
  975.  
  976.     popm    ecx,edx
  977.  
  978.     ret
  979.  
  980.     align    16
  981.  
  982. ;takes eax=cos angle, returns ax=angle
  983. fix_acos:    pushm    ebx,ecx,edx
  984.  
  985.     abs_eax        ;get abs value
  986.     push    edx    ;save sign
  987.  
  988.     cmp    eax,10000h
  989.     jle    no_acos_oflow
  990.     mov    eax,10000h
  991. no_acos_oflow:
  992.     movzx    ecx,al    ;save low byte (fraction)
  993.  
  994.     mov    edx,eax
  995.  
  996.     sar    edx,8    ;get high byte (+1 bit)
  997.     movsx    eax,acos_table[edx*2]
  998.     movsx    ebx,acos_table+2[edx*2]
  999.     sub    ebx,eax
  1000.     imul    ebx,ecx    ;mul by fraction
  1001.     sar    ebx,8
  1002.     add    eax,ebx    ;add in frac part
  1003.  
  1004.     pop    edx    ;get sign back
  1005.     xor    eax,edx
  1006.     sub    eax,edx    ;make correct sign
  1007.     and    edx,8000h    ;zero or 1/2
  1008.     add    eax,edx
  1009.  
  1010.     popm    ebx,ecx,edx
  1011.  
  1012.     ret
  1013.  
  1014. ;takes eax=sin angle, returns ax=angle
  1015. fix_asin:    pushm    ebx,ecx,edx
  1016.  
  1017.     abs_eax        ;get abs value
  1018.     push    edx    ;save sign
  1019.  
  1020.     cmp    eax,10000h
  1021.     jle    no_asin_oflow
  1022.     mov    eax,10000h
  1023. no_asin_oflow:
  1024.     movzx    ecx,al    ;save low byte (fraction)
  1025.  
  1026.     mov    edx,eax
  1027.  
  1028.     sar    edx,8    ;get high byte (+1 bit)
  1029.     movsx    eax,asin_table[edx*2]
  1030.     movsx    ebx,asin_table+2[edx*2]
  1031.     sub    ebx,eax
  1032.     imul    ebx,ecx    ;mul by fraction
  1033.     sar    ebx,8
  1034.     add    eax,ebx    ;add in frac part
  1035.  
  1036.     pop    edx    ;get sign back
  1037.     xor    eax,edx    ;make sign correct
  1038.     sub    eax,edx
  1039.  
  1040.     popm    ebx,ecx,edx
  1041.  
  1042.     ret
  1043.  
  1044. ;given cos & sin of an angle, return that angle. takes eax=cos,ebx=sin. 
  1045. ;returns ax. parms need not be normalized, that is, the ratio eax/ebx must
  1046. ;equal the ratio cos/sin, but the parms need not be the actual cos & sin.  
  1047. ;NOTE: this is different from the standard C atan2, since it is left-handed.
  1048. ;trashes ebx
  1049. ;uses either asin or acos, to get better precision
  1050. fix_atan2:    pushm    ecx,edx
  1051.  
  1052.     ifndef    NDEBUG
  1053.     mov    edx,eax
  1054.     or    edx,ebx
  1055.     break_if    z,'Both parms to atan2 are zero!'
  1056.     endif
  1057.  
  1058.     push    ebx
  1059.     push    eax
  1060.  
  1061. ;find smaller of two
  1062.     pushm    eax,ebx    ;save
  1063.     abs_eax
  1064.     xchg    eax,ebx
  1065.     abs_eax
  1066.     cmp    ebx,eax    ;compare x to y
  1067.     popm    eax,ebx
  1068.     jl    use_cos
  1069.  
  1070. ;sin is smaller, use arcsin
  1071.  
  1072.     imul    eax
  1073.     xchg    eax,ebx
  1074.     mov    ecx,edx
  1075.     imul    eax
  1076.     add    eax,ebx
  1077.     adc    edx,ecx
  1078.     call    quad_sqrt
  1079.     mov    ecx,eax    ;ecx = mag
  1080.  
  1081.     pop    ebx    ;get cos, save in ebx
  1082.     pop    eax    ;get sin
  1083.     jecxz    sign_ok    ;abort!
  1084.     fixdiv    ecx    ;normalize it
  1085.     call    fix_asin    ;get angle
  1086.     or    ebx,ebx    ;check sign of cos
  1087.     jns    sign_ok
  1088.     sub    eax,8000h    ;adjust
  1089.     neg    eax
  1090. sign_ok:
  1091.     popm    ecx,edx
  1092.     ret
  1093.  
  1094.  
  1095. ;cos is smaller, use arccos
  1096.  
  1097. use_cos:    imul    eax
  1098.     xchg    eax,ebx
  1099.     mov    ecx,edx
  1100.     imul    eax
  1101.     add    eax,ebx
  1102.     adc    edx,ecx
  1103.     call    quad_sqrt
  1104.     mov    ecx,eax
  1105.  
  1106.     pop    eax    ;get cos
  1107.     fixdiv    ecx    ;normalize it
  1108.     call    fix_acos    ;get angle
  1109.     mov    ebx,eax    ;save in ebx
  1110.     pop    eax    ;get sin
  1111.     cdq        ;get sign of sin
  1112.     mov    eax,ebx    ;get cos back
  1113.     xor    eax,edx
  1114.     sub    eax,edx    ;make sign correct
  1115.  
  1116.     popm    ecx,edx
  1117.     ret
  1118.  
  1119.  
  1120.     public    fix_fastsincos_,fix_sincos_
  1121.  
  1122. ;C version - takes ax=angle, esi,edi=*sin,*cos. fills in sin&cos.
  1123. ;either (or both) pointers can be null
  1124. ;trashes eax,ebx
  1125. fix_fastsincos_:    call    fix_fastsincos
  1126.     or    esi,esi
  1127.     jz    no_sin
  1128.     mov    [esi],eax
  1129. no_sin:    or    edi,edi
  1130.     jz    no_cos
  1131.     mov    [edi],ebx
  1132. no_cos:    ret
  1133.  
  1134. ;C version - takes ax=angle, esi,edi=*sin,*cos. fills in sin&cos.
  1135. ;trashes eax,ebx
  1136. ;either (or both) pointers can be null
  1137. fix_sincos_:    call    fix_sincos
  1138.     or    esi,esi
  1139.     jz    no_sin
  1140.     mov    [esi],eax
  1141.     or    edi,edi
  1142.     jz    no_cos
  1143.     mov    [edi],ebx
  1144.     ret
  1145.  
  1146.  
  1147. ;standard Newtonian-iteration square root routine.  takes eax, returns ax
  1148. ;trashes eax,ebx,ecx,edx,esi,edi
  1149. long_sqrt:    or    eax,eax    ;check sign
  1150.     jle    error    ;zero or negative
  1151.  
  1152.     pushm    ebx,ecx,edx,esi,edi
  1153.  
  1154.     mov    edx,eax
  1155.     shr    edx,16    ;split eax -> dx:ax
  1156.  
  1157. ;get a good first quess by checking which byte most significant bit is in
  1158.     xor    ebx,ebx    ;clear high bytes for index
  1159.  
  1160.     or    dh,dh    ;highest byte
  1161.     jz    not_dh
  1162.     mov    bl,dh    ;get value for lookup
  1163.     mov    cl,12
  1164.     jmp    got_guess
  1165. not_dh:    or    dl,dl
  1166.     jz    not_dl
  1167.     mov    bl,dl    ;get value for lookup
  1168.     mov    cl,8
  1169.     jmp    got_guess
  1170. not_dl:    or    ah,ah
  1171.     jz    not_ah
  1172.     mov    bl,ah    ;get value for lookup
  1173.     mov    cl,4
  1174.     jmp    got_guess
  1175. not_ah:    mov    bl,al    ;get value for lookup
  1176.     mov    cl,0
  1177. got_guess:
  1178.     movzx    ebx,guess_table[ebx] ;get byte guess
  1179.     sal    ebx,cl    ;get in right place
  1180.  
  1181.     mov    ecx,eax
  1182.     mov    esi,edx    ;save dx:ax
  1183.  
  1184. ;the loop nearly always executes 3 times, so we'll unroll it 2 times and
  1185. ;not do any checking until after the third time.  By my calcutations, the
  1186. ;loop is executed 2 times in 99.97% of cases, 3 times in 93.65% of cases, 
  1187. ;four times in 16.18% of cases, and five times in 0.44% of cases.  It never
  1188. ;executes more than five times.  By timing, I determined that is is faster
  1189. ;to always execute three times and not check for termination the first two
  1190. ;times through.  This means that in 93.65% of cases, we save 6 cmp/jcc pairs,
  1191. ;and in 6.35% of cases we do an extra divide.  In real life, these numbers
  1192. ;might not be the same.
  1193.  
  1194. ;newt_loop:
  1195.  rept 2
  1196.     mov    eax,ecx
  1197.     mov    edx,esi    ;restore dx:ax
  1198.     div    bx    ;dx:ax / bx
  1199.     mov    edi,ebx    ;save for compare
  1200.     add    bx,ax
  1201.     rcr    bx,1    ;next guess = (d + q)/2
  1202.  endm
  1203.  
  1204. newt_loop:    mov    ax,cx
  1205.     mov    dx,si    ;restore dx:ax
  1206.     div    bx    ;dx:ax / bx
  1207.     cmp    ax,bx    ;correct?
  1208.     je    got_it    ;..yep
  1209.     mov    di,bx    ;save for compare
  1210.     add    bx,ax
  1211.     rcr    bx,1    ;next guess = (d + q)/2
  1212.     cmp    bx,ax
  1213.     je    almost_got_it
  1214.     cmp    bx,di
  1215.     jne    newt_loop
  1216.  
  1217. almost_got_it:    mov    ax,bx
  1218.     or    dx,dx    ;check remainder
  1219.     jz    got_it
  1220.     inc    ax
  1221. got_it:    popm    ebx,ecx,edx,esi,edi
  1222.     ret
  1223.  
  1224. ;sqrt called with zero or negative input. return zero
  1225. error:    xor    eax,eax
  1226.     ret
  1227.  
  1228. ;call the longword square root for quad with high longword equal zero
  1229. call_long:    call    long_sqrt
  1230.     movzx    eax,ax    ;return longword result
  1231.     ret
  1232.  
  1233.  
  1234. ;standard Newtonian-iteration square root routine.  takes edx:eax, returns eax
  1235. quad_sqrt:    or    edx,edx    ;check sign
  1236.     js    error    ;can't do negative number!
  1237.     jnz    must_do_quad    ;we really must do 64/32 div
  1238.     or    eax,eax    ;check high bit of low longword
  1239.     jns    call_long    ;we can use longword version
  1240. must_do_quad:
  1241.  
  1242.     pushm    ebx,ecx,edx,esi,edi
  1243.  
  1244. ;get a good first quess by checking which byte most significant bit is in
  1245.     xor    ebx,ebx    ;clear high bytes for index
  1246.  
  1247.     ror    edx,16    ;get high 2 bytes
  1248.  
  1249.     or    dh,dh
  1250.     jz    q_not_dh
  1251.     mov    bl,dh    ;get value for lookup
  1252.     mov    cl,12+16
  1253.     ror    edx,16    ;restore edx
  1254.     jmp    q_got_guess
  1255. q_not_dh:    or    dl,dl
  1256.     jz    q_not_dl
  1257.     mov    bl,dl    ;get value for lookup
  1258.     mov    cl,8+16
  1259.     ror    edx,16    ;restore edx
  1260.     jmp    q_got_guess
  1261. q_not_dl:    ror    edx,16    ;restore edx
  1262.     or    dh,dh
  1263.     jz    q_not_ah
  1264.     mov    bl,dh    ;get value for lookup
  1265.     mov    cl,4+16
  1266.     jmp    q_got_guess
  1267. q_not_ah:    mov    bl,dl    ;get value for lookup
  1268.     mov    cl,0+16
  1269. q_got_guess:
  1270.     movzx    ebx,guess_table[ebx] ;get byte guess
  1271.     sal    ebx,cl    ;get in right place
  1272.  
  1273. q_really_got_guess:
  1274.     mov    ecx,eax
  1275.     mov    esi,edx    ;save edx:eax
  1276.  
  1277. ;quad loop usually executes 4 times
  1278.  
  1279. ;q_newt_loop:
  1280.  rept 3
  1281.     mov    eax,ecx
  1282.     mov    edx,esi    ;restore dx:ax
  1283.     div    ebx    ;dx:ax / bx
  1284.     mov    edi,ebx    ;save for compare
  1285.     add    ebx,eax
  1286.     rcr    ebx,1    ;next guess = (d + q)/2
  1287.  endm
  1288.  
  1289. q_newt_loop:    mov    eax,ecx
  1290.     mov    edx,esi    ;restore dx:ax
  1291.     div    ebx    ;dx:ax / bx
  1292.     cmp    eax,ebx    ;correct?
  1293.     je    q_got_it    ;..yep
  1294.     mov    edi,ebx    ;save for compare
  1295.     add    ebx,eax
  1296.     rcr    ebx,1    ;next guess = (d + q)/2
  1297.     cmp    ebx,eax
  1298.     je    q_almost_got_it
  1299.     cmp    ebx,edi
  1300.     jne    q_newt_loop
  1301.  
  1302. q_almost_got_it:    mov    eax,ebx
  1303.     or    edx,edx    ;check remainder
  1304.     jz    q_got_it
  1305.     inc    eax
  1306. q_got_it:    popm    ebx,ecx,edx,esi,edi
  1307.     ret
  1308.  
  1309.  
  1310. ;fixed-point square root
  1311. fix_sqrt:    call    long_sqrt
  1312.     movzx    eax,ax
  1313.     sal    eax,8
  1314.     ret
  1315.  
  1316.  
  1317. _TEXT    ends
  1318.  
  1319.     end
  1320.