📄 extended.cpp
字号:
// Extended.cpp: implementation of the Extended class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Extended.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
const long double _tempHalf=0.5;
const long double _tempHalfHalf=0.25;
const long double _tempMaxTanhDomain = 5678.22249441322; // Ln(MaxExtended)/2
//在VC文档中指出类的成员函数默认调用约定为thiscall,this指针放在ecx寄存器
#define _reg_this ecx
bool operator> (const Extended &x0, const Extended &x1)
{
bool result;
__asm
{
mov eax,[x0]
fld _ExFloatPtr [eax]
mov eax,[x1]
fld _ExFloatPtr [eax]
fcompp
fstsw ax
sahf
jnb elsex
mov result,1
jmp exitx
elsex:
mov result,0
exitx:
}
return result;
}
void Extended::Add (const Extended & x)
{
_asm
{
mov eax,[x]
fld _ExFloatPtr [_reg_this]
fld _ExFloatPtr [eax]
faddp st(1),st
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Sub (const Extended & x)
{
_asm
{
mov eax,[x]
fld _ExFloatPtr [_reg_this]
fld _ExFloatPtr [eax]
fsubp st(1),st
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Mul (const Extended & x)
{
_asm
{
mov eax,[x]
fld _ExFloatPtr [_reg_this]
fld _ExFloatPtr [eax]
fmulp st(1),st
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Div (const Extended & x)
{
_asm
{
mov eax,[x]
fld _ExFloatPtr [_reg_this]
fld _ExFloatPtr [eax]
fdivp st(1),st
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Divi (const Extended & x) //整除
{
WORD t1,t2;
_asm
{
mov eax,[x]
fld _ExFloatPtr [_reg_this]
fld _ExFloatPtr [eax]
fdivp st(1),st
FNSTCW Word ptr t1 // 保存协处理器控制字,用来恢复
FNSTCW Word ptr t2 // 保存协处理器控制字,用来修改
FWAIT
OR Word ptr t2, 0F00H // 使RC场向零取整 改为 RC=11
FLDCW Word ptr t2 // 载入协处理器控制字,RC场已经修改
FRNDINT
FWAIT
FLDCW Word ptr t1 // 恢复协处理器控制字
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Mod (const Extended & x) //求余
{
__asm
{
mov eax,[x]
fld _ExFloatPtr [eax]
fld _ExFloatPtr [_reg_this]
fprem
fstp st(1)
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Round () //四舍五入取整(正好在中间时向偶数取整)
{
__asm
{
fld _ExFloatPtr [_reg_this]
frndint
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Floor () //向负无穷大取整 (不大于this的最大整数)
{
WORD t1,t2;
__asm
{
fld _ExFloatPtr [_reg_this]
FNSTCW Word ptr t1 // 保存协处理器控制字,用来恢复
FNSTCW Word ptr t2 // 保存协处理器控制字,用来修改
FWAIT
OR Word ptr t2, 0700H // 使RC场向负无穷大取整 //必须保证 RC=00 然后改为 RC=01
FLDCW Word ptr t2 // 载入协处理器控制字,RC场已经修改
FRNDINT
FWAIT
FLDCW Word ptr t1 // 恢复协处理器控制字
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Trunc () //截断取整 即 向零取整
{
WORD t1,t2;
__asm
{
fld _ExFloatPtr [_reg_this]
FNSTCW Word ptr t1 // 保存协处理器控制字,用来恢复
FNSTCW Word ptr t2 // 保存协处理器控制字,用来修改
FWAIT
OR Word ptr t2, 0f00H // 使RC场向零取整 改为 RC=11
FLDCW Word ptr t2 // 载入协处理器控制字,RC场已经修改
FRNDINT
FWAIT
FLDCW Word ptr t1 // 恢复协处理器控制字
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Ceil () //向无穷大取整
{
WORD t1,t2;
__asm
{
fld _ExFloatPtr [_reg_this]
FNSTCW Word ptr t1 // 保存协处理器控制字,用来恢复
FNSTCW Word ptr t2 // 保存协处理器控制字,用来修改
FWAIT
OR Word ptr t2,0B00H // 使RC场向负无穷大取整 //必须保证 RC=00 然后改为 RC=10
FLDCW Word ptr t2 // 载入协处理器控制字,RC场已经修改
FRNDINT
FWAIT
FLDCW Word ptr t1 // 恢复协处理器控制字
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Max (const Extended & x) //最大值
{
__asm
{
mov eax,[x]
fld _ExFloatPtr [_reg_this]
fld _ExFloatPtr [eax]
fst st(2)
fxch st(1)
fst st(3)
fcompp
fstsw ax
sahf
jnb else1
jmp exit1
else1:
fxch st(1)
fstp st(1)
exit1:
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Min (const Extended & x) //最小值
{
__asm
{
mov eax,[x]
fld _ExFloatPtr [_reg_this]
fld _ExFloatPtr [eax]
fst st(2)
fxch st(1)
fst st(3)
fcompp
fstsw ax
sahf
jb else1
jmp exit1
else1:
fxch st(1)
fstp st(1)
exit1:
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Power (const Extended & y) //指数 this^y
{
__asm
{
mov eax,[y]
fld _ExFloatPtr [eax]
fld _ExFloatPtr [_reg_this]
//if0:
fldz
fcomp
fstsw ax
sahf
jb else //(this>0)
//elseif1:
fxch st(1)
fldz
fcomp
fxch st(1)
fstsw ax
sahf
jne elseif2 //(y<>0)
fld1
fstp st(1)
fstp st(1)
jmp end
elseif2:
fldz
fcomp
fstsw ax
sahf
jne elseif3 //(this<>0)
fldz
fcomp st(2)
fstsw ax
sahf
jnb elseif3 //(y<=0)
fldz
fstp st(1)
fstp st(1)
jmp end
elseif3:
fld st(1)
fld1
fxch st(1)
fprem
fstp st(1)
fldz
fcomp
fxch st(1)
fstp st(1)
fstsw ax
sahf
jne else //(y mod 1<>0)
fchs
fldln2
fxch
fyl2x
fmul st,st(1)
FLDL2E //exp()...
FMULp st(1),st
FLD ST(0)
FRNDINT
FSUB ST(1), ST
FXCH ST(1)
F2XM1
FLD1
FADDp st(1),st
FSCALE
FSTP ST(1)
fxch st(1)
fld1
fadd st,st(0)
fxch st(1)
fprem
fstp st(1)
fldz
fcomp
fxch st(1)
fstp st(1)
fstsw ax
sahf
je end //(y mod 2=0)
fchs
jmp end
else:
fldln2
fxch
fyl2x
fmulp st(1),st
FLDL2E //exp()...
FMULp st(1),st
FLD ST(0)
FRNDINT
FSUB ST(1), ST
FXCH ST(1)
F2XM1
FLD1
FADDp st(1),st
FSCALE
FSTP ST(1)
end:
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Exp () //自然数e的次方
{
__asm
{
fld _ExFloatPtr [_reg_this]
FLDL2E //exp()...
FMULp st(1),st
FLD ST(0)
FRNDINT
FSUB ST(1), ST
FXCH ST(1)
F2XM1
FLD1
FADDp st(1),st
FSCALE
FSTP ST(1)
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Ln () //自然数e的对数
{
__asm
{
fld _ExFloatPtr [_reg_this]
fldln2
fxch
fyl2x
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Log2 () //2的对数
{
__asm
{
fld _ExFloatPtr [_reg_this]
fld1
fxch
fyl2x
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Log10 () //10的对数
{
__asm
{
fld _ExFloatPtr [_reg_this]
fldlg2
fxch
fyl2x
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Rev () //求倒数 1/this
{
__asm
{
fld _ExFloatPtr [_reg_this]
fld1
fdivrp st(1), st
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Chs () //求负 -this
{
__asm
{
fld _ExFloatPtr [_reg_this]
fchs
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Sqr () //平方
{
__asm
{
fld _ExFloatPtr [_reg_this]
fmul st,st(0)
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Sqr3 () //立方
{
__asm
{
fld _ExFloatPtr [_reg_this]
fld st(0)
fmul st,st(0)
fmulp st(1),st
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Sqr4 () //平方的平方
{
__asm
{
fld _ExFloatPtr [_reg_this]
fmul st,st(0)
fmul st,st(0)
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Sqrt () //开方
{
__asm
{
fld _ExFloatPtr [_reg_this]
fsqrt
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Abs () //绝对值
{
__asm
{
fld _ExFloatPtr [_reg_this]
fabs
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Sgn () //符号函数
{
__asm
{
fld _ExFloatPtr [_reg_this]
fldz
fcomp
fstsw ax
sahf
jnb else
fld1
fstp st(1)
jmp endx
else:
fldz
fcomp
fstsw ax
sahf
jbe else0
fld1
fchs
fstp st(1)
jmp endx
else0:
fldz
fstp st(1)
endx:
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Hypot (const Extended & x) //Sqrt(this*this+x*x)
{
__asm
{
mov eax,[x]
fld _ExFloatPtr [_reg_this]
fld _ExFloatPtr [eax]
fmul st,st(0)
fxch st(1)
fmul st,st(0)
faddp st(1),st
fsqrt
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Sin () //正弦
{
__asm
{
fld _ExFloatPtr [_reg_this]
fsin
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Cos () //余弦
{
__asm
{
fld _ExFloatPtr [_reg_this]
fcos
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::Tan () //正切
{
__asm
{
fld _ExFloatPtr [_reg_this]
fptan
fstp st(0)
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::SinCos (Extended * xSin,Extended * xCos) const//同时求出正弦和余弦,并通过参数传出,this值不变,
//该函数的计算时间只相当于求一次Sin(或Cos)的时间!!!
{
__asm
{
fld _ExFloatPtr [_reg_this]
fsincos
mov eax,[xCos]
fstp _ExFloatPtr [eax]
mov eax,[xSin]
fstp _ExFloatPtr [eax]
}
}
void Extended::ArcSin () //反正弦
{
__asm
{
fld _ExFloatPtr [_reg_this]
FST st(1)
Fmul st,st(1)
fld1
Fsubr
Fsqrt
Fpatan
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::ArcCos () //反余弦
{
__asm
{
fld _ExFloatPtr [_reg_this]
FST st(1)
Fmul st,st(0)
fld1
Fsubr
Fsqrt
Fxch
Fpatan
fstp _ExFloatPtr [_reg_this]
}
}
void Extended::ArcTan () //反正切
{
__asm
{
fld _ExFloatPtr [_reg_this]
Fld1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -