Calculate the source program of the circumferential rate with a MACHIN formula

xiaoxiao2021-03-06  65

Calculate the source program of the circumferential rate with a MACHIN formula

/ * Program to Compute Pi, by jason chen, may 1999

**

** Open VC IDE, New A Win32 Console Application Project,

** Add this file to the project and build it.

**

** HomePage: http://www.jason314.com

** email: jason@szonline.net

* /

#include

#include

#include

#include

#include

#include

#include

#include

INT * ARCTG5, * ARCTG239, * TMP;

Int Declen, Binlen;

INT X, N, SIGN, NONZEROPTR;

Int Step;

CHAR FN_STATUS [] = "~ pi_sts .___";

CHAR FN_ARCTG5 [] = "~ ATG5 .___";

Char fn_ARCTG239 [] = "~ ATG239 .___";

CHAR FN_TMP [] = "~ TMP .___";

Time_t TotalTime, Time1, Time2;

Void __cdecl firstdiv (int * arctG_Array)

{

__ASM {

Mov ESI, ArctG_Array

Mov DWORD PTR [ESI], 1

XOR EDX, EDX

Mov EBX, X

Mov ECX, Binlen

FD1: MOV EAX, [ESI]

Div EBX

MOV [ESI], EAX

Add ESI, 4

Loop fd1

Mov ESI, ArctG_Array

Mov EDI, TMP

Mov ECX, Binlen

Pushf

CLD

REP MOVSD

POPF

}

}

Void __cdecl arctgx (int * arctG_Array)

{

Int nonzerobyteptr;

Int key;

File * fp;

For (; Nonzeroptr

Nonzerobyteptr = Nonzeroptr * 4;

IF (_kbhit ()) {

Key = _Getch ();

IF (key == 0 || key == 0xe0) _Getch ();

IF (Key == 'P') {

Printf ("Swap Data To Disk ... / N");

IF (x == 25)

Step = 1;

Else

Step = 2;

Time (& TIME2);

TOTALTIME = Time2 - Time1;

IF (FP = FOPEN (FN_STATUS, "WT")) == null) {

Printf ("CREATE FILE% S Error !! / N", FN_STATUS;

exit (0);

}

FPRINTF (FP, "% D% D% D% D% D% D% D% D / N", Step, Declen, Binlen, N, Sign, Nonzeroptr, TotalTime;

Fclose (fp);

IF ((fp = fopen (fn_ARCTG5, "WB") == null) {

Printf ("CREATE FILE% S Error !! / N", FN_ARCTG5);

exit (0);

}

IF (FWRITE (ArctG5, 4, Binlen, FP)! = (unsigned) binlen) {

Printf ("Write File% S Error !! / N", FN_ARCTG5);

exit (0);

}

Fclose (fp);

IF ((fp = fopen (fn_ARCTG239, "WB")) == null) {

Printf ("CREATE FILE% S Error !! / N", FN_ARCTG239);

exit (0);

}

IF (FWRITE (ArctG239, 4, Binlen, FP)! = (unsigned) binlen) {

Printf ("Write File% S Error !! / N", FN_ARCTG239);

exit (0);

}

Fclose (fp);

IF ((fp = fopen (FN_TMP, "WB")) == null) {

Printf ("CREATE FILE% S Error !! / N", FN_TMP);

exit (0);

}

IF (FWRITE (TMP, 4, Binlen, FP)! = (unsigned) binlen) {

Printf ("Write File% S Error !! / N", FN_TMP);

exit (0);

}

Fclose (fp);

Printf ("exit./N");

exit (0);

}

}

__ASM {

MOV ESI, TMP

Add ESI, Nonzerobyteptr

XOR EDX, EDX

Mov EBX, X

Mov ECX, Binlen

SUB ECX, Nonzeroptr

ArctG1: MOV Eax, [ESI]

Div EBX

MOV [ESI], EAX

Add ESI, 4

Loop ArctG1

CMP SIGN, 1

JNE SUB_

MOV ESI, TMP

Add ESI, Nonzerobyteptr

Mov Edi, ArctG_Array

Add Edi, Nonzerobyteptr

XOR EDX, EDX

MOV EBX, N

Mov ECX, Binlen

SUB ECX, Nonzeroptr

Add_1: MOV Eax, [ESI]

Div EBX

Add [EDI], EAX

ADC DWORD PTR [EDI-4], 0

JNC Add_3

Push EDI

SUB EDI, 4

Add_2: Sub EDI, 4

Add DWORD PTR [EDI], 1

JC Add_2

POP EDI

Add_3: Add ESI, 4

Add Edi, 4

LOOP Add_1

JMP adj_var

Sub_: MOV ESI, TMP

Add ESI, Nonzerobyteptr

Mov Edi, ArctG_Array

Add Edi, Nonzerobyteptr

XOR EDX, EDX

MOV EBX, N

Mov ECX, Binlen

SUB ECX, Nonzeroptr

Sub_1: Mov EAX, [ESI] DIV EBX

SUB [EDI], EAX

SBB DWORD PTR [EDI-4], 0

JNC SUB_3

Push EDI

SUB EDI, 4

SUB_2: SUB EDI, 4

Sub DWORD PTR [EDI], 1

JC SUB_2

POP EDI

SUB_3: Add ESI, 4

Add Edi, 4

Loop Sub_1

Adj_var: add n, 2

NEG SIGN

MOV ESI, TMP

Add ESI, Nonzerobyteptr

CMP DWORD PTR [ESI], 0

JNE Adj_var_ok

Inc Nonzeroptr

Adj_var_ok:

}

}

}

Void __cdecl Mul_Array (int * array, int multiplicator)

{

__ASM {

Mov ESI, BINLEN

Dec ESI

SHL ESI, 2

Add ESI, Array

Mov ECX, Binlen

MOV EBX, Multiplicator

XOR EDI, EDI

Mul1: MOV Eax, [ESI]

Mul EBX

Add Eax, EDI

ADC EDX, 0

MOV [ESI], EAX

MOV EDI, EDX

SUB ESI, 4

Loop Mul1

MOV [ESI], EDX

}

}

Void __cdecl Sub2Array (int * array1, int * array2)

{

__ASM {

Mov ESI, Array1

Mov Edi, Array2

Mov ECX, Binlen

Dec ECX

SUB1: MOV Eax, [EDI ECX * 4]

SBB [ESI ECX * 4], EAX

Loop Sub1

}

}

Void main (void)

{

StructTM * TS;

File * pi, * fp;

INT I, TAIL, P10TAIL;

Printf ("/ Nprogram to Compute Pi, By Jason Chen, May 1999./N");

Printf ("DEC Length Time (H: M: S) / N");

Printf ("20000 00: 00: 07 / N");

Printf ("100000 00: 02: 54 / N");

Printf ("(" (Running on PII-233, 128MB, WIN98 DOS MODE) / N ");

Printf ("Homepage: http://www.jason314.com/n");

Printf ("email: jason@szonline.net/n/n");

IF ((fp = fopen (FN_STATUS, "RT")) == NULL) {

Printf ("Decimal Length =");

Scanf ("% d", & declen);

IF (Declen <100) Declen = 100;

Binlen = (int) (Declen / Log10 (2) / 32) 2;

Step = 0;

TOTALTIME = 0;

}

Else {

Printf ("Reading Previous Data ... / N"; fscanf (fp, "% D% D% D% D% D% D% D",

& Step, & Declen, & Binlen, & N, & Sign, & Nonzeroptr, & TotalTime

Fclose (fp);

IF (Step * Declen * binlen * n * sign * nonzeroptr * Totaltime == 0) {

Printf ("File% S Error !! / Nexit! / N", FN_STATUS);

exit (0);

}

}

ArctG5 = Calloc (Binlen, 4);

ArctG239 = Calloc (Binlen, 4);

TMP = Calloc (Binlen, 4);

IF (ArctG5 == Null || ArctG239 == Null || TMP == NULL) {

Printf ("Not Enough Memory !! / N);

exit (0);

}

IF (step == 0) {

MEMSET (ArctG5, 0, Binlen * 4);

MEMSET (ArctG239, 0, Binlen * 4);

MEMSET (TMP, 0, Binlen * 4);

}

Else {

IF (fp = fopen (fn_ARCTG5, "RB")) == null) {

Printf ("Open File% S Error !! / N", FN_ARCTG5);

exit (0);

}

IF (FREAD (ArctG5, 4, Binlen, FP)! = (unsigned) binlen) {

Printf ("File% S Error !! / N", FN_ARCTG5);

exit (0);

}

Fclose (fp);

IF ((fp = fopen (fp ")) == null) {

Printf ("Open File% S Error !! / N", FN_ARCTG239);

exit (0);

}

IF (FREAD (ArctG239, 4, Binlen, FP)! = (unsigned) binlen) {

Printf ("File% S Error !! / N", FN_ARCTG239);

exit (0);

}

Fclose (fp);

IF ((fp = fopen (FN_TMP, "RB")) == null) {

Printf ("Open File% S Error !! / N", FN_TMP);

exit (0);

}

IF (Fread (TMP, 4, Binlen, FP)! = (unsigned) binlen) {

Printf ("File% S Error !! / N", FN_TMP);

exit (0);

}

Fclose (fp);

}

Printf ("Working ... / N");

Printf ("Press 'P' to Pause & Exit / N");

Time (& TIME1);

IF (step == 0) {

X = 5;

Firstdiv (ArctG5);

x = x * x;

n = 3;

SIGN = -1;

Nonzeroptr = 1;

ArctGX (ArctG5);

}

Else IF (step == 1) {x = 5 * 5;

ArctGX (ArctG5);

}

IF (Step == 0 || Step == 1) {

X = 239;

Firstdiv (ArctG239);

x = x * x;

n = 3;

SIGN = -1;

Nonzeroptr = 1;

Arctgx (arctG239);

}

Else {

X = 239 * 239;

Arctgx (arctG239);

}

Mul_Array (ArctG5, 16);

Mul_Array (ArctG239, 4);

Sub2aray (ArctG5, ArctG239);

IF ((pi = fopen ("pi.txt", "wt"))) == null) {

Printf ("CREATE FILE PI.TXT Error !! / N");

exit (0);

}

Printf ("Writing Result to File: pi.txt ... / n");

FPRINTF (Pi, "% D", ArctG5 [0]);

For (i = 1; i <= DECLEN / 9; i ) {

ArctG5 [0] = 0;

Mul_Array (ArctG5, 1000000000);

FPRINTF (Pi, "% 09D", ArctG5 [0]);

}

Tail = Declen% 9;

P10TAIL = 1;

For (i = 1; i <= tail; i ) p10tail * = 10;

ArctG5 [0] = 0;

Mul_Array (ArctG5, p10tail);

FPRINTF (Pi, "% 0 * D", TAIL, ArctG5 [0]);

Fclose (pi);

Time (& TIME2);

TOTALTIME = Time2 - Time1;

Printf ("DONE !! / N");

Ts = gmtime (& TOTALTIME);

TS-> TM_MON -;

TS-> TM_MDAY = TS-> TM_MDAY - 1 TS-> TM_MON * 31;

Printf ("TIME:");

IF (TS-> TM_MDAY> 0) Printf ("% D Day (s)", TS-> TM_MDAY;

Printf ("% 02D:% 02D:% 02D / N", TS-> TM_HOUR, TS-> TM_MIN, TS-> TM_SEC);

IF (_Unlink (fn_status) == 0) {

_unlink (fn_ARCTG5);

_unlink (fn_ARCTG239);

_unlink (fn_tmp);

}

}

转载请注明原文地址:https://www.9cbs.com/read-119000.html

New Post(0)