File indexing completed on 2025-05-11 08:23:48
0001 #include "fpsp-namespace.h"
0002 //
0003 //
0004 // scale.sa 3.3 7/30/91
0005 //
0006 // The entry point sSCALE computes the destination operand
0007 // scaled by the source operand. If the absolute value of
0008 // the source operand is (>= 2^14) an overflow or underflow
0009 // is returned.
0010 //
0011 // The entry point sscale is called from do_func to emulate
0012 // the fscale unimplemented instruction.
0013 //
0014 // Input: Double-extended destination operand in FPTEMP,
0015 // double-extended source operand in ETEMP.
0016 //
0017 // Output: The function returns scale(X,Y) to fp0.
0018 //
0019 // Modifies: fp0.
0020 //
0021 // Algorithm:
0022 //
0023 // Copyright (C) Motorola, Inc. 1990
0024 // All Rights Reserved
0025 //
0026 // THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF MOTOROLA
0027 // The copyright notice above does not evidence any
0028 // actual or intended publication of such source code.
0029
0030 //SCALE idnt 2,1 | Motorola 040 Floating Point Software Package
0031
0032 |section 8
0033
0034 #include "fpsp.defs"
0035
0036 |xref t_ovfl2
0037 |xref t_unfl
0038 |xref round
0039 |xref t_resdnrm
0040
0041 SRC_BNDS: .short 0x3fff,0x400c
0042
0043 //
0044 // This entry point is used by the unimplemented instruction exception
0045 // handler.
0046 //
0047 //
0048 //
0049 // FSCALE
0050 //
0051 .global sscale
0052 sscale:
0053 fmovel #0,%fpcr //clr user enabled exc
0054 clrl %d1
0055 movew FPTEMP(%a6),%d1 //get dest exponent
0056 smi L_SCR1(%a6) //use L_SCR1 to hold sign
0057 andil #0x7fff,%d1 //strip sign
0058 movew ETEMP(%a6),%d0 //check src bounds
0059 andiw #0x7fff,%d0 //clr sign bit
0060 cmp2w SRC_BNDS,%d0
0061 bccs src_in
0062 cmpiw #0x400c,%d0 //test for too large
0063 bge src_out
0064 //
0065 // The source input is below 1, so we check for denormalized numbers
0066 // and set unfl.
0067 //
0068 src_small:
0069 moveb DTAG(%a6),%d0
0070 andib #0xe0,%d0
0071 tstb %d0
0072 beqs no_denorm
0073 st STORE_FLG(%a6) //dest already contains result
0074 orl #unfl_mask,USER_FPSR(%a6) //set UNFL
0075 den_done:
0076 leal FPTEMP(%a6),%a0
0077 bra t_resdnrm
0078 no_denorm:
0079 fmovel USER_FPCR(%a6),%FPCR
0080 fmovex FPTEMP(%a6),%fp0 //simply return dest
0081 rts
0082
0083
0084 //
0085 // Source is within 2^14 range. To perform the int operation,
0086 // move it to d0.
0087 //
0088 src_in:
0089 fmovex ETEMP(%a6),%fp0 //move in src for int
0090 fmovel #rz_mode,%fpcr //force rz for src conversion
0091 fmovel %fp0,%d0 //int src to d0
0092 fmovel #0,%FPSR //clr status from above
0093 tstw ETEMP(%a6) //check src sign
0094 blt src_neg
0095 //
0096 // Source is positive. Add the src to the dest exponent.
0097 // The result can be denormalized, if src = 0, or overflow,
0098 // if the result of the add sets a bit in the upper word.
0099 //
0100 src_pos:
0101 tstw %d1 //check for denorm
0102 beq dst_dnrm
0103 addl %d0,%d1 //add src to dest exp
0104 beqs denorm //if zero, result is denorm
0105 cmpil #0x7fff,%d1 //test for overflow
0106 bges ovfl
0107 tstb L_SCR1(%a6)
0108 beqs spos_pos
0109 orw #0x8000,%d1
0110 spos_pos:
0111 movew %d1,FPTEMP(%a6) //result in FPTEMP
0112 fmovel USER_FPCR(%a6),%FPCR
0113 fmovex FPTEMP(%a6),%fp0 //write result to fp0
0114 rts
0115 ovfl:
0116 tstb L_SCR1(%a6)
0117 beqs sovl_pos
0118 orw #0x8000,%d1
0119 sovl_pos:
0120 movew FPTEMP(%a6),ETEMP(%a6) //result in ETEMP
0121 movel FPTEMP_HI(%a6),ETEMP_HI(%a6)
0122 movel FPTEMP_LO(%a6),ETEMP_LO(%a6)
0123 bra t_ovfl2
0124
0125 denorm:
0126 tstb L_SCR1(%a6)
0127 beqs den_pos
0128 orw #0x8000,%d1
0129 den_pos:
0130 tstl FPTEMP_HI(%a6) //check j bit
0131 blts nden_exit //if set, not denorm
0132 movew %d1,ETEMP(%a6) //input expected in ETEMP
0133 movel FPTEMP_HI(%a6),ETEMP_HI(%a6)
0134 movel FPTEMP_LO(%a6),ETEMP_LO(%a6)
0135 orl #unfl_bit,USER_FPSR(%a6) //set unfl
0136 leal ETEMP(%a6),%a0
0137 bra t_resdnrm
0138 nden_exit:
0139 movew %d1,FPTEMP(%a6) //result in FPTEMP
0140 fmovel USER_FPCR(%a6),%FPCR
0141 fmovex FPTEMP(%a6),%fp0 //write result to fp0
0142 rts
0143
0144 //
0145 // Source is negative. Add the src to the dest exponent.
0146 // (The result exponent will be reduced). The result can be
0147 // denormalized.
0148 //
0149 src_neg:
0150 addl %d0,%d1 //add src to dest
0151 beqs denorm //if zero, result is denorm
0152 blts fix_dnrm //if negative, result is
0153 // ;needing denormalization
0154 tstb L_SCR1(%a6)
0155 beqs sneg_pos
0156 orw #0x8000,%d1
0157 sneg_pos:
0158 movew %d1,FPTEMP(%a6) //result in FPTEMP
0159 fmovel USER_FPCR(%a6),%FPCR
0160 fmovex FPTEMP(%a6),%fp0 //write result to fp0
0161 rts
0162
0163
0164 //
0165 // The result exponent is below denorm value. Test for catastrophic
0166 // underflow and force zero if true. If not, try to shift the
0167 // mantissa right until a zero exponent exists.
0168 //
0169 fix_dnrm:
0170 cmpiw #0xffc0,%d1 //lower bound for normalization
0171 blt fix_unfl //if lower, catastrophic unfl
0172 movew %d1,%d0 //use d0 for exp
0173 movel %d2,-(%a7) //free d2 for norm
0174 movel FPTEMP_HI(%a6),%d1
0175 movel FPTEMP_LO(%a6),%d2
0176 clrl L_SCR2(%a6)
0177 fix_loop:
0178 addw #1,%d0 //drive d0 to 0
0179 lsrl #1,%d1 //while shifting the
0180 roxrl #1,%d2 //mantissa to the right
0181 bccs no_carry
0182 st L_SCR2(%a6) //use L_SCR2 to capture inex
0183 no_carry:
0184 tstw %d0 //it is finished when
0185 blts fix_loop //d0 is zero or the mantissa
0186 tstb L_SCR2(%a6)
0187 beqs tst_zero
0188 orl #unfl_inx_mask,USER_FPSR(%a6)
0189 // ;set unfl, aunfl, ainex
0190 //
0191 // Test for zero. If zero, simply use fmove to return +/- zero
0192 // to the fpu.
0193 //
0194 tst_zero:
0195 clrw FPTEMP_EX(%a6)
0196 tstb L_SCR1(%a6) //test for sign
0197 beqs tst_con
0198 orw #0x8000,FPTEMP_EX(%a6) //set sign bit
0199 tst_con:
0200 movel %d1,FPTEMP_HI(%a6)
0201 movel %d2,FPTEMP_LO(%a6)
0202 movel (%a7)+,%d2
0203 tstl %d1
0204 bnes not_zero
0205 tstl FPTEMP_LO(%a6)
0206 bnes not_zero
0207 //
0208 // Result is zero. Check for rounding mode to set lsb. If the
0209 // mode is rp, and the zero is positive, return smallest denorm.
0210 // If the mode is rm, and the zero is negative, return smallest
0211 // negative denorm.
0212 //
0213 btstb #5,FPCR_MODE(%a6) //test if rm or rp
0214 beqs no_dir
0215 btstb #4,FPCR_MODE(%a6) //check which one
0216 beqs zer_rm
0217 zer_rp:
0218 tstb L_SCR1(%a6) //check sign
0219 bnes no_dir //if set, neg op, no inc
0220 movel #1,FPTEMP_LO(%a6) //set lsb
0221 bras sm_dnrm
0222 zer_rm:
0223 tstb L_SCR1(%a6) //check sign
0224 beqs no_dir //if clr, neg op, no inc
0225 movel #1,FPTEMP_LO(%a6) //set lsb
0226 orl #neg_mask,USER_FPSR(%a6) //set N
0227 bras sm_dnrm
0228 no_dir:
0229 fmovel USER_FPCR(%a6),%FPCR
0230 fmovex FPTEMP(%a6),%fp0 //use fmove to set cc's
0231 rts
0232
0233 //
0234 // The rounding mode changed the zero to a smallest denorm. Call
0235 // t_resdnrm with exceptional operand in ETEMP.
0236 //
0237 sm_dnrm:
0238 movel FPTEMP_EX(%a6),ETEMP_EX(%a6)
0239 movel FPTEMP_HI(%a6),ETEMP_HI(%a6)
0240 movel FPTEMP_LO(%a6),ETEMP_LO(%a6)
0241 leal ETEMP(%a6),%a0
0242 bra t_resdnrm
0243
0244 //
0245 // Result is still denormalized.
0246 //
0247 not_zero:
0248 orl #unfl_mask,USER_FPSR(%a6) //set unfl
0249 tstb L_SCR1(%a6) //check for sign
0250 beqs fix_exit
0251 orl #neg_mask,USER_FPSR(%a6) //set N
0252 fix_exit:
0253 bras sm_dnrm
0254
0255
0256 //
0257 // The result has underflowed to zero. Return zero and set
0258 // unfl, aunfl, and ainex.
0259 //
0260 fix_unfl:
0261 orl #unfl_inx_mask,USER_FPSR(%a6)
0262 btstb #5,FPCR_MODE(%a6) //test if rm or rp
0263 beqs no_dir2
0264 btstb #4,FPCR_MODE(%a6) //check which one
0265 beqs zer_rm2
0266 zer_rp2:
0267 tstb L_SCR1(%a6) //check sign
0268 bnes no_dir2 //if set, neg op, no inc
0269 clrl FPTEMP_EX(%a6)
0270 clrl FPTEMP_HI(%a6)
0271 movel #1,FPTEMP_LO(%a6) //set lsb
0272 bras sm_dnrm //return smallest denorm
0273 zer_rm2:
0274 tstb L_SCR1(%a6) //check sign
0275 beqs no_dir2 //if clr, neg op, no inc
0276 movew #0x8000,FPTEMP_EX(%a6)
0277 clrl FPTEMP_HI(%a6)
0278 movel #1,FPTEMP_LO(%a6) //set lsb
0279 orl #neg_mask,USER_FPSR(%a6) //set N
0280 bra sm_dnrm //return smallest denorm
0281
0282 no_dir2:
0283 tstb L_SCR1(%a6)
0284 bges pos_zero
0285 neg_zero:
0286 clrl FP_SCR1(%a6) //clear the exceptional operand
0287 clrl FP_SCR1+4(%a6) //for gen_except.
0288 clrl FP_SCR1+8(%a6)
0289 fmoves #0x80000000,%fp0
0290 rts
0291 pos_zero:
0292 clrl FP_SCR1(%a6) //clear the exceptional operand
0293 clrl FP_SCR1+4(%a6) //for gen_except.
0294 clrl FP_SCR1+8(%a6)
0295 fmoves #0x00000000,%fp0
0296 rts
0297
0298 //
0299 // The destination is a denormalized number. It must be handled
0300 // by first shifting the bits in the mantissa until it is normalized,
0301 // then adding the remainder of the source to the exponent.
0302 //
0303 dst_dnrm:
0304 moveml %d2/%d3,-(%a7)
0305 movew FPTEMP_EX(%a6),%d1
0306 movel FPTEMP_HI(%a6),%d2
0307 movel FPTEMP_LO(%a6),%d3
0308 dst_loop:
0309 tstl %d2 //test for normalized result
0310 blts dst_norm //exit loop if so
0311 tstl %d0 //otherwise, test shift count
0312 beqs dst_fin //if zero, shifting is done
0313 subil #1,%d0 //dec src
0314 lsll #1,%d3
0315 roxll #1,%d2
0316 bras dst_loop
0317 //
0318 // Destination became normalized. Simply add the remaining
0319 // portion of the src to the exponent.
0320 //
0321 dst_norm:
0322 addw %d0,%d1 //dst is normalized; add src
0323 tstb L_SCR1(%a6)
0324 beqs dnrm_pos
0325 orl #0x8000,%d1
0326 dnrm_pos:
0327 movemw %d1,FPTEMP_EX(%a6)
0328 moveml %d2,FPTEMP_HI(%a6)
0329 moveml %d3,FPTEMP_LO(%a6)
0330 fmovel USER_FPCR(%a6),%FPCR
0331 fmovex FPTEMP(%a6),%fp0
0332 moveml (%a7)+,%d2/%d3
0333 rts
0334
0335 //
0336 // Destination remained denormalized. Call t_excdnrm with
0337 // exceptional operand in ETEMP.
0338 //
0339 dst_fin:
0340 tstb L_SCR1(%a6) //check for sign
0341 beqs dst_exit
0342 orl #neg_mask,USER_FPSR(%a6) //set N
0343 orl #0x8000,%d1
0344 dst_exit:
0345 movemw %d1,ETEMP_EX(%a6)
0346 moveml %d2,ETEMP_HI(%a6)
0347 moveml %d3,ETEMP_LO(%a6)
0348 orl #unfl_mask,USER_FPSR(%a6) //set unfl
0349 moveml (%a7)+,%d2/%d3
0350 leal ETEMP(%a6),%a0
0351 bra t_resdnrm
0352
0353 //
0354 // Source is outside of 2^14 range. Test the sign and branch
0355 // to the appropriate exception handler.
0356 //
0357 src_out:
0358 tstb L_SCR1(%a6)
0359 beqs scro_pos
0360 orl #0x8000,%d1
0361 scro_pos:
0362 movel FPTEMP_HI(%a6),ETEMP_HI(%a6)
0363 movel FPTEMP_LO(%a6),ETEMP_LO(%a6)
0364 tstw ETEMP(%a6)
0365 blts res_neg
0366 res_pos:
0367 movew %d1,ETEMP(%a6) //result in ETEMP
0368 bra t_ovfl2
0369 res_neg:
0370 movew %d1,ETEMP(%a6) //result in ETEMP
0371 leal ETEMP(%a6),%a0
0372 bra t_unfl
0373 |end