home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 15 / MA_Cover_15.iso / source / winquake / amiga_ppc_mathlib.s < prev    next >
Encoding:
Text File  |  2000-02-19  |  3.5 KB  |  145 lines

  1. # Copyright (C) 2000 Peter McGavin.
  2. #
  3. # This program is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License
  5. # as published by the Free Software Foundation; either version 2
  6. # of the License, or (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  11. #
  12. # See the GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  
  18. ########################################################################
  19.         .section    ".text"
  20.  
  21.         .align    2
  22.         .globl    Length
  23.         .globl    _Length
  24.         .type    Length,@function
  25.         .type    _Length,@function
  26.  
  27. Length:
  28. _Length:
  29.         lfs    f0,4(r3)    # f0 = v[1]
  30.         fmuls    f0,f0,f0    # f0 = v[1]*v[1]
  31.         lfs    f13,0(r3)    # f13 = v[0]
  32.         lfs    f12,8(r3)    # f12 = v[2]
  33.         fmadds    f13,f13,f13,f0    # f13 = v[0]*v[0] + f0
  34.         fmadds    f1,f12,f12,f13    # f1 = v[2]*v[2] + f13
  35. #        b    sqrt
  36. .Length_end:
  37.         .size            Length,.Length_end-Length
  38.  
  39. # No blr !!   Drop thru to sqrt !!
  40.  
  41. ########################################################################
  42.         .section    ".rodata"
  43.         .align    2
  44. .consts:
  45.         .long    0x3f000000    # 0.5
  46.         .long    0x3f800000    # 1.0
  47.  
  48.         .section    ".text"
  49.  
  50.         .align    2
  51.         .globl    sqrt
  52.         .globl    _sqrt
  53.         .type    sqrt,@function
  54.         .type    _sqrt,@function
  55.  
  56. sqrt:
  57. _sqrt:
  58.  
  59. # Returns approx sqrt(f1) in f1.
  60. # Accurate to about 1 part in 1000.
  61. # Preserve the following registers: r3, r4, f10, f11, f12
  62. # Also return 0.0 in f2 and 1.0 in f3
  63.  
  64.         fsubs    f2,f1,f1    # f2 = 0.0
  65.         lis    r9,.consts@ha
  66.         fcmpu    cr0,f1,f2    # f1 <= 0.0?
  67.         la    r9,.consts@l(r9)
  68.         ble-    .end
  69.         frsqrte    f9,f1        # f9 ~ 1.0 / sqrt(f1)
  70.         lfs    f3,4(r9)    # f3 = 1.0
  71.         lfs    f7,0(r9)    # f7 = 0.5
  72.         fdivs    f8,f3,f9    # f8 = 1.0 / f9
  73.         fmadds    f9,f1,f9,f8    # f9 = f1 * f9 + f8
  74.         fmuls    f1,f7,f9    # f1 = 0.5 * f9
  75.  
  76. # To get accuracy to 1 part in 1000000, replace the previous fmuls with
  77. # the following:
  78. #        fmuls    f9,f7,f9    # f9 *= 0.5
  79. #        fmuls    f8,f7,f9    # f8 = 0.5 * f9
  80. #        fdivs    f9,f1,f9    # f9 = f1 / f9
  81. #        fmadds    f1,f7,f9,f8    # f1 = 0.5 * f9 + f8
  82. .end:
  83.         blr
  84. .sqrt_end:
  85.         .size            sqrt,.sqrt_end-sqrt
  86.  
  87. ########################################################################
  88.         .section    ".rodata"
  89.         .align    2
  90. .one:
  91.         .long    0x3f800000    # 1.0
  92.  
  93.         .section    ".text"
  94.  
  95.         .align    2
  96.         .globl    VectorNormalize
  97.         .globl    _VectorNormalize
  98.         .type    VectorNormalize,@function
  99.         .type    _VectorNormalize,@function
  100.  
  101. VectorNormalize:
  102. _VectorNormalize:
  103.  
  104. # float VectorNormalize (vec3_t v)
  105. # {
  106. #   float length, ilength;
  107. #
  108. #   length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
  109. #   length = sqrt (length);
  110. #   if (length) {
  111. #     ilength = 1/length;
  112. #     v[0] *= ilength;
  113. #     v[1] *= ilength;
  114. #     v[2] *= ilength;
  115. #   }
  116. #   return length;
  117. # }
  118.  
  119.         mflr    r4
  120.  
  121.         lfs    f10,0(r3)    # f10 = v[0]
  122.         lfs    f11,4(r3)    # f11 = v[1]
  123.         lfs    f12,8(r3)    # f12 = v[2]
  124.         fmuls    f1,f10,f10    # f1 = v[0]*v[0]
  125.         fmadds    f1,f11,f11,f1    # f1 = v[1]*v[1] + f1
  126.         fmadds    f1,f12,f12,f1    # f1 = v[2]*v[2] + f1
  127.  
  128.         bl    sqrt        # f1 = sqrt(f1), f2 = 0.0, f3 = 1.0
  129.  
  130.         fcmpu    cr0,f1,f2
  131.         mtlr    r4
  132.         ble-    .skip
  133.  
  134.         fdivs    f0,f3,f1    # ilength = 1.0 / length
  135.         fmuls    f10,f10,f0    # f10 *= ilength
  136.         fmuls    f11,f11,f0    # f11 *= ilength
  137.         fmuls    f12,f12,f0    # f12 *= ilength
  138.         stfs    f10,0(r3)    # v[0] = f10
  139.         stfs    f11,4(r3)    # v[1] = f11
  140.         stfs    f12,8(r3)    # v[2] = f12
  141. .skip:
  142.         blr
  143. .VectorNormalize_end:
  144.         .size            VectorNormalize,.VectorNormalize_end-VectorNormalize
  145.