summaryrefslogtreecommitdiff
path: root/rtl/mips/mathu.inc
blob: 8a4be08746a8eba43738054faf754711512af3b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
{
    This file is part of the Free Pascal run time library.
    Copyright (c) 1999-2000 by Florian Klaempfl
    member of the Free Pascal development team

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}

{ exported by the system unit }
function get_fsr : dword;external name 'FPC_GETFSR';
procedure set_fsr(fsr : dword);external name 'FPC_SETFSR';

const
  { FPU enable exception bits for FCSR register }
  fpu_enable_inexact   =  $80;
  fpu_enable_underflow = $100;
  fpu_enable_overflow  = $200;
  fpu_enable_div_zero  = $400;
  fpu_enable_invalid   = $800;
  fpu_enable_mask      = $F80;
  default_fpu_enable = fpu_enable_div_zero or fpu_enable_invalid;

  fpu_flags_mask = $7C;
  fpu_cause_mask = $3F000;

  { FPU rounding mask and values }
  fpu_rounding_mask    = $3;
  fpu_rounding_nearest = 0;
  fpu_rounding_towards_zero = 1;
  fpu_rounding_plus_inf = 2;
  fpu_rounding_minus_inf = 3;

const
  roundmode2fsr : array [TFPURoundingMode] of byte=(
    fpu_rounding_nearest,
    fpu_rounding_minus_inf,
    fpu_rounding_plus_inf,
    fpu_rounding_towards_zero
  );

  fsr2roundmode : array [0..3] of TFPURoundingMode = (
    rmNearest,
    rmTruncate,
    rmUp,
    rmDown
  );

function GetRoundMode: TFPURoundingMode;
  begin
    result:=fsr2roundmode[get_fsr and fpu_rounding_mask];
  end;

function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  var
    fsr: longword;
  begin
    fsr:=get_fsr;
    result:=fsr2roundmode[fsr and fpu_rounding_mask];
    set_fsr((fsr and not fpu_rounding_mask) or roundmode2fsr[RoundMode]);
  end;


function GetPrecisionMode: TFPUPrecisionMode;
  begin
    result:=pmDouble;
  end;


function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  begin
    result:=pmDouble;
  end;


function fsr2ExceptionMask(fsr: longword): TFPUExceptionMask;
  begin
    result:=[];
    { invalid operation }
    if (fsr and fpu_enable_invalid)=0 then
      include(result,exInvalidOp);

    { zero divide }
    if (fsr and fpu_enable_div_zero)=0 then
      include(result,exZeroDivide);

    { overflow }
    if (fsr and fpu_enable_overflow)=0 then
      include(result,exOverflow);

    { underflow: }
    if (fsr and fpu_enable_underflow)=0 then
      include(result,exUnderflow);

    { Precision (inexact result) }
    if (fsr and fpu_enable_inexact)=0 then
      include(result,exPrecision);
  end;


function GetExceptionMask: TFPUExceptionMask;
  begin
    result:=fsr2ExceptionMask(get_fsr);
  end;


function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  var
    fsr : longword;
  begin
    fsr:=get_fsr;
    result:=fsr2ExceptionMask(fsr);

    { Reset flags, cause and enables }
    fsr := fsr and not (fpu_flags_mask or fpu_cause_mask or fpu_enable_mask);

    { invalid operation }
    if not (exInvalidOp in mask) then
      fsr:=fsr or (fpu_enable_invalid);

    { zero divide }
    if not (exZeroDivide in mask) then
      fsr:=fsr or (fpu_enable_div_zero);

    { overflow }
    if not (exOverflow in mask) then
      fsr:=fsr or (fpu_enable_overflow);

    { underflow }
    if not (exUnderflow in mask) then
      fsr:=fsr or (fpu_enable_underflow);

    { Precision (inexact result) }
    if not (exPrecision in mask) then
      fsr:=fsr or (fpu_enable_inexact);

    { update control register contents }
    set_fsr(fsr);
  end;


procedure ClearExceptions(RaisePending: Boolean =true);
  begin
    set_fsr(get_fsr and not (fpu_flags_mask or fpu_cause_mask));
  end;