Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add primitive type tutorial blog #321

Closed
wants to merge 2 commits into from
Closed

Conversation

yuehhua
Copy link
Contributor

@yuehhua yuehhua commented Mar 2, 2019

With English and Traditional Chinese version.

@yuehhua
Copy link
Contributor Author

yuehhua commented Mar 2, 2019

@ViralBShah
@iblis17

@ViralBShah
Copy link
Member

We need some reviews.

@yuehhua
Copy link
Contributor Author

yuehhua commented Mar 6, 2019

Sure.

author: 鄭景文(Peter Cheng), 杜岳華(Yueh-Hua Tu)
---

Julia provide some APIs for you to declare your own primitive types in Julia. However, when you open [the official docs about primitive type](https://docs.julialang.org/en/v1/manual/types/#Primitive-Types-1), you still don't know how to make your new primitive types work in your code. Therefore, here is a simple tutorial that tell you how to do so.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Julia provides

No need to repeat "in Julia" at the end :)

primitive type «name» «bits» end
primitive type «name» <: «supertype» «bits» end

Currently, Julia only support sizes that are multiples of 8 bits. So like the `Bool` only take 1 bit, you still need to declare it as 8 bits. For example:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Julia only supports
So even though Bool only takes 1 bit, you need to declare it as 8 bits

@StefanKarpinski
Copy link
Member

In the URL: primitiv => primitive.

@chengchingwen
Copy link
Member

There is an English version included. We wrote in both Chinese and English


1. `Core.Intrinsics.bitcase(type, value)`

`bitcase` told the LLVM to make a new same sized value of a primitive type instance `value` and mark it as type `type`. The type of `value` must have the same bitsize as `type`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why these spaces between value and "and". Same applies to next lines

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I guess it's due to some problems in my editor, let me remove it.

primitive type chinese standard

author name

primitive type chinese reference

fix primitive type chinese typo

Update blog/_posts/2019-03-02-custom-primitiv-type-zh_tw.md

Co-Authored-By: chengchingwen <adgjl5645@hotmail.com>

Update blog/_posts/2019-03-02-custom-primitiv-type-zh_tw.md

Co-Authored-By: chengchingwen <adgjl5645@hotmail.com>

Update blog/_posts/2019-03-02-custom-primitiv-type-zh_tw.md

Co-Authored-By: chengchingwen <adgjl5645@hotmail.com>

Update blog/_posts/2019-03-02-custom-primitiv-type-zh_tw.md

Co-Authored-By: chengchingwen <adgjl5645@hotmail.com>

Update blog/_posts/2019-03-02-custom-primitiv-type-zh_tw.md

Co-Authored-By: chengchingwen <adgjl5645@hotmail.com>

Update blog/_posts/2019-03-02-custom-primitiv-type-zh_tw.md

Co-Authored-By: chengchingwen <adgjl5645@hotmail.com>

Update blog/_posts/2019-03-02-custom-primitiv-type-zh_tw.md

Co-Authored-By: chengchingwen <adgjl5645@hotmail.com>

Update blog/_posts/2019-03-02-custom-primitiv-type-zh_tw.md

Co-Authored-By: chengchingwen <adgjl5645@hotmail.com>

Update blog/_posts/2019-03-02-custom-primitiv-type.md

Co-Authored-By: chengchingwen <adgjl5645@hotmail.com>

Update blog/_posts/2019-03-02-custom-primitiv-type.md

Co-Authored-By: chengchingwen <adgjl5645@hotmail.com>

Update blog/_posts/2019-03-02-custom-primitiv-type.md

Co-Authored-By: chengchingwen <adgjl5645@hotmail.com>

Update blog/_posts/2019-03-02-custom-primitiv-type.md

Co-Authored-By: chengchingwen <adgjl5645@hotmail.com>

Update blog/_posts/2019-03-02-custom-primitiv-type.md

Co-Authored-By: chengchingwen <adgjl5645@hotmail.com>

Update blog/_posts/2019-03-02-custom-primitiv-type.md

Co-Authored-By: chengchingwen <adgjl5645@hotmail.com>

add s, remove space

fix typo in filename
Co-Authored-By: chengchingwen <adgjl5645@hotmail.com>
@chethega
Copy link

Do we really want to document/blog about this as long as

julia> primitive type B 24 end
julia> B(i)=Core.Intrinsics.trunc_int(B,Int128(i));

julia> [B(i) for i=1:100]
signal (11): Segmentation fault

aka JuliaLang/julia#29193 JuliaLang/julia#29053 JuliaLang/julia#26026 ?

I'd feel much better if we deferred documentation / public blogging / advertisement of primitive types until they actually work reliably.

@chethega
Copy link

FWIW, the same issue appears with your bitsize, just harder to spot. Example:

julia> primitive type A 72 end
julia> A(i)=Core.Intrinsics.zext_int(A,i);
julia> arr=fill(A(1), 10^7);
ERROR: ReadOnlyMemoryError()
julia> arr=[A(i) for i=1:30]
30-element Array{A,1}:
 A(0x000000000000000001)
 A(0x000000000000000002)
 A(0x000000000000000003)
 A(0x000000000000000004)
 A(0x000000000000000005)
 A(0x000000000000000006)
 A(0x000000000000000007)
 A(0x000000000000000008)
 A(0x000000000000000009)
 A(0x00000000000000000a)
 ⋮                      
 A(0x600000000000000023)
 A(0xc000007fa6e4335630)
 A(0x020000000000000001)
 A(0x030000000000000023)
 A(0x050000000000000004)
 A(0x070000000000000006)
 A(0x090000000000000008)
 A(0x0b000000000000000a)
 A(0x0d000000000000000c)

The issue is that everybody has different opinions on whether the size of A is 9 byte or 16 byte (8 byte + 1 byte + 7 bytes padding for alignment). Hence the allocated buffer is sometimes too small and we corrupt memory. Also, arrayref, unsafe_load(pointer(arr, i)), unsafe_load(pointer(arr), i)) are inconsistent in computing offsets, and I think arrayref is inconsistent between interpretation (REPL) and compiled code.

@yuehhua
Copy link
Contributor Author

yuehhua commented Apr 11, 2019

@chethega Thank you for telling us these issues.
Maybe we could hold on the PR until the issues are solved.
@chengchingwen How do you think?

@chengchingwen
Copy link
Member

Ah, so that's the reason why there is no documentation yet. @yuehhua I'm fine with it, but after that we might need to change the content a little bit.

@ViralBShah
Copy link
Member

Bump. Is this happening?

@chengchingwen
Copy link
Member

@ViralBShah As @chethega mentioned, there are still some bugs of primitive type. Should we just ignore those bugs, or should we close this PR for now?

@ViralBShah
Copy link
Member

We can close for now and always reopen in the future

@ViralBShah ViralBShah closed this May 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants