Steve,
That indeed seems to be the case: check out the output of `@code_llvm` on
the dummy functions I define below. LLVM bitcode can be a bit intimidating
to parse at first, but if you compare the two, you'll see that one has
branches for out of bounds and `foo2()` has `getelementptr inbounds` for
both return values.
function foo()
a = rand(10)
if a[3] < .5
return a[2]
else
return a[1]
end
end
function foo2()
a = rand(10)
@inbounds if a[3] < .5
return a[2]
else
return a[1]
end
end
@code_llvm foo()
@code_llvm foo2()
-Jacob
On Mon, Aug 4, 2014 at 3:10 PM, <[email protected]> wrote:
> Jacob,
>
> Just for clarification: if one writes "@inbounds" in front of an "if"
> statement, then its effect lasts for the entire if-else block and not just
> for the boolean expression of the "if"?
>
> -- Steve
>
>
> On Monday, August 4, 2014 1:45:21 PM UTC-4, [email protected] wrote:
>
>> Dear Julia users,
>>
>> The usage of the @inbounds macro is not explained the manual, and its
>> syntax appears to be strange. Consider the three functions at the end of
>> this posting. Only the third one works -- why?
>>
>> In general, I think @inbounds is broken. Besides the weird syntax, it
>> has two other issues. First, there is no way to apply the macro to one
>> subscript operation but not another in a long expression (as far as I
>> know). Second, it is not extensible in the sense that if programmer A
>> implements his/her own array-like structure with his/her own getindex and
>> setindex operations, he/she might like to have two versions of
>> getindex/setindex, one safe/slower and the other unsafe/faster, but there
>> is no way for programmer A to detect whether user B, a user of his/her new
>> array-like structure, has requested @inbounds or not.
>>
>> I would like to propose the following replacement for @inbounds, which
>> solves all three problems. Instead of a macro, there should be two
>> different subscript operations, say a[1] and a[$ 1 $], where the first is
>> safe/slow and the second is unsafe/fast. The compiler will compile the
>> first as getindex/setindex and the second as getindexUnsafe/setindexUnsafe.
>>
>> -- Steve Vavasis
>>
>>
>>
>> function sqrtfirst{T}(a::Array{T, 1})
>> @assert(size(a,1) >= 1)
>> @inbounds sqrt(a[1])
>> end
>>
>> function sqrtfirst{T}(a::Array{T, 1})
>> @assert(size(a,1) >= 1)
>> return @inbounds sqrt(a[1])
>> end
>>
>> function sqrtfirst{T}(a::Array{T, 1})
>> @assert(size(a,1) >= 1)
>> @inbounds return sqrt(a[1])
>> end
>>
>>
>>
>>