mathu.inc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2004 by Florian Klaempfl
  4. member of the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$ifdef wince}
  12. const
  13. _DN_SAVE = $00000000;
  14. _DN_FLUSH = $01000000;
  15. _EM_INVALID = $00000010;
  16. _EM_DENORMAL = $00080000;
  17. _EM_ZERODIVIDE = $00000008;
  18. _EM_OVERFLOW = $00000004;
  19. _EM_UNDERFLOW = $00000002;
  20. _EM_INEXACT = $00000001;
  21. _IC_AFFINE = $00040000;
  22. _IC_PROJECTIVE = $00000000;
  23. _RC_CHOP = $00000300;
  24. _RC_UP = $00000200;
  25. _RC_DOWN = $00000100;
  26. _RC_NEAR = $00000000;
  27. _PC_24 = $00020000;
  28. _PC_53 = $00010000;
  29. _PC_64 = $00000000;
  30. _MCW_DN = $03000000;
  31. _MCW_EM = $0008001F;
  32. _MCW_IC = $00040000;
  33. _MCW_RC = $00000300;
  34. _MCW_PC = $00030000;
  35. function _controlfp(new: DWORD; mask: DWORD): DWORD; cdecl; external 'coredll';
  36. function GetRoundMode: TFPURoundingMode;
  37. var
  38. c: dword;
  39. begin
  40. c:=_controlfp(0, 0);
  41. Result:=TFPURoundingMode((c shr 16) and 3);
  42. end;
  43. function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  44. var
  45. c: dword;
  46. begin
  47. c:=Ord(RoundMode) shl 16;
  48. c:=_controlfp(c, _MCW_RC);
  49. Result:=TFPURoundingMode((c shr 16) and 3);
  50. end;
  51. function GetPrecisionMode: TFPUPrecisionMode;
  52. var
  53. c: dword;
  54. begin
  55. c:=_controlfp(0, 0);
  56. if c and _MCW_PC = _PC_64 then
  57. Result:=pmDouble
  58. else
  59. Result:=pmSingle;
  60. end;
  61. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  62. var
  63. c: dword;
  64. begin
  65. Result:=GetPrecisionMode;
  66. if Precision = pmSingle then
  67. c:=_PC_24
  68. else
  69. c:=_PC_64;
  70. _controlfp(c, _MCW_PC);
  71. end;
  72. function ConvertExceptionMask(em: dword): TFPUExceptionMask;
  73. begin
  74. Result:=[];
  75. if em and _EM_INVALID <> 0 then
  76. Result:=Result + [exInvalidOp];
  77. if em and _EM_DENORMAL <> 0 then
  78. Result:=Result + [exDenormalized];
  79. if em and _EM_ZERODIVIDE <> 0 then
  80. Result:=Result + [exZeroDivide];
  81. if em and _EM_OVERFLOW <> 0 then
  82. Result:=Result + [exOverflow];
  83. if em and _EM_UNDERFLOW <> 0 then
  84. Result:=Result + [exUnderflow];
  85. if em and _EM_INEXACT <> 0 then
  86. Result:=Result + [exPrecision];
  87. end;
  88. function GetExceptionMask: TFPUExceptionMask;
  89. begin
  90. Result:=ConvertExceptionMask(_controlfp(0, 0));
  91. end;
  92. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  93. var
  94. c: dword;
  95. begin
  96. c:=0;
  97. if exInvalidOp in Mask then
  98. c:=c or _EM_INVALID;
  99. if exDenormalized in Mask then
  100. c:=c or _EM_DENORMAL;
  101. if exZeroDivide in Mask then
  102. c:=c or _EM_ZERODIVIDE;
  103. if exOverflow in Mask then
  104. c:=c or _EM_OVERFLOW;
  105. if exUnderflow in Mask then
  106. c:=c or _EM_UNDERFLOW;
  107. if exPrecision in Mask then
  108. c:=c or _EM_INEXACT;
  109. c:=_controlfp(c, _MCW_EM);
  110. Result:=ConvertExceptionMask(c);
  111. softfloat_exception_mask:=dword(Mask);
  112. end;
  113. procedure ClearExceptions(RaisePending: Boolean =true);
  114. begin
  115. end;
  116. {$else wince}
  117. {*****************************************************************************
  118. FPA code
  119. *****************************************************************************}
  120. {
  121. Docs from uclib
  122. * We have a slight terminology confusion here. On the ARM, the register
  123. * we're interested in is actually the FPU status word - the FPU control
  124. * word is something different (which is implementation-defined and only
  125. * accessible from supervisor mode.)
  126. *
  127. * The FPSR looks like this:
  128. *
  129. * 31-24 23-16 15-8 7-0
  130. * | system ID | trap enable | system control | exception flags |
  131. *
  132. * We ignore the system ID bits; for interest's sake they are:
  133. *
  134. * 0000 "old" FPE
  135. * 1000 FPPC hardware
  136. * 0001 FPE 400
  137. * 1001 FPA hardware
  138. *
  139. * The trap enable and exception flags are both structured like this:
  140. *
  141. * 7 - 5 4 3 2 1 0
  142. * | reserved | INX | UFL | OFL | DVZ | IVO |
  143. *
  144. * where a `1' bit in the enable byte means that the trap can occur, and
  145. * a `1' bit in the flags byte means the exception has occurred.
  146. *
  147. * The exceptions are:
  148. *
  149. * IVO - invalid operation
  150. * DVZ - divide by zero
  151. * OFL - overflow
  152. * UFL - underflow
  153. * INX - inexact (do not use; implementations differ)
  154. *
  155. * The system control byte looks like this:
  156. *
  157. * 7-5 4 3 2 1 0
  158. * | reserved | AC | EP | SO | NE | ND |
  159. *
  160. * where the bits mean
  161. *
  162. * ND - no denormalised numbers (force them all to zero)
  163. * NE - enable NaN exceptions
  164. * SO - synchronous operation
  165. * EP - use expanded packed-decimal format
  166. * AC - use alternate definition for C flag on compare operations
  167. */
  168. /* masking of interrupts */
  169. #define _FPU_MASK_IM 0x00010000 /* invalid operation */
  170. #define _FPU_MASK_ZM 0x00020000 /* divide by zero */
  171. #define _FPU_MASK_OM 0x00040000 /* overflow */
  172. #define _FPU_MASK_UM 0x00080000 /* underflow */
  173. #define _FPU_MASK_PM 0x00100000 /* inexact */
  174. #define _FPU_MASK_DM 0x00000000 /* denormalized operation */
  175. /* The system id bytes cannot be changed.
  176. Only the bottom 5 bits in the trap enable byte can be changed.
  177. Only the bottom 5 bits in the system control byte can be changed.
  178. Only the bottom 5 bits in the exception flags are used.
  179. The exception flags are set by the fpu, but can be zeroed by the user. */
  180. #define _FPU_RESERVED 0xffe0e0e0 /* These bits are reserved. */
  181. /* The fdlibm code requires strict IEEE double precision arithmetic,
  182. no interrupts for exceptions, rounding to nearest. Changing the
  183. rounding mode will break long double I/O. Turn on the AC bit,
  184. the compiler generates code that assumes it is on. */
  185. #define _FPU_DEFAULT 0x00001000 /* Default value. */
  186. #define _FPU_IEEE 0x001f1000 /* Default + exceptions enabled. */
  187. }
  188. {$if not(defined(gba)) and not(defined(nds)) and not(defined(FPUSOFT)) and not(defined(FPULIBGCC))}
  189. const
  190. _FPU_MASK_IM = $00010000; { invalid operation }
  191. _FPU_MASK_ZM = $00020000; { divide by zero }
  192. _FPU_MASK_OM = $00040000; { overflow }
  193. _FPU_MASK_UM = $00080000; { underflow }
  194. _FPU_MASK_PM = $00100000; { inexact }
  195. _FPU_MASK_DM = $00000000; { denormalized operation }
  196. _FPU_MASK_ALL = $001f0000; { mask for all flags }
  197. function FPU_GetCW : dword; nostackframe; assembler;
  198. asm
  199. rfs r0
  200. end;
  201. procedure FPU_SetCW(cw : dword); nostackframe; assembler;
  202. asm
  203. wfs r0
  204. end;
  205. {$endif}
  206. function GetRoundMode: TFPURoundingMode;
  207. begin
  208. { does not apply }
  209. end;
  210. function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  211. begin
  212. { does not apply }
  213. end;
  214. function GetPrecisionMode: TFPUPrecisionMode;
  215. begin
  216. { does not apply }
  217. end;
  218. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  219. begin
  220. { does not apply }
  221. end;
  222. function GetExceptionMask: TFPUExceptionMask;
  223. var
  224. cw : dword;
  225. begin
  226. {$if not(defined(gba)) and not(defined(nds)) and not(defined(FPUSOFT)) and not(defined(FPULIBGCC))}
  227. Result:=[];
  228. cw:=FPU_GetCW;
  229. if (cw and _FPU_MASK_IM)=0 then
  230. include(Result,exInvalidOp);
  231. if (cw and _FPU_MASK_DM)=0 then
  232. include(Result,exDenormalized);
  233. if (cw and _FPU_MASK_ZM)=0 then
  234. include(Result,exZeroDivide);
  235. if (cw and _FPU_MASK_OM)=0 then
  236. include(Result,exOverflow);
  237. if (cw and _FPU_MASK_UM)=0 then
  238. include(Result,exUnderflow);
  239. if (cw and _FPU_MASK_PM)=0 then
  240. include(Result,exPrecision);
  241. {$else}
  242. dword(Result):=softfloat_exception_mask;
  243. {$endif}
  244. end;
  245. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  246. var
  247. cw : dword;
  248. begin
  249. {$if not(defined(gba)) and not(defined(nds)) and not(defined(FPUSOFT)) and not(defined(FPULIBGCC))}
  250. cw:=FPU_GetCW or _FPU_MASK_ALL;
  251. if exInvalidOp in Mask then
  252. cw:=cw and not(_FPU_MASK_IM);
  253. if exDenormalized in Mask then
  254. cw:=cw and not(_FPU_MASK_DM);
  255. if exZeroDivide in Mask then
  256. cw:=cw and not(_FPU_MASK_ZM);
  257. if exOverflow in Mask then
  258. cw:=cw and not(_FPU_MASK_OM);
  259. if exUnderflow in Mask then
  260. cw:=cw and not(_FPU_MASK_UM);
  261. if exPrecision in Mask then
  262. cw:=cw and not(_FPU_MASK_PM);
  263. FPU_SetCW(cw);
  264. {$endif}
  265. softfloat_exception_mask:=dword(Mask);
  266. end;
  267. procedure ClearExceptions(RaisePending: Boolean =true);
  268. begin
  269. end;
  270. {$endif wince}