xref: /aosp_15_r20/external/libcap/contrib/bug216610/go/fibber/fib.go (revision 2810ac1b38eead2603277920c78344c84ddf3aff)
1*2810ac1bSKiyoung Kim// Package fibber implements a Fibonacci sequence generator using a C
2*2810ac1bSKiyoung Kim// coded compute kernel (a .syso file).
3*2810ac1bSKiyoung Kimpackage fibber
4*2810ac1bSKiyoung Kim
5*2810ac1bSKiyoung Kimimport (
6*2810ac1bSKiyoung Kim	"unsafe"
7*2810ac1bSKiyoung Kim)
8*2810ac1bSKiyoung Kim
9*2810ac1bSKiyoung Kim// State is the native Go form of the C.state structure.
10*2810ac1bSKiyoung Kimtype State struct {
11*2810ac1bSKiyoung Kim	B, A uint32
12*2810ac1bSKiyoung Kim}
13*2810ac1bSKiyoung Kim
14*2810ac1bSKiyoung Kim// cPtr converts State into a C pointer suitable as an argument for
15*2810ac1bSKiyoung Kim// sysoCaller.
16*2810ac1bSKiyoung Kimfunc (s *State) cPtr() unsafe.Pointer {
17*2810ac1bSKiyoung Kim	return unsafe.Pointer(&s.B)
18*2810ac1bSKiyoung Kim}
19*2810ac1bSKiyoung Kim
20*2810ac1bSKiyoung Kim// NewState initializes a Fibonacci Number sequence generator.  Upon
21*2810ac1bSKiyoung Kim// return s.A=0 and s.B=1 are the first two numbers in the sequence.
22*2810ac1bSKiyoung Kimfunc NewState() *State {
23*2810ac1bSKiyoung Kim	s := &State{}
24*2810ac1bSKiyoung Kim	syso__fib_init.call(s.cPtr())
25*2810ac1bSKiyoung Kim	return s
26*2810ac1bSKiyoung Kim}
27*2810ac1bSKiyoung Kim
28*2810ac1bSKiyoung Kim// Next advances the state to the next number in the sequence. Upon
29*2810ac1bSKiyoung Kim// return, s.B is the most recently calculated value.
30*2810ac1bSKiyoung Kimfunc (s *State) Next() {
31*2810ac1bSKiyoung Kim	syso__fib_next.call(s.cPtr())
32*2810ac1bSKiyoung Kim}
33