summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorLaban Kimotho <laban@amazon.com>2014-01-24 10:59:53 +0200
committerLaban Kimotho <laban@amazon.com>2014-01-24 10:59:53 +0200
commit400b00aa87a98f0601a566f94038d2b2197aae1a (patch)
tree3df562bd82daa54782378faf035d69f32845a5b8
parentbb7d69154d2ed3afae1a415942dd59646b65a2d5 (diff)
typo fixes + view generated low level code
-rw-r--r--julia.html.markdown79
1 files changed, 77 insertions, 2 deletions
diff --git a/julia.html.markdown b/julia.html.markdown
index 4b946d46..58ff7608 100644
--- a/julia.html.markdown
+++ b/julia.html.markdown
@@ -427,7 +427,7 @@ end
keyword_args(name2="ness") #=> ["name2"=>"ness","k1"=>4]
keyword_args(k1="mine") #=> ["k1"=>"mine","name2"=>"hello"]
-keyword_args() #=> ["name2"=>"hello","k2"=>4]
+keyword_args() #=> ["name2"=>"hello","k1"=>4]
# You can combine all kinds of arguments in the same function
function all_the_args(normal_arg, optional_positional_arg=2; keyword_arg="foo")
@@ -560,7 +560,7 @@ type Panther <: Cat # Panther is also a subtype of Cat
Panther() = new("green")
# Panthers will only have this constructor, and no default constructor.
end
-# Using inner constructors, like Panter does, gives you control
+# Using inner constructors, like Panther does, gives you control
# over how values of the type can be created.
# When possible, you should use outer constructors rather than inner ones.
@@ -657,6 +657,81 @@ fight(Lion("RAR"),Lion("brown","rarrr")) #=> prints The victorious cat says rarr
fight(l::Lion,l2::Lion) = println("The lions come to a tie")
fight(Lion("RAR"),Lion("brown","rarrr")) #=> prints The lions come to a tie
+
+# Under the hood
+# You can take a look at the llvm intermediate code and the assembly code generated.
+
+square_area(l) = l * l # square_area (generic function with 1 method)
+
+square_area(5) #25
+
+code_native(square_area, (Int32,)) # What happens when we feed square_area an integer?
+ # .section __TEXT,__text,regular,pure_instructions
+ # Filename: none
+ # Source line: 1 # Prologue
+ # push RBP
+ # mov RBP, RSP
+ # Source line: 1
+ # movsxd RAX, EDI # Fetch l from memory?
+ # imul RAX, RAX # 32bit square of l and store the result in RAX
+ # pop RBP # Restore old base pointer
+ # ret # Result will still be in RAX
+
+code_native(square_area, (Float32,))
+ # .section __TEXT,__text,regular,pure_instructions
+ # Filename: none
+ # Source line: 1
+ # push RBP
+ # mov RBP, RSP
+ # Source line: 1
+ # vmulss XMM0, XMM0, XMM0 # Scalar single precision multiplication (AVX) (in this case square the number)
+ # pop RBP
+ # ret
+
+code_native(square_area, (Float64,))
+ # .section __TEXT,__text,regular,pure_instructions
+ # Filename: none
+ # Source line: 1
+ # push RBP
+ # mov RBP, RSP
+ # Source line: 1
+ # vmulsd XMM0, XMM0, XMM0 # Scalar double precision multiplacation (AVX)
+ # pop RBP
+ # ret
+ #
+# Note that julia will use floating point instructions if any of the arguements are floats.
+# Let's calculate the area of a circle
+circle_area(r) = pi * r * r # circle_area (generic function with 1 method)
+circle_area(5) # 78.53981633974483
+
+code_native(circle_area, (Int32,))
+ # .section __TEXT,__text,regular,pure_instructions
+ # Filename: none
+ # Source line: 1
+ # push RBP
+ # mov RBP, RSP
+ # Source line: 1
+ # vcvtsi2sd XMM0, XMM0, EDI # Load integer (r) from memory
+ # movabs RAX, 4593140240 # Load pi
+ # vmulsd XMM1, XMM0, QWORD PTR [RAX] # pi * r
+ # vmulsd XMM0, XMM0, XMM1 # (pi * r) * r
+ # pop RBP
+ # ret
+ #
+
+code_native(circle_area, (Float64,))
+ # .section __TEXT,__text,regular,pure_instructions
+ # Filename: none
+ # Source line: 1
+ # push RBP
+ # mov RBP, RSP
+ # movabs RAX, 4593140496
+ # Source line: 1
+ # vmulsd XMM1, XMM0, QWORD PTR [RAX]
+ # vmulsd XMM0, XMM1, XMM0
+ # pop RBP
+ # ret
+ #
```
## Further Reading