1- __precompile__ ()
21module Quadmath
32using Requires
43
54export Float128, ComplexF128
65
76import Base: (* ), + , - , / , < , <= , == , ^ , convert,
87 reinterpret, sign_mask, exponent_mask, exponent_one, exponent_half,
9- significand_mask,
8+ significand_mask, exponent, significand,
109 promote_rule, widen,
1110 string, print, show, parse,
1211 acos, acosh, asin, asinh, atan, atanh, cosh, cos,
1312 exp, expm1, log, log2, log10, log1p, sin, sinh, sqrt,
1413 tan, tanh,
1514 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,
1817 eps, isinf, isnan, isfinite, floatmin, floatmax, precision, signbit,
1918 Int32,Int64,Float64,BigFloat
2019
5655 Float128 ((VecElement (flo), VecElement (fhi)))
5756 end
5857 reinterpret (:: Type{Unsigned} , x:: Float128 ) = reinterpret (UInt128, x)
58+ reinterpret (:: Type{Signed} , x:: Float128 ) = reinterpret (Int128, x)
5959
6060 reinterpret (:: Type{Int128} , x:: Float128 ) =
6161 reinterpret (Int128, reinterpret (UInt128, x))
@@ -170,14 +170,16 @@ for f in (:copysign, :hypot, )
170170 end
171171end
172172
173+ flipsign (x:: Float128 , y:: Float128 ) = signbit (y) ? - x : x
174+
173175function atan (x:: Float128 , y:: Float128 )
174176 Float128 (ccall ((:atan2q , libquadmath), Cfloat128, (Cfloat128, Cfloat128), x, y))
175177end
176178
177179# # misc
178180fma (x:: Float128 , y:: Float128 , z:: Float128 ) =
179181 Float128 (ccall ((:fmaq ,libquadmath), Cfloat128, (Cfloat128, Cfloat128, Cfloat128), x, y, z))
180-
182+
181183isnan (x:: Float128 ) =
182184 0 != ccall ((:isnanq ,libquadmath), Cint, (Cfloat128, ), x)
183185isinf (x:: Float128 ) =
@@ -204,6 +206,51 @@ function frexp(x::Float128)
204206 return y, Int (r[])
205207end
206208
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+
207254Float128 (:: Irrational{:π} ) = reinterpret (Float128, 0x4000921fb54442d18469898cc51701b8 )
208255Float128 (:: Irrational{:e} ) = reinterpret (Float128, 0x40005bf0a8b1457695355fb8ac404e7a )
209256
0 commit comments