Leaked source code of windows server 2003
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.

533 lines
11 KiB

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