Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

545 lines
11 KiB

  1. .file "fmodf.s"
  2. // Copyright (c) 2000, Intel Corporation
  3. // All rights reserved.
  4. //
  5. // Contributed 2/2/2000 by John Harrison, Cristina Iordache, Ted Kubaska,
  6. // Bob Norin, Shane Story, and Ping Tak Peter Tang of the Computational
  7. // Software Lab, Intel Corporation.
  8. //
  9. // WARRANTY DISCLAIMER
  10. //
  11. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  12. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  13. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  14. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS
  15. // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  16. // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  17. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  18. // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  19. // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING
  20. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. //
  23. // Intel Corporation is the author of this code, and requests that all
  24. // problem reports or change requests be submitted to it directly at
  25. // http://developer.intel.com/opensource.
  26. //
  27. // History
  28. //====================================================================
  29. // 2/02/00 Initial version
  30. // 3/02/00 New Algorithm
  31. // 4/04/00 Unwind support added
  32. // 8/15/00 Bundle added after call to __libm_error_support to properly
  33. // set [the previously overwritten] GR_Parameter_RESULT.
  34. //
  35. // API
  36. //====================================================================
  37. // float fmodf(float,float);
  38. //
  39. // Overview of operation
  40. //====================================================================
  41. // fmod(a,b)=a-i*b,
  42. // where i is an integer such that, if b!=0,
  43. // |i|<|a/b| and |a/b-i|<1
  44. // Algorithm
  45. //====================================================================
  46. // a). if |a|<|b|, return a
  47. // b). get quotient and reciprocal overestimates accurate to
  48. // 33 bits (q2,y2)
  49. // c). if the exponent difference (exponent(a)-exponent(b))
  50. // is less than 32, truncate quotient to integer and
  51. // finish in one iteration
  52. // d). if exponent(a)-exponent(b)>=32 (q2>=2^32)
  53. // round quotient estimate to single precision (k=RN(q2)),
  54. // calculate partial remainder (a'=a-k*b),
  55. // get quotient estimate (a'*y2), and repeat from c).
  56. // Special cases
  57. //====================================================================
  58. // b=+/-0: return NaN, call libm_error_support
  59. // a=+/-Inf, a=NaN or b=NaN: return NaN
  60. // Registers used
  61. //====================================================================
  62. // Predicate registers: p6-p11
  63. // General registers: r2,r29,r32 (ar.pfs), r33-r39
  64. // Floating point registers: f6-f15
  65. .section .text
  66. GR_SAVE_B0 = r33
  67. GR_SAVE_PFS = r34
  68. GR_SAVE_GP = r35
  69. GR_SAVE_SP = r36
  70. GR_Parameter_X = r37
  71. GR_Parameter_Y = r38
  72. GR_Parameter_RESULT = r39
  73. GR_Parameter_TAG = r40
  74. FR_X = f10
  75. FR_Y = f9
  76. FR_RESULT = f8
  77. .proc fmodf#
  78. .align 32
  79. .global fmodf#
  80. .align 32
  81. fmodf:
  82. // inputs in f8, f9
  83. // result in f8
  84. { .mfi
  85. alloc r32=ar.pfs,1,4,4,0
  86. // f6=|a|
  87. fmerge.s f6=f0,f8
  88. mov r2 = 0x0ffdd
  89. }
  90. {.mfi
  91. nop.m 0
  92. // f7=|b|
  93. fmerge.s f7=f0,f9
  94. nop.i 0;;
  95. }
  96. { .mfi
  97. setf.exp f11 = r2
  98. // (1) y0
  99. frcpa.s1 f10,p6=f6,f7
  100. nop.i 0
  101. }
  102. // eliminate special cases
  103. // Y +-NAN, +-inf, +-0? p7
  104. { .mfi
  105. nop.m 999
  106. (p0) fclass.m.unc p7,p0 = f9, 0xe7
  107. nop.i 999;;
  108. }
  109. // qnan snan inf norm unorm 0 -+
  110. // 1 1 1 0 0 0 11
  111. // e 3
  112. // X +-NAN, +-inf, ? p9
  113. { .mfi
  114. nop.m 999
  115. (p0) fclass.m.unc p9,p0 = f8, 0xe3
  116. nop.i 999
  117. }
  118. // |x| < |y|? Return x p8
  119. { .mfi
  120. nop.m 999
  121. (p0) fcmp.lt.unc.s1 p8,p0 = f6,f7
  122. nop.i 999 ;;
  123. }
  124. { .mfi
  125. nop.m 0
  126. // normalize y (if |x|<|y|)
  127. (p8) fma.s0 f9=f9,f1,f0
  128. nop.i 0;;
  129. }
  130. { .mfi
  131. mov r2=0x1001f
  132. // (2) q0=a*y0
  133. (p6) fma.s1 f13=f6,f10,f0
  134. nop.i 0
  135. }
  136. { .mfi
  137. nop.m 0
  138. // (3) e0 = 1 - b * y0
  139. (p6) fnma.s1 f12=f7,f10,f1
  140. nop.i 0;;
  141. }
  142. {.mfi
  143. nop.m 0
  144. // normalize x (if |x|<|y|)
  145. (p8) fma.s.s0 f8=f8,f1,f0
  146. nop.i 0
  147. }
  148. {.bbb
  149. (p9) br.cond.spnt FMOD_X_NAN_INF
  150. (p7) br.cond.spnt FMOD_Y_NAN_INF_ZERO
  151. // if |x|<|y|, return
  152. (p8) br.ret.spnt b0;;
  153. }
  154. {.mfi
  155. nop.m 0
  156. // normalize x
  157. fma.s0 f6=f6,f1,f0
  158. nop.i 0
  159. }
  160. {.mfi
  161. nop.m 0
  162. // normalize y
  163. fma.s0 f7=f7,f1,f0
  164. nop.i 0;;
  165. }
  166. {.mfi
  167. // f15=2^32
  168. setf.exp f15=r2
  169. // (4) q1=q0+e0*q0
  170. (p6) fma.s1 f13=f12,f13,f13
  171. nop.i 0
  172. }
  173. { .mfi
  174. nop.m 0
  175. // (5) e1 = e0 * e0 + 2^-34
  176. (p6) fma.s1 f14=f12,f12,f11
  177. nop.i 0;;
  178. }
  179. {.mlx
  180. nop.m 0
  181. movl r2=0x33a00000;;
  182. }
  183. { .mfi
  184. nop.m 0
  185. // (6) y1 = y0 + e0 * y0
  186. (p6) fma.s1 f10=f12,f10,f10
  187. nop.i 0;;
  188. }
  189. {.mfi
  190. // set f12=1.25*2^{-24}
  191. setf.s f12=r2
  192. // (7) q2=q1+e1*q1
  193. (p6) fma.s1 f13=f13,f14,f13
  194. nop.i 0;;
  195. }
  196. {.mfi
  197. nop.m 0
  198. fmerge.s f9=f8,f9
  199. nop.i 0
  200. }
  201. { .mfi
  202. nop.m 0
  203. // (8) y2 = y1 + e1 * y1
  204. (p6) fma.s1 f10=f14,f10,f10
  205. // set p6=0, p10=0
  206. cmp.ne.and p6,p10=r0,r0;;
  207. }
  208. .align 32
  209. loop24:
  210. {.mfi
  211. nop.m 0
  212. // compare q2, 2^32
  213. fcmp.lt.unc.s1 p8,p7=f13,f15
  214. nop.i 0
  215. }
  216. {.mfi
  217. nop.m 0
  218. // will truncate quotient to integer, if exponent<32 (in advance)
  219. fcvt.fx.trunc.s1 f11=f13
  220. nop.i 0;;
  221. }
  222. {.mfi
  223. nop.m 0
  224. // if exponent>32, round quotient to single precision (perform in advance)
  225. fma.s.s1 f13=f13,f1,f0
  226. nop.i 0;;
  227. }
  228. {.mfi
  229. nop.m 0
  230. // set f12=sgn(a)
  231. (p8) fmerge.s f12=f8,f1
  232. nop.i 0
  233. }
  234. {.mfi
  235. nop.m 0
  236. // normalize truncated quotient
  237. (p8) fcvt.xf f13=f11
  238. nop.i 0;;
  239. }
  240. { .mfi
  241. nop.m 0
  242. // calculate remainder (assuming f13=RZ(Q))
  243. (p7) fnma.s1 f14=f13,f7,f6
  244. nop.i 0
  245. }
  246. {.mfi
  247. nop.m 0
  248. // also if exponent>32, round quotient to single precision
  249. // and subtract 1 ulp: q=q-q*(1.25*2^{-24})
  250. (p7) fnma.s.s1 f11=f13,f12,f13
  251. nop.i 0;;
  252. }
  253. {.mfi
  254. nop.m 0
  255. // (p8) calculate remainder (82-bit format)
  256. (p8) fnma.s1 f11=f13,f7,f6
  257. nop.i 0
  258. }
  259. {.mfi
  260. nop.m 0
  261. // (p7) calculate remainder (assuming f11=RZ(Q))
  262. (p7) fnma.s1 f6=f11,f7,f6
  263. nop.i 0;;
  264. }
  265. {.mfi
  266. nop.m 0
  267. // Final iteration (p8): is f6 the correct remainder (quotient was not overestimated) ?
  268. (p8) fcmp.lt.unc.s1 p6,p10=f11,f0
  269. nop.i 0;;
  270. }
  271. {.mfi
  272. nop.m 0
  273. // get new quotient estimation: a'*y2
  274. (p7) fma.s1 f13=f14,f10,f0
  275. nop.i 0
  276. }
  277. {.mfb
  278. nop.m 0
  279. // was f14=RZ(Q) ? (then new remainder f14>=0)
  280. (p7) fcmp.lt.unc.s1 p7,p9=f14,f0
  281. nop.b 0;;
  282. }
  283. .pred.rel "mutex",p6,p10
  284. {.mfb
  285. nop.m 0
  286. // add b to estimated remainder (to cover the case when the quotient was overestimated)
  287. // also set correct sign by using f9=|b|*sgn(a), f12=sgn(a)
  288. (p6) fma.s.s0 f8=f11,f12,f9
  289. nop.b 0
  290. }
  291. {.mfb
  292. nop.m 0
  293. // calculate remainder (single precision)
  294. // set correct sign of result before returning
  295. (p10) fma.s.s0 f8=f11,f12,f0
  296. (p8) br.ret.sptk b0;;
  297. }
  298. {.mfi
  299. nop.m 0
  300. // if f13!=RZ(Q), get alternative quotient estimation: a''*y2
  301. (p7) fma.s1 f13=f6,f10,f0
  302. nop.i 0
  303. }
  304. {.mfb
  305. nop.m 0
  306. // if f14 was RZ(Q), set remainder to f14
  307. (p9) mov f6=f14
  308. br.cond.sptk loop24;;
  309. }
  310. { .mmb
  311. nop.m 0
  312. nop.m 0
  313. br.ret.sptk b0;;
  314. }
  315. FMOD_X_NAN_INF:
  316. // Y zero ?
  317. {.mfi
  318. nop.m 0
  319. fma.s1 f10=f9,f1,f0
  320. nop.i 0;;
  321. }
  322. {.mfi
  323. nop.m 0
  324. fcmp.eq.unc.s1 p11,p0=f10,f0
  325. nop.i 0;;
  326. }
  327. {.mib
  328. nop.m 0
  329. nop.i 0
  330. // if Y zero
  331. (p11) br.cond.spnt FMOD_Y_ZERO;;
  332. }
  333. // X infinity? Return QNAN indefinite
  334. { .mfi
  335. nop.m 999
  336. (p0) fclass.m.unc p8,p9 = f8, 0x23
  337. nop.i 999;;
  338. }
  339. // Y NaN ?
  340. {.mfi
  341. nop.m 999
  342. (p8) fclass.m p9,p8=f9,0xc3
  343. nop.i 0;;
  344. }
  345. {.mfi
  346. nop.m 999
  347. (p8) frcpa.s0 f8,p0 = f8,f8
  348. nop.i 0
  349. }
  350. { .mfi
  351. nop.m 999
  352. // also set Denormal flag if necessary
  353. (p8) fma.s0 f9=f9,f1,f0
  354. nop.i 999 ;;
  355. }
  356. { .mfb
  357. nop.m 999
  358. (p8) fma.s f8=f8,f1,f0
  359. nop.b 999 ;;
  360. }
  361. { .mfb
  362. nop.m 999
  363. (p9) frcpa.s0 f8,p7=f8,f9
  364. br.ret.sptk b0 ;;
  365. }
  366. FMOD_Y_NAN_INF_ZERO:
  367. // Y INF
  368. { .mfi
  369. nop.m 999
  370. (p0) fclass.m.unc p7,p0 = f9, 0x23
  371. nop.i 999 ;;
  372. }
  373. { .mfb
  374. nop.m 999
  375. (p7) fma.s f8=f8,f1,f0
  376. (p7) br.ret.spnt b0 ;;
  377. }
  378. // Y NAN?
  379. { .mfi
  380. nop.m 999
  381. (p0) fclass.m.unc p9,p0 = f9, 0xc3
  382. nop.i 999 ;;
  383. }
  384. { .mfb
  385. nop.m 999
  386. (p9) fma.s f8=f9,f1,f0
  387. (p9) br.ret.spnt b0 ;;
  388. }
  389. FMOD_Y_ZERO:
  390. // Y zero? Must be zero at this point
  391. // because it is the only choice left.
  392. // Return QNAN indefinite
  393. {.mfi
  394. nop.m 0
  395. // set Invalid
  396. frcpa f12,p0=f0,f0
  397. nop.i 999
  398. }
  399. // X NAN?
  400. { .mfi
  401. nop.m 999
  402. (p0) fclass.m.unc p9,p10 = f8, 0xc3
  403. nop.i 999 ;;
  404. }
  405. { .mfi
  406. nop.m 999
  407. (p10) fclass.nm p9,p10 = f8, 0xff
  408. nop.i 999 ;;
  409. }
  410. {.mfi
  411. nop.m 999
  412. (p9) frcpa f11,p7=f8,f0
  413. nop.i 0;;
  414. }
  415. { .mfi
  416. nop.m 999
  417. (p10) frcpa f11,p7 = f0,f0
  418. nop.i 999;;
  419. }
  420. { .mfi
  421. nop.m 999
  422. (p0) fmerge.s f10 = f8, f8
  423. nop.i 999
  424. }
  425. { .mfi
  426. nop.m 999
  427. (p0) fma.s f8=f11,f1,f0
  428. nop.i 999;;
  429. }
  430. EXP_ERROR_RETURN:
  431. { .mib
  432. nop.m 0
  433. (p0) mov GR_Parameter_TAG=122
  434. (p0) br.sptk __libm_error_region;;
  435. }
  436. .endp fmodf
  437. .proc __libm_error_region
  438. __libm_error_region:
  439. .prologue
  440. { .mfi
  441. add GR_Parameter_Y=-32,sp // Parameter 2 value
  442. nop.f 0
  443. .save ar.pfs,GR_SAVE_PFS
  444. mov GR_SAVE_PFS=ar.pfs // Save ar.pfs
  445. }
  446. { .mfi
  447. .fframe 64
  448. add sp=-64,sp // Create new stack
  449. nop.f 0
  450. mov GR_SAVE_GP=gp // Save gp
  451. };;
  452. { .mmi
  453. stfs [GR_Parameter_Y] = FR_Y,16 // Save Parameter 2 on stack
  454. add GR_Parameter_X = 16,sp // Parameter 1 address
  455. .save b0, GR_SAVE_B0
  456. mov GR_SAVE_B0=b0 // Save b0
  457. };;
  458. .body
  459. { .mib
  460. stfs [GR_Parameter_X] = FR_X // Store Parameter 1 on stack
  461. add GR_Parameter_RESULT = 0,GR_Parameter_Y
  462. nop.b 0 // Parameter 3 address
  463. }
  464. { .mib
  465. stfs [GR_Parameter_Y] = FR_RESULT // Store Parameter 3 on stack
  466. add GR_Parameter_Y = -16,GR_Parameter_Y
  467. br.call.sptk b0=__libm_error_support#;; // Call error handling function
  468. }
  469. { .mmi
  470. nop.m 0
  471. nop.m 0
  472. add GR_Parameter_RESULT = 48,sp
  473. };;
  474. { .mmi
  475. ldfs f8 = [GR_Parameter_RESULT] // Get return result off stack
  476. .restore
  477. add sp = 64,sp // Restore stack pointer
  478. mov b0 = GR_SAVE_B0 // Restore return address
  479. };;
  480. { .mib
  481. mov gp = GR_SAVE_GP // Restore gp
  482. mov ar.pfs = GR_SAVE_PFS // Restore ar.pfs
  483. br.ret.sptk b0 // Return
  484. };;
  485. .endp __libm_error_region
  486. .type __libm_error_support#,@function
  487. .global __libm_error_support#