Reinterpreting bits types with rem?

55 views
Skip to first unread message

Galen Lynch

unread,
Jul 28, 2015, 9:08:12 AM7/28/15
to julia-users
Does rem(x, bitsT) or x % bitsT reinterpret the bits in variable x as a bits type T, and is this "in place"? I noticed that in some places when Julia gets a pointer from ccall it uses " % T" after the ccall,  presumably to give Julia information about its type (boxing it?). Simple tests, such as typemax(UInt32) % Int32 seem to behave like reinterpreting the underlying bits.

Yichao Yu

unread,
Jul 28, 2015, 9:18:07 AM7/28/15
to Julia Users
On Tue, Jul 28, 2015 at 9:02 AM, Galen Lynch <galen...@gmail.com> wrote:
> Does rem(x, bitsT) or x % bitsT reinterpret the bits in variable x as a bits

AFAICT, `rem` is basically doing the C cast.

> type T, and is this "in place"? I noticed that in some places when Julia

Bitstype are immutable so this is never in place. However, it doesn't
need any boxing either. It should be as efficient as when you write a
c cast in c.

```julia
julia> @code_llvm rem(1, UInt32)

define i32 @julia_rem_20919(i64, %jl_value_t*) {
top:
%2 = trunc i64 %0 to i32
ret i32 %2
}
```

> gets a pointer from ccall it uses " % T" after the ccall, presumably to
> give Julia information about its type (boxing it?). Simple tests, such as

I don't see a method for rem that applies to `Ptr`. Where do you see it?

Galen Lynch

unread,
Jul 28, 2015, 10:03:28 AM7/28/15
to julia-users, yyc...@gmail.com
Awesome, thanks for your reply!

C cast does a type conversion on the value, right? For example, you could cast an integer 1 into a float 1.0 with cast. It seems like Julia's rem is doing something a little different, as doing `1 % Float32` is invalid.

I don't see a method for rem that applies to `Ptr`. Where do you see it? 

Ah, you're right, I was reading over it too quickly and misunderstood the code. Here's the line I was thinking of (in isostream.jl, line 169)
```
function read{T<:Union{UInt16, Int16, UInt32, Int32, UInt64, Int64}}(s::IOStream, ::Type{T}) ccall(:jl_ios_get_nbyte_int, UInt64, (Ptr{Void}, Csize_t), s.ios, sizeof(T)) % T end ``` Thanks again!

Yichao Yu

unread,
Jul 28, 2015, 10:14:29 AM7/28/15
to Galen Lynch, julia-users
On Tue, Jul 28, 2015 at 10:03 AM, Galen Lynch <galen...@gmail.com> wrote:
Awesome, thanks for your reply!

C cast does a type conversion on the value, right? For example, you could cast an integer 1 into a float 1.0 with cast. It seems like Julia's rem is doing something a little different, as doing `1 % Float32` is invalid.

Well, it doesn't allow pointer either. What I meant was that for conversions that are allowed by `rem`, it basically behave like a c cast. (you can use `methods(rem)` to see what it is defined for).

This is different from the normal convert which will give you an error on overflow.

Galen Lynch

unread,
Jul 28, 2015, 10:40:11 AM7/28/15
to julia-users, yyc...@gmail.com
Ah ok that makes sense.
Reply all
Reply to author
Forward
0 new messages