github.com/SahandAslani/gomobile@v0.0.0-20210909130135-2cb2d44c09b2/exp/f32/f32.go (about)

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:generate go run gen.go -output table.go
     6  
     7  // Package f32 implements some linear algebra and GL helpers for float32s.
     8  //
     9  // Types defined in this package have methods implementing common
    10  // mathematical operations. The common form for these functions is
    11  //
    12  //	func (dst *T) Op(lhs, rhs *T)
    13  //
    14  // which reads in traditional mathematical notation as
    15  //
    16  //	dst = lhs op rhs.
    17  //
    18  // It is safe to use the destination address as the left-hand side,
    19  // that is, dst *= rhs is dst.Mul(dst, rhs).
    20  //
    21  // WARNING
    22  //
    23  // The interface to this package is not stable. It will change considerably.
    24  // Only use functions that provide package documentation. Semantics are
    25  // non-obvious. Be prepared for the package name to change.
    26  package f32 // import "github.com/SahandAslani/gomobile/exp/f32"
    27  
    28  import (
    29  	"encoding/binary"
    30  	"fmt"
    31  	"math"
    32  )
    33  
    34  type Radian float32
    35  
    36  func Cos(x float32) float32 {
    37  	const n = sinTableLen
    38  	i := uint32(int32(x * (n / math.Pi)))
    39  	i += n / 2
    40  	i &= 2*n - 1
    41  	if i >= n {
    42  		return -sinTable[i&(n-1)]
    43  	}
    44  	return sinTable[i&(n-1)]
    45  }
    46  
    47  func Sin(x float32) float32 {
    48  	const n = sinTableLen
    49  	i := uint32(int32(x * (n / math.Pi)))
    50  	i &= 2*n - 1
    51  	if i >= n {
    52  		return -sinTable[i&(n-1)]
    53  	}
    54  	return sinTable[i&(n-1)]
    55  }
    56  
    57  func Sqrt(x float32) float32 {
    58  	return float32(math.Sqrt(float64(x))) // TODO(crawshaw): implement
    59  }
    60  
    61  func Tan(x float32) float32 {
    62  	return float32(math.Tan(float64(x))) // TODO(crawshaw): fast version
    63  }
    64  
    65  // Bytes returns the byte representation of float32 values in the given byte
    66  // order. byteOrder must be either binary.BigEndian or binary.LittleEndian.
    67  func Bytes(byteOrder binary.ByteOrder, values ...float32) []byte {
    68  	le := false
    69  	switch byteOrder {
    70  	case binary.BigEndian:
    71  	case binary.LittleEndian:
    72  		le = true
    73  	default:
    74  		panic(fmt.Sprintf("invalid byte order %v", byteOrder))
    75  	}
    76  
    77  	b := make([]byte, 4*len(values))
    78  	for i, v := range values {
    79  		u := math.Float32bits(v)
    80  		if le {
    81  			b[4*i+0] = byte(u >> 0)
    82  			b[4*i+1] = byte(u >> 8)
    83  			b[4*i+2] = byte(u >> 16)
    84  			b[4*i+3] = byte(u >> 24)
    85  		} else {
    86  			b[4*i+0] = byte(u >> 24)
    87  			b[4*i+1] = byte(u >> 16)
    88  			b[4*i+2] = byte(u >> 8)
    89  			b[4*i+3] = byte(u >> 0)
    90  		}
    91  	}
    92  	return b
    93  }