github.com/kitech/dl@v0.0.0-20201225001532-be4f4faa4070/asmcgocall/examples/sum.go (about)

     1  package main
     2  
     3  /*
     4  int sum(struct {
     5  		int p0;
     6  		int p1;
     7  		int p2;
     8  		int p3;
     9  		int p4;
    10  		int r;
    11  	} *a) {
    12  
    13  	//printf("p1 = %d, p2 = %d, p3 = %d, p4 = %d, p5 = %d, r = %d\n", a->p0, a->p1, a->p2, a->p3, a->p4, a->r);
    14  
    15  	a->r = a->p0 + a->p1 + a->p2 + a->p3 + a->p4;
    16  
    17  	// error code
    18  	return 0;
    19  }
    20  
    21  int regular_sum(int p0, int p1, int p2, int p3, int p4) {
    22  	return p0 + p1 + p2 + p3 + p4;
    23  }
    24  
    25  
    26  */
    27  import "C"
    28  
    29  import (
    30  	"unsafe"
    31  
    32  	"github.com/kitech/dl/asmcgocall"
    33  )
    34  
    35  var sumAsmcgocall = func(a0, a1, a2, a3, a4 int) int {
    36  	argv := struct {
    37  		a0, a1, a2, a3, a4 C.int
    38  		r                  C.int
    39  	}{C.int(a0), C.int(a1), C.int(a2), C.int(a3), C.int(a4), 0}
    40  	asmcgocall.Asmcc(C.sum, unsafe.Pointer(&argv))
    41  	return int(argv.r)
    42  }
    43  
    44  func sumCgocall(p0, p1, p2, p3, p4 C.int) int {
    45  	return int(C.regular_sum(p0, p1, p2, p3, p4))
    46  }