1
- __precompile__ ()
2
1
module Quadmath
3
2
using Requires
4
3
5
4
export Float128, ComplexF128
6
5
7
6
import Base: (* ), + , - , / , < , <= , == , ^ , convert,
8
7
reinterpret, sign_mask, exponent_mask, exponent_one, exponent_half,
9
- significand_mask,
8
+ significand_mask, exponent, significand,
10
9
promote_rule, widen,
11
10
string, print, show, parse,
12
11
acos, acosh, asin, asinh, atan, atanh, cosh, cos,
13
12
exp, expm1, log, log2, log10, log1p, sin, sinh, sqrt,
14
13
tan, tanh,
15
14
ceil, floor, trunc, round, fma,
16
- copysign, max, min, hypot, abs,
17
- ldexp, frexp,
15
+ copysign, flipsign, max, min, hypot, abs,
16
+ ldexp, frexp, nextfloat,
18
17
eps, isinf, isnan, isfinite, floatmin, floatmax, precision, signbit,
19
18
Int32,Int64,Float64,BigFloat
20
19
56
55
Float128 ((VecElement (flo), VecElement (fhi)))
57
56
end
58
57
reinterpret (:: Type{Unsigned} , x:: Float128 ) = reinterpret (UInt128, x)
58
+ reinterpret (:: Type{Signed} , x:: Float128 ) = reinterpret (Int128, x)
59
59
60
60
reinterpret (:: Type{Int128} , x:: Float128 ) =
61
61
reinterpret (Int128, reinterpret (UInt128, x))
@@ -170,14 +170,16 @@ for f in (:copysign, :hypot, )
170
170
end
171
171
end
172
172
173
+ flipsign (x:: Float128 , y:: Float128 ) = signbit (y) ? - x : x
174
+
173
175
function atan (x:: Float128 , y:: Float128 )
174
176
Float128 (ccall ((:atan2q , libquadmath), Cfloat128, (Cfloat128, Cfloat128), x, y))
175
177
end
176
178
177
179
# # misc
178
180
fma (x:: Float128 , y:: Float128 , z:: Float128 ) =
179
181
Float128 (ccall ((:fmaq ,libquadmath), Cfloat128, (Cfloat128, Cfloat128, Cfloat128), x, y, z))
180
-
182
+
181
183
isnan (x:: Float128 ) =
182
184
0 != ccall ((:isnanq ,libquadmath), Cint, (Cfloat128, ), x)
183
185
isinf (x:: Float128 ) =
@@ -204,6 +206,51 @@ function frexp(x::Float128)
204
206
return y, Int (r[])
205
207
end
206
208
209
+ significand (x:: Float128 ) = frexp (x)[1 ] * 2
210
+ function exponent (x:: Float128 )
211
+ ! isfinite (x) && throw (DomainError (" Cannot be NaN or Inf." ))
212
+ abs (x) > 0 && return frexp (x)[2 ] - 1
213
+ throw (DomainError (" Cannot be subnormal converted to 0." ))
214
+ end
215
+
216
+ function nextfloat (f:: Float128 , d:: Integer )
217
+ F = typeof (f)
218
+ fumax = reinterpret (Unsigned, F (Inf ))
219
+ U = typeof (fumax)
220
+
221
+ isnan (f) && return f
222
+ fi = reinterpret (Signed, f)
223
+ fneg = fi < 0
224
+ fu = unsigned (fi & typemax (fi))
225
+
226
+ dneg = d < 0
227
+ da = uabs (d)
228
+ if da > typemax (U)
229
+ fneg = dneg
230
+ fu = fumax
231
+ else
232
+ du = da % U
233
+ if fneg ⊻ dneg
234
+ if du > fu
235
+ fu = min (fumax, du - fu)
236
+ fneg = ! fneg
237
+ else
238
+ fu = fu - du
239
+ end
240
+ else
241
+ if fumax - fu < du
242
+ fu = fumax
243
+ else
244
+ fu = fu + du
245
+ end
246
+ end
247
+ end
248
+ if fneg
249
+ fu |= sign_mask (F)
250
+ end
251
+ reinterpret (F, fu)
252
+ end
253
+
207
254
Float128 (:: Irrational{:π} ) = reinterpret (Float128, 0x4000921fb54442d18469898cc51701b8 )
208
255
Float128 (:: Irrational{:e} ) = reinterpret (Float128, 0x40005bf0a8b1457695355fb8ac404e7a )
209
256
0 commit comments