# An idea for using ccall less in BigFloat operations

**URL:** <https://discourse.julialang.org/t/an-idea-for-using-ccall-less-in-bigfloat-operations/92755>\
**Category:** Internals & Design\
**Created:** [January 10, 2023, 1:15pm UTC](https://discourse.julialang.org/t/an-idea-for-using-ccall-less-in-bigfloat-operations/92755 "2023-01-10T13:15:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [January 10, 2023, 1:15pm UTC](https://discourse.julialang.org/t/an-idea-for-using-ccall-less-in-bigfloat-operations/92755/1 "2023-01-10T13:15:05Z")

</div>

I got an idea for a way to slightly decrease the `ccall`-ing of libmpfr within the `Base.MPFR` module, which implements `BigFloat`.

Julia defines the `BigFloat` type as a `mutable struct` with a number of fields, and it seems like it could be worthwhile to mutate the fields directly in more places withing mpfr.jl, instead of calling C:

> <https://github.com/JuliaLang/julia/blob/6deb98f56ef3c2b6ff3d4364c966c26e43895da6/base/mpfr.jl#L90-L96>

For example, currently the method `-(::BigFloat)` in mpfr.jl `ccall`s `mpfr_neg`:

> <https://github.com/JuliaLang/julia/blob/6deb98f56ef3c2b6ff3d4364c966c26e43895da6/base/mpfr.jl#L616-L620>

The `ccall` overhead seems unnecessary however, as all that’s necessary for implementing `-(::BigFloat)` is copying the contents and, flipping the `sign` field of the copy, and returning the copy.

The trouble is with the `BigFloat` field that holds the mantissa, `_d`. It’s a `String`, an immutable type, and I’m not sure how to safely copy an immutable object that’s going to be mutated by non-Julia code later. It seems an implementation of such a copy method already exists here, but I don’t understand it or feel comfortable adapting the code:

> <https://github.com/JuliaLang/julia/blob/6deb98f56ef3c2b6ff3d4364c966c26e43895da6/base/mpfr.jl#L1083-L1092>

If anyone get’s around to improving `-(::BigFloat)` in this manner, it would probably also make sense to add `abs(::BigFloat)`, implemented analogously. Similarly for functions that only modify the exponent.
