/
bigfloat.jl
168 lines (157 loc) · 4.34 KB
/
bigfloat.jl
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
mutability(::Type{BigFloat}) = IsMutable()
# copied from `deepcopy_internal` implementation in Julia:
# https://github.com/JuliaLang/julia/blob/7d41d1eb610cad490cbaece8887f9bbd2a775021/base/mpfr.jl#L1041-L1050
function mutable_copy(x::BigFloat)
d = x._d
d′ = GC.@preserve d unsafe_string(pointer(d), sizeof(d)) # creates a definitely-new String
return Base.MPFR._BigFloat(x.prec, x.sign, x.exp, d′)
end
@static if VERSION >= v"1.1.0-DEV.683"
const _MPFRRoundingMode = Base.MPFR.MPFRRoundingMode
else
const _MPFRRoundingMode = Int32
end
# zero
promote_operation(::typeof(zero), ::Type{BigFloat}) = BigFloat
function _set_si!(x::BigFloat, value)
ccall(
(:mpfr_set_si, :libmpfr),
Int32,
(Ref{BigFloat}, Clong, _MPFRRoundingMode),
x,
value,
Base.MPFR.ROUNDING_MODE[],
)
return x
end
operate!(::typeof(zero), x::BigFloat) = _set_si!(x, 0)
# one
promote_operation(::typeof(one), ::Type{BigFloat}) = BigFloat
operate!(::typeof(one), x::BigFloat) = _set_si!(x, 1)
# +
promote_operation(::typeof(+), ::Vararg{Type{BigFloat},N}) where {N} = BigFloat
function operate_to!(output::BigFloat, ::typeof(+), a::BigFloat, b::BigFloat)
ccall(
(:mpfr_add, :libmpfr),
Int32,
(Ref{BigFloat}, Ref{BigFloat}, Ref{BigFloat}, _MPFRRoundingMode),
output,
a,
b,
Base.MPFR.ROUNDING_MODE[],
)
return output
end
#function operate_to!(output::BigFloat, op::typeof(+), a::BigFloat, b::LinearAlgebra.UniformScaling)
# return operate_to!(output, op, a, b.λ)
#end
# -
promote_operation(::typeof(-), ::Vararg{Type{BigFloat},N}) where {N} = BigFloat
function operate_to!(output::BigFloat, ::typeof(-), a::BigFloat, b::BigFloat)
ccall(
(:mpfr_sub, :libmpfr),
Int32,
(Ref{BigFloat}, Ref{BigFloat}, Ref{BigFloat}, _MPFRRoundingMode),
output,
a,
b,
Base.MPFR.ROUNDING_MODE[],
)
return output
end
# *
promote_operation(::typeof(*), ::Vararg{Type{BigFloat},N}) where {N} = BigFloat
function operate_to!(output::BigFloat, ::typeof(*), a::BigFloat, b::BigFloat)
ccall(
(:mpfr_mul, :libmpfr),
Int32,
(Ref{BigFloat}, Ref{BigFloat}, Ref{BigFloat}, _MPFRRoundingMode),
output,
a,
b,
Base.MPFR.ROUNDING_MODE[],
)
return output
end
function operate_to!(
output::BigFloat,
op::Union{typeof(+),typeof(-),typeof(*)},
a::BigFloat,
b::BigFloat,
c::Vararg{BigFloat,N},
) where {N}
operate_to!(output, op, a, b)
return operate!(op, output, c...)
end
function operate!(op::Function, x::BigFloat, args::Vararg{Any,N}) where {N}
operate_to!(x, op, x, args...)
end
# add_mul and sub_mul
# Buffer to hold the product
buffer_for(::AddSubMul, args::Vararg{Type{BigFloat},N}) where {N} = BigFloat()
function operate_to!(
output::BigFloat,
op::AddSubMul,
x::BigFloat,
y::BigFloat,
z::BigFloat,
args::Vararg{BigFloat,N},
) where {N}
return buffered_operate_to!(BigFloat(), output, op, x, y, z, args...)
end
function buffered_operate_to!(
buffer::BigFloat,
output::BigFloat,
op::AddSubMul,
a::BigFloat,
x::BigFloat,
y::BigFloat,
args::Vararg{BigFloat,N},
) where {N}
operate_to!(buffer, *, x, y, args...)
return operate_to!(output, add_sub_op(op), a, buffer)
end
function buffered_operate!(
buffer::BigFloat,
op::AddSubMul,
x::BigFloat,
args::Vararg{Any,N},
) where {N}
return buffered_operate_to!(buffer, x, op, x, args...)
end
_scaling_to_bigfloat(x) = _scaling_to(BigFloat, x)
function operate_to!(
output::BigFloat,
op::Union{typeof(+),typeof(-),typeof(*)},
args::Vararg{Scaling,N},
) where {N}
return operate_to!(output, op, _scaling_to_bigfloat.(args)...)
end
function operate_to!(
output::BigFloat,
op::AddSubMul,
x::Scaling,
y::Scaling,
z::Scaling,
args::Vararg{Scaling,N},
) where {N}
return operate_to!(
output,
op,
_scaling_to_bigfloat(x),
_scaling_to_bigfloat(y),
_scaling_to_bigfloat(z),
_scaling_to_bigfloat.(args)...,
)
end
# Called for instance if `args` is `(v', v)` for a vector `v`.
function operate_to!(
output::BigFloat,
op::AddSubMul,
x,
y,
z,
args::Vararg{Any,N},
) where {N}
return operate_to!(output, add_sub_op(op), x, *(y, z, args...))
end