exp_amd64.s 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //go:build !tinygo && !noasm
  2. // Copyright 2014 Xuanyi Chew. All rights reserved.
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. //
  6. // The original code is lifted from the Go standard library which is governed by
  7. // a BSD-style licence which can be found here: https://golang.org/LICENSE
  8. #include "textflag.h"
  9. // The method is based on a paper by Naoki Shibata: "Efficient evaluation
  10. // methods of elementary functions suitable for SIMD computation", Proc.
  11. // of International Supercomputing Conference 2010 (ISC'10), pp. 25 -- 32
  12. // (May 2010). The paper is available at
  13. // http://www.springerlink.com/content/340228x165742104/
  14. //
  15. // The original code and the constants below are from the author's
  16. // implementation available at http://freshmeat.net/projects/sleef.
  17. // The README file says, "The software is in public domain.
  18. // You can use the software without any obligation."
  19. //
  20. // This code is a simplified version of the original.
  21. // The magic numbers for the float32 are lifted from the same project
  22. #define LN2 0.693147182464599609375 // log_e(2)
  23. #define LOG2E 1.44269502162933349609375 // 1/LN2
  24. #define LN2U 0.693145751953125 // upper half LN2
  25. #define LN2L 1.428606765330187045e-06 // lower half LN2
  26. #define T0 1.0
  27. #define T1 0.5
  28. #define T2 0.166665524244308471679688
  29. #define T3 0.0416710823774337768554688
  30. #define T4 0.00836596917361021041870117
  31. #define PosInf 0x7F800000
  32. #define NegInf 0xFF800000
  33. // func archExp(x float32) float32
  34. TEXT ·archExp(SB),NOSPLIT,$0
  35. // test bits for not-finite
  36. MOVL x+0(FP), BX
  37. MOVQ $~(1<<31), AX // sign bit mask
  38. MOVL BX, DX
  39. ANDL AX, DX
  40. MOVL $PosInf, AX
  41. CMPL AX, DX
  42. JLE notFinite
  43. MOVL BX, X0
  44. MOVSS $LOG2E, X1
  45. MULSS X0, X1
  46. CVTSS2SL X1, BX // BX = exponent
  47. CVTSL2SS BX, X1
  48. MOVSS $LN2U, X2
  49. MULSS X1, X2
  50. SUBSS X2, X0
  51. MOVSS $LN2L, X2
  52. MULSS X1, X2
  53. SUBSS X2, X0
  54. // reduce argument
  55. MULSS $0.0625, X0
  56. // Taylor series evaluation
  57. ADDSS $T4, X1
  58. MULSS X0, X1
  59. ADDSS $T3, X1
  60. MULSS X0, X1
  61. ADDSS $T2, X1
  62. MULSS X0, X1
  63. ADDSS $T1, X1
  64. MULSS X0, X1
  65. ADDSS $T0, X1
  66. MULSS X1, X0
  67. MOVSS $2.0, X1
  68. ADDSS X0, X1
  69. MULSS X1, X0
  70. MOVSS $2.0, X1
  71. ADDSS X0, X1
  72. MULSS X1, X0
  73. MOVSS $2.0, X1
  74. ADDSS X0, X1
  75. MULSS X1, X0
  76. MOVSS $2.0, X1
  77. ADDSS X0, X1
  78. MULSS X1, X0
  79. ADDSS $1.0, X0
  80. // return fr * 2**exponent
  81. MOVL $0x7F, AX // bias
  82. ADDL AX, BX
  83. JLE underflow
  84. CMPL BX, $0xFF
  85. JGE overflow
  86. MOVL $23, CX
  87. SHLQ CX, BX
  88. MOVL BX, X1
  89. MULSS X1, X0
  90. MOVSS X0, ret+8(FP)
  91. RET
  92. notFinite:
  93. // test bits for -Inf
  94. MOVL $NegInf, AX
  95. CMPQ AX, BX
  96. JNE notNegInf
  97. // -Inf, return 0
  98. underflow: // return 0
  99. MOVL $0, AX
  100. MOVL AX, ret+8(FP)
  101. RET
  102. overflow: // return +Inf
  103. MOVL $PosInf, BX
  104. notNegInf: // NaN or +Inf, return x
  105. MOVL BX, ret+8(FP)
  106. RET
  107. TEXT ·archExp2(SB),NOSPLIT,$0
  108. JMP ·exp2(SB)