//==========================================================================;
//
//  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
//  PURPOSE.
//
//  Copyright (c) 1992, 1993  Microsoft Corporation.  All Rights Reserved.
//
//--------------------------------------------------------------------------;
//
//  muldiv32.c
//
//  Description:
//      math routines for 32 bit signed and unsiged numbers.
//
//      MulDiv32(a,b,c) = (a * b) / c         (round down, signed)
//
//      MulDivRD(a,b,c) = (a * b) / c         (round down, unsigned)
//      MulDivRN(a,b,c) = (a * b + c/2) / c   (round nearest, unsigned)
//      MulDivRU(a,b,c) = (a * b + c-1) / c   (round up, unsigned)
//
//==========================================================================;

#include <windows.h>
#include "muldiv32.h"

#if !defined(WIN32) && !defined(_WIN32)

#pragma warning(disable:4035 4704)

//--------------------------------------------------------------------------;
//  
//  LONG MulDiv32(a,b,c)    = (a * b) / c
//  
//--------------------------------------------------------------------------;

LONG FAR PASCAL MulDiv32(LONG a,LONG b,LONG c)
{
    _asm _emit 0x66 _asm    mov     ax,word ptr a   //  mov  eax, a
    _asm _emit 0x66 _asm    mov     bx,word ptr b   //  mov  ebx, b
    _asm _emit 0x66 _asm    mov     cx,word ptr c   //  mov  ecx, c
    _asm _emit 0x66 _asm    imul    bx              //  imul ebx
    _asm _emit 0x66 _asm    idiv    cx              //  idiv ecx
    _asm _emit 0x66                                 //  shld edx, eax, 16
    _asm _emit 0x0F
    _asm _emit 0xA4
    _asm _emit 0xC2
    _asm _emit 0x10

} // MulDiv32()

//--------------------------------------------------------------------------;
//  
//  DWORD MulDivRN(a,b,c)   = (a * b + c/2) / c
//  
//--------------------------------------------------------------------------;

DWORD FAR PASCAL MulDivRN(DWORD a,DWORD b,DWORD c)
{
    _asm _emit 0x66 _asm    mov     ax,word ptr a   //  mov  eax, a
    _asm _emit 0x66 _asm    mov     bx,word ptr b   //  mov  ebx, b
    _asm _emit 0x66 _asm    mov     cx,word ptr c   //  mov  ecx, c
    _asm _emit 0x66 _asm    mul     bx              //  mul  ebx
    _asm _emit 0x66 _asm    mov     bx,cx           //  mov  ebx,ecx
    _asm _emit 0x66 _asm    shr     bx,1            //  sar  ebx,1
    _asm _emit 0x66 _asm    add     ax,bx           //  add  eax,ebx
    _asm _emit 0x66 _asm    adc     dx,0            //  adc  edx,0
    _asm _emit 0x66 _asm    div     cx              //  div  ecx
    _asm _emit 0x66                                 //  shld edx, eax, 16
    _asm _emit 0x0F
    _asm _emit 0xA4
    _asm _emit 0xC2
    _asm _emit 0x10

} // MulDiv32()

//--------------------------------------------------------------------------;
//  
//  DWORD MulDivRU(a,b,c)   = (a * b + c-1) / c
//  
//--------------------------------------------------------------------------;

DWORD FAR PASCAL MulDivRU(DWORD a,DWORD b,DWORD c)
{
    _asm _emit 0x66 _asm    mov     ax,word ptr a   //  mov  eax, a
    _asm _emit 0x66 _asm    mov     bx,word ptr b   //  mov  ebx, b
    _asm _emit 0x66 _asm    mov     cx,word ptr c   //  mov  ecx, c
    _asm _emit 0x66 _asm    mul     bx              //  mul  ebx
    _asm _emit 0x66 _asm    mov     bx,cx           //  mov  ebx,ecx
    _asm _emit 0x66 _asm    dec     bx              //  dec  ebx
    _asm _emit 0x66 _asm    add     ax,bx           //  add  eax,ebx
    _asm _emit 0x66 _asm    adc     dx,0            //  adc  edx,0
    _asm _emit 0x66 _asm    div     cx              //  div  ecx
    _asm _emit 0x66                                 //  shld edx, eax, 16
    _asm _emit 0x0F
    _asm _emit 0xA4
    _asm _emit 0xC2
    _asm _emit 0x10

} // MulDivRU32()

//--------------------------------------------------------------------------;
//  
//  DWORD MulDivRD(a,b,c)   = (a * b) / c
//  
//--------------------------------------------------------------------------;

DWORD FAR PASCAL MulDivRD(DWORD a,DWORD b,DWORD c)
{
    _asm _emit 0x66 _asm    mov     ax,word ptr a   //  mov  eax, a
    _asm _emit 0x66 _asm    mov     bx,word ptr b   //  mov  ebx, b
    _asm _emit 0x66 _asm    mov     cx,word ptr c   //  mov  ecx, c
    _asm _emit 0x66 _asm    mul     bx              //  mul  ebx
    _asm _emit 0x66 _asm    div     cx              //  div  ecx
    _asm _emit 0x66                                 //  shld edx, eax, 16
    _asm _emit 0x0F
    _asm _emit 0xA4
    _asm _emit 0xC2
    _asm _emit 0x10

} // MulDivRD32()

#pragma warning(default:4035 4704)

#else   // WIN32

#include <largeint.h>

//--------------------------------------------------------------------------;
//  
//  LONG MulDiv32(a,b,c)    = (a * b) / c
//  
//--------------------------------------------------------------------------;

LONG FAR PASCAL MulDiv32(LONG a,LONG b,LONG c)
{
    LARGE_INTEGER l, lRemain;

    l = LargeIntegerDivide(
        EnlargedIntegerMultiply(a,b),
        ConvertLongToLargeInteger(c),
        &lRemain);

    return l.LowPart;

} // MulDiv32()

//--------------------------------------------------------------------------;
//  
//  DWORD MulDivRD(a,b,c)   = (a * b) / c
//  
//--------------------------------------------------------------------------;

DWORD FAR PASCAL MulDivRD(DWORD a,DWORD b,DWORD c)
{
    return ExtendedLargeIntegerDivide(
        EnlargedUnsignedMultiply(a,b), c, &a).LowPart;

} // MulDivRD()

//--------------------------------------------------------------------------;
//  
//  DWORD MulDivRU(a,b,c)   = (a * b + c-1) / c
//  
//--------------------------------------------------------------------------;

DWORD FAR PASCAL MulDivRU(DWORD a,DWORD b,DWORD c)
{
    return ExtendedLargeIntegerDivide(
        LargeIntegerAdd(
            EnlargedUnsignedMultiply(a,b),
            ConvertUlongToLargeInteger(c-1)),
        c,&a).LowPart;

} // MulDivRU()

#if 0 // we use Win32 GDI MulDiv function, not this.
//--------------------------------------------------------------------------;
//  
//  DWORD MulDivRN(a,b,c)   = (a * b + c/2) / c
//  
//--------------------------------------------------------------------------;

DWORD FAR PASCAL MulDivRN(DWORD a,DWORD b,DWORD c)
{
    return ExtendedLargeIntegerDivide(
        LargeIntegerAdd(
            EnlargedUnsignedMultiply(a,b),
            ConvertUlongToLargeInteger(c/2)),
        c,&a).LowPart;

} // MulDivRN()

#endif

#endif  // WIN32
