⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vmlargeintmath.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************/
/*                              SOURCE FILE                                  */
/*****************************************************************************/
/*
       $Archive:   $

      $Revision:   $
          $Date:   $
        $Author:   $

    Description:   Implementation of a set of tools for mathematical functions
                   on large integers

                      TOOL And XML FORMS License
                      ==========================

                      Except where otherwise noted, all of the documentation 
                      and software included in the TOOL package is 
                      copyrighted by Michael Swartzendruber.

                      Copyright (C) 2005 Michael John Swartzendruber. 
                      All rights reserved.

                      Access to this code, whether intentional or accidental,
                      does NOT IMPLY any transfer of rights.

                      This software is provided "as-is," without any express 
                      or implied warranty. In no event shall the author be held
                      liable for any damages arising from the use of this software.

                      Permission is granted to anyone to use this software for 
                      any purpose, including commercial applications, and to 
                      alter and redistribute it, provided that the following 
                      conditions are met:

                      1. All redistributions of source code files must retain 
                         all copyright notices that are currently in place, 
                         and this list of conditions without modification.

                      2. The origin of this software must not be misrepresented;
                         you must not claim that you wrote the original software.

                      3. If you use this software in another product, an acknowledgment
                         in the product documentation would be appreciated but is
                         not required.

                      4. Modified versions in source or binary form must be plainly 
                         marked as such, and must not be misrepresented as being 
                         the original software.
*/
static char OBJECT_ID[] = "$Revision:   $ : $Date:   $";
/*****************************************************************************/

#pragma warning( disable : 4731 )

#include "VMLargeIntMath.h"


LARGE_INTEGER VMLargeIntZero = {0,0};



/*****************************************************************************/
/*

     FUNCTION NAME:  VMLargeIntAssign

       DESCRIPTION:  assigns the given ulong to a large integer

             INPUT:  ulValue - the value to assign
            OUTPUT:  none

           RETURNS:  LARGE_INTEGER equivalent for the value given
*/
LARGE_INTEGER VMLargeIntAssign( ULONG ulValue )
{
  LARGE_INTEGER xResult;
  xResult.LowPart  = ulValue;
  xResult.HighPart = 0;
  return( xResult );
}
/* End of function "VMLargeIntAssign"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMLargeIntShiftRight

       DESCRIPTION:  shifts the large integer right by the given amount

             INPUT:  xValue - the value to shift
                     chShift - the places to shift
            OUTPUT:  

           RETURNS:  LARGE_INTEGER for the shifted value
*/
LARGE_INTEGER VMLargeIntShiftRight( LARGE_INTEGER xValue, CCHAR chShift )
{
  LARGE_INTEGER xResult;

  __asm 
  {
    mov         ecx,dword ptr [chShift]
    and         ecx,3Fh
    cmp         ecx,20h
    jae         label1

    mov         eax,dword ptr [xValue].LowPart
    mov         edx,dword ptr [xValue].HighPart
    shrd        eax,edx,cl
    shr         edx,cl
    jmp        label2

    label1:

    mov         eax,dword ptr [xValue].HighPart
    xor         edx,edx
    shr         eax,cl

    label2:

    mov         [xResult].LowPart,eax
    mov         [xResult].HighPart,edx
  }

  return( xResult );
}
/* End of function "VMLargeIntShiftRight"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMLargeIntMultiply

       DESCRIPTION:  multiply two large integers together

             INPUT:  ulValueOne - the first value
                     ulValueTwo - the second value
            OUTPUT:  

           RETURNS:  LARGE_INTEGER  result of the multiplication
*/
LARGE_INTEGER VMLargeIntMultiply(ULONG ulValueOne, ULONG ulValueTwo )
{
  LARGE_INTEGER xResult;

  __asm 
  {
    mov         eax,dword ptr [ulValueOne]
    mul         dword ptr [ulValueTwo]
    mov         [xResult].LowPart,eax
    mov         [xResult].HighPart,edx
  }

  return( xResult );
}
/* End of function "VMLargeIntMultiply"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMLargeIntMultiply

       DESCRIPTION:  multiply two large integers together

             INPUT:  lValueOne - the first value
                     lValueTwo - the second value
            OUTPUT:  

           RETURNS:  LARGE_INTEGER  result of the multiplication
*/
LARGE_INTEGER VMLargeIntMultiply( LONG lValueOne , LONG lValueTwo )
{
  LARGE_INTEGER xResult;

  __asm {
    mov         eax,dword ptr [lValueOne]
    imul        dword ptr [lValueTwo]
    mov         [xResult].LowPart,eax
    mov         [xResult].HighPart,edx
  }

  return( xResult );
}
/* End of function "VMLargeIntMultiply"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMLargeIntMultiply

       DESCRIPTION:  multiply two large integers together

             INPUT:  xValueOne - the first value
                     lValueTwo - the second value
            OUTPUT:  

           RETURNS:  LARGE_INTEGER  result of the multiplication
*/
LARGE_INTEGER VMLargeIntMultiply( LARGE_INTEGER xValueOne, LONG lValueTwo )
{
  LARGE_INTEGER xResult;

  __asm 
  {
    mov eax,dword ptr xValueOne
    mov edx,dword ptr lValueTwo

    push        esi

    mov         esi,dword ptr [ebp+10h]
    xor         esi,dword ptr [ebp+0Ch]
    test        dword ptr [ebp+0Ch],80000000h
    je          label1

    neg         dword ptr [ebp+0Ch]
    neg         dword ptr [ebp+8]
    sbb         dword ptr [ebp+0Ch],0

    label1:

    test        dword ptr [ebp+10h],80000000h
    je          label2

    neg         dword ptr [ebp+10h]
    
    label2:

    mov         eax,dword ptr [ebp+10h]
    mul         dword ptr [ebp+8]

    mov         ebx,edx
    mov         ecx,eax
    mov         eax,dword ptr [ebp+10h]
    mul         dword ptr [ebp+0Ch]
    add         eax,ebx
    test        esi,80000000h
    je          label3

    neg         eax
    neg         ecx
    sbb         eax,0

    label3:

    mov         [xResult].HighPart,eax
    mov         [xResult].LowPart,ecx

    pop         esi
  }

  return( xResult );
}
/* End of function "VMLargeIntMultiply"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMLargeIntDivide

       DESCRIPTION:  perform a large integer divide operation

             INPUT:  xDividend - value to divide
                     xDivisor - value to divide by
                     pxRemainder - pointer to a remainder value
            OUTPUT:  

           RETURNS:  LARGE_INTEGER - the quotient 
*/
LARGE_INTEGER VMLargeIntDivide(LARGE_INTEGER xDividend, LARGE_INTEGER xDivisor, LARGE_INTEGER* pxRemainder)
{
  LARGE_INTEGER xResult;
  DWORD dwTempOne;
  DWORD dwTempTwo;
  DWORD dwCounter;

  __asm
  {
    mov         dword ptr [dwCounter],64
    mov         dword ptr [dwTempOne],0
    mov         dword ptr [dwTempTwo],0

    mov         eax,dword ptr [xDividend].LowPart
    mov         ecx,dword ptr [xDividend].HighPart
    mov         dword ptr [xResult].LowPart,eax
    mov         dword ptr [xResult].HighPart,ecx

    label1:

    mov         eax,dword ptr [dwTempTwo]
    add         eax,eax
    mov         ecx,dword ptr [dwTempOne]
    shr         ecx,1Fh
    or          eax,ecx
    mov         dword ptr [dwTempTwo],eax

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -