/Src/PKG/Math/abs. Go source code reading and talking about golang and compilation

Source: Internet
Author: User

Fragment at the beginning:

When I joined the public platform, I started to sort strings. After all, golang was a little time-consuming. Many things could be found on the Internet and they could be found directly on the Internet, the result is that several examples of Code are not easy to use, so it is easy to use. It is implemented from the beginning, and there are too many code. I thought, Google should encapsulate all these things. Otherwise, a new language only has basic syntax, and there is no powerful standard library. Who should use it. That is, the first time I came into contact with the SRC folder, I found that those go files in PKG are excellent learning materials.

So where can I start with the introduction of many files and folders? In my principle, first find those with no dependencies, that is, those without import, and then find the math folder. Stupid method, starting from a in order. This won't happen to ABS. Go.

Lines 3 that are hard to understand:

// Copyright 2009 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package math// Abs returns the absolute value of x.//// Special cases are://    Abs(±Inf) = +Inf//    Abs(NaN) = NaNfunc Abs(x float64) float64func abs(x float64) float64 {    switch {    case x < 0:        return -x    case x == 0:        return 0 // return correctly abs(-0)    }    return x}

A single line, a method declaration, nothing else, completely puzzled.

The ABS () below doesn't need to be mentioned. It's very simple. Take the absolute value.

No matter what, go to the math folder and check whether the three files abs_386.s, abs_amd64.s, and abs_arm.s are related to the code that the line does not know ,. S end, assembly language files, continue to launch the power of Google, golang, assembly mixing, so you find the http://www.mikespook.com/2013/02/%E7%BF%BB%E8%AF%91go-%E5%92%8C%E6%B1%87%E7%BC%96/

Run the program:

Let's start this program first, because my machine is 64-bit, so I put abs. go and abs_amd64.s files are copied elsewhere, Abs. the package of Go is changed to mymath, and a simple test program is written.

package mainimport(    "fmt"    "mymath")func main(){    fmt.Println(mymath.Abs(-12.00))}

There are many useful tools in/PKG/tool/windows_amd64, such as 6g and 6l, but common tools are encapsulated by go commands, directly go build, Go install, and other commands.

The Assembly is mainly involved in 6a. The above code is compiled in the following order:

6a abs_amd64.s (generate abs_amd64.6)

6g-o _ gO _. 6 Abs. Go (generate _ gO _. 6)

Pack GRC abs. A _ gO _. 6 abs_amd64.6 (generate abs.)

Originally, we wanted the main program to directly call the library in the directory. Import ". /mymath ". However, an error occurs in the import path contains invalid characte ':" C:/XXX/xxx "in windows. A is thrown to the PKG/windows_amd64 folder.

The rest is:

6g test. Go (generate test.6)

6l test.6(generate 6.out.exe)

Start with a simple compilation automatically generated by the golang Compiler:

Let's first look at a new command. The golang compiler automatically generates the intermediate code assembly command, go tool 6g-s XXX. in fact, the above commands can also be replaced by go tool XXX. The go commands encapsulate these commands.

Here is the simplest code:

package asmfunc Asm(i int)int{    return i}

Go tool 6g-s ASM. Go:

--- prog list "Asm" ---0000 (asm.go:3) TEXT    Asm+0(SB),$0-160001 (asm.go:3) LOCALS  ,$00002 (asm.go:3) TYPE    i+0(FP){int},$80003 (asm.go:3) TYPE    ~anon1+8(FP){int},$80004 (asm.go:4) MOVQ    i+0(FP),BX0005 (asm.go:4) MOVQ    BX,~anon1+8(FP)0006 (asm.go:4) RET     ,

The syntax of plan9 assembly is quite similar to that of at&t. Passing values is the source, followed by the purpose. This is the opposite of MASM and NASM.

000 rows:Text is equivalent to defining a function. The ASM function name, which is generated by the + 0 (SB) golang function; $0-16, after repeated attempts, at least for int and float64, yes (number of parameters + number of returned values) x 8 (these are verified by myself and there is no scientific basis. I have also read some related documents, however, the bird language is not enough. I will make bold assumptions that I don't need anything that I can understand, so I can't do it right now ).

001 rows:I guess it is the position where the command is executed, but this is not important. The key is the back-end.

002 and 003 rows:I is the variable name ,~ Anon1 is actually a variable name (automatically generated by the system)

Slightly modify

func Asm(i int) (j int){    j=i    return}

Then, row 003 becomes J + 8 (FP)

As for 0 (FP) and 8 (FP), for int, each number occupies 8 bytes (64-bit), so the input parameter, the first is + 0 (FP), the second is + 8 (FP), the third is + 16 (FP), and the fourth is + 24 (FP )...

Return value. If there are multiple return values, the first plus (8 + the value of the last input parameter) (FP), followed by plus eight

{Int} indicates the data type. $8 indicates that it occupies 8 bytes.

Row 004:Pass the parameter value to the registers BX, movq, and pass four characters

Line 005:Pass the value in Bx to the return value.

Line 006:RET

Let's see what float64 looks like:

package asmfunc Asm(f float64)float64{    return f}
--- prog list "Asm" ---0000 (asm.go:3) TEXT    Asm+0(SB),$0-160001 (asm.go:3) LOCALS  ,$00002 (asm.go:3) TYPE    i+0(FP){int},$80003 (asm.go:3) TYPE    ~anon1+8(FP){float64},$80004 (asm.go:4) MOVQ    i+0(FP),X00005 (asm.go:4) MOVQ    X0,~anon1+8(FP)0006 (asm.go:4) RET     ,

It can be seen that it is roughly the same as the previous attempt to use int, but the Bx register is changed to x0. It can be inferred that x0 is a floating point register, and x0 is available. We can boldly speculate that there will be x1, x2, X3...

Try it.

package asmfunc Asm(f1,f2 float64) float64{    return f1+f2}
--- prog list "Asm" ---0000 (asm.go:3) TEXT    Asm+0(SB),$0-240001 (asm.go:3) LOCALS  ,$00002 (asm.go:3) TYPE    f1+0(FP){float64},$80003 (asm.go:3) TYPE    f2+8(FP){float64},$80004 (asm.go:3) TYPE    ~anon2+16(FP){float64},$80005 (asm.go:4) MOVSD   f1+0(FP),X00006 (asm.go:4) MOVSD   f2+8(FP),X10007 (asm.go:4) ADDSD   X1,X00008 (asm.go:4) MOVSD   X0,~anon2+16(FP)0009 (asm.go:4) RET     ,

Abs_amd64.s:

// Copyright 2010 The Go Authors.  All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.// func Abs(x float64) float64TEXT ·Abs(SB),7,$0        MOVQ   $(1<<63), BX        MOVQ   BX, X0 // movsd $(-0.0), x0        MOVSD  x+0(FP), X1    ANDNPD X1, X0    MOVSD  X0, ret+8(FP)    RET

This is the end of the competition.

The first line should be fixed format, and the function name should be replaced.

MOVQ   $(1<<63), BXMOVQ   BX, X0

1 move 63 bits to the right and pass them to x0. In this case, the x0 binary value is 1000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000.

MOVSD  x+0(FP), X1

Movsd moves the scalar double-precision floating point value and transmits the value of parameter X to X1

ANDNPD X1, X0

Andnpd compresses the logical bitwise AND non-precision of double-precision floating-point values. It reverts the target operand and then executes the logical bitwise "and" operation with the source operand. The result is stored to the target operand.

That is, the result is reversed to x0 and then stored in x0 with the X1 phase.

The above operation completes the task of getting the absolute value from the zero position of the symbol.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.