comparesf2.S 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //===-- comparesf2.S - Implement single-precision soft-float comparisons --===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the following soft-fp_t comparison routines:
  10. //
  11. // __eqsf2 __gesf2 __unordsf2
  12. // __lesf2 __gtsf2
  13. // __ltsf2
  14. // __nesf2
  15. //
  16. // The semantics of the routines grouped in each column are identical, so there
  17. // is a single implementation for each, with multiple names.
  18. //
  19. // The routines behave as follows:
  20. //
  21. // __lesf2(a,b) returns -1 if a < b
  22. // 0 if a == b
  23. // 1 if a > b
  24. // 1 if either a or b is NaN
  25. //
  26. // __gesf2(a,b) returns -1 if a < b
  27. // 0 if a == b
  28. // 1 if a > b
  29. // -1 if either a or b is NaN
  30. //
  31. // __unordsf2(a,b) returns 0 if both a and b are numbers
  32. // 1 if either a or b is NaN
  33. //
  34. // Note that __lesf2( ) and __gesf2( ) are identical except in their handling of
  35. // NaN values.
  36. //
  37. //===----------------------------------------------------------------------===//
  38. #include "../assembly.h"
  39. .syntax unified
  40. .text
  41. DEFINE_CODE_STATE
  42. .macro COMPARESF2_FUNCTION_BODY handle_nan:req
  43. #if defined(COMPILER_RT_ARMHF_TARGET)
  44. vmov r0, s0
  45. vmov r1, s1
  46. #endif
  47. // Make copies of a and b with the sign bit shifted off the top. These will
  48. // be used to detect zeros and NaNs.
  49. #if defined(USE_THUMB_1)
  50. push {r6, lr}
  51. lsls r2, r0, #1
  52. lsls r3, r1, #1
  53. #else
  54. mov r2, r0, lsl #1
  55. mov r3, r1, lsl #1
  56. #endif
  57. // We do the comparison in three stages (ignoring NaN values for the time
  58. // being). First, we orr the absolute values of a and b; this sets the Z
  59. // flag if both a and b are zero (of either sign). The shift of r3 doesn't
  60. // effect this at all, but it *does* make sure that the C flag is clear for
  61. // the subsequent operations.
  62. #if defined(USE_THUMB_1)
  63. lsrs r6, r3, #1
  64. orrs r6, r2
  65. #else
  66. orrs r12, r2, r3, lsr #1
  67. #endif
  68. // Next, we check if a and b have the same or different signs. If they have
  69. // opposite signs, this eor will set the N flag.
  70. #if defined(USE_THUMB_1)
  71. beq 1f
  72. movs r6, r0
  73. eors r6, r1
  74. 1:
  75. #else
  76. it ne
  77. eorsne r12, r0, r1
  78. #endif
  79. // If a and b are equal (either both zeros or bit identical; again, we're
  80. // ignoring NaNs for now), this subtract will zero out r0. If they have the
  81. // same sign, the flags are updated as they would be for a comparison of the
  82. // absolute values of a and b.
  83. #if defined(USE_THUMB_1)
  84. bmi 1f
  85. subs r0, r2, r3
  86. 1:
  87. #else
  88. it pl
  89. subspl r0, r2, r3
  90. #endif
  91. // If a is smaller in magnitude than b and both have the same sign, place
  92. // the negation of the sign of b in r0. Thus, if both are negative and
  93. // a > b, this sets r0 to 0; if both are positive and a < b, this sets
  94. // r0 to -1.
  95. //
  96. // This is also done if a and b have opposite signs and are not both zero,
  97. // because in that case the subtract was not performed and the C flag is
  98. // still clear from the shift argument in orrs; if a is positive and b
  99. // negative, this places 0 in r0; if a is negative and b positive, -1 is
  100. // placed in r0.
  101. #if defined(USE_THUMB_1)
  102. bhs 1f
  103. // Here if a and b have the same sign and absA < absB, the result is thus
  104. // b < 0 ? 1 : -1. Same if a and b have the opposite sign (ignoring Nan).
  105. movs r0, #1
  106. lsrs r1, #31
  107. bne LOCAL_LABEL(CHECK_NAN\@)
  108. negs r0, r0
  109. b LOCAL_LABEL(CHECK_NAN\@)
  110. 1:
  111. #else
  112. it lo
  113. mvnlo r0, r1, asr #31
  114. #endif
  115. // If a is greater in magnitude than b and both have the same sign, place
  116. // the sign of b in r0. Thus, if both are negative and a < b, -1 is placed
  117. // in r0, which is the desired result. Conversely, if both are positive
  118. // and a > b, zero is placed in r0.
  119. #if defined(USE_THUMB_1)
  120. bls 1f
  121. // Here both have the same sign and absA > absB.
  122. movs r0, #1
  123. lsrs r1, #31
  124. beq LOCAL_LABEL(CHECK_NAN\@)
  125. negs r0, r0
  126. 1:
  127. #else
  128. it hi
  129. movhi r0, r1, asr #31
  130. #endif
  131. // If you've been keeping track, at this point r0 contains -1 if a < b and
  132. // 0 if a >= b. All that remains to be done is to set it to 1 if a > b.
  133. // If a == b, then the Z flag is set, so we can get the correct final value
  134. // into r0 by simply or'ing with 1 if Z is clear.
  135. // For Thumb-1, r0 contains -1 if a < b, 0 if a > b and 0 if a == b.
  136. #if !defined(USE_THUMB_1)
  137. it ne
  138. orrne r0, r0, #1
  139. #endif
  140. // Finally, we need to deal with NaNs. If either argument is NaN, replace
  141. // the value in r0 with 1.
  142. #if defined(USE_THUMB_1)
  143. LOCAL_LABEL(CHECK_NAN\@):
  144. movs r6, #0xff
  145. lsls r6, #24
  146. cmp r2, r6
  147. bhi 1f
  148. cmp r3, r6
  149. 1:
  150. bls 2f
  151. \handle_nan
  152. 2:
  153. pop {r6, pc}
  154. #else
  155. cmp r2, #0xff000000
  156. ite ls
  157. cmpls r3, #0xff000000
  158. \handle_nan
  159. JMP(lr)
  160. #endif
  161. .endm
  162. @ int __eqsf2(float a, float b)
  163. .p2align 2
  164. DEFINE_COMPILERRT_FUNCTION(__eqsf2)
  165. .macro __eqsf2_handle_nan
  166. #if defined(USE_THUMB_1)
  167. movs r0, #1
  168. #else
  169. movhi r0, #1
  170. #endif
  171. .endm
  172. COMPARESF2_FUNCTION_BODY __eqsf2_handle_nan
  173. END_COMPILERRT_FUNCTION(__eqsf2)
  174. DEFINE_COMPILERRT_FUNCTION_ALIAS(__lesf2, __eqsf2)
  175. DEFINE_COMPILERRT_FUNCTION_ALIAS(__ltsf2, __eqsf2)
  176. DEFINE_COMPILERRT_FUNCTION_ALIAS(__nesf2, __eqsf2)
  177. #if defined(__ELF__)
  178. // Alias for libgcc compatibility
  179. DEFINE_COMPILERRT_FUNCTION_ALIAS(__cmpsf2, __lesf2)
  180. #endif
  181. @ int __gtsf2(float a, float b)
  182. .p2align 2
  183. DEFINE_COMPILERRT_FUNCTION(__gtsf2)
  184. .macro __gtsf2_handle_nan
  185. #if defined(USE_THUMB_1)
  186. movs r0, #1
  187. negs r0, r0
  188. #else
  189. movhi r0, #-1
  190. #endif
  191. .endm
  192. COMPARESF2_FUNCTION_BODY __gtsf2_handle_nan
  193. END_COMPILERRT_FUNCTION(__gtsf2)
  194. DEFINE_COMPILERRT_FUNCTION_ALIAS(__gesf2, __gtsf2)
  195. @ int __unordsf2(float a, float b)
  196. .p2align 2
  197. DEFINE_COMPILERRT_FUNCTION(__unordsf2)
  198. #if defined(COMPILER_RT_ARMHF_TARGET)
  199. vmov r0, s0
  200. vmov r1, s1
  201. #endif
  202. // Return 1 for NaN values, 0 otherwise.
  203. lsls r2, r0, #1
  204. lsls r3, r1, #1
  205. movs r0, #0
  206. #if defined(USE_THUMB_1)
  207. movs r1, #0xff
  208. lsls r1, #24
  209. cmp r2, r1
  210. bhi 1f
  211. cmp r3, r1
  212. 1:
  213. bls 2f
  214. movs r0, #1
  215. 2:
  216. #else
  217. cmp r2, #0xff000000
  218. ite ls
  219. cmpls r3, #0xff000000
  220. movhi r0, #1
  221. #endif
  222. JMP(lr)
  223. END_COMPILERRT_FUNCTION(__unordsf2)
  224. #if defined(COMPILER_RT_ARMHF_TARGET)
  225. DEFINE_COMPILERRT_FUNCTION(__aeabi_fcmpun)
  226. vmov s0, r0
  227. vmov s1, r1
  228. b SYMBOL_NAME(__unordsf2)
  229. END_COMPILERRT_FUNCTION(__aeabi_fcmpun)
  230. #else
  231. DEFINE_AEABI_FUNCTION_ALIAS(__aeabi_fcmpun, __unordsf2)
  232. #endif
  233. NO_EXEC_STACK_DIRECTIVE