1. /* snmanage.h by K.Tsuru */
  2. /**************************************************************************
  3. SNManager class
  4. Provides how to treat error, management of various sizes and how to use FFT
  5. multiplication.
  6. ***************************************************************************/
  7. #ifndef SNMANAGER_H
  8. #define SNMANAGER_H
  9. // sfft.h
  10. #ifndef S_FFT_H
  11. #include "sfft.h"
  12. #endif // S_FFT_H
  13. /**********************************
  14. various sizes of array
  15. maxArraySize for SLong class
  16. 14 Sep, 2000
  17. Change the value "defaultFFTMinSize"
  18. from 64 to 16.
  19. ***********************************/
  20. const uint defaultMaxArraySize = uint( maxSizeOfMemoryBlock/(ulong)sizeof(fType) );
  21. const uint minArraySize = 4u; // minimum size of array
  22. const uint defaultEffFig = 61u; // default effective figures of SDouble in DRADIX
  23. const uint defaultHidden = 2u; // default hidden figures
  24. const uint defaultFFTMinSize = 16u; // default threshold size of FFT multiplication
  25. // OutOfMemory() is registered by "set_new_handler()".
  26. void OutOfMemory();
  27. class SNManager {
  28. friend class RealSize;
  29. friend class SNumber; // to access "objectCounter"
  30. public:
  31. // type of number SNumber::type;
  32. // lowest 1bit : integer, 2nd bit : real, 3rd bit : BRADIX type
  33. // UNKNOWN is used in StringToNumber class.
  34. // (DETECT_IR & type) gives a value DEC_INT or REAL
  35. enum NumberType { MIN_SIZE = 0, DEC_INT = 1, REAL = 2, DEC_RDX = 3, // changed "INTEGER" to "DEC_INT" since version 2.30
  36. BIN_RDX = 4, BIN_INT = 5, BIN_DEC = 6, NUMBER_TYPE = 7, UNKNOWN = 8};
  37. enum { DETECT_IR = 3 }; // = (DEC_INT | REAL)
  38. // error ID
  39. enum SNErrorFlag {
  40. NO_ERR = 0, OVERFLOW_ERR, UNDERFLOW_ERR, DIVIDED_BY_ZERO, SIGN_ERR,
  41. NOT_CONVERGE, DOMAIN_ERR, OUT_OF_RANGE, RADIX_ERR, UNDEC_VALUE,
  42. SET_EFF_FIG, SYNTAX_ERR, FFT_ERR, TOO_LONG, TOO_LARGE_EXP,
  43. TLOSS_ERR, VERIFY, FATAL
  44. };
  45. private:
  46. static long objectCounter; // counter for objects. initial value is -1
  47. static uint effFigures; // effective figure reserved by user
  48. static uint hidden; // hidden figures
  49. static uint maxArraySize;
  50. static uint maxSize[NUMBER_TYPE];// maximum size for various types of number
  51. static FILE* stream; // output stream. default is "stdout".
  52. static ostream& sn_ost; // class std::output stream. default is "std::cout".
  53. // for error statements
  54. static SNErrorFlag errorFlag;
  55. static char* place; // place where an error occured.
  56. static void ErrorHandler(int id);// id > 0 fatal error, id <= 0 warning. 001
  57. static void SetMaxSize();// Provides the maxmum sizes of various types. 002
  58. static bool verify; // verify or not in the routine of division, etc.
  59. static bool showMessage; // Show a message "Calculating Pi() now." etc. or not.
  60. static bool fftVerify; // verify or not in the rounding off FFT multiplication
  61. static uint fftMinSize; // threshold size of FFT multiplication
  62. static bool fftUse; // use FFT multiplication or not
  63. protected:
  64. static uint fftArraySize; // size of FFT(=0 when no use)
  65. static long fftUsedTimes; // the number of times which "LLMultFFT()" was called
  66. static long KaratsubaHHMultUsedTimes; // the number of times which Karatsuba's multiplication "KHHMult()" was called
  67. static uint SNEffFig(NumberType tp);// accessible in derived class. 003
  68. /************************************************************************
  69. Permit reduction of maximum size (ENABLE) or not (DISABLE).
  70. In the functions which uses Newton's method, reducing temporarily the
  71. effective figures this value has to be set by DISABLE.
  72. *************************************************************************/
  73. static bool reduceMaxSize;
  74. /*****************************************************************************
  75. If you prefer to speed rather than precision, set this value ON by a statement
  76. PreferSpeed(ON);.
  77. default : preferSpeed = false.
  78. An object of "RealSize class" can also call this function.
  79. ******************************************************************************/
  80. private:
  81. static bool preferSpeed;
  82. public:
  83. static void PreferSpeed(bool p){ preferSpeed = p; }
  84. static bool PreferSpeed() { return preferSpeed; }
  85. /*
  86. For E(), Pi(), Log10() show message "Evaluating XXX() now..... Finished."
  87. Use to count its called times etc. for debug.
  88. */
  89. static bool ShowMessage() { return showMessage; }
  90. static void ShowMessage(int sw) { showMessage = sw ? ON : OFF; }
  91. static void ShowMessage(const char* msg);
  92. /*********************
  93. output stream using structure FILE.
  94. It isn't recommended to use since SN library version 2.30.
  95. Prease use <iostream> STL.
  96. **********************/
  97. /****************************************************************************
  98. Provides a method which change the output stream.
  99. fname : file name
  100. If fname == NULL, "fclose()" and reset stream = stdout.
  101. If the file could not be opened, close program by "exit()" function.
  102. 1. When ovw == NO(default), the program ask whether you want overwrite on the
  103. existing file or not.If you chose NO, ask whether you would continue the program.
  104. 2. If ovw == YES, the program overwite without warning. This provides a method
  105. to keep a data on a disk which causes an abnormal termination by an error.
  106. 3. It returns "FILE* stream".
  107. *****************************************************************************/
  108. static FILE* OutPutFile(const char* fname, bool ovw = false); // 004
  109. void CloseFile();
  110. // output a literal s to present stream. In an optional format use two steps
  111. //1. sprintf(buff,fmt,...); 2. FPuts(buff);
  112. static int FPuts(const char* s){ return fprintf(stream, "%s", s); }
  113. // output a character c to present stream
  114. static int FPutc(int c){ return putc(c, stream); }
  115. /********************************************************************
  116. Provides a method which changes the output stream into "stderr" etc.
  117. If ostr == NULL, reset stream = stdout.
  118. ********************************************************************/
  119. static void OutPutStream(FILE* ostr) {
  120. stream = (ostr != NULL) ? ostr : stdout;
  121. }
  122. static FILE* FileStream() { return stream; }
  123. static SNErrorFlag SNError(){
  124. SNErrorFlag err = errorFlag;
  125. errorFlag = NO_ERR; // reset
  126. return err;
  127. }
  128. /****************************************************************************
  129. Open a file "fname" which includes text data of number(s) and read onto the
  130. static memory "readBuff" and return a constant pointer of "readBuff".
  131. If you would like to receive as a literal
  132. const char* num;
  133. num = ReadNumber(fname);
  134. For a direct substitution
  135. SLong a;
  136. a = ReadNumber(fname);
  137. The memory is allocated on a static area, then previous data will be replaced by
  138. new one.
  139. size: Reading real number(s),give a reasonable figures in the unit 4 (e.g. MaxSize()
  140. for SDouble). When size == 0, all figures will be read. This maybe causes an out of
  141. memory error when the file is too long.
  142. del : Give a non-zero value if you want to delete the file after reading.
  143. *****************************************************************************/
  144. static const char* ReadNumber(const char* fname, uint size = 0, int del = 0); // 005
  145. // fclose() and free the memory of "readBuff"
  146. static void CloseReadNumber(); // 006
  147. // static SNStack <void (*)()> MemFreeFunc; deleted since ver.3.0
  148. // constructor
  149. SNManager(){
  150. if(objectCounter < 0){ // initial value : objectCounter = -1
  151. set_new_handler(OutOfMemory);
  152. SetMaxSize();
  153. objectCounter = 0;
  154. }
  155. }
  156. // destructor
  157. virtual ~SNManager();
  158. // Return the maximum size
  159. // Initial value : objectCounter < 0 and maxSize[] = {0,0,...,0}.
  160. static uint SNMaxSize(NumberType tp){
  161. if(objectCounter < 0) SetMaxSize();
  162. return maxSize[tp];
  163. }
  164. // Return a proper hidden precision size for effective figures "eff_fig".
  165. static uint HiddenSize(uint eff_fig); // 007
  166. // added on May 24, 2001
  167. static uint MaxArraySize(){ return maxArraySize; }
  168. //If s > defaultMaxArraySize, it changes the value of "maxArraySize".
  169. static void SetMaxArraySize(uint s);
  170. /******************************************************************
  171. errID > 0 : fatal error and terminate the program
  172. errID <= 0 : display a warning and continue the program
  173. place_ : the name of function where an error occures.
  174. ********************************************************************/
  175. static void SetError(SNErrorFlag error_flag, const char* place_,
  176. int errID); // 008
  177. static void SetErrorFlag(SNErrorFlag err){ errorFlag = err; }
  178. // display the version of "SN library"
  179. static void Version(){ SetError(NO_ERR, NULL, 0); }
  180. static long SNObjects(){ return objectCounter; }
  181. static uint Hidden() { return hidden; }
  182. static uint EffFigures() { return effFigures; }
  183. //functions related to FFT multiplication
  184. static void FFTVerify(bool v){ fftVerify = v; }
  185. static bool FFTVerify() { return fftVerify; }
  186. static uint FFTSize() { return fftUse ? fftArraySize : 0; }
  187. static uint MFFTMaxArraySize(){ return fftMaxArraySize; }
  188. static uint MFFTMinSize() { return fftMinSize; }
  189. static bool FFTUse() { return fftUse; }
  190. static long FFTUsedTimes(bool set_zero = false) {
  191. long u = fftUsedTimes;
  192. if(set_zero) fftUsedTimes = 0; // reset
  193. return u;
  194. }
  195. /********************************************************************
  196. Provides a method which change the parameter of FFT multiplication.
  197. If fft_min_sz == 0, do not use.
  198. If fft_min_sz == 16, use greater than or equal to 16.
  199. default : fftMinSize = defaultFFTMinSize
  200. If 0 < fft_min_sz < 2*minArraySize(=8), set fftMinSize = 8.
  201. **********************************************************************/
  202. static void SetFFTMinSize(uint fft_min_sz) {
  203. fftUse = fft_min_sz ? ON : OFF;
  204. if(!fftUse) fftMinSize = 0; // CloseFFT(); has removed. since version 2.31
  205. else fftMinSize = max(ceilpow2(fft_min_sz), 2u*minArraySize);
  206. }
  207. static long KHHMultUsedTimes(bool set_zero = false) {
  208. long u = KaratsubaHHMultUsedTimes;
  209. if(set_zero) KaratsubaHHMultUsedTimes = 0; // reset
  210. return u;
  211. }
  212. // verify switch
  213. static void Verify(bool v){ verify = fftVerify = v; }
  214. static bool Verify() { return (preferSpeed == true) ? false : verify; }
  215. };
  216. inline int SystemBit(){ // return the bit of your system
  217. #ifdef _WIN64 // 64bit
  218. return 64;
  219. #else
  220. return 32;
  221. #endif
  222. }
  223. void SNInfo(ostream& os=std::cout); // Show Information about sizes, etc. 010
  224. void FFTInfo(ostream& os=std::cout);
  225. #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).