bmnumbers

package
v0.0.0-...-66f9876 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 24, 2025 License: Apache-2.0 Imports: 18 Imported by: 0

README

BMNumbers

bmnumbers is part of BondMachine project. bmnumbers is both a command line tool to convert or cast numbers to and from different formats and a library to do the same. It is used within the BondMachine every time numbers are handled.

Installation

go install github.com/BondMachineHQ/BondMachine/cmd/bmnumbers@latest

Command Line Options

Option Description
-convert <type> Convert the input number to the specified type
-cast <type> Cast the input number to the specified type (reinterpret bits)
-show <format> Output format: native (default), bin, or unsigned
-with-size Include size information in binary output
-get-prefix <type> Get the prefix for a number type
-get-size <type> Get the bit size of a number type
-get-instructions <type> Get hardware instructions for a number type (JSON)
-use-files Process CSV files instead of command line arguments
-omit-prefix Omit type prefix in output
-serve Start REST API server
-linear-data-range <file> Load a linear data range file
-v Enable verbose output
-d Enable debug output

Usage Examples

Basic Number Input
# Parse and display a number
bmnumbers 0u42        # unsigned integer: 42
bmnumbers 0x2a        # hexadecimal: 0x<8>2a
bmnumbers 0b101010    # binary: 0b<6>101010
bmnumbers 0f32.5      # float32: 0f<32>32.5
Show Output in Different Formats
# Show as binary
bmnumbers -show bin 0u42           # Output: 101010
bmnumbers -show bin -with-size 0u42  # Output: 0b<64>101010

# Show as unsigned integer
bmnumbers -show unsigned 0f32.5    # Output: 1107427328
Cast Between Types
# Cast binary to unsigned (reinterpret the bits)
bmnumbers -cast unsigned 0b101010  # Output: 42
Get Type Information
# Get the prefix for a type
bmnumbers -get-prefix unsigned     # Output: 0u
bmnumbers -get-prefix float32      # Output: 0f<32>

# Get the size of a type
bmnumbers -get-size float32        # Output: 32

# Get hardware instructions for a type
bmnumbers -get-instructions float32
# Output: {"addop":"addf","divop":"divf","multop":"multf","powop":"multf"}
Process CSV Files
# Process numbers in CSV files (creates output files with .out extension)
bmnumbers -use-files -cast unsigned input.csv

Supported number types

The supported number types are listed in the following table.

Type Name Prefixes Description Static Length
unsigned none
0u
0d
Unsigned integer yes any
signed 0s
0sd
Signed integer yes any
bin 0b
0b<s>
Binary number yes any
s bits
hex 0x Hexadecimal number yes any
float16 0f<16> IEEE 754 half precision floating point number yes 16 bits
float32 0f
0f<32>
IEEE 754 single precision floating point number yes 32 bits
lqs[s]t[t] 0lq<s.t> Linear quantized number with size s and type t no s bits
fps[s]f[f] 0fp<s.f> Fixed point number with size s and fraction f no s bits
flp[e]f[f] 0flp<e.f> FloPoCo floating point number with exponent e and mantissa f no e+f+3 bits

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AllDynamicalTypes []DynamicalType
View Source
var AllMatchers map[string]ImportFunc
View Source
var AllTypes []BMNumberType

Functions

func CastType

func CastType(n *BMNumber, t BMNumberType) error

func EventuallyCreateType

func EventuallyCreateType(name string, param interface{}) (bool, error)

func ListTypes

func ListTypes()

func LoadLinearDataRangesFromFile

func LoadLinearDataRangesFromFile(filename string) error

func Serve

func Serve(conf *BMNumberConfig)

REST API to convert numbers

Types

type BMNumber

type BMNumber struct {
	// contains filtered or unexported fields
}

BMNumber is a binary representation of a number as a slice of bytes

func ImportBytes

func ImportBytes(input []byte, bits int) (*BMNumber, error)

func ImportString

func ImportString(input string) (*BMNumber, error)

InitBMNumber creates a new BMNumber for a string

func ImportUint

func ImportUint(input interface{}, optionalBits int) (*BMNumber, error)

func (*BMNumber) ExportBinary

func (n *BMNumber) ExportBinary(withSize bool) (string, error)

func (*BMNumber) ExportBinaryNBits

func (n *BMNumber) ExportBinaryNBits(bits int) (string, error)

func (*BMNumber) ExportString

func (n *BMNumber) ExportString(c *BMNumberConfig) (string, error)

func (*BMNumber) ExportUint64

func (n *BMNumber) ExportUint64() (uint64, error)

func (*BMNumber) ExportVerilogBinary

func (n *BMNumber) ExportVerilogBinary() (string, error)

func (*BMNumber) GetBytes

func (n *BMNumber) GetBytes() []byte

func (*BMNumber) GetTypeName

func (n *BMNumber) GetTypeName() string

type BMNumberConfig

type BMNumberConfig struct {
	OmitPrefix bool
}

type BMNumberServer

type BMNumberServer struct {
	// contains filtered or unexported fields
}

func (*BMNumberServer) ExecRequest

func (s *BMNumberServer) ExecRequest(w http.ResponseWriter, r *http.Request)

func (*BMNumberServer) HomePage

func (s *BMNumberServer) HomePage(w http.ResponseWriter, r *http.Request)

type BMNumberType

type BMNumberType interface {
	GetName() string

	GetSize() int

	Convert(*BMNumber) error
	ExportString(*BMNumber) (string, error)
	ShowInstructions() map[string]string
	ShowPrefix() string
	// contains filtered or unexported methods
}

func GetType

func GetType(name string) BMNumberType

type BMNumbersRequest

type BMNumbersRequest struct {
	Action   string   `json:"action"`
	Numbers  []string `json:"numbers"`
	ReqType  string   `json:"reqType"`
	DumpMode string   `json:"viewMode"`
}

type BMNumbersResponse

type BMNumbersResponse struct {
	Numbers []string `json:"numbers"`
}

type Bin

type Bin struct{}

func (Bin) Convert

func (d Bin) Convert(n *BMNumber) error

func (Bin) ExportString

func (d Bin) ExportString(n *BMNumber) (string, error)

func (Bin) GetName

func (d Bin) GetName() string

func (Bin) GetSize

func (d Bin) GetSize() int

func (Bin) ShowInstructions

func (d Bin) ShowInstructions() map[string]string

func (Bin) ShowPrefix

func (d Bin) ShowPrefix() string

type DynFixedPoint

type DynFixedPoint struct {
}

func (DynFixedPoint) CreateType

func (d DynFixedPoint) CreateType(name string, param interface{}) (BMNumberType, error)

func (DynFixedPoint) GetName

func (d DynFixedPoint) GetName() string

func (DynFixedPoint) MatchName

func (d DynFixedPoint) MatchName(name string) bool

type DynFloPoCo

type DynFloPoCo struct{}

func (DynFloPoCo) CreateType

func (d DynFloPoCo) CreateType(name string, param interface{}) (BMNumberType, error)

func (DynFloPoCo) GetName

func (d DynFloPoCo) GetName() string

func (DynFloPoCo) MatchName

func (d DynFloPoCo) MatchName(name string) bool

type DynLinearQuantizer

type DynLinearQuantizer struct {
	Ranges *map[int]LinearDataRange
}

func (DynLinearQuantizer) CreateType

func (d DynLinearQuantizer) CreateType(name string, param interface{}) (BMNumberType, error)

func (DynLinearQuantizer) GetName

func (d DynLinearQuantizer) GetName() string

func (DynLinearQuantizer) MatchName

func (d DynLinearQuantizer) MatchName(name string) bool

type DynamicalType

type DynamicalType interface {
	GetName() string
	MatchName(string) bool
	CreateType(string, interface{}) (BMNumberType, error)
}

type FixedPoint

type FixedPoint struct {
	FixedPointName string
	// contains filtered or unexported fields
}

func (FixedPoint) Convert

func (d FixedPoint) Convert(n *BMNumber) error

func (FixedPoint) ExportString

func (d FixedPoint) ExportString(n *BMNumber) (string, error)

func (FixedPoint) GetName

func (d FixedPoint) GetName() string

func (FixedPoint) GetSize

func (d FixedPoint) GetSize() int

func (FixedPoint) ShowInstructions

func (d FixedPoint) ShowInstructions() map[string]string

func (FixedPoint) ShowPrefix

func (d FixedPoint) ShowPrefix() string

type FloPoCo

type FloPoCo struct {
	// contains filtered or unexported fields
}

func (FloPoCo) Convert

func (d FloPoCo) Convert(n *BMNumber) error

func (FloPoCo) ExportString

func (d FloPoCo) ExportString(n *BMNumber) (string, error)

func (FloPoCo) GetName

func (d FloPoCo) GetName() string

func (FloPoCo) GetSize

func (d FloPoCo) GetSize() int

func (FloPoCo) ShowInstructions

func (d FloPoCo) ShowInstructions() map[string]string

func (FloPoCo) ShowPrefix

func (d FloPoCo) ShowPrefix() string

type Float16

type Float16 struct{}

func (Float16) Convert

func (d Float16) Convert(n *BMNumber) error

func (Float16) ExportString

func (d Float16) ExportString(n *BMNumber) (string, error)

func (Float16) GetName

func (d Float16) GetName() string

func (Float16) GetSize

func (d Float16) GetSize() int

func (Float16) ShowInstructions

func (d Float16) ShowInstructions() map[string]string

func (Float16) ShowPrefix

func (d Float16) ShowPrefix() string

type Float32

type Float32 struct{}

func (Float32) Convert

func (d Float32) Convert(n *BMNumber) error

func (Float32) ExportString

func (d Float32) ExportString(n *BMNumber) (string, error)

func (Float32) GetName

func (d Float32) GetName() string

func (Float32) GetSize

func (d Float32) GetSize() int

func (Float32) ShowInstructions

func (d Float32) ShowInstructions() map[string]string

func (Float32) ShowPrefix

func (d Float32) ShowPrefix() string

type Hex

type Hex struct{}

func (Hex) Convert

func (d Hex) Convert(n *BMNumber) error

func (Hex) ExportString

func (b Hex) ExportString(n *BMNumber) (string, error)

func (Hex) GetName

func (d Hex) GetName() string

func (Hex) GetSize

func (d Hex) GetSize() int

func (Hex) ShowInstructions

func (b Hex) ShowInstructions() map[string]string

func (Hex) ShowPrefix

func (b Hex) ShowPrefix() string

type ImportFunc

type ImportFunc func(*regexp.Regexp, string) (*BMNumber, error)

type LinearDataRange

type LinearDataRange struct {
	Max float64
}

type LinearQuantizer

type LinearQuantizer struct {
	// contains filtered or unexported fields
}

func (LinearQuantizer) Convert

func (d LinearQuantizer) Convert(n *BMNumber) error

func (LinearQuantizer) ExportString

func (d LinearQuantizer) ExportString(n *BMNumber) (string, error)

func (LinearQuantizer) GetName

func (d LinearQuantizer) GetName() string

func (LinearQuantizer) GetSize

func (d LinearQuantizer) GetSize() int

func (LinearQuantizer) ShowInstructions

func (d LinearQuantizer) ShowInstructions() map[string]string

func (LinearQuantizer) ShowPrefix

func (d LinearQuantizer) ShowPrefix() string

type Signed

type Signed struct{}

func (Signed) Convert

func (d Signed) Convert(n *BMNumber) error

func (Signed) ExportString

func (d Signed) ExportString(n *BMNumber) (string, error)

func (Signed) GetName

func (d Signed) GetName() string

func (Signed) GetSize

func (d Signed) GetSize() int

func (Signed) ShowInstructions

func (d Signed) ShowInstructions() map[string]string

func (Signed) ShowPrefix

func (d Signed) ShowPrefix() string

type Unsigned

type Unsigned struct{}

func (Unsigned) Convert

func (d Unsigned) Convert(n *BMNumber) error

func (Unsigned) ExportString

func (d Unsigned) ExportString(n *BMNumber) (string, error)

func (Unsigned) GetName

func (d Unsigned) GetName() string

func (Unsigned) GetSize

func (d Unsigned) GetSize() int

func (Unsigned) ShowInstructions

func (d Unsigned) ShowInstructions() map[string]string

func (Unsigned) ShowPrefix

func (d Unsigned) ShowPrefix() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL