summaryrefslogtreecommitdiff
path: root/rtl/inc/cgenmath.inc
blob: 174d224b8cd59fca49c0b2300c8b78594d3cbe50 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
{
    This file is part of the Free Pascal run time library.
    Copyright (c) 1999-2001 by Several contributors

    Generic mathemtical routines in libc

    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.

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

{ for 80x86, we can easily write the optimal inline code }
{ Furthermore, the routines below only go up to double   }
{ precision and we need extended precision if supported  }
{$ifndef FPC_HAS_TYPE_EXTENDED}

{$ifdef aix}
    { aix math library routines don't raise exceptions, you have to manually
      check for them }
    function feclearexcept(flags: longint): longint; cdecl; external 'c';
    function fetestexcept(flags: longint): longint; cdecl; external 'c';

    const
      FE_DIVBYZERO  = $04000000;
      FE_INEXACT    = $02000000;
      FE_INVALID    = $20000000;
      FE_OVERFLOW   = $10000000;
      FE_UNDERFLOW  = $08000000;
      FE_ALL_EXCEPT = $3E000000;

    procedure resetexcepts;
      begin
        seterrno(0);
        feclearexcept(FE_ALL_EXCEPT);
      end;

    procedure checkexcepts;
      var
        feres: longint;
        sfexcepts: TFPUExceptionMask;
      begin
        feres:=fetestexcept(FE_ALL_EXCEPT);
        sfexcepts:=[];
        if feres<>0 then
          begin
            if (feres and FE_DIVBYZERO) <> 0 then
              include(sfexcepts,float_flag_divbyzero);
            if (feres and FE_INEXACT) <> 0 then
              include(sfexcepts,float_flag_inexact);
            if (feres and FE_INVALID) <> 0 then
              include(sfexcepts,float_flag_invalid);
            if (feres and FE_OVERFLOW) <> 0 then
              include(sfexcepts,float_flag_overflow);
            if (feres and FE_UNDERFLOW) <> 0 then
              include(sfexcepts,float_flag_underflow);
          end
        { unknown error }
        else if (geterrno<>0) then
          include(sfexcepts,float_flag_invalid);
        if sfexcepts<>[] then
          float_raise(sfexcepts);
      end;

{$else aix}
    procedure resetexcepts; inline;
      begin
      end;


    procedure checkexcepts; inline;
      begin
      end;

{$endif aix}


{$ifndef SOLARIS}

{$ifndef FPC_SYSTEM_HAS_INT}
{$define FPC_SYSTEM_HAS_INT}

{$ifdef SUPPORT_DOUBLE}
    function c_trunc(d: double): double; cdecl; external 'c' name 'trunc';

    function fpc_int_real(d: ValReal): ValReal;compilerproc; {$ifdef MATHINLINE}inline;{$endif}
    begin
      resetexcepts;
      result := c_trunc(d);
      checkexcepts;
    end;


{$else SUPPORT_DOUBLE}

    function c_truncf(d: single): double; cdecl; external 'c' name 'truncf';

    function fpc_int_real(d: ValReal): ValReal;compilerproc; {$ifdef MATHINLINE}inline;{$endif}
      begin
        { this will be correct since real = single in the case of }
        { the motorola version of the compiler...                 }
        resetexcepts;
        int:=c_truncf(d);
        checkexcepts;
      end;
{$endif SUPPORT_DOUBLE}

{$endif FPC_SYSTEM_HAS_INT}
{$endif SOLARIS}

{$ifndef SYSTEM_HAS_FREXP}
{$define SYSTEM_HAS_FREXP}
    function c_frexp(x: double; out e: longint): double; cdecl; external 'c' name 'frexp';

    function frexp(x:ValReal; out e:Integer ):ValReal; {$ifdef MATHINLINE}inline;{$endif}
    var
      l: longint;
    begin
      resetexcepts;
      frexp := c_frexp(x,l);
      e := l;
      checkexcepts;
    end;
{$endif not SYSTEM_HAS_FREXP}


{$ifndef SYSTEM_HAS_LDEXP}
{$define SYSTEM_HAS_LDEXP}
    function c_ldexp(x: double; n: longint): double; cdecl; external 'c' name 'ldexp';

    function ldexp( x: ValReal; N: Integer):ValReal;{$ifdef MATHINLINE}inline;{$endif}
    begin
      resetexcepts;
      ldexp := c_ldexp(x,n);
      checkexcepts;
    end;
{$endif not SYSTEM_HAS_LDEXP}


{$ifndef FPC_SYSTEM_HAS_SQRT}
{$define FPC_SYSTEM_HAS_SQRT}

    function c_sqrt(d: double): double; cdecl; external 'c' name 'sqrt';

    function fpc_sqrt_real(d:ValReal):ValReal;compilerproc; {$ifdef MATHINLINE}inline;{$endif}
    begin
      resetexcepts;
      result := c_sqrt(d);
      checkexcepts;
    end;

{$endif}


{$ifndef FPC_SYSTEM_HAS_EXP}
{$define FPC_SYSTEM_HAS_EXP}
    function c_exp(d: double): double; cdecl; external 'c' name 'exp';


    function fpc_Exp_real(d:ValReal):ValReal;compilerproc; {$ifdef MATHINLINE}inline;{$endif}
      begin
        resetexcepts;
        result := c_exp(d);
        checkexcepts;
    end;
{$endif}


{ buggy on aix, sets DIV_BY_ZERO flag for some valid inputs }
{$ifndef aix}
{$ifndef FPC_SYSTEM_HAS_LN}
{$define FPC_SYSTEM_HAS_LN}

    function c_log(d: double): double; cdecl; external 'c' name 'log';

    function fpc_Ln_real(d:ValReal):ValReal;compilerproc;{$ifdef MATHINLINE}inline;{$endif}
    begin
      resetexcepts;
      result := c_log(d);
      checkexcepts;
    end;
{$endif}
{$endif}


{$ifndef FPC_SYSTEM_HAS_SIN}
{$define FPC_SYSTEM_HAS_SIN}
    function c_sin(d: double): double; cdecl; external 'c' name 'sin';

    function fpc_Sin_real(d:ValReal):ValReal;compilerproc; {$ifdef MATHINLINE}inline;{$endif}
    begin
      resetexcepts;
      result := c_sin(d);
      checkexcepts;
    end;
{$endif}



{$ifndef FPC_SYSTEM_HAS_COS}
{$define FPC_SYSTEM_HAS_COS}
    function c_cos(d: double): double; cdecl; external 'c' name 'cos';

    function fpc_Cos_real(d:ValReal):ValReal;compilerproc; {$ifdef MATHINLINE}inline;{$endif}
    begin
      resetexcepts;
      result := c_cos(d);
      checkexcepts;
    end;
{$endif}



{$ifndef FPC_SYSTEM_HAS_ARCTAN}
{$define FPC_SYSTEM_HAS_ARCTAN}
    function c_atan(d: double): double; cdecl; external 'c' name 'atan';

    function fpc_ArcTan_real(d:ValReal):ValReal;compilerproc; {$ifdef MATHINLINE}inline;{$endif}
    begin
      resetexcepts;
      result := c_atan(d);
      checkexcepts;
    end;
{$endif}

{$endif not FPC_HAS_TYPE_EXTENDED}