root/arch/i386/math-emu/reg_u_div.S

/* [previous][next][first][last][top][bottom][index][help] */
   1         .file   "reg_u_div.S"
   2 /*---------------------------------------------------------------------------+
   3  |  reg_u_div.S                                                              |
   4  |                                                                           |
   5  | Core division routines                                                    |
   6  |                                                                           |
   7  | Copyright (C) 1992,1993                                                   |
   8  |                       W. Metzenthen, 22 Parker St, Ormond, Vic 3163,      |
   9  |                       Australia.  E-mail   billm@vaxc.cc.monash.edu.au    |
  10  |                                                                           |
  11  |                                                                           |
  12  +---------------------------------------------------------------------------*/
  13 
  14 /*---------------------------------------------------------------------------+
  15  |  Kernel for the division routines.                                        |
  16  |                                                                           |
  17  |  void reg_u_div(FPU_REG *a, FPU_REG *a,                                   |
  18  |                 FPU_REG *dest, unsigned int control_word)                 |
  19  |                                                                           |
  20  |  Does not compute the destination exponent, but does adjust it.           |
  21  +---------------------------------------------------------------------------*/
  22 
  23 #include "exception.h"
  24 #include "fpu_asm.h"
  25 #include "control_w.h"
  26 
  27 
  28 /* #define      dSIGL(x)        (x) */
  29 /* #define      dSIGH(x)        4(x) */
  30 
  31 
  32 #ifndef NON_REENTRANT_FPU
  33 /*
  34         Local storage on the stack:
  35         Result:         FPU_accum_3:FPU_accum_2:FPU_accum_1:FPU_accum_0
  36         Overflow flag:  ovfl_flag
  37  */
  38 #define FPU_accum_3     -4(%ebp)
  39 #define FPU_accum_2     -8(%ebp)
  40 #define FPU_accum_1     -12(%ebp)
  41 #define FPU_accum_0     -16(%ebp)
  42 #define FPU_result_1    -20(%ebp)
  43 #define FPU_result_2    -24(%ebp)
  44 #define FPU_ovfl_flag   -28(%ebp)
  45 
  46 #else
  47 .data
  48 /*
  49         Local storage in a static area:
  50         Result:         FPU_accum_3:FPU_accum_2:FPU_accum_1:FPU_accum_0
  51         Overflow flag:  ovfl_flag
  52  */
  53         .align 2,0
  54 FPU_accum_3:
  55         .long   0
  56 FPU_accum_2:
  57         .long   0
  58 FPU_accum_1:
  59         .long   0
  60 FPU_accum_0:
  61         .long   0
  62 FPU_result_1:
  63         .long   0
  64 FPU_result_2:
  65         .long   0
  66 FPU_ovfl_flag:
  67         .byte   0
  68 #endif NON_REENTRANT_FPU
  69 
  70 
  71 .text
  72 ENTRY(reg_u_div)
  73         pushl   %ebp
  74         movl    %esp,%ebp
  75 #ifndef NON_REENTRANT_FPU
  76         subl    $28,%esp
  77 #endif NON_REENTRANT_FPU
  78 
  79         pushl   %esi
  80         pushl   %edi
  81         pushl   %ebx
  82 
  83         movl    PARAM1,%esi     /* pointer to num */
  84         movl    PARAM2,%ebx     /* pointer to denom */
  85         movl    PARAM3,%edi     /* pointer to answer */
  86 
  87 #ifdef DENORM_OPERAND
  88         movl    EXP(%esi),%eax
  89         cmpl    EXP_UNDER,%eax
  90         jg      xOp1_not_denorm
  91 
  92         call    SYMBOL_NAME(denormal_operand)
  93         orl     %eax,%eax
  94         jnz     fpu_Arith_exit
  95 
  96 xOp1_not_denorm:
  97         movl    EXP(%ebx),%eax
  98         cmpl    EXP_UNDER,%eax
  99         jg      xOp2_not_denorm
 100 
 101         call    SYMBOL_NAME(denormal_operand)
 102         orl     %eax,%eax
 103         jnz     fpu_Arith_exit
 104 
 105 xOp2_not_denorm:
 106 #endif DENORM_OPERAND
 107 
 108 ENTRY(divide_kernel)
 109 #ifdef PARANOID
 110 /*      testl   $0x80000000, SIGH(%esi) // Dividend */
 111 /*      je      L_bugged */
 112         testl   $0x80000000, SIGH(%ebx) /* Divisor */
 113         je      L_bugged
 114 #endif PARANOID
 115 
 116 /* Check if the divisor can be treated as having just 32 bits */
 117         cmpl    $0,SIGL(%ebx)
 118         jnz     L_Full_Division /* Can't do a quick divide */
 119 
 120 /* We should be able to zip through the division here */
 121         movl    SIGH(%ebx),%ecx /* The divisor */
 122         movl    SIGH(%esi),%edx /* Dividend */
 123         movl    SIGL(%esi),%eax /* Dividend */
 124 
 125         cmpl    %ecx,%edx
 126         setaeb  FPU_ovfl_flag   /* Keep a record */
 127         jb      L_no_adjust
 128 
 129         subl    %ecx,%edx       /* Prevent the overflow */
 130 
 131 L_no_adjust:
 132         /* Divide the 64 bit number by the 32 bit denominator */
 133         divl    %ecx
 134         movl    %eax,FPU_result_2
 135 
 136         /* Work on the remainder of the first division */
 137         xorl    %eax,%eax
 138         divl    %ecx
 139         movl    %eax,FPU_result_1
 140 
 141         /* Work on the remainder of the 64 bit division */
 142         xorl    %eax,%eax
 143         divl    %ecx
 144 
 145         testb   $255,FPU_ovfl_flag      /* was the num > denom ? */
 146         je      L_no_overflow
 147 
 148         /* Do the shifting here */
 149         /* increase the exponent */
 150         incl    EXP(%edi)
 151 
 152         /* shift the mantissa right one bit */
 153         stc                     /* To set the ms bit */
 154         rcrl    FPU_result_2
 155         rcrl    FPU_result_1
 156         rcrl    %eax
 157 
 158 L_no_overflow:
 159         jmp     LRound_precision        /* Do the rounding as required */
 160 
 161 
 162 /*---------------------------------------------------------------------------+
 163  |  Divide:   Return  arg1/arg2 to arg3.                                     |
 164  |                                                                           |
 165  |  This routine does not use the exponents of arg1 and arg2, but does       |
 166  |  adjust the exponent of arg3.                                             |
 167  |                                                                           |
 168  |  The maximum returned value is (ignoring exponents)                       |
 169  |               .ffffffff ffffffff                                          |
 170  |               ------------------  =  1.ffffffff fffffffe                  |
 171  |               .80000000 00000000                                          |
 172  | and the minimum is                                                        |
 173  |               .80000000 00000000                                          |
 174  |               ------------------  =  .80000000 00000001   (rounded)       |
 175  |               .ffffffff ffffffff                                          |
 176  |                                                                           |
 177  +---------------------------------------------------------------------------*/
 178 
 179 
 180 L_Full_Division:
 181         /* Save extended dividend in local register */
 182         movl    SIGL(%esi),%eax
 183         movl    %eax,FPU_accum_2
 184         movl    SIGH(%esi),%eax
 185         movl    %eax,FPU_accum_3
 186         xorl    %eax,%eax
 187         movl    %eax,FPU_accum_1        /* zero the extension */
 188         movl    %eax,FPU_accum_0        /* zero the extension */
 189 
 190         movl    SIGL(%esi),%eax /* Get the current num */
 191         movl    SIGH(%esi),%edx
 192 
 193 /*----------------------------------------------------------------------*/
 194 /* Initialization done.
 195    Do the first 32 bits. */
 196 
 197         movb    $0,FPU_ovfl_flag
 198         cmpl    SIGH(%ebx),%edx /* Test for imminent overflow */
 199         jb      LLess_than_1
 200         ja      LGreater_than_1
 201 
 202         cmpl    SIGL(%ebx),%eax
 203         jb      LLess_than_1
 204 
 205 LGreater_than_1:
 206 /* The dividend is greater or equal, would cause overflow */
 207         setaeb  FPU_ovfl_flag           /* Keep a record */
 208 
 209         subl    SIGL(%ebx),%eax
 210         sbbl    SIGH(%ebx),%edx /* Prevent the overflow */
 211         movl    %eax,FPU_accum_2
 212         movl    %edx,FPU_accum_3
 213 
 214 LLess_than_1:
 215 /* At this point, we have a dividend < divisor, with a record of
 216    adjustment in FPU_ovfl_flag */
 217 
 218         /* We will divide by a number which is too large */
 219         movl    SIGH(%ebx),%ecx
 220         addl    $1,%ecx
 221         jnc     LFirst_div_not_1
 222 
 223         /* here we need to divide by 100000000h,
 224            i.e., no division at all.. */
 225         mov     %edx,%eax
 226         jmp     LFirst_div_done
 227 
 228 LFirst_div_not_1:
 229         divl    %ecx            /* Divide the numerator by the augmented
 230                                    denom ms dw */
 231 
 232 LFirst_div_done:
 233         movl    %eax,FPU_result_2       /* Put the result in the answer */
 234 
 235         mull    SIGH(%ebx)      /* mul by the ms dw of the denom */
 236 
 237         subl    %eax,FPU_accum_2        /* Subtract from the num local reg */
 238         sbbl    %edx,FPU_accum_3
 239 
 240         movl    FPU_result_2,%eax       /* Get the result back */
 241         mull    SIGL(%ebx)      /* now mul the ls dw of the denom */
 242 
 243         subl    %eax,FPU_accum_1        /* Subtract from the num local reg */
 244         sbbl    %edx,FPU_accum_2
 245         sbbl    $0,FPU_accum_3
 246         je      LDo_2nd_32_bits         /* Must check for non-zero result here */
 247 
 248 #ifdef PARANOID
 249         jb      L_bugged_1
 250 #endif PARANOID
 251 
 252         /* need to subtract another once of the denom */
 253         incl    FPU_result_2    /* Correct the answer */
 254 
 255         movl    SIGL(%ebx),%eax
 256         movl    SIGH(%ebx),%edx
 257         subl    %eax,FPU_accum_1        /* Subtract from the num local reg */
 258         sbbl    %edx,FPU_accum_2
 259 
 260 #ifdef PARANOID
 261         sbbl    $0,FPU_accum_3
 262         jne     L_bugged_1      /* Must check for non-zero result here */
 263 #endif PARANOID
 264 
 265 /*----------------------------------------------------------------------*/
 266 /* Half of the main problem is done, there is just a reduced numerator
 267    to handle now.
 268    Work with the second 32 bits, FPU_accum_0 not used from now on */
 269 LDo_2nd_32_bits:
 270         movl    FPU_accum_2,%edx        /* get the reduced num */
 271         movl    FPU_accum_1,%eax
 272 
 273         /* need to check for possible subsequent overflow */
 274         cmpl    SIGH(%ebx),%edx
 275         jb      LDo_2nd_div
 276         ja      LPrevent_2nd_overflow
 277 
 278         cmpl    SIGL(%ebx),%eax
 279         jb      LDo_2nd_div
 280 
 281 LPrevent_2nd_overflow:
 282 /* The numerator is greater or equal, would cause overflow */
 283         /* prevent overflow */
 284         subl    SIGL(%ebx),%eax
 285         sbbl    SIGH(%ebx),%edx
 286         movl    %edx,FPU_accum_2
 287         movl    %eax,FPU_accum_1
 288 
 289         incl    FPU_result_2    /* Reflect the subtraction in the answer */
 290 
 291 #ifdef PARANOID
 292         je      L_bugged_2      /* Can't bump the result to 1.0 */
 293 #endif PARANOID
 294 
 295 LDo_2nd_div:
 296         cmpl    $0,%ecx         /* augmented denom msw */
 297         jnz     LSecond_div_not_1
 298 
 299         /* %ecx == 0, we are dividing by 1.0 */
 300         mov     %edx,%eax
 301         jmp     LSecond_div_done
 302 
 303 LSecond_div_not_1:
 304         divl    %ecx            /* Divide the numerator by the denom ms dw */
 305 
 306 LSecond_div_done:
 307         movl    %eax,FPU_result_1       /* Put the result in the answer */
 308 
 309         mull    SIGH(%ebx)      /* mul by the ms dw of the denom */
 310 
 311         subl    %eax,FPU_accum_1        /* Subtract from the num local reg */
 312         sbbl    %edx,FPU_accum_2
 313 
 314 #ifdef PARANOID
 315         jc      L_bugged_2
 316 #endif PARANOID
 317 
 318         movl    FPU_result_1,%eax       /* Get the result back */
 319         mull    SIGL(%ebx)      /* now mul the ls dw of the denom */
 320 
 321         subl    %eax,FPU_accum_0        /* Subtract from the num local reg */
 322         sbbl    %edx,FPU_accum_1        /* Subtract from the num local reg */
 323         sbbl    $0,FPU_accum_2
 324 
 325 #ifdef PARANOID
 326         jc      L_bugged_2
 327 #endif PARANOID
 328 
 329         jz      LDo_3rd_32_bits
 330 
 331 #ifdef PARANOID
 332         cmpl    $1,FPU_accum_2
 333         jne     L_bugged_2
 334 #endif PARANOID
 335 
 336         /* need to subtract another once of the denom */
 337         movl    SIGL(%ebx),%eax
 338         movl    SIGH(%ebx),%edx
 339         subl    %eax,FPU_accum_0        /* Subtract from the num local reg */
 340         sbbl    %edx,FPU_accum_1
 341         sbbl    $0,FPU_accum_2
 342 
 343 #ifdef PARANOID
 344         jc      L_bugged_2
 345         jne     L_bugged_2
 346 #endif PARANOID
 347 
 348         addl    $1,FPU_result_1 /* Correct the answer */
 349         adcl    $0,FPU_result_2
 350 
 351 #ifdef PARANOID
 352         jc      L_bugged_2      /* Must check for non-zero result here */
 353 #endif PARANOID
 354 
 355 /*----------------------------------------------------------------------*/
 356 /* The division is essentially finished here, we just need to perform
 357    tidying operations.
 358    Deal with the 3rd 32 bits */
 359 LDo_3rd_32_bits:
 360         movl    FPU_accum_1,%edx                /* get the reduced num */
 361         movl    FPU_accum_0,%eax
 362 
 363         /* need to check for possible subsequent overflow */
 364         cmpl    SIGH(%ebx),%edx /* denom */
 365         jb      LRound_prep
 366         ja      LPrevent_3rd_overflow
 367 
 368         cmpl    SIGL(%ebx),%eax /* denom */
 369         jb      LRound_prep
 370 
 371 LPrevent_3rd_overflow:
 372         /* prevent overflow */
 373         subl    SIGL(%ebx),%eax
 374         sbbl    SIGH(%ebx),%edx
 375         movl    %edx,FPU_accum_1
 376         movl    %eax,FPU_accum_0
 377 
 378         addl    $1,FPU_result_1 /* Reflect the subtraction in the answer */
 379         adcl    $0,FPU_result_2
 380         jne     LRound_prep
 381         jnc     LRound_prep
 382 
 383         /* This is a tricky spot, there is an overflow of the answer */
 384         movb    $255,FPU_ovfl_flag              /* Overflow -> 1.000 */
 385 
 386 LRound_prep:
 387 /*
 388  * Prepare for rounding.
 389  * To test for rounding, we just need to compare 2*accum with the
 390  * denom.
 391  */
 392         movl    FPU_accum_0,%ecx
 393         movl    FPU_accum_1,%edx
 394         movl    %ecx,%eax
 395         orl     %edx,%eax
 396         jz      LRound_ovfl             /* The accumulator contains zero. */
 397 
 398         /* Multiply by 2 */
 399         clc
 400         rcll    $1,%ecx
 401         rcll    $1,%edx
 402         jc      LRound_large            /* No need to compare, denom smaller */
 403 
 404         subl    SIGL(%ebx),%ecx
 405         sbbl    SIGH(%ebx),%edx
 406         jnc     LRound_not_small
 407 
 408         movl    $0x70000000,%eax        /* Denom was larger */
 409         jmp     LRound_ovfl
 410 
 411 LRound_not_small:
 412         jnz     LRound_large
 413 
 414         movl    $0x80000000,%eax        /* Remainder was exactly 1/2 denom */
 415         jmp     LRound_ovfl
 416 
 417 LRound_large:
 418         movl    $0xff000000,%eax        /* Denom was smaller */
 419 
 420 LRound_ovfl:
 421 /* We are now ready to deal with rounding, but first we must get
 422    the bits properly aligned */
 423         testb   $255,FPU_ovfl_flag      /* was the num > denom ? */
 424         je      LRound_precision
 425 
 426         incl    EXP(%edi)
 427 
 428         /* shift the mantissa right one bit */
 429         stc                     /* Will set the ms bit */
 430         rcrl    FPU_result_2
 431         rcrl    FPU_result_1
 432         rcrl    %eax
 433 
 434 /* Round the result as required */
 435 LRound_precision:
 436         decl    EXP(%edi)       /* binary point between 1st & 2nd bits */
 437 
 438         movl    %eax,%edx
 439         movl    FPU_result_1,%ebx
 440         movl    FPU_result_2,%eax
 441         jmp     fpu_reg_round
 442 
 443 
 444 #ifdef PARANOID
 445 /* The logic is wrong if we got here */
 446 L_bugged:
 447         pushl   EX_INTERNAL|0x202
 448         call    EXCEPTION
 449         pop     %ebx
 450         jmp     L_exit
 451 
 452 L_bugged_1:
 453         pushl   EX_INTERNAL|0x203
 454         call    EXCEPTION
 455         pop     %ebx
 456         jmp     L_exit
 457 
 458 L_bugged_2:
 459         pushl   EX_INTERNAL|0x204
 460         call    EXCEPTION
 461         pop     %ebx
 462         jmp     L_exit
 463 
 464 L_exit:
 465         popl    %ebx
 466         popl    %edi
 467         popl    %esi
 468 
 469         leave
 470         ret
 471 #endif PARANOID

/* [previous][next][first][last][top][bottom][index][help] */