mathu.inc 10 KB

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