I feel my suggestion with Ref went a bit unnoticed..
const a = Ref(0.0)
inc_ref() = a[] += 1
@code_llvm inc_ref()
define i64 @julia_inc_ref_50057() #0 {
top:
%0 = load i64, i64* inttoptr (i64 140258091307984 to i64*), align 16
%1 = add i64 %0, 1
store i64 %1, i64* inttoptr (i64 140258091307984 to i64*), align 16
ret i64 %1
}
type wrapper{T}
x::T
end
const w = wrapper(0.0)
function inc_global()
w.x += 1
end
@code_llvm inc_global()
define double @julia_inc_global_50131() #0 {
top:
%0 = load double, double* inttoptr (i64 140258000874464 to double*),
align 32
%1 = fadd double %0, 1.000000e+00
store double %1, double* inttoptr (i64 140258000874464 to double*), align
32
ret double %1
}
Same code is generated so Ref is pretty much the official way of doing this
type of wrapping.
On Tuesday, June 14, 2016 at 6:51:32 AM UTC+2, Eric Forgy wrote:
>
> Creating a global instance of a special type seems reasonable to me. I
> also use global Dict's for this kind of thing.
>
> Instead of inc_global, I might define
>
> Base.(:+)(w::wrapper,s) = w.x + s
>
> so you can just do `w += 1` :)
>
> If you find a better way, I would love to see it.
>
> PS: Minor point, but looks like you have a typo. Should be `const w =
> wrapper(0.0)` I think. No biggie.
>
> On Tuesday, June 14, 2016 at 8:41:40 AM UTC+8, Arch Robison wrote:
>>
>> Indeed the one-element vector case was what inspired my wrapper. I
>> created the wrapper to avoid the run-time bounds check on the vector,
>> though I suppose I could sprinkle @inbounds to eliminate it.
>>
>> On Mon, Jun 13, 2016 at 6:40 PM, Eric Forgy <[email protected]> wrote:
>>
>>> Not sure if this is the best way (curious about 'Ref'), but you could
>>> also make x a 1-element vector:
>>>
>>> const x = [0]
>>>
>>> x[1] += 1
>>>
>>
>>