init: v1.0.0
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
OS=$(shell uname)
|
||||
|
||||
ifeq (${OS}, Linux)
|
||||
LIB=libgcl.so
|
||||
endif
|
||||
ifeq (${OS}, Darwin)
|
||||
LIB=libgcl.dylib
|
||||
endif
|
||||
|
||||
TEST=test
|
||||
|
||||
All: lib test
|
||||
|
||||
lib: staticlib gcl.c
|
||||
gcc -fPIC -shared -o ${LIB} -L. gcl.c -lgcl_go
|
||||
|
||||
test: lib main.c
|
||||
gcc -o ${TEST} main.c -L. -lgcl -lpthread
|
||||
|
||||
staticlib: lib.go
|
||||
go build -buildmode=c-archive -o libgcl_go.a lib.go
|
||||
android:
|
||||
GOOS=android GOARCH=arm64 go build -buildmode=c-archive -o libgcl_go.a lib.go
|
||||
clean:
|
||||
rm -f libgcl_go.a *.dylib *.so ${LIB} ${TEST}
|
||||
@@ -0,0 +1,35 @@
|
||||
# 简介
|
||||
输出gcl中的功能为C库
|
||||
|
||||
步骤:
|
||||
- lib.go中编写要输出的函数:
|
||||
```
|
||||
//export Sm3
|
||||
func Sm3(in []byte, out []byte) {
|
||||
d := sm3.Sum(in)
|
||||
copy(out, d[:])
|
||||
}
|
||||
```
|
||||
- 生成go导出的C库(c-archive)
|
||||
```
|
||||
go build -buildmode=c-archive -o libgcl_go.a lib.go
|
||||
```
|
||||
- lib.c中编写C包装代码
|
||||
```
|
||||
int sm3(void *data, int dataLen, void *digest){
|
||||
// input check, omit
|
||||
|
||||
GoSlice in = {
|
||||
data, dataLen, dataLen
|
||||
};
|
||||
GoSlice out = {
|
||||
digest, 32, 32
|
||||
};
|
||||
Sm3(in,out);
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
- gcc编译gcl.c为动态库
|
||||
```
|
||||
gcc -shared -o libgcl.dylib -L. -lgcl_go gcl.c
|
||||
```
|
||||
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"xdx.jelly/xgcl/sm/sm3"
|
||||
)
|
||||
|
||||
//export Sm3
|
||||
func Sm3(in []byte, out []byte) {
|
||||
d := sm3.Sum(in)
|
||||
copy(out, d[:])
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("...")
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#include "libgcl_go.h"
|
||||
|
||||
int sm3(void *data, int dataLen, void *digest){
|
||||
GoSlice in = {
|
||||
data, dataLen, dataLen
|
||||
};
|
||||
GoSlice out = {
|
||||
digest, 32, 32
|
||||
};
|
||||
Sm3(in,out);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef LIBGCL_H_
|
||||
#define LIBGCL_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
int sm3(void *data, int dataLen, void *digest);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // LIBGCL_H_
|
||||
@@ -0,0 +1,81 @@
|
||||
/* Code generated by cmd/cgo; DO NOT EDIT. */
|
||||
|
||||
/* package command-line-arguments */
|
||||
|
||||
|
||||
#line 1 "cgo-builtin-export-prolog"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifndef GO_CGO_EXPORT_PROLOGUE_H
|
||||
#define GO_CGO_EXPORT_PROLOGUE_H
|
||||
|
||||
#ifndef GO_CGO_GOSTRING_TYPEDEF
|
||||
typedef struct { const char *p; ptrdiff_t n; } _GoString_;
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/* Start of preamble from import "C" comments. */
|
||||
|
||||
|
||||
|
||||
|
||||
/* End of preamble from import "C" comments. */
|
||||
|
||||
|
||||
/* Start of boilerplate cgo prologue. */
|
||||
#line 1 "cgo-gcc-export-header-prolog"
|
||||
|
||||
#ifndef GO_CGO_PROLOGUE_H
|
||||
#define GO_CGO_PROLOGUE_H
|
||||
|
||||
typedef signed char GoInt8;
|
||||
typedef unsigned char GoUint8;
|
||||
typedef short GoInt16;
|
||||
typedef unsigned short GoUint16;
|
||||
typedef int GoInt32;
|
||||
typedef unsigned int GoUint32;
|
||||
typedef long long GoInt64;
|
||||
typedef unsigned long long GoUint64;
|
||||
typedef GoInt64 GoInt;
|
||||
typedef GoUint64 GoUint;
|
||||
typedef size_t GoUintptr;
|
||||
typedef float GoFloat32;
|
||||
typedef double GoFloat64;
|
||||
#ifdef _MSC_VER
|
||||
#include <complex.h>
|
||||
typedef _Fcomplex GoComplex64;
|
||||
typedef _Dcomplex GoComplex128;
|
||||
#else
|
||||
typedef float _Complex GoComplex64;
|
||||
typedef double _Complex GoComplex128;
|
||||
#endif
|
||||
|
||||
/*
|
||||
static assertion to make sure the file is being used on architecture
|
||||
at least with matching size of GoInt.
|
||||
*/
|
||||
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];
|
||||
|
||||
#ifndef GO_CGO_GOSTRING_TYPEDEF
|
||||
typedef _GoString_ GoString;
|
||||
#endif
|
||||
typedef void *GoMap;
|
||||
typedef void *GoChan;
|
||||
typedef struct { void *t; void *v; } GoInterface;
|
||||
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
|
||||
|
||||
#endif
|
||||
|
||||
/* End of boilerplate cgo prologue. */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern void Sm3(GoSlice in, GoSlice out);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "gcl.h"
|
||||
|
||||
|
||||
int main() {
|
||||
int msgLen = 10*1024*1024;
|
||||
char *msg = malloc(msgLen);
|
||||
char digest[32];
|
||||
|
||||
clock_t start, end;
|
||||
int count = 100;
|
||||
start = clock();
|
||||
for (int i = 0; i < count; i++) {
|
||||
sm3(msg, msgLen, &digest);
|
||||
}
|
||||
end = clock();
|
||||
printf("sm3: %.1fMBps\n", (double)count * msgLen/1024/1024/ ((double)(end - start) / CLOCKS_PER_SEC));
|
||||
free(msg);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user