lowmath.inc 38 KB

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