scale.sa revision 1.1 1 1.1 mycroft * MOTOROLA MICROPROCESSOR & MEMORY TECHNOLOGY GROUP
2 1.1 mycroft * M68000 Hi-Performance Microprocessor Division
3 1.1 mycroft * M68040 Software Package
4 1.1 mycroft *
5 1.1 mycroft * M68040 Software Package Copyright (c) 1993, 1994 Motorola Inc.
6 1.1 mycroft * All rights reserved.
7 1.1 mycroft *
8 1.1 mycroft * THE SOFTWARE is provided on an "AS IS" basis and without warranty.
9 1.1 mycroft * To the maximum extent permitted by applicable law,
10 1.1 mycroft * MOTOROLA DISCLAIMS ALL WARRANTIES WHETHER EXPRESS OR IMPLIED,
11 1.1 mycroft * INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A
12 1.1 mycroft * PARTICULAR PURPOSE and any warranty against infringement with
13 1.1 mycroft * regard to the SOFTWARE (INCLUDING ANY MODIFIED VERSIONS THEREOF)
14 1.1 mycroft * and any accompanying written materials.
15 1.1 mycroft *
16 1.1 mycroft * To the maximum extent permitted by applicable law,
17 1.1 mycroft * IN NO EVENT SHALL MOTOROLA BE LIABLE FOR ANY DAMAGES WHATSOEVER
18 1.1 mycroft * (INCLUDING WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS
19 1.1 mycroft * PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR
20 1.1 mycroft * OTHER PECUNIARY LOSS) ARISING OF THE USE OR INABILITY TO USE THE
21 1.1 mycroft * SOFTWARE. Motorola assumes no responsibility for the maintenance
22 1.1 mycroft * and support of the SOFTWARE.
23 1.1 mycroft *
24 1.1 mycroft * You are hereby granted a copyright license to use, modify, and
25 1.1 mycroft * distribute the SOFTWARE so long as this entire notice is retained
26 1.1 mycroft * without alteration in any modified and/or redistributed versions,
27 1.1 mycroft * and that such modified versions are clearly identified as such.
28 1.1 mycroft * No licenses are granted by implication, estoppel or otherwise
29 1.1 mycroft * under any patents or trademarks of Motorola, Inc.
30 1.1 mycroft
31 1.1 mycroft *
32 1.1 mycroft * scale.sa 3.3 7/30/91
33 1.1 mycroft *
34 1.1 mycroft * The entry point sSCALE computes the destination operand
35 1.1 mycroft * scaled by the source operand. If the absoulute value of
36 1.1 mycroft * the source operand is (>= 2^14) an overflow or underflow
37 1.1 mycroft * is returned.
38 1.1 mycroft *
39 1.1 mycroft * The entry point sscale is called from do_func to emulate
40 1.1 mycroft * the fscale unimplemented instruction.
41 1.1 mycroft *
42 1.1 mycroft * Input: Double-extended destination operand in FPTEMP,
43 1.1 mycroft * double-extended source operand in ETEMP.
44 1.1 mycroft *
45 1.1 mycroft * Output: The function returns scale(X,Y) to fp0.
46 1.1 mycroft *
47 1.1 mycroft * Modifies: fp0.
48 1.1 mycroft *
49 1.1 mycroft * Algorithm:
50 1.1 mycroft *
51 1.1 mycroft
52 1.1 mycroft SCALE IDNT 2,1 Motorola 040 Floating Point Software Package
53 1.1 mycroft
54 1.1 mycroft section 8
55 1.1 mycroft
56 1.1 mycroft include fpsp.h
57 1.1 mycroft
58 1.1 mycroft xref t_ovfl2
59 1.1 mycroft xref t_unfl
60 1.1 mycroft xref round
61 1.1 mycroft xref t_resdnrm
62 1.1 mycroft
63 1.1 mycroft SRC_BNDS dc.w $3fff,$400c
64 1.1 mycroft
65 1.1 mycroft *
66 1.1 mycroft * This entry point is used by the unimplemented instruction exception
67 1.1 mycroft * handler.
68 1.1 mycroft *
69 1.1 mycroft *
70 1.1 mycroft *
71 1.1 mycroft * FSCALE
72 1.1 mycroft *
73 1.1 mycroft xdef sscale
74 1.1 mycroft sscale:
75 1.1 mycroft fmove.l #0,fpcr ;clr user enabled exc
76 1.1 mycroft clr.l d1
77 1.1 mycroft move.w FPTEMP(a6),d1 ;get dest exponent
78 1.1 mycroft smi L_SCR1(a6) ;use L_SCR1 to hold sign
79 1.1 mycroft andi.l #$7fff,d1 ;strip sign
80 1.1 mycroft move.w ETEMP(a6),d0 ;check src bounds
81 1.1 mycroft andi.w #$7fff,d0 ;clr sign bit
82 1.1 mycroft cmp2.w SRC_BNDS,d0
83 1.1 mycroft bcc.b src_in
84 1.1 mycroft cmpi.w #$400c,d0 ;test for too large
85 1.1 mycroft bge.w src_out
86 1.1 mycroft *
87 1.1 mycroft * The source input is below 1, so we check for denormalized numbers
88 1.1 mycroft * and set unfl.
89 1.1 mycroft *
90 1.1 mycroft src_small:
91 1.1 mycroft move.b DTAG(a6),d0
92 1.1 mycroft andi.b #$e0,d0
93 1.1 mycroft tst.b d0
94 1.1 mycroft beq.b no_denorm
95 1.1 mycroft st STORE_FLG(a6) ;dest already contains result
96 1.1 mycroft or.l #unfl_mask,USER_FPSR(a6) ;set UNFL
97 1.1 mycroft den_done:
98 1.1 mycroft lea.l FPTEMP(a6),a0
99 1.1 mycroft bra t_resdnrm
100 1.1 mycroft no_denorm:
101 1.1 mycroft fmove.l USER_FPCR(a6),FPCR
102 1.1 mycroft fmove.x FPTEMP(a6),fp0 ;simply return dest
103 1.1 mycroft rts
104 1.1 mycroft
105 1.1 mycroft
106 1.1 mycroft *
107 1.1 mycroft * Source is within 2^14 range. To perform the int operation,
108 1.1 mycroft * move it to d0.
109 1.1 mycroft *
110 1.1 mycroft src_in:
111 1.1 mycroft fmove.x ETEMP(a6),fp0 ;move in src for int
112 1.1 mycroft fmove.l #rz_mode,fpcr ;force rz for src conversion
113 1.1 mycroft fmove.l fp0,d0 ;int src to d0
114 1.1 mycroft fmove.l #0,FPSR ;clr status from above
115 1.1 mycroft tst.w ETEMP(a6) ;check src sign
116 1.1 mycroft blt.w src_neg
117 1.1 mycroft *
118 1.1 mycroft * Source is positive. Add the src to the dest exponent.
119 1.1 mycroft * The result can be denormalized, if src = 0, or overflow,
120 1.1 mycroft * if the result of the add sets a bit in the upper word.
121 1.1 mycroft *
122 1.1 mycroft src_pos:
123 1.1 mycroft tst.w d1 ;check for denorm
124 1.1 mycroft beq.w dst_dnrm
125 1.1 mycroft add.l d0,d1 ;add src to dest exp
126 1.1 mycroft beq.b denorm ;if zero, result is denorm
127 1.1 mycroft cmpi.l #$7fff,d1 ;test for overflow
128 1.1 mycroft bge.b ovfl
129 1.1 mycroft tst.b L_SCR1(a6)
130 1.1 mycroft beq.b spos_pos
131 1.1 mycroft or.w #$8000,d1
132 1.1 mycroft spos_pos:
133 1.1 mycroft move.w d1,FPTEMP(a6) ;result in FPTEMP
134 1.1 mycroft fmove.l USER_FPCR(a6),FPCR
135 1.1 mycroft fmove.x FPTEMP(a6),fp0 ;write result to fp0
136 1.1 mycroft rts
137 1.1 mycroft ovfl:
138 1.1 mycroft tst.b L_SCR1(a6)
139 1.1 mycroft beq.b sovl_pos
140 1.1 mycroft or.w #$8000,d1
141 1.1 mycroft sovl_pos:
142 1.1 mycroft move.w FPTEMP(a6),ETEMP(a6) ;result in ETEMP
143 1.1 mycroft move.l FPTEMP_HI(a6),ETEMP_HI(a6)
144 1.1 mycroft move.l FPTEMP_LO(a6),ETEMP_LO(a6)
145 1.1 mycroft bra t_ovfl2
146 1.1 mycroft
147 1.1 mycroft denorm:
148 1.1 mycroft tst.b L_SCR1(a6)
149 1.1 mycroft beq.b den_pos
150 1.1 mycroft or.w #$8000,d1
151 1.1 mycroft den_pos:
152 1.1 mycroft tst.l FPTEMP_HI(a6) ;check j bit
153 1.1 mycroft blt.b nden_exit ;if set, not denorm
154 1.1 mycroft move.w d1,ETEMP(a6) ;input expected in ETEMP
155 1.1 mycroft move.l FPTEMP_HI(a6),ETEMP_HI(a6)
156 1.1 mycroft move.l FPTEMP_LO(a6),ETEMP_LO(a6)
157 1.1 mycroft or.l #unfl_bit,USER_FPSR(a6) ;set unfl
158 1.1 mycroft lea.l ETEMP(a6),a0
159 1.1 mycroft bra t_resdnrm
160 1.1 mycroft nden_exit:
161 1.1 mycroft move.w d1,FPTEMP(a6) ;result in FPTEMP
162 1.1 mycroft fmove.l USER_FPCR(a6),FPCR
163 1.1 mycroft fmove.x FPTEMP(a6),fp0 ;write result to fp0
164 1.1 mycroft rts
165 1.1 mycroft
166 1.1 mycroft *
167 1.1 mycroft * Source is negative. Add the src to the dest exponent.
168 1.1 mycroft * (The result exponent will be reduced). The result can be
169 1.1 mycroft * denormalized.
170 1.1 mycroft *
171 1.1 mycroft src_neg:
172 1.1 mycroft add.l d0,d1 ;add src to dest
173 1.1 mycroft beq.b denorm ;if zero, result is denorm
174 1.1 mycroft blt.b fix_dnrm ;if negative, result is
175 1.1 mycroft * ;needing denormalization
176 1.1 mycroft tst.b L_SCR1(a6)
177 1.1 mycroft beq.b sneg_pos
178 1.1 mycroft or.w #$8000,d1
179 1.1 mycroft sneg_pos:
180 1.1 mycroft move.w d1,FPTEMP(a6) ;result in FPTEMP
181 1.1 mycroft fmove.l USER_FPCR(a6),FPCR
182 1.1 mycroft fmove.x FPTEMP(a6),fp0 ;write result to fp0
183 1.1 mycroft rts
184 1.1 mycroft
185 1.1 mycroft
186 1.1 mycroft *
187 1.1 mycroft * The result exponent is below denorm value. Test for catastrophic
188 1.1 mycroft * underflow and force zero if true. If not, try to shift the
189 1.1 mycroft * mantissa right until a zero exponent exists.
190 1.1 mycroft *
191 1.1 mycroft fix_dnrm:
192 1.1 mycroft cmpi.w #$ffc0,d1 ;lower bound for normalization
193 1.1 mycroft blt.w fix_unfl ;if lower, catastrophic unfl
194 1.1 mycroft move.w d1,d0 ;use d0 for exp
195 1.1 mycroft move.l d2,-(a7) ;free d2 for norm
196 1.1 mycroft move.l FPTEMP_HI(a6),d1
197 1.1 mycroft move.l FPTEMP_LO(a6),d2
198 1.1 mycroft clr.l L_SCR2(a6)
199 1.1 mycroft fix_loop:
200 1.1 mycroft add.w #1,d0 ;drive d0 to 0
201 1.1 mycroft lsr.l #1,d1 ;while shifting the
202 1.1 mycroft roxr.l #1,d2 ;mantissa to the right
203 1.1 mycroft bcc.b no_carry
204 1.1 mycroft st L_SCR2(a6) ;use L_SCR2 to capture inex
205 1.1 mycroft no_carry:
206 1.1 mycroft tst.w d0 ;it is finished when
207 1.1 mycroft blt.b fix_loop ;d0 is zero or the mantissa
208 1.1 mycroft tst.b L_SCR2(a6)
209 1.1 mycroft beq.b tst_zero
210 1.1 mycroft or.l #unfl_inx_mask,USER_FPSR(a6)
211 1.1 mycroft * ;set unfl, aunfl, ainex
212 1.1 mycroft *
213 1.1 mycroft * Test for zero. If zero, simply use fmove to return +/- zero
214 1.1 mycroft * to the fpu.
215 1.1 mycroft *
216 1.1 mycroft tst_zero:
217 1.1 mycroft clr.w FPTEMP_EX(a6)
218 1.1 mycroft tst.b L_SCR1(a6) ;test for sign
219 1.1 mycroft beq.b tst_con
220 1.1 mycroft or.w #$8000,FPTEMP_EX(a6) ;set sign bit
221 1.1 mycroft tst_con:
222 1.1 mycroft move.l d1,FPTEMP_HI(a6)
223 1.1 mycroft move.l d2,FPTEMP_LO(a6)
224 1.1 mycroft move.l (a7)+,d2
225 1.1 mycroft tst.l d1
226 1.1 mycroft bne.b not_zero
227 1.1 mycroft tst.l FPTEMP_LO(a6)
228 1.1 mycroft bne.b not_zero
229 1.1 mycroft *
230 1.1 mycroft * Result is zero. Check for rounding mode to set lsb. If the
231 1.1 mycroft * mode is rp, and the zero is positive, return smallest denorm.
232 1.1 mycroft * If the mode is rm, and the zero is negative, return smallest
233 1.1 mycroft * negative denorm.
234 1.1 mycroft *
235 1.1 mycroft btst.b #5,FPCR_MODE(a6) ;test if rm or rp
236 1.1 mycroft beq.b no_dir
237 1.1 mycroft btst.b #4,FPCR_MODE(a6) ;check which one
238 1.1 mycroft beq.b zer_rm
239 1.1 mycroft zer_rp:
240 1.1 mycroft tst.b L_SCR1(a6) ;check sign
241 1.1 mycroft bne.b no_dir ;if set, neg op, no inc
242 1.1 mycroft move.l #1,FPTEMP_LO(a6) ;set lsb
243 1.1 mycroft bra.b sm_dnrm
244 1.1 mycroft zer_rm:
245 1.1 mycroft tst.b L_SCR1(a6) ;check sign
246 1.1 mycroft beq.b no_dir ;if clr, neg op, no inc
247 1.1 mycroft move.l #1,FPTEMP_LO(a6) ;set lsb
248 1.1 mycroft or.l #neg_mask,USER_FPSR(a6) ;set N
249 1.1 mycroft bra.b sm_dnrm
250 1.1 mycroft no_dir:
251 1.1 mycroft fmove.l USER_FPCR(a6),FPCR
252 1.1 mycroft fmove.x FPTEMP(a6),fp0 ;use fmove to set cc's
253 1.1 mycroft rts
254 1.1 mycroft
255 1.1 mycroft *
256 1.1 mycroft * The rounding mode changed the zero to a smallest denorm. Call
257 1.1 mycroft * t_resdnrm with exceptional operand in ETEMP.
258 1.1 mycroft *
259 1.1 mycroft sm_dnrm:
260 1.1 mycroft move.l FPTEMP_EX(a6),ETEMP_EX(a6)
261 1.1 mycroft move.l FPTEMP_HI(a6),ETEMP_HI(a6)
262 1.1 mycroft move.l FPTEMP_LO(a6),ETEMP_LO(a6)
263 1.1 mycroft lea.l ETEMP(a6),a0
264 1.1 mycroft bra t_resdnrm
265 1.1 mycroft
266 1.1 mycroft *
267 1.1 mycroft * Result is still denormalized.
268 1.1 mycroft *
269 1.1 mycroft not_zero:
270 1.1 mycroft or.l #unfl_mask,USER_FPSR(a6) ;set unfl
271 1.1 mycroft tst.b L_SCR1(a6) ;check for sign
272 1.1 mycroft beq.b fix_exit
273 1.1 mycroft or.l #neg_mask,USER_FPSR(a6) ;set N
274 1.1 mycroft fix_exit:
275 1.1 mycroft bra.b sm_dnrm
276 1.1 mycroft
277 1.1 mycroft
278 1.1 mycroft *
279 1.1 mycroft * The result has underflowed to zero. Return zero and set
280 1.1 mycroft * unfl, aunfl, and ainex.
281 1.1 mycroft *
282 1.1 mycroft fix_unfl:
283 1.1 mycroft or.l #unfl_inx_mask,USER_FPSR(a6)
284 1.1 mycroft btst.b #5,FPCR_MODE(a6) ;test if rm or rp
285 1.1 mycroft beq.b no_dir2
286 1.1 mycroft btst.b #4,FPCR_MODE(a6) ;check which one
287 1.1 mycroft beq.b zer_rm2
288 1.1 mycroft zer_rp2:
289 1.1 mycroft tst.b L_SCR1(a6) ;check sign
290 1.1 mycroft bne.b no_dir2 ;if set, neg op, no inc
291 1.1 mycroft clr.l FPTEMP_EX(a6)
292 1.1 mycroft clr.l FPTEMP_HI(a6)
293 1.1 mycroft move.l #1,FPTEMP_LO(a6) ;set lsb
294 1.1 mycroft bra.b sm_dnrm ;return smallest denorm
295 1.1 mycroft zer_rm2:
296 1.1 mycroft tst.b L_SCR1(a6) ;check sign
297 1.1 mycroft beq.b no_dir2 ;if clr, neg op, no inc
298 1.1 mycroft move.w #$8000,FPTEMP_EX(a6)
299 1.1 mycroft clr.l FPTEMP_HI(a6)
300 1.1 mycroft move.l #1,FPTEMP_LO(a6) ;set lsb
301 1.1 mycroft or.l #neg_mask,USER_FPSR(a6) ;set N
302 1.1 mycroft bra.w sm_dnrm ;return smallest denorm
303 1.1 mycroft
304 1.1 mycroft no_dir2:
305 1.1 mycroft tst.b L_SCR1(a6)
306 1.1 mycroft bge.b pos_zero
307 1.1 mycroft neg_zero:
308 1.1 mycroft clr.l FP_SCR1(a6) ;clear the exceptional operand
309 1.1 mycroft clr.l FP_SCR1+4(a6) ;for gen_except.
310 1.1 mycroft clr.l FP_SCR1+8(a6)
311 1.1 mycroft fmove.s #:80000000,fp0
312 1.1 mycroft rts
313 1.1 mycroft pos_zero:
314 1.1 mycroft clr.l FP_SCR1(a6) ;clear the exceptional operand
315 1.1 mycroft clr.l FP_SCR1+4(a6) ;for gen_except.
316 1.1 mycroft clr.l FP_SCR1+8(a6)
317 1.1 mycroft fmove.s #:00000000,fp0
318 1.1 mycroft rts
319 1.1 mycroft
320 1.1 mycroft *
321 1.1 mycroft * The destination is a denormalized number. It must be handled
322 1.1 mycroft * by first shifting the bits in the mantissa until it is normalized,
323 1.1 mycroft * then adding the remainder of the source to the exponent.
324 1.1 mycroft *
325 1.1 mycroft dst_dnrm:
326 1.1 mycroft movem.l d2/d3,-(a7)
327 1.1 mycroft move.w FPTEMP_EX(a6),d1
328 1.1 mycroft move.l FPTEMP_HI(a6),d2
329 1.1 mycroft move.l FPTEMP_LO(a6),d3
330 1.1 mycroft dst_loop:
331 1.1 mycroft tst.l d2 ;test for normalized result
332 1.1 mycroft blt.b dst_norm ;exit loop if so
333 1.1 mycroft tst.l d0 ;otherwise, test shift count
334 1.1 mycroft beq.b dst_fin ;if zero, shifting is done
335 1.1 mycroft subi.l #1,d0 ;dec src
336 1.1 mycroft lsl.l #1,d3
337 1.1 mycroft roxl.l #1,d2
338 1.1 mycroft bra.b dst_loop
339 1.1 mycroft *
340 1.1 mycroft * Destination became normalized. Simply add the remaining
341 1.1 mycroft * portion of the src to the exponent.
342 1.1 mycroft *
343 1.1 mycroft dst_norm:
344 1.1 mycroft add.w d0,d1 ;dst is normalized; add src
345 1.1 mycroft tst.b L_SCR1(a6)
346 1.1 mycroft beq.b dnrm_pos
347 1.1 mycroft or.l #$8000,d1
348 1.1 mycroft dnrm_pos:
349 1.1 mycroft movem.w d1,FPTEMP_EX(a6)
350 1.1 mycroft movem.l d2,FPTEMP_HI(a6)
351 1.1 mycroft movem.l d3,FPTEMP_LO(a6)
352 1.1 mycroft fmove.l USER_FPCR(a6),FPCR
353 1.1 mycroft fmove.x FPTEMP(a6),fp0
354 1.1 mycroft movem.l (a7)+,d2/d3
355 1.1 mycroft rts
356 1.1 mycroft
357 1.1 mycroft *
358 1.1 mycroft * Destination remained denormalized. Call t_excdnrm with
359 1.1 mycroft * exceptional operand in ETEMP.
360 1.1 mycroft *
361 1.1 mycroft dst_fin:
362 1.1 mycroft tst.b L_SCR1(a6) ;check for sign
363 1.1 mycroft beq.b dst_exit
364 1.1 mycroft or.l #neg_mask,USER_FPSR(a6) ;set N
365 1.1 mycroft or.l #$8000,d1
366 1.1 mycroft dst_exit:
367 1.1 mycroft movem.w d1,ETEMP_EX(a6)
368 1.1 mycroft movem.l d2,ETEMP_HI(a6)
369 1.1 mycroft movem.l d3,ETEMP_LO(a6)
370 1.1 mycroft or.l #unfl_mask,USER_FPSR(a6) ;set unfl
371 1.1 mycroft movem.l (a7)+,d2/d3
372 1.1 mycroft lea.l ETEMP(a6),a0
373 1.1 mycroft bra t_resdnrm
374 1.1 mycroft
375 1.1 mycroft *
376 1.1 mycroft * Source is outside of 2^14 range. Test the sign and branch
377 1.1 mycroft * to the appropriate exception handler.
378 1.1 mycroft *
379 1.1 mycroft src_out:
380 1.1 mycroft tst.b L_SCR1(a6)
381 1.1 mycroft beq.b scro_pos
382 1.1 mycroft or.l #$8000,d1
383 1.1 mycroft scro_pos:
384 1.1 mycroft move.l FPTEMP_HI(a6),ETEMP_HI(a6)
385 1.1 mycroft move.l FPTEMP_LO(a6),ETEMP_LO(a6)
386 1.1 mycroft tst.w ETEMP(a6)
387 1.1 mycroft blt.b res_neg
388 1.1 mycroft res_pos:
389 1.1 mycroft move.w d1,ETEMP(a6) ;result in ETEMP
390 1.1 mycroft bra t_ovfl2
391 1.1 mycroft res_neg:
392 1.1 mycroft move.w d1,ETEMP(a6) ;result in ETEMP
393 1.1 mycroft lea.l ETEMP(a6),a0
394 1.1 mycroft bra t_unfl
395 1.1 mycroft end
396