lowmath.inc 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Carl-Eric Codere,
  4. member of the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {*************************************************************************}
  12. { lowmath.inc }
  13. { Ported to FPC-Pascal by Carl Eric Codere }
  14. { Terms of use: This source code is freeware. }
  15. {*************************************************************************}
  16. { This inc. implements low-level mathemtical routines for the motorola }
  17. { 68000 family of processors. }
  18. {*************************************************************************}
  19. { single floating point routines taken from GCC 2.5.2 Atari compiler }
  20. { library source. }
  21. { Original credits: }
  22. { written by Kai-Uwe Bloem ([email protected]). }
  23. { Based on a 80x86 floating point packet from comp.os.minix, }
  24. { written by P.Housel }
  25. { Patched by Olaf Flebbe ([email protected]) }
  26. { Revision by michal 05-93 ([email protected]) }
  27. {*************************************************************************}
  28. {--------------------------------------------------------------------}
  29. { LEFT TO DO: }
  30. { o Add support for FPU if present. }
  31. { o Verify if single comparison works in all cases. }
  32. { o Add support for NANs in SINGLE_CMP }
  33. { o Add comp (80-bit) multiplication,addition,substract,division, }
  34. { shift. }
  35. { o Add stack checking for the routines which use the stack. }
  36. { (This will probably have to be done in the code generator). }
  37. {--------------------------------------------------------------------}
  38. Procedure Single_Norm;[alias : 'FPC_SINGLE_NORM'];Assembler;
  39. {--------------------------------------------}
  40. { Low-level routine to normalize single e }
  41. { IEEE floating point values. Never called }
  42. { directly. }
  43. { On Exit: }
  44. { d0 = result. }
  45. { Registers destroyed: d0,d1 }
  46. {--------------------------------------------}
  47. Asm
  48. tst.l d4 { rounding and u.mant == 0 ? }
  49. bne @normlab1
  50. tst.b d1
  51. beq @retzok
  52. @normlab1:
  53. clr.b d2 { "sticky byte" }
  54. @normlab3:
  55. move.l #$ff000000,d5
  56. @normlab4:
  57. tst.w d0 { divide (shift) }
  58. ble @normlab2 { denormalized number }
  59. move.l d4,d3
  60. and.l d5,d3 { or until no bits above 23 }
  61. beq @normlab5
  62. @normlab2:
  63. addq.w #1,d0 { increment exponent }
  64. lsr.l #1,d4
  65. or.b d1,d2 { set "sticky" }
  66. roxr.b #1,d1 { shift into rounding bits }
  67. bra @normlab4
  68. @normlab5:
  69. and.b #1,d2
  70. or.b d2,d1 { make least sig bit "sticky" }
  71. asr.l #1,d5 { #0xff800000 -> d5 }
  72. @normlab6:
  73. move.l d4,d3 { multiply (shift) until }
  74. and.l d5,d3 { one in "implied" position }
  75. bne @normlab7
  76. subq.w #1,d0 { decrement exponent }
  77. beq @normlab7 { too small. store as denormalized number }
  78. add.b d1,d1 { some doubt about this one * }
  79. addx.l d4,d4
  80. bra @normlab6
  81. @normlab7:
  82. tst.b d1 { check rounding bits }
  83. bge @normlab9 { round down - no action neccessary }
  84. neg.b d1
  85. bvc @normlab8 { round up }
  86. move.w d4,d1 { tie case - round to even }
  87. { dont need rounding bits any more }
  88. and.w #1,d1 { check if even }
  89. beq @normlab9 { mantissa is even - no action necessary }
  90. { fall through }
  91. @normlab8:
  92. clr.w d1 { zero rounding bits }
  93. add.l #1,d4
  94. tst.w d0
  95. bne @normlab10 { renormalize if number was denormalized }
  96. add.w #1,d0 { correct exponent for denormalized numbers }
  97. bra @normlab3
  98. @normlab10:
  99. move.l d4,d3 { check for rounding overflow }
  100. asl.l #1,d5 { #0xff000000 -> d5 }
  101. and.l d5,d3
  102. bne @normlab4 { go back and renormalize }
  103. @normlab9:
  104. tst.l d4 { check if normalization caused an underflow }
  105. beq @retz
  106. tst.w d0 { check for exponent overflow or underflow }
  107. blt @retz
  108. cmp.w #255,d0
  109. bge @oflow
  110. lsl.w #8,d0 { re-position exponent - one bit too high }
  111. lsl.w #1,d2 { get X bit }
  112. roxr.w #1,d0 { shift it into sign position }
  113. swap d0 { map to upper word }
  114. clr.w d0
  115. and.l #$7fffff,d4 { top mantissa bits }
  116. or.l d4,d0 { insert exponent and sign }
  117. movem.l (sp)+,d2-d5
  118. rts
  119. @retz:
  120. { handling underflow should be done here... }
  121. { by default simply return 0 as retzok... }
  122. @retzok:
  123. moveq.l #0,d0
  124. lsl.w #1,d2
  125. roxr.l #1,d0 { sign of 0 is the same as of d2 }
  126. movem.l (sp)+,d2-d5
  127. rts
  128. @oflow:
  129. move.l #$7f800000,d0 { +infinity as proposed by IEEE }
  130. tst.w d2 { transfer sign }
  131. bge @ofl_clear { (mjr++) }
  132. bset #31,d0 { }
  133. @ofl_clear:
  134. or.b #2,ccr { set overflow flag. }
  135. movem.l (sp)+,d2-d5
  136. rts
  137. end;
  138. Procedure Single_AddSub; Assembler;
  139. {--------------------------------------------}
  140. { Low-level routine to add/subtract single }
  141. { IEEE floating point values. Never called }
  142. { directly. }
  143. { On Exit: }
  144. { d0 = result -- from normalize routine }
  145. { Flags : V set if overflow. }
  146. { on underflow d0 = 0 }
  147. { Registers destroyed: d0,d1 }
  148. {--------------------------------------------}
  149. Asm
  150. {--------------------------------------------}
  151. { On Entry: }
  152. { d1-d0 = single values to subtract. }
  153. {--------------------------------------------}
  154. XDEF SINGLE_SUB
  155. eor.l #$80000000,d0 { reverse sign of v }
  156. {--------------------------------------------}
  157. { On Entry: }
  158. { d0, d1 = single values to add. }
  159. {--------------------------------------------}
  160. XDEF SINGLE_ADD
  161. movem.l d2-d5,-(sp) { save registers }
  162. move.l d0,d4 { d4 = d0 = v }
  163. move.l d1,d5 { d5 = d1 = u }
  164. move.l #$7fffff,d3
  165. move.l d5,d0 { d0 = u.exp }
  166. move.l d5,d2 { d2.h = u.sign }
  167. swap d0
  168. move.w d0,d2 { d2 = u.sign }
  169. and.l d3,d5 { remove exponent from u.mantissa }
  170. move.l d4,d1 { d1 = v.exp }
  171. and.l d3,d4 { remove exponent from v.mantissa }
  172. swap d1
  173. eor.w d1,d2 { d2 = u.sign ^ v.sign (in bit 15)}
  174. clr.b d2 { we will use the lowest byte as a flag }
  175. moveq.l #15,d3
  176. bclr d3,d1 { kill sign bit u.exp }
  177. bclr d3,d0 { kill sign bit u.exp }
  178. btst d3,d2 { same sign for u and v? }
  179. beq @slabel1
  180. cmp.l d0,d1 { different signs - maybe x - x ? }
  181. seq d2 { set 'cancellation' flag }
  182. @slabel1:
  183. lsr.w #7,d0 { keep here exponents only }
  184. lsr.w #7,d1
  185. {--------------------------------------------------------------------}
  186. { Now perform testing of NaN and infinities }
  187. {--------------------------------------------------------------------}
  188. moveq.l #-1,d3
  189. cmp.b d3,d0
  190. beq @alabel1
  191. cmp.b d3,d1
  192. bne @nospec
  193. bra @alabel2
  194. {--------------------------------------------------------------------}
  195. { u is special. }
  196. {--------------------------------------------------------------------}
  197. @alabel1:
  198. tst.b d2
  199. bne @retnan { cancellation of specials -> NaN }
  200. tst.l d5
  201. bne @retnan { arith with Nan gives always NaN }
  202. addq.w #4,a0 { here is an infinity }
  203. cmp.b d3,d1
  204. bne @alabel3 { skip check for NaN if v not special }
  205. {--------------------------------------------------------------------}
  206. { v is special. }
  207. {--------------------------------------------------------------------}
  208. @alabel2:
  209. tst.l d4
  210. bne @retnan
  211. @alabel3:
  212. move.l (a0),d0
  213. bra @return
  214. {--------------------------------------------------------------------}
  215. { Return a quiet nan }
  216. {--------------------------------------------------------------------}
  217. @retnan:
  218. moveq.l #-1,d0
  219. lsr.l #1,d0 { 0x7fffffff -> d0 }
  220. bra @return
  221. { Ok, no inifinty or NaN involved.. }
  222. @nospec:
  223. tst.b d2
  224. beq @alabel4
  225. moveq.l #0,d0 { x - x hence we always return +0 }
  226. @return:
  227. movem.l (sp)+,d2-d5
  228. rts
  229. @alabel4:
  230. moveq.l #23,d3
  231. bset d3,d5 { restore implied leading "1" }
  232. tst.w d0 { check for zero exponent - no leading "1" }
  233. bne @alabel5
  234. bclr d3,d5 { remove it }
  235. addq.w #1,d0 { "normalize" exponent }
  236. @alabel5:
  237. bset d3,d4 { restore implied leading "1" }
  238. tst.w d1 { check for zero exponent - no leading "1" }
  239. bne @alabel6
  240. bclr d3,d4 { remove it }
  241. addq.w #1,d1 { "normalize" exponent }
  242. @alabel6:
  243. moveq.l #0,d3 { (put initial zero rounding bits in d3) }
  244. neg.w d1 { d1 = u.exp - v.exp }
  245. add.w d0,d1
  246. beq @alabel8 { exponents are equal - no shifting neccessary }
  247. bgt @alabel7 { not equal but no exchange neccessary }
  248. exg d4,d5 { exchange u and v }
  249. sub.w d1,d0 { d0 = u.exp - (u.exp - v.exp) = v.exp }
  250. neg.w d1
  251. tst.w d2 { d2.h = u.sign ^ (u.sign ^ v.sign) = v.sign }
  252. bpl @alabel7
  253. bchg #31,d2
  254. @alabel7:
  255. cmp.w #26,d1 { is u so much bigger that v is not }
  256. bge @alabel9 { significant ? }
  257. {--------------------------------------------------------------------}
  258. { shift mantissa left two digits, to allow cancellation of }
  259. { most significant digit, while gaining an additional digit for }
  260. { rounding. }
  261. {--------------------------------------------------------------------}
  262. moveq.l #1,d3
  263. @alabel10:
  264. add.l d5,d5
  265. subq.w #1,d0 { decrement exponent }
  266. subq.w #1,d1 { done shifting altogether ? }
  267. dbeq d3,@alabel10 { loop if still can shift u.mant more }
  268. moveq.l #0,d3
  269. cmp.w #16,d1 { see if fast rotate possible }
  270. blt @alabel11
  271. or.w d4,d3 { set rounding bits }
  272. clr.w d4
  273. swap d4
  274. subq.w #8,d1
  275. subq.w #8,d1
  276. bra @alabel11
  277. @alabel12:
  278. move.b d4,d2
  279. and.b #1,d2
  280. or.b d2,d3
  281. lsr.l #1,d4 { shift v.mant right the rest of the way }
  282. @alabel11:
  283. dbra d1,@alabel12 { loop }
  284. @alabel8:
  285. tst.w d2 { are the signs equal ? }
  286. bpl @alabel13 { yes, no negate necessary }
  287. tst.w d3 { negate rounding bits and v.mant }
  288. beq @alabel14
  289. addq.l #1,d4
  290. @alabel14:
  291. neg.l d4
  292. @alabel13:
  293. add.l d4,d5 { u.mant = u.mant + v.mant }
  294. bcs @alabel9 { needn not negate }
  295. tst.w d2 { opposite signs ? }
  296. bpl @alabel9 { do not need to negate result }
  297. neg.l d5
  298. not.l d2 { switch sign }
  299. @alabel9:
  300. move.l d5,d4 { move result for normalization }
  301. clr.l d1
  302. tst.l d3
  303. beq @alabel15
  304. moveq.l #-1,d1
  305. @alabel15:
  306. swap d2 { put sign into d2 (exponent is in d0) }
  307. jmp FPC_SINGLE_NORM { leave registers on stack for norm_sf }
  308. end;
  309. Procedure Single_Mul;Assembler;
  310. {--------------------------------------------}
  311. { Low-level routine to multiply two single }
  312. { IEEE floating point values. Never called }
  313. { directly. }
  314. { Om Entry: }
  315. { d0,d1 = values to multiply }
  316. { On Exit: }
  317. { d0 = result. }
  318. { Registers destroyed: d0,d1 }
  319. { stack space used (and restored): 8 bytes. }
  320. {--------------------------------------------}
  321. Asm
  322. XDEF SINGLE_MUL
  323. movem.l d2-d5,-(sp)
  324. move.l d0,d4 { d4 = v }
  325. move.l d1,d5 { d5 = u }
  326. move.l #$7fffff,d3
  327. move.l d5,d0 { d0 = u.exp }
  328. and.l d3,d5 { remove exponent from u.mantissa }
  329. swap d0
  330. move.w d0,d2 { d2 = u.sign }
  331. move.l d4,d1 { d1 = v.exp }
  332. and.l d3,d4 { remove exponent from v.mantissa }
  333. swap d1
  334. eor.w d1,d2 { d2 = u.sign ^ v.sign (in bit 15)}
  335. moveq.l #15,d3
  336. bclr d3,d0 { kill sign bit }
  337. bclr d3,d1 { kill sign bit }
  338. tst.l d0 { test if one of factors is 0 }
  339. beq @mlabel1
  340. tst.l d1
  341. @mlabel1:
  342. seq d2 { 'one of factors is 0' flag in the lowest byte }
  343. lsr.w #7,d0 { keep here exponents only }
  344. lsr.w #7,d1
  345. {--------------------------------------------------------------------}
  346. { Now perform testing of NaN and infinities }
  347. {--------------------------------------------------------------------}
  348. moveq.l #-1,d3
  349. cmp.b d3,d0
  350. beq @mlabel2
  351. cmp.b d3,d1
  352. bne @mnospec
  353. bra @mlabel3
  354. {--------------------------------------------------------------------}
  355. { first operand is special }
  356. {--------------------------------------------------------------------}
  357. @mlabel2:
  358. tst.l d5 { is it NaN? }
  359. bne @mretnan
  360. @mlabel3:
  361. tst.b d2 { 0 times special or special times 0 ? }
  362. bne @mretnan { yes -> NaN }
  363. cmp.b d3,d1 { is the other special ? }
  364. beq @mlabel4 { maybe it is NaN }
  365. {--------------------------------------------------------------------}
  366. { Return infiny with correct sign }
  367. {--------------------------------------------------------------------}
  368. @mretinf:
  369. move.l #$ff000000,d0 { we will return #0xff800000 or #0x7f800000 }
  370. lsl.w #1,d2
  371. roxr.l #1,d0 { shift in high bit as given by d2 }
  372. @mreturn:
  373. movem.l (sp)+,d2-d5
  374. rts
  375. {--------------------------------------------------------------------}
  376. { v is special. }
  377. {--------------------------------------------------------------------}
  378. @mlabel4:
  379. tst.l d4 { is this NaN? }
  380. beq @mretinf { we know that the other is not zero }
  381. @mretnan:
  382. moveq.l #-1,d0
  383. lsr.l #1,d0 { 0x7fffffff -> d0 }
  384. bra @mreturn
  385. {--------------------------------------------------------------------}
  386. { End of NaN and Inf }
  387. {--------------------------------------------------------------------}
  388. @mnospec:
  389. tst.b d2 { not needed - but we can waste two instr. }
  390. bne @mretzz { return signed 0 if one of factors is 0 }
  391. moveq.l #23,d3
  392. bset d3,d5 { restore implied leading "1" }
  393. subq.w #8,sp { multiplication accumulator }
  394. tst.w d0 { check for zero exponent - no leading "1" }
  395. bne @mlabel5
  396. bclr d3,d5 { remove it }
  397. addq.w #1,d0 { "normalize" exponent }
  398. @mlabel5:
  399. tst.l d5
  400. beq @mretz { multiplying zero }
  401. moveq.l #23,d3
  402. bset d3,d4 { restore implied leading "1" }
  403. tst.w d1 { check for zero exponent - no leading "1" }
  404. bne @mlabel6
  405. bclr d3,d4 { remove it }
  406. addq.w #1,d1 { "normalize" exponent }
  407. @mlabel6:
  408. tst.l d4
  409. beq @mretz { multiply by zero }
  410. add.w d1,d0 { add exponents, }
  411. sub.w #BIAS4+16-8,d0 { remove excess bias, acnt for repositioning }
  412. clr.l (sp) { initialize 64-bit product to zero }
  413. clr.l 4(sp)
  414. {--------------------------------------------------------------------}
  415. { see Knuth, Seminumerical Algorithms, section 4.3. algorithm M }
  416. {--------------------------------------------------------------------}
  417. move.w d4,d3
  418. mulu.w d5,d3 { mulitply with bigit from multiplier }
  419. move.l d3,4(sp) { store into result }
  420. move.l d4,d3
  421. swap d3
  422. mulu.w d5,d3
  423. add.l d3,2(sp) { add to result }
  424. swap d5 { [TOP 8 BITS SHOULD BE ZERO !] }
  425. move.w d4,d3
  426. mulu.w d5,d3 { mulitply with bigit from multiplier }
  427. add.l d3,2(sp) { store into result (no carry can occur here) }
  428. move.l d4,d3
  429. swap d3
  430. mulu.w d5,d3
  431. add.l d3,(sp) { add to result }
  432. { [TOP 16 BITS SHOULD BE ZERO !] }
  433. movem.l 2(sp),d4-d5 { get the 48 valid mantissa bits }
  434. clr.w d5 { (pad to 64) }
  435. move.l #$0000ffff,d3
  436. @mlabel7:
  437. cmp.l d3,d4 { multiply (shift) until }
  438. bhi @mlabel8 { 1 in upper 16 result bits }
  439. cmp.w #9,d0 { give up for denormalized numbers }
  440. ble @mlabel8
  441. swap d4 { (we''re getting here only when multiplying }
  442. swap d5 { with a denormalized number; there''s an }
  443. move.w d5,d4 { eventual loss of 4 bits in the rounding }
  444. clr.w d5 { byte -- what a pity 8-) }
  445. subq.w #8,d0 { decrement exponent }
  446. subq.w #8,d0
  447. bra @mlabel7
  448. @mlabel8:
  449. move.l d5,d1 { get rounding bits }
  450. rol.l #8,d1
  451. move.l d1,d3 { see if sticky bit should be set }
  452. and.l #$ffffff00,d3
  453. beq @mlabel9
  454. or.b #1,d1 { set "sticky bit" if any low-order set }
  455. @mlabel9:
  456. addq.w #8,sp { remove accumulator from stack }
  457. jmp FPC_SINGLE_NORM{ (result in d4) }
  458. @mretz:
  459. addq.w #8,sp { release accumulator space }
  460. @mretzz:
  461. moveq.l #0,d0 { save zero as result }
  462. lsl.w #1,d2 { and set it sign as for d2 }
  463. roxr.l #1,d0
  464. movem.l (sp)+,d2-d5
  465. rts { no normalizing neccessary }
  466. end;
  467. Procedure Single_Div;Assembler;
  468. {--------------------------------------------}
  469. { Low-level routine to dividr two single }
  470. { IEEE floating point values. Never called }
  471. { directly. }
  472. { Om Entry: }
  473. { d1/d0 = u/v = operation to perform. }
  474. { On Exit: }
  475. { d0 = result. }
  476. { Registers destroyed: d0,d1 }
  477. { stack space used (and restored): 8 bytes. }
  478. {--------------------------------------------}
  479. ASM
  480. XDEF SINGLE_DIV
  481. { u = d1 = dividend }
  482. { v = d0 = divisor }
  483. tst.l d0 { check if divisor is 0 }
  484. bne @dno_exception
  485. move.l #$7f800000,d0
  486. btst #31,d1 { transfer sign of dividend }
  487. beq @dclear
  488. bset #31,d0
  489. @dclear:
  490. rts
  491. @dno_exception:
  492. move.l d1,d4 { d4 = u, d5 = v }
  493. move.l d0,d5
  494. movem.l d2-d5,-(sp) { save registers }
  495. move.l #$7fffff,d3
  496. move.l d4,d0 { d0 = u.exp }
  497. and.l d3,d4 { remove exponent from u.mantissa }
  498. swap d0
  499. move.w d0,d2 { d2 = u.sign }
  500. move.l d5,d1 { d1 = v.exp }
  501. and.l d3,d5 { remove exponent from v.mantissa }
  502. swap d1
  503. eor.w d1,d2 { d2 = u.sign ^ v.sign (in bit 15) }
  504. moveq.l #15,d3
  505. bclr d3,d0 { kill sign bit }
  506. bclr d3,d1 { kill sign bit }
  507. lsr.w #7,d0
  508. lsr.w #7,d1
  509. moveq.l #-1,d3
  510. cmp.b d3,d0 { comparison with #0xff }
  511. beq @dlabel1 { u == NaN ;; u== Inf }
  512. cmp.b d3,d1
  513. beq @dlabel2 { v == NaN ;; v == Inf }
  514. tst.b d0
  515. bne @dlabel4 { u not zero nor denorm }
  516. tst.l d4
  517. beq @dlabel3 { 0/ ? }
  518. @dlabel4:
  519. tst.w d1
  520. bne @dnospec
  521. tst.l d5
  522. bne @dnospec
  523. bra @dretinf { x/0 -> +/- Inf }
  524. @dlabel1:
  525. tst.l d4 { u == NaN ? }
  526. bne @dretnan { NaN/ x }
  527. cmp.b d3,d1
  528. beq @dretnan { Inf/Inf or Inf/NaN }
  529. { bra dretinf ; Inf/x ; x != Inf && x != NaN }
  530. {--------------------------------------------------------------------}
  531. { Return infinity with correct sign. }
  532. {--------------------------------------------------------------------}
  533. @dretinf:
  534. move.l #$ff000000,d0
  535. lsl.w #1,d2
  536. roxr.l #1,d0 { shift in high bit as given by d2 }
  537. @dreturn:
  538. movem.l (sp)+,d2-d5
  539. rts
  540. @dlabel2:
  541. tst.l d5
  542. bne @dretnan { x/NaN }
  543. { bra dretzero ; x/Inf -> +/- 0 }
  544. {--------------------------------------------------------------------}
  545. { Return correct signed zero. }
  546. {--------------------------------------------------------------------}
  547. @dretzero:
  548. moveq.l #0,d0 { zero destination }
  549. lsl.w #1,d2 { set X bit accordingly }
  550. roxr.l #1,d0
  551. bra @dreturn
  552. @dlabel3:
  553. tst.w d1
  554. bne @dretzero { 0/x ->+/- 0 }
  555. tst.l d4
  556. bne @dretzero { 0/x }
  557. { bra dretnan 0/0 }
  558. {--------------------------------------------------------------------}
  559. { Return NotANumber }
  560. {--------------------------------------------------------------------}
  561. @dretnan:
  562. move.l d3,d0 { d3 contains 0xffffffff }
  563. lsr.l #1,d0
  564. bra @dreturn
  565. {--------------------------------------------------------------------}
  566. { End of Special Handling }
  567. {--------------------------------------------------------------------}
  568. @dnospec:
  569. moveq.l #23,d3
  570. bset d3,d4 { restore implied leading "1" }
  571. tst.w d0 { check for zero exponent - no leading "1" }
  572. bne @dlabel5
  573. bclr d3,d4 { remove it }
  574. add.w #1,d0 { "normalize" exponent }
  575. @dlabel5:
  576. tst.l d4
  577. beq @dretzero { dividing zero }
  578. bset d3,d5 { restore implied leading "1" }
  579. tst.w d1 { check for zero exponent - no leading "1"}
  580. bne @dlabel6
  581. bclr d3,d5 { remove it }
  582. add.w #1,d1 { "normalize" exponent }
  583. @dlabel6:
  584. sub.w d1,d0 { subtract exponents, }
  585. add.w #BIAS4-8+1,d0 { add bias back in, account for shift }
  586. add.w #34,d0 { add loop offset, +2 for extra rounding bits}
  587. { for denormalized numbers (2 implied by dbra)}
  588. move.w #27,d1 { bit number for "implied" pos (+4 for rounding)}
  589. moveq.l #-1,d3 { zero quotient (for speed a one''s complement) }
  590. sub.l d5,d4 { initial subtraction, u = u - v }
  591. @dlabel7:
  592. btst d1,d3 { divide until 1 in implied position }
  593. beq @dlabel9
  594. add.l d4,d4
  595. bcs @dlabel8 { if carry is set, add, else subtract }
  596. addx.l d3,d3 { shift quotient and set bit zero }
  597. sub.l d5,d4 { subtract, u = u - v }
  598. dbra d0,@dlabel7 { give up if result is denormalized }
  599. bra @dlabel9
  600. @dlabel8:
  601. addx.l d3,d3 { shift quotient and clear bit zero }
  602. add.l d5,d4 { add (restore), u = u + v }
  603. dbra d0,@dlabel7 { give up if result is denormalized }
  604. @dlabel9:
  605. subq.w #2,d0 { remove rounding offset for denormalized nums }
  606. not.l d3 { invert quotient to get it right }
  607. clr.l d1 { zero rounding bits }
  608. tst.l d4 { check for exact result }
  609. beq @dlabel10
  610. moveq.l #-1,d1 { prevent tie case }
  611. @dlabel10:
  612. move.l d3,d4 { save quotient mantissa }
  613. jmp FPC_SINGLE_NORM{ (registers on stack removed by norm_sf) }
  614. end;
  615. Procedure Single_Cmp; Assembler;
  616. {--------------------------------------------}
  617. { Low-level routine to compare single two }
  618. { single point values.. }
  619. { Never called directly. }
  620. { On Entry: }
  621. { d1 and d0 Values to compare }
  622. { d0 = first operand }
  623. { On Exit: }
  624. { Flags according to result }
  625. { Registers destroyed: d0,d1 }
  626. {--------------------------------------------}
  627. Asm
  628. XDEF SINGLE_CMP
  629. tst.l d0 { check sign bit }
  630. bpl @cmplab1
  631. neg.l d0 { negate }
  632. bchg #31,d0 { toggle sign bit }
  633. @cmplab1:
  634. tst.l d1 { check sign bit }
  635. bpl @cmplab2
  636. neg.l d1 { negate }
  637. bchg #31,d1 { toggle sign bit }
  638. @cmplab2:
  639. cmp.l d0,d1 { compare... }
  640. rts
  641. end;
  642. Procedure LongMul;Assembler;
  643. {--------------------------------------------}
  644. { Low-level routine to multiply two signed }
  645. { 32-bit values. Never called directly. }
  646. { On entry: d1,d0 = 32-bit signed values to }
  647. { multiply. }
  648. { On Exit: }
  649. { d0 = result. }
  650. { Registers destroyed: d0,d1 }
  651. { stack space used and restored: 10 bytes }
  652. {--------------------------------------------}
  653. Asm
  654. XDEF LONGMUL
  655. cmp.b #2,Test68000 { Are we on a 68020+ cpu }
  656. blt @Lmulcontinue
  657. muls.l d1,d0 { yes, then directly mul... }
  658. rts { return... result in d0 }
  659. @Lmulcontinue:
  660. move.l d2,a0 { save registers }
  661. move.l d3,a1
  662. move.l d0,-(sp)
  663. move.l d1,-(sp)
  664. movem.w (sp)+,d0-d3 { u = d0-d1, v = d2-d3 }
  665. move.w d0,-(sp) { sign flag }
  666. bpl @LMul1 { is u negative ? }
  667. neg.w d1 { yes, force it positive }
  668. negx.w d0
  669. @LMul1:
  670. tst.w d2 { is v negative ? }
  671. bpl @LMul2
  672. neg.w d3 { yes, force it positive ... }
  673. negx.w d2
  674. not.w (sp) { ... and modify flag word }
  675. @LMul2:
  676. ext.l d0 { u.h <> 0 ? }
  677. beq @LMul3
  678. mulu.w d3,d0 { r = v.l * u.h }
  679. @LMul3:
  680. tst.w d2 { v.h <> 0 ? }
  681. beq @LMul4
  682. mulu.w d1,d2 { r += v.h * u.l }
  683. add.w d2,d0
  684. @LMul4:
  685. swap d0
  686. clr.w d0
  687. mulu.w d3,d1 { r += v.l * u.l }
  688. add.l d1,d0
  689. move.l a1,d3
  690. move.l a0,d2
  691. tst.w (sp)+ { should the result be negated ? }
  692. bpl @LMul5 { no, just return }
  693. neg.l d0 { else r = -r }
  694. @LMul5:
  695. rts
  696. end;
  697. Procedure Long2Single;Assembler;
  698. {--------------------------------------------}
  699. { Low-level routine to convert a longint }
  700. { to a single floating point value. }
  701. { On entry: d0 = longint value to convert. }
  702. { On Exit: }
  703. { d0 = single IEEE value }
  704. { Registers destroyed: d0,d1 }
  705. { stack space used and restored: 8 bytes }
  706. {--------------------------------------------}
  707. Asm
  708. XDEF LONG2SINGLE
  709. movem.l d2-d5,-(sp) { save registers to make norm_sf happy}
  710. move.l d0,d4 { prepare result mantissa }
  711. move.w #BIAS4+32-8,d0 { radix point after 32 bits }
  712. move.l d4,d2 { set sign flag }
  713. bge @l2slabel1 { nonnegative }
  714. neg.l d4 { take absolute value }
  715. @l2slabel1:
  716. swap d2 { follow SINGLE_NORM conventions }
  717. clr.w d1 { set rounding = 0 }
  718. jmp FPC_SINGLE_NORM
  719. end;
  720. Procedure LongDiv; [alias : 'FPC_LONGDIV'];Assembler;
  721. {--------------------------------------------}
  722. { Low-level routine to do signed long }
  723. { division. }
  724. { On entry: d0/d1 operation to perform }
  725. { On Exit: }
  726. { d0 = quotient }
  727. { d1 = remainder }
  728. { Registers destroyed: d0,d1,d6 }
  729. { stack space used and restored: 10 bytes }
  730. {--------------------------------------------}
  731. asm
  732. XDEF LONGDIV
  733. cmp.b #2,Test68000 { can we use divs ? }
  734. blt @continue
  735. tst.l d1
  736. beq @zerodiv2
  737. move.l d1,d6
  738. clr.l d1 { clr }
  739. tst.l d0 { check sign of d0 }
  740. bpl @posdiv
  741. move.l #$ffffffff,d1{ sign extend into d1 }
  742. @posdiv:
  743. divsl.l d6,d1:d0
  744. rts
  745. @continue:
  746. move.l d2,a0 { save registers }
  747. move.l d3,a1
  748. move.l d4,-(sp) { divisor = d1 = d4 }
  749. move.l d5,-(sp) { divident = d0 = d5 }
  750. move.l d1,d4 { save divisor }
  751. move.l d0,d5 { save dividend }
  752. clr.w -(sp) { sign flag }
  753. clr.l d0 { prepare result }
  754. move.l d4,d2 { get divisor }
  755. beq @zerodiv { divisor = 0 ? }
  756. bpl @LDiv1 { divisor < 0 ? }
  757. neg.l d2 { negate it }
  758. not.w (sp) { remember sign }
  759. @LDiv1:
  760. move.l d5,d1 { get dividend }
  761. bpl @LDiv2 { dividend < 0 ? }
  762. neg.l d1 { negate it }
  763. not.w (sp) { remember sign }
  764. @LDiv2:
  765. {;== case 1) divident < divisor}
  766. cmp.l d2,d1 { is divident smaller then divisor ? }
  767. bcs @LDiv7 { yes, return immediately }
  768. {;== case 2) divisor has <= 16 significant bits}
  769. move.l d4,d6 { put divisor in d6 register }
  770. lsr.l #8,d6 { rotate into low word }
  771. lsr.l #8,d6
  772. tst.l d6
  773. bne @LDiv3 { divisor has only 16 bits }
  774. move.w d1,d3 { save dividend }
  775. clr.w d1 { divide dvd.h by dvs }
  776. swap d1
  777. beq @LDiv4 { (no division necessary if dividend zero)}
  778. divu d2,d1
  779. @LDiv4:
  780. move.w d1,d0 { save quotient.h }
  781. swap d0
  782. move.w d3,d1 { (d0.h = remainder of prev divu) }
  783. divu d2,d1 { divide dvd.l by dvs }
  784. move.w d1,d0 { save quotient.l }
  785. clr.w d1 { get remainder }
  786. swap d1
  787. bra @LDiv7 { and return }
  788. {;== case 3) divisor > 16 bits (corollary is dividend > 16 bits, see case 1)}
  789. @LDiv3:
  790. moveq.l #31,d3 { loop count }
  791. @LDiv5:
  792. add.l d1,d1 { shift divident ... }
  793. addx.l d0,d0 { ... into d0 }
  794. cmp.l d2,d0 { compare with divisor }
  795. bcs @LDiv6
  796. sub.l d2,d0 { big enough, subtract }
  797. addq.w #1,d1 { and note bit into result }
  798. @LDiv6:
  799. dbra d3,@LDiv5
  800. exg d0,d1 { put quotient and remainder in their registers}
  801. @LDiv7:
  802. tst.l d5 { must the remainder be corrected ? }
  803. bpl @LDiv8
  804. neg.l d1 { yes, apply sign }
  805. { the following line would be correct if modulus is defined as in algebra}
  806. {; add.l sp@(6),d1 ; algebraic correction: modulus can only be >= 0}
  807. @LDiv8:
  808. tst.w (sp)+ { result should be negative ? }
  809. bpl @LDiv9
  810. neg.l d0 { yes, negate it }
  811. @LDiv9:
  812. move.l a1,d3
  813. move.l a0,d2
  814. move.l (sp)+,d5
  815. move.l (sp)+,d4
  816. rts { en exit : remainder = d1, quotient = d0 }
  817. @zerodiv:
  818. move.l a1,d3 { restore stack... }
  819. move.l a0,d2
  820. move.w (sp)+,d1 { remove sign word }
  821. move.l (sp)+,d5
  822. move.l (sp)+,d4
  823. @zerodiv2:
  824. move.l #200,d0
  825. jsr FPC_HALT_ERROR { RunError(200) }
  826. rts { this should never occur... }
  827. end;
  828. Procedure LongMod;[alias : 'FPC_LONGMOD'];Assembler;
  829. { see longdiv for info on calling convention }
  830. asm
  831. XDEF LONGMOD
  832. jsr FPC_LONGDIV
  833. move.l d1,d0 { return the remainder in d0 }
  834. rts
  835. end;