- /* snmanage.h by K.Tsuru */
- /**************************************************************************
- SNManager class
- Provides how to treat error, management of various sizes and how to use FFT
- multiplication.
- ***************************************************************************/
- #ifndef SNMANAGER_H
- #define SNMANAGER_H
- // sfft.h
- #ifndef S_FFT_H
- #include "sfft.h"
- #endif // S_FFT_H
-
- /**********************************
- various sizes of array
- maxArraySize for SLong class
- 14 Sep, 2000
- Change the value "defaultFFTMinSize"
- from 64 to 16.
- ***********************************/
- const uint defaultMaxArraySize = uint( maxSizeOfMemoryBlock/(ulong)sizeof(fType) );
- const uint minArraySize = 4u; // minimum size of array
- const uint defaultEffFig = 61u; // default effective figures of SDouble in DRADIX
- const uint defaultHidden = 2u; // default hidden figures
- const uint defaultFFTMinSize = 16u; // default threshold size of FFT multiplication
-
- // OutOfMemory() is registered by "set_new_handler()".
- void OutOfMemory();
-
- class SNManager {
- friend class RealSize;
- friend class SNumber; // to access "objectCounter"
- public:
- // type of number SNumber::type;
- // lowest 1bit : integer, 2nd bit : real, 3rd bit : BRADIX type
- // UNKNOWN is used in StringToNumber class.
- // (DETECT_IR & type) gives a value DEC_INT or REAL
- enum NumberType { MIN_SIZE = 0, DEC_INT = 1, REAL = 2, DEC_RDX = 3, // changed "INTEGER" to "DEC_INT" since version 2.30
- BIN_RDX = 4, BIN_INT = 5, BIN_DEC = 6, NUMBER_TYPE = 7, UNKNOWN = 8};
- enum { DETECT_IR = 3 }; // = (DEC_INT | REAL)
- // error ID
- enum SNErrorFlag {
- NO_ERR = 0, OVERFLOW_ERR, UNDERFLOW_ERR, DIVIDED_BY_ZERO, SIGN_ERR,
- NOT_CONVERGE, DOMAIN_ERR, OUT_OF_RANGE, RADIX_ERR, UNDEC_VALUE,
- SET_EFF_FIG, SYNTAX_ERR, FFT_ERR, TOO_LONG, TOO_LARGE_EXP,
- TLOSS_ERR, VERIFY, FATAL
- };
- private:
- static long objectCounter; // counter for objects. initial value is -1
- static uint effFigures; // effective figure reserved by user
- static uint hidden; // hidden figures
- static uint maxArraySize;
- static uint maxSize[NUMBER_TYPE];// maximum size for various types of number
- static FILE* stream; // output stream. default is "stdout".
- static ostream& sn_ost; // class std::output stream. default is "std::cout".
- // for error statements
- static SNErrorFlag errorFlag;
- static char* place; // place where an error occured.
- static void ErrorHandler(int id);// id > 0 fatal error, id <= 0 warning. 001
- static void SetMaxSize();// Provides the maxmum sizes of various types. 002
- static bool verify; // verify or not in the routine of division, etc.
- static bool showMessage; // Show a message "Calculating Pi() now." etc. or not.
- static bool fftVerify; // verify or not in the rounding off FFT multiplication
- static uint fftMinSize; // threshold size of FFT multiplication
- static bool fftUse; // use FFT multiplication or not
- protected:
- static uint fftArraySize; // size of FFT(=0 when no use)
- static long fftUsedTimes; // the number of times which "LLMultFFT()" was called
- static long KaratsubaHHMultUsedTimes; // the number of times which Karatsuba's multiplication "KHHMult()" was called
- static uint SNEffFig(NumberType tp);// accessible in derived class. 003
- /************************************************************************
- Permit reduction of maximum size (ENABLE) or not (DISABLE).
- In the functions which uses Newton's method, reducing temporarily the
- effective figures this value has to be set by DISABLE.
- *************************************************************************/
- static bool reduceMaxSize;
- /*****************************************************************************
- If you prefer to speed rather than precision, set this value ON by a statement
- PreferSpeed(ON);.
- default : preferSpeed = false.
- An object of "RealSize class" can also call this function.
- ******************************************************************************/
- private:
- static bool preferSpeed;
- public:
- static void PreferSpeed(bool p){ preferSpeed = p; }
- static bool PreferSpeed() { return preferSpeed; }
- /*
- For E(), Pi(), Log10() show message "Evaluating XXX() now..... Finished."
- Use to count its called times etc. for debug.
- */
- static bool ShowMessage() { return showMessage; }
- static void ShowMessage(int sw) { showMessage = sw ? ON : OFF; }
- static void ShowMessage(const char* msg);
- /*********************
- output stream using structure FILE.
- It isn't recommended to use since SN library version 2.30.
- Prease use <iostream> STL.
- **********************/
- /****************************************************************************
- Provides a method which change the output stream.
- fname : file name
- If fname == NULL, "fclose()" and reset stream = stdout.
- If the file could not be opened, close program by "exit()" function.
- 1. When ovw == NO(default), the program ask whether you want overwrite on the
- existing file or not.If you chose NO, ask whether you would continue the program.
- 2. If ovw == YES, the program overwite without warning. This provides a method
- to keep a data on a disk which causes an abnormal termination by an error.
- 3. It returns "FILE* stream".
- *****************************************************************************/
- static FILE* OutPutFile(const char* fname, bool ovw = false); // 004
- void CloseFile();
- // output a literal s to present stream. In an optional format use two steps
- //1. sprintf(buff,fmt,...); 2. FPuts(buff);
- static int FPuts(const char* s){ return fprintf(stream, "%s", s); }
- // output a character c to present stream
- static int FPutc(int c){ return putc(c, stream); }
- /********************************************************************
- Provides a method which changes the output stream into "stderr" etc.
- If ostr == NULL, reset stream = stdout.
- ********************************************************************/
- static void OutPutStream(FILE* ostr) {
- stream = (ostr != NULL) ? ostr : stdout;
- }
- static FILE* FileStream() { return stream; }
- static SNErrorFlag SNError(){
- SNErrorFlag err = errorFlag;
- errorFlag = NO_ERR; // reset
- return err;
- }
- /****************************************************************************
- Open a file "fname" which includes text data of number(s) and read onto the
- static memory "readBuff" and return a constant pointer of "readBuff".
- If you would like to receive as a literal
- const char* num;
- num = ReadNumber(fname);
- For a direct substitution
- SLong a;
- a = ReadNumber(fname);
- The memory is allocated on a static area, then previous data will be replaced by
- new one.
- size: Reading real number(s),give a reasonable figures in the unit 4 (e.g. MaxSize()
- for SDouble). When size == 0, all figures will be read. This maybe causes an out of
- memory error when the file is too long.
- del : Give a non-zero value if you want to delete the file after reading.
- *****************************************************************************/
- static const char* ReadNumber(const char* fname, uint size = 0, int del = 0); // 005
- // fclose() and free the memory of "readBuff"
- static void CloseReadNumber(); // 006
- // static SNStack <void (*)()> MemFreeFunc; deleted since ver.3.0
- // constructor
- SNManager(){
- if(objectCounter < 0){ // initial value : objectCounter = -1
- set_new_handler(OutOfMemory);
- SetMaxSize();
- objectCounter = 0;
- }
- }
- // destructor
- virtual ~SNManager();
- // Return the maximum size
- // Initial value : objectCounter < 0 and maxSize[] = {0,0,...,0}.
- static uint SNMaxSize(NumberType tp){
- if(objectCounter < 0) SetMaxSize();
- return maxSize[tp];
- }
- // Return a proper hidden precision size for effective figures "eff_fig".
- static uint HiddenSize(uint eff_fig); // 007
- // added on May 24, 2001
- static uint MaxArraySize(){ return maxArraySize; }
- //If s > defaultMaxArraySize, it changes the value of "maxArraySize".
- static void SetMaxArraySize(uint s);
- /******************************************************************
- errID > 0 : fatal error and terminate the program
- errID <= 0 : display a warning and continue the program
- place_ : the name of function where an error occures.
- ********************************************************************/
- static void SetError(SNErrorFlag error_flag, const char* place_,
- int errID); // 008
- static void SetErrorFlag(SNErrorFlag err){ errorFlag = err; }
- // display the version of "SN library"
- static void Version(){ SetError(NO_ERR, NULL, 0); }
- static long SNObjects(){ return objectCounter; }
- static uint Hidden() { return hidden; }
- static uint EffFigures() { return effFigures; }
-
- //functions related to FFT multiplication
- static void FFTVerify(bool v){ fftVerify = v; }
- static bool FFTVerify() { return fftVerify; }
- static uint FFTSize() { return fftUse ? fftArraySize : 0; }
- static uint MFFTMaxArraySize(){ return fftMaxArraySize; }
- static uint MFFTMinSize() { return fftMinSize; }
- static bool FFTUse() { return fftUse; }
- static long FFTUsedTimes(bool set_zero = false) {
- long u = fftUsedTimes;
- if(set_zero) fftUsedTimes = 0; // reset
- return u;
- }
- /********************************************************************
- Provides a method which change the parameter of FFT multiplication.
- If fft_min_sz == 0, do not use.
- If fft_min_sz == 16, use greater than or equal to 16.
- default : fftMinSize = defaultFFTMinSize
- If 0 < fft_min_sz < 2*minArraySize(=8), set fftMinSize = 8.
- **********************************************************************/
- static void SetFFTMinSize(uint fft_min_sz) {
- fftUse = fft_min_sz ? ON : OFF;
- if(!fftUse) fftMinSize = 0; // CloseFFT(); has removed. since version 2.31
- else fftMinSize = max(ceilpow2(fft_min_sz), 2u*minArraySize);
- }
-
- static long KHHMultUsedTimes(bool set_zero = false) {
- long u = KaratsubaHHMultUsedTimes;
- if(set_zero) KaratsubaHHMultUsedTimes = 0; // reset
- return u;
- }
-
- // verify switch
- static void Verify(bool v){ verify = fftVerify = v; }
- static bool Verify() { return (preferSpeed == true) ? false : verify; }
- };
-
- inline int SystemBit(){ // return the bit of your system
- #ifdef _WIN64 // 64bit
- return 64;
- #else
- return 32;
- #endif
- }
- void SNInfo(ostream& os=std::cout); // Show Information about sizes, etc. 010
- void FFTInfo(ostream& os=std::cout);
-
- #endif // SNManager_H
snmanage.h : last modifiled at 2017/09/29 11:38:07(10,531 bytes)
created at 2016/04/11 11:18:58
The creation time of this html file is 2017/10/11 16:07:52 (Wed Oct 11 16:07:52 2017).