mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
run vfmt on math and sha; add vfmt check to test-compiler
This commit is contained in:
parent
9198285688
commit
a251db068f
@ -1,78 +1,70 @@
|
||||
/*
|
||||
https://benchmarksgame-team.pages.debian.net/benchmarksgame/performance/spectralnorm.html
|
||||
Added: Pradeep Verghese
|
||||
Benchmarks:
|
||||
Benchmarks:
|
||||
Used v -prod spectral.v
|
||||
Command: time ./spectral 5500
|
||||
Output: 1.274224153
|
||||
|
||||
Time: 11.67s user 0.02s system 99% cpu 11.721 total
|
||||
*/
|
||||
|
||||
module main
|
||||
|
||||
import math
|
||||
import os
|
||||
|
||||
|
||||
fn evala(i, j int) int {
|
||||
return ((i+j)*(i+j+1)/2 + i + 1)
|
||||
return ((i + j) * (i + j + 1) / 2 + i + 1)
|
||||
}
|
||||
|
||||
fn times(v mut []f64, u []f64) {
|
||||
for i := 0; i < v.len; i++ {
|
||||
mut a := f64(0)
|
||||
for j :=0; j< u.len; j++ {
|
||||
a += u[j] /f64(evala(i,j))
|
||||
}
|
||||
v[i] = a
|
||||
}
|
||||
for i := 0; i < v.len; i++ {
|
||||
mut a := f64(0)
|
||||
for j := 0; j < u.len; j++ {
|
||||
a += u[j] / f64(evala(i, j))
|
||||
}
|
||||
v[i] = a
|
||||
}
|
||||
}
|
||||
|
||||
fn times_trans(v mut []f64, u []f64) {
|
||||
for i := 0; i< v.len; i++ {
|
||||
mut a := f64(0)
|
||||
for j :=0; j< u.len; j++ {
|
||||
a += u[j] / f64(evala(j,i))
|
||||
}
|
||||
v[i] = a
|
||||
}
|
||||
for i := 0; i < v.len; i++ {
|
||||
mut a := f64(0)
|
||||
for j := 0; j < u.len; j++ {
|
||||
a += u[j] / f64(evala(j, i))
|
||||
}
|
||||
v[i] = a
|
||||
}
|
||||
}
|
||||
|
||||
fn a_times_transp(v mut []f64, u []f64) {
|
||||
mut x := [f64(0)].repeat(u.len)
|
||||
times(mut x, u)
|
||||
times_trans(mut v, x)
|
||||
}
|
||||
mut x := [f64(0)].repeat(u.len)
|
||||
times(mut x, u)
|
||||
times_trans(mut v, x)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
args := os.args
|
||||
mut n := int(0)
|
||||
|
||||
if args.len == 2 {
|
||||
n = args[1].int()
|
||||
}
|
||||
else {
|
||||
n = 0
|
||||
}
|
||||
mut u := [f64(1.0)].repeat(n)
|
||||
mut v := [f64(1.0)].repeat(n)
|
||||
|
||||
for i := 0; i< 10; i++ {
|
||||
a_times_transp(mut v, u)
|
||||
a_times_transp(mut u, v)
|
||||
}
|
||||
|
||||
mut vbv := f64(0)
|
||||
mut vv := f64(0)
|
||||
|
||||
for i :=0; i< n; i++ {
|
||||
vbv += u[i] * v[i]
|
||||
vv += v[i] * v[i]
|
||||
}
|
||||
ans := math.sqrt(vbv/vv)
|
||||
|
||||
println('${ans:0.9f}')
|
||||
|
||||
|
||||
args := os.args
|
||||
mut n := int(0)
|
||||
if args.len == 2 {
|
||||
n = args[1].int()
|
||||
}
|
||||
else {
|
||||
n = 0
|
||||
}
|
||||
mut u := [f64(1.0)].repeat(n)
|
||||
mut v := [f64(1.0)].repeat(n)
|
||||
for i := 0; i < 10; i++ {
|
||||
a_times_transp(mut v, u)
|
||||
a_times_transp(mut u, v)
|
||||
}
|
||||
mut vbv := f64(0)
|
||||
mut vv := f64(0)
|
||||
for i := 0; i < n; i++ {
|
||||
vbv += u[i] * v[i]
|
||||
vv += v[i] * v[i]
|
||||
}
|
||||
ans := math.sqrt(vbv / vv)
|
||||
println('${ans:0.9f}')
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,12 @@ fn main() {
|
||||
}
|
||||
|
||||
fn v_test_compiler(vargs string){
|
||||
v_test_compiler2(vargs)
|
||||
//make_sure_vfmt_was_run()
|
||||
}
|
||||
|
||||
fn v_test_compiler2(vargs string){
|
||||
|
||||
vexe := testing.vexe_path()
|
||||
parent_dir := os.dir(vexe)
|
||||
testing.vlib_should_be_present( parent_dir )
|
||||
@ -74,6 +80,8 @@ fn v_test_compiler(vargs string){
|
||||
vmark.stop()
|
||||
eprintln( 'Installing a v module took: ' + vmark.total_duration().str() + 'ms')
|
||||
|
||||
make_sure_vfmt_was_run()
|
||||
|
||||
if building_tools_failed ||
|
||||
compiler_test_session.failed ||
|
||||
building_examples_failed ||
|
||||
@ -82,3 +90,13 @@ fn v_test_compiler(vargs string){
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn make_sure_vfmt_was_run() {
|
||||
files := os.walk_ext('.', '_test.v')
|
||||
for file in files {
|
||||
println(file)
|
||||
os.exec('./vfmt $file') or {
|
||||
println('$file failed')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2513,7 +2513,7 @@ fn (p mut Parser) array_init() string {
|
||||
const_name := p.prepend_mod(p.lit)
|
||||
if p.table.known_const(const_name) {
|
||||
c := p.table.find_const(const_name) or {
|
||||
// p.error('unknown const `$p.lit`')
|
||||
p.error('unknown const `$const_name`')
|
||||
exit(1)
|
||||
}
|
||||
if c.typ == 'int' && p.peek() == .rsbr {
|
||||
@ -2582,8 +2582,9 @@ fn (p mut Parser) array_init() string {
|
||||
}
|
||||
if p.tok != .rsbr && p.tok != .semicolon {
|
||||
p.gen(', ')
|
||||
line_nr := p.tok
|
||||
p.check(.comma)
|
||||
p.fspace()
|
||||
p.fspace_or_newline()
|
||||
}
|
||||
i++
|
||||
// Repeat (a = [0;5] )
|
||||
|
@ -2,34 +2,35 @@ module main
|
||||
|
||||
import os
|
||||
import term
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
/// This file will get compiled as part of the main program,
|
||||
/// for a _test.v file.
|
||||
/// The methods defined here are called back by the test program's
|
||||
/// assert statements, on each success/fail. The goal is to make
|
||||
/// customizing the look & feel of the assertions results easier,
|
||||
/// since it is done in normal V code, instead of in embedded C ...
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
fn cb_assertion_failed(filename string, line int, sourceline string, funcname string){
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
// / This file will get compiled as part of the main program,
|
||||
// / for a _test.v file.
|
||||
// / The methods defined here are called back by the test program's
|
||||
// / assert statements, on each success/fail. The goal is to make
|
||||
// / customizing the look & feel of the assertions results easier,
|
||||
// / since it is done in normal V code, instead of in embedded C ...
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
fn cb_assertion_failed(filename string, line int, sourceline string, funcname string) {
|
||||
color_on := term.can_show_color_on_stderr()
|
||||
use_relative_paths := match os.getenv('VERROR_PATHS') {
|
||||
'absolute' { false }
|
||||
else { true }
|
||||
}
|
||||
final_filename := if use_relative_paths { filename } else { os.realpath( filename ) }
|
||||
final_funcname := funcname.replace('main__','').replace('__','.')
|
||||
|
||||
'absolute'{
|
||||
false
|
||||
}
|
||||
else {
|
||||
true}}
|
||||
final_filename := if use_relative_paths { filename } else { os.realpath(filename) }
|
||||
final_funcname := funcname.replace('main__', '').replace('__', '.')
|
||||
mut fail_message := 'FAILED assertion'
|
||||
if color_on { fail_message = term.bold(term.red(fail_message)) }
|
||||
|
||||
if color_on {
|
||||
fail_message = term.bold(term.red(fail_message))
|
||||
}
|
||||
eprintln('$final_filename:$line: $fail_message')
|
||||
eprintln('Function: $final_funcname')
|
||||
eprintln('Source : $sourceline')
|
||||
}
|
||||
|
||||
fn cb_assertion_ok(filename string, line int, sourceline string, funcname string){
|
||||
//do nothing for now on an OK assertion
|
||||
//println('OK ${line:5d}|$sourceline ')
|
||||
fn cb_assertion_ok(filename string, line int, sourceline string, funcname string) {
|
||||
// do nothing for now on an OK assertion
|
||||
// println('OK ${line:5d}|$sourceline ')
|
||||
}
|
||||
|
||||
|
@ -52,6 +52,19 @@ fn (p mut Parser) fspace() {
|
||||
p.fgen(' ')
|
||||
}
|
||||
|
||||
[if vfmt]
|
||||
fn (p mut Parser) fspace_or_newline() {
|
||||
if p.first_pass() {
|
||||
return
|
||||
}
|
||||
if p.token_idx >= 2 && p.tokens[p.token_idx-1].line_nr !=
|
||||
p.tokens[p.token_idx-2].line_nr {
|
||||
p.fgen_nl()
|
||||
} else {
|
||||
p.fgen(' ')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[if vfmt]
|
||||
fn (p mut Parser) fgenln(s string) {
|
||||
|
@ -21,3 +21,4 @@ pub enum Hash {
|
||||
blake2b_384
|
||||
blake2b_512
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,10 @@
|
||||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// Package sha512 implements the SHA-384, SHA-512, SHA-512/224, and SHA-512/256
|
||||
// hash algorithms as defined in FIPS 180-4.
|
||||
|
||||
// Based off: https://github.com/golang/go/tree/master/src/crypto/sha512
|
||||
// Last commit: https://github.com/golang/go/commit/3ce865d7a0b88714cc433454ae2370a105210c01
|
||||
|
||||
module sha512
|
||||
|
||||
import (
|
||||
@ -16,7 +13,7 @@ import (
|
||||
)
|
||||
|
||||
pub const (
|
||||
// size is the size, in bytes, of a SHA-512 checksum.
|
||||
// size is the size, in bytes, of a SHA-512 checksum.
|
||||
size = 64
|
||||
// size224 is the size, in bytes, of a SHA-512/224 checksum.
|
||||
size224 = 28
|
||||
@ -30,15 +27,15 @@ pub const (
|
||||
)
|
||||
|
||||
const (
|
||||
Chunk = 128
|
||||
init0 = 0x6a09e667f3bcc908
|
||||
init1 = 0xbb67ae8584caa73b
|
||||
init2 = 0x3c6ef372fe94f82b
|
||||
init3 = 0xa54ff53a5f1d36f1
|
||||
init4 = 0x510e527fade682d1
|
||||
init5 = 0x9b05688c2b3e6c1f
|
||||
init6 = 0x1f83d9abfb41bd6b
|
||||
init7 = 0x5be0cd19137e2179
|
||||
Chunk = 128
|
||||
init0 = 0x6a09e667f3bcc908
|
||||
init1 = 0xbb67ae8584caa73b
|
||||
init2 = 0x3c6ef372fe94f82b
|
||||
init3 = 0xa54ff53a5f1d36f1
|
||||
init4 = 0x510e527fade682d1
|
||||
init5 = 0x9b05688c2b3e6c1f
|
||||
init6 = 0x1f83d9abfb41bd6b
|
||||
init7 = 0x5be0cd19137e2179
|
||||
init0_224 = 0x8c3d37c819544da2
|
||||
init1_224 = 0x73e1996689dcd4d6
|
||||
init2_224 = 0x1dfab7ae32ff9c82
|
||||
@ -64,7 +61,6 @@ const (
|
||||
init6_384 = 0xdb0c2e0d64f98fa7
|
||||
init7_384 = 0x47b5481dbefa4fa4
|
||||
)
|
||||
|
||||
// digest represents the partial evaluation of a checksum.
|
||||
struct Digest {
|
||||
mut:
|
||||
@ -79,54 +75,55 @@ fn (d mut Digest) reset() {
|
||||
d.h = [u64(0)].repeat(8)
|
||||
d.x = [byte(0)].repeat(Chunk)
|
||||
match d.function {
|
||||
.sha384 {
|
||||
d.h[0] = init0_384
|
||||
d.h[1] = init1_384
|
||||
d.h[2] = init2_384
|
||||
d.h[3] = init3_384
|
||||
d.h[4] = init4_384
|
||||
d.h[5] = init5_384
|
||||
d.h[6] = init6_384
|
||||
d.h[7] = init7_384
|
||||
}
|
||||
.sha512_224 {
|
||||
d.h[0] = init0_224
|
||||
d.h[1] = init1_224
|
||||
d.h[2] = init2_224
|
||||
d.h[3] = init3_224
|
||||
d.h[4] = init4_224
|
||||
d.h[5] = init5_224
|
||||
d.h[6] = init6_224
|
||||
d.h[7] = init7_224
|
||||
}
|
||||
.sha512_256 {
|
||||
d.h[0] = init0_256
|
||||
d.h[1] = init1_256
|
||||
d.h[2] = init2_256
|
||||
d.h[3] = init3_256
|
||||
d.h[4] = init4_256
|
||||
d.h[5] = init5_256
|
||||
d.h[6] = init6_256
|
||||
d.h[7] = init7_256
|
||||
}
|
||||
else {
|
||||
d.h[0] = init0
|
||||
d.h[1] = init1
|
||||
d.h[2] = init2
|
||||
d.h[3] = init3
|
||||
d.h[4] = init4
|
||||
d.h[5] = init5
|
||||
d.h[6] = init6
|
||||
d.h[7] = init7
|
||||
}
|
||||
}
|
||||
.sha384 {
|
||||
d.h[0] = init0_384
|
||||
d.h[1] = init1_384
|
||||
d.h[2] = init2_384
|
||||
d.h[3] = init3_384
|
||||
d.h[4] = init4_384
|
||||
d.h[5] = init5_384
|
||||
d.h[6] = init6_384
|
||||
d.h[7] = init7_384
|
||||
}
|
||||
.sha512_224 {
|
||||
d.h[0] = init0_224
|
||||
d.h[1] = init1_224
|
||||
d.h[2] = init2_224
|
||||
d.h[3] = init3_224
|
||||
d.h[4] = init4_224
|
||||
d.h[5] = init5_224
|
||||
d.h[6] = init6_224
|
||||
d.h[7] = init7_224
|
||||
}
|
||||
.sha512_256 {
|
||||
d.h[0] = init0_256
|
||||
d.h[1] = init1_256
|
||||
d.h[2] = init2_256
|
||||
d.h[3] = init3_256
|
||||
d.h[4] = init4_256
|
||||
d.h[5] = init5_256
|
||||
d.h[6] = init6_256
|
||||
d.h[7] = init7_256
|
||||
}
|
||||
else {
|
||||
d.h[0] = init0
|
||||
d.h[1] = init1
|
||||
d.h[2] = init2
|
||||
d.h[3] = init3
|
||||
d.h[4] = init4
|
||||
d.h[5] = init5
|
||||
d.h[6] = init6
|
||||
d.h[7] = init7
|
||||
}}
|
||||
d.nx = 0
|
||||
d.len = 0
|
||||
}
|
||||
|
||||
// internal
|
||||
fn new_digest(hash crypto.Hash) &Digest {
|
||||
mut d := &Digest{function: hash}
|
||||
mut d := &Digest{
|
||||
function: hash
|
||||
}
|
||||
d.reset()
|
||||
return d
|
||||
}
|
||||
@ -164,16 +161,18 @@ fn (d mut Digest) write(p_ []byte) int {
|
||||
}
|
||||
if n >= p.len {
|
||||
p = []
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
p = p[n..]
|
||||
}
|
||||
}
|
||||
if p.len >= Chunk {
|
||||
n := p.len &~ (Chunk - 1)
|
||||
n := p.len & ~(Chunk - 1)
|
||||
block(mut d, p[..n])
|
||||
if n >= p.len {
|
||||
p = []
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
p = p[n..]
|
||||
}
|
||||
}
|
||||
@ -189,27 +188,26 @@ fn (d &Digest) sum(b_in []byte) []byte {
|
||||
hash := d0.checksum()
|
||||
mut b_out := b_in.clone()
|
||||
match d0.function {
|
||||
.sha384 {
|
||||
for b in hash[..size384] {
|
||||
b_out << b
|
||||
.sha384 {
|
||||
for b in hash[..size384] {
|
||||
b_out << b
|
||||
}
|
||||
}
|
||||
}
|
||||
.sha512_224 {
|
||||
for b in hash[..size224] {
|
||||
b_out << b
|
||||
.sha512_224 {
|
||||
for b in hash[..size224] {
|
||||
b_out << b
|
||||
}
|
||||
}
|
||||
}
|
||||
.sha512_256 {
|
||||
for b in hash[..size256] {
|
||||
b_out << b
|
||||
.sha512_256 {
|
||||
for b in hash[..size256] {
|
||||
b_out << b
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
for b in hash {
|
||||
b_out << b
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
for b in hash {
|
||||
b_out << b
|
||||
}
|
||||
}}
|
||||
return b_out
|
||||
}
|
||||
|
||||
@ -218,26 +216,21 @@ fn (d mut Digest) checksum() []byte {
|
||||
mut len := d.len
|
||||
mut tmp := [byte(0)].repeat(128)
|
||||
tmp[0] = 0x80
|
||||
|
||||
if int(len)%128 < 112 {
|
||||
d.write(tmp[..112-int(len)%128])
|
||||
} else {
|
||||
d.write(tmp[..128+112-int(len)%128])
|
||||
if int(len) % 128 < 112 {
|
||||
d.write(tmp[..112 - int(len) % 128])
|
||||
}
|
||||
else {
|
||||
d.write(tmp[..128 + 112 - int(len) % 128])
|
||||
}
|
||||
|
||||
// Length in bits.
|
||||
len <<= u64(3)
|
||||
|
||||
binary.big_endian_put_u64(mut tmp, u64(0)) // upper 64 bits are always zero, because len variable has type u64
|
||||
binary.big_endian_put_u64(mut tmp[8..], len)
|
||||
d.write(tmp[..16])
|
||||
|
||||
if d.nx != 0 {
|
||||
panic('d.nx != 0')
|
||||
}
|
||||
|
||||
mut digest := [byte(0)].repeat(size)
|
||||
|
||||
binary.big_endian_put_u64(mut digest, d.h[0])
|
||||
binary.big_endian_put_u64(mut digest[8..], d.h[1])
|
||||
binary.big_endian_put_u64(mut digest[16..], d.h[2])
|
||||
@ -248,7 +241,6 @@ fn (d mut Digest) checksum() []byte {
|
||||
binary.big_endian_put_u64(mut digest[48..], d.h[6])
|
||||
binary.big_endian_put_u64(mut digest[56..], d.h[7])
|
||||
}
|
||||
|
||||
return digest
|
||||
}
|
||||
|
||||
@ -297,16 +289,37 @@ fn block(dig mut Digest, p []byte) {
|
||||
|
||||
pub fn (d &Digest) size() int {
|
||||
match d.function {
|
||||
.sha512_224 { return size224 }
|
||||
.sha512_256 { return size256 }
|
||||
.sha384 { return size384 }
|
||||
else { return size }
|
||||
}
|
||||
.sha512_224 {
|
||||
return size224
|
||||
}
|
||||
.sha512_256 {
|
||||
return size256
|
||||
}
|
||||
.sha384 {
|
||||
return size384
|
||||
}
|
||||
else {
|
||||
return size
|
||||
}}
|
||||
}
|
||||
|
||||
pub fn (d &Digest) block_size() int { return block_size }
|
||||
pub fn (d &Digest) block_size() int {
|
||||
return block_size
|
||||
}
|
||||
|
||||
pub fn hexhash(s string) string {
|
||||
return sum512(s.bytes()).hex()
|
||||
}
|
||||
|
||||
pub fn hexhash_384(s string) string {
|
||||
return sum384(s.bytes()).hex()
|
||||
}
|
||||
|
||||
pub fn hexhash_512_224(s string) string {
|
||||
return sum512_224(s.bytes()).hex()
|
||||
}
|
||||
|
||||
pub fn hexhash_512_256(s string) string {
|
||||
return sum512_256(s.bytes()).hex()
|
||||
}
|
||||
|
||||
pub fn hexhash(s string) string { return sum512(s.bytes()).hex() }
|
||||
pub fn hexhash_384(s string) string { return sum384(s.bytes()).hex() }
|
||||
pub fn hexhash_512_224(s string) string { return sum512_224(s.bytes()).hex() }
|
||||
pub fn hexhash_512_256(s string) string { return sum512_256(s.bytes()).hex() }
|
||||
|
@ -1,9 +1,9 @@
|
||||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
import crypto.sha512
|
||||
|
||||
fn test_crypto_sha512() {
|
||||
fn test_crypto_sha512() {
|
||||
assert sha512.sum512('This is a sha512 checksum.'.bytes()).hex() == '4143e55fcba7e39b20f62a1368e5eb28f64a8859458886117ac66027832e0f9f5263daec688c439d2d0fa07059334668d39e59543039703dbb7e03ec9da7f8d7'
|
||||
}
|
||||
|
||||
|
@ -1,106 +1,101 @@
|
||||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// SHA512 block step.
|
||||
// This is the generic version with no architecture optimizations.
|
||||
// In its own file so that an architecture
|
||||
// optimized verision can be substituted
|
||||
|
||||
module sha512
|
||||
|
||||
import math.bits
|
||||
|
||||
const(
|
||||
_k = [
|
||||
0x428a2f98d728ae22,
|
||||
0x7137449123ef65cd,
|
||||
0xb5c0fbcfec4d3b2f,
|
||||
0xe9b5dba58189dbbc,
|
||||
0x3956c25bf348b538,
|
||||
0x59f111f1b605d019,
|
||||
0x923f82a4af194f9b,
|
||||
0xab1c5ed5da6d8118,
|
||||
0xd807aa98a3030242,
|
||||
0x12835b0145706fbe,
|
||||
0x243185be4ee4b28c,
|
||||
0x550c7dc3d5ffb4e2,
|
||||
0x72be5d74f27b896f,
|
||||
0x80deb1fe3b1696b1,
|
||||
0x9bdc06a725c71235,
|
||||
0xc19bf174cf692694,
|
||||
0xe49b69c19ef14ad2,
|
||||
0xefbe4786384f25e3,
|
||||
0x0fc19dc68b8cd5b5,
|
||||
0x240ca1cc77ac9c65,
|
||||
0x2de92c6f592b0275,
|
||||
0x4a7484aa6ea6e483,
|
||||
0x5cb0a9dcbd41fbd4,
|
||||
0x76f988da831153b5,
|
||||
0x983e5152ee66dfab,
|
||||
0xa831c66d2db43210,
|
||||
0xb00327c898fb213f,
|
||||
0xbf597fc7beef0ee4,
|
||||
0xc6e00bf33da88fc2,
|
||||
0xd5a79147930aa725,
|
||||
0x06ca6351e003826f,
|
||||
0x142929670a0e6e70,
|
||||
0x27b70a8546d22ffc,
|
||||
0x2e1b21385c26c926,
|
||||
0x4d2c6dfc5ac42aed,
|
||||
0x53380d139d95b3df,
|
||||
0x650a73548baf63de,
|
||||
0x766a0abb3c77b2a8,
|
||||
0x81c2c92e47edaee6,
|
||||
0x92722c851482353b,
|
||||
0xa2bfe8a14cf10364,
|
||||
0xa81a664bbc423001,
|
||||
0xc24b8b70d0f89791,
|
||||
0xc76c51a30654be30,
|
||||
0xd192e819d6ef5218,
|
||||
0xd69906245565a910,
|
||||
0xf40e35855771202a,
|
||||
0x106aa07032bbd1b8,
|
||||
0x19a4c116b8d2d0c8,
|
||||
0x1e376c085141ab53,
|
||||
0x2748774cdf8eeb99,
|
||||
0x34b0bcb5e19b48a8,
|
||||
0x391c0cb3c5c95a63,
|
||||
0x4ed8aa4ae3418acb,
|
||||
0x5b9cca4f7763e373,
|
||||
0x682e6ff3d6b2b8a3,
|
||||
0x748f82ee5defb2fc,
|
||||
0x78a5636f43172f60,
|
||||
0x84c87814a1f0ab72,
|
||||
0x8cc702081a6439ec,
|
||||
0x90befffa23631e28,
|
||||
0xa4506cebde82bde9,
|
||||
0xbef9a3f7b2c67915,
|
||||
0xc67178f2e372532b,
|
||||
0xca273eceea26619c,
|
||||
0xd186b8c721c0c207,
|
||||
0xeada7dd6cde0eb1e,
|
||||
0xf57d4f7fee6ed178,
|
||||
0x06f067aa72176fba,
|
||||
0x0a637dc5a2c898a6,
|
||||
0x113f9804bef90dae,
|
||||
0x1b710b35131c471b,
|
||||
0x28db77f523047d84,
|
||||
0x32caab7b40c72493,
|
||||
0x3c9ebe0a15c9bebc,
|
||||
0x431d67c49c100d4c,
|
||||
0x4cc5d4becb3e42b6,
|
||||
0x597f299cfc657e2a,
|
||||
0x5fcb6fab3ad6faec,
|
||||
0x6c44198c4a475817,
|
||||
const (
|
||||
_k = [0x428a2f98d728ae22,
|
||||
0x7137449123ef65cd,
|
||||
0xb5c0fbcfec4d3b2f,
|
||||
0xe9b5dba58189dbbc,
|
||||
0x3956c25bf348b538,
|
||||
0x59f111f1b605d019,
|
||||
0x923f82a4af194f9b,
|
||||
0xab1c5ed5da6d8118,
|
||||
0xd807aa98a3030242,
|
||||
0x12835b0145706fbe,
|
||||
0x243185be4ee4b28c,
|
||||
0x550c7dc3d5ffb4e2,
|
||||
0x72be5d74f27b896f,
|
||||
0x80deb1fe3b1696b1,
|
||||
0x9bdc06a725c71235,
|
||||
0xc19bf174cf692694,
|
||||
0xe49b69c19ef14ad2,
|
||||
0xefbe4786384f25e3,
|
||||
0x0fc19dc68b8cd5b5,
|
||||
0x240ca1cc77ac9c65,
|
||||
0x2de92c6f592b0275,
|
||||
0x4a7484aa6ea6e483,
|
||||
0x5cb0a9dcbd41fbd4,
|
||||
0x76f988da831153b5,
|
||||
0x983e5152ee66dfab,
|
||||
0xa831c66d2db43210,
|
||||
0xb00327c898fb213f,
|
||||
0xbf597fc7beef0ee4,
|
||||
0xc6e00bf33da88fc2,
|
||||
0xd5a79147930aa725,
|
||||
0x06ca6351e003826f,
|
||||
0x142929670a0e6e70,
|
||||
0x27b70a8546d22ffc,
|
||||
0x2e1b21385c26c926,
|
||||
0x4d2c6dfc5ac42aed,
|
||||
0x53380d139d95b3df,
|
||||
0x650a73548baf63de,
|
||||
0x766a0abb3c77b2a8,
|
||||
0x81c2c92e47edaee6,
|
||||
0x92722c851482353b,
|
||||
0xa2bfe8a14cf10364,
|
||||
0xa81a664bbc423001,
|
||||
0xc24b8b70d0f89791,
|
||||
0xc76c51a30654be30,
|
||||
0xd192e819d6ef5218,
|
||||
0xd69906245565a910,
|
||||
0xf40e35855771202a,
|
||||
0x106aa07032bbd1b8,
|
||||
0x19a4c116b8d2d0c8,
|
||||
0x1e376c085141ab53,
|
||||
0x2748774cdf8eeb99,
|
||||
0x34b0bcb5e19b48a8,
|
||||
0x391c0cb3c5c95a63,
|
||||
0x4ed8aa4ae3418acb,
|
||||
0x5b9cca4f7763e373,
|
||||
0x682e6ff3d6b2b8a3,
|
||||
0x748f82ee5defb2fc,
|
||||
0x78a5636f43172f60,
|
||||
0x84c87814a1f0ab72,
|
||||
0x8cc702081a6439ec,
|
||||
0x90befffa23631e28,
|
||||
0xa4506cebde82bde9,
|
||||
0xbef9a3f7b2c67915,
|
||||
0xc67178f2e372532b,
|
||||
0xca273eceea26619c,
|
||||
0xd186b8c721c0c207,
|
||||
0xeada7dd6cde0eb1e,
|
||||
0xf57d4f7fee6ed178,
|
||||
0x06f067aa72176fba,
|
||||
0x0a637dc5a2c898a6,
|
||||
0x113f9804bef90dae,
|
||||
0x1b710b35131c471b,
|
||||
0x28db77f523047d84,
|
||||
0x32caab7b40c72493,
|
||||
0x3c9ebe0a15c9bebc,
|
||||
0x431d67c49c100d4c,
|
||||
0x4cc5d4becb3e42b6,
|
||||
0x597f299cfc657e2a,
|
||||
0x5fcb6fab3ad6faec,
|
||||
0x6c44198c4a475817,
|
||||
]
|
||||
)
|
||||
|
||||
fn block_generic(dig mut Digest, p_ []byte) {
|
||||
mut p := p_
|
||||
|
||||
mut w := [u64(0)].repeat(80)
|
||||
|
||||
mut h0 := dig.h[0]
|
||||
mut h1 := dig.h[1]
|
||||
mut h2 := dig.h[2]
|
||||
@ -109,22 +104,18 @@ fn block_generic(dig mut Digest, p_ []byte) {
|
||||
mut h5 := dig.h[5]
|
||||
mut h6 := dig.h[6]
|
||||
mut h7 := dig.h[7]
|
||||
|
||||
for p.len >= Chunk {
|
||||
for i := 0; i < 16; i++ {
|
||||
j := i * 8
|
||||
w[i] = u64(u64(u64(p[j])<<56) | u64(u64(p[j+1])<<48) | u64(u64(p[j+2])<<40) | u64(u64(p[j+3])<<32) |
|
||||
u64(u64(p[j+4])<<24) | u64(u64(p[j+5])<<16) | u64(u64(p[j+6])<<8) | u64(p[j+7]))
|
||||
w[i] = u64(u64(u64(p[j])<<56) | u64(u64(p[j + 1])<<48) | u64(u64(p[j + 2])<<40) | u64(u64(p[j + 3])<<32) | u64(u64(p[j + 4])<<24) | u64(u64(p[j + 5])<<16) | u64(u64(p[j + 6])<<8) | u64(p[j + 7]))
|
||||
}
|
||||
for i := 16; i < 80; i++ {
|
||||
v1 := w[i-2]
|
||||
t1 := bits.rotate_left_64(v1, -19) ^ bits.rotate_left_64(v1, -61) ^ (v1 >> 6)
|
||||
v2 := w[i-15]
|
||||
t2 := bits.rotate_left_64(v2, -1) ^ bits.rotate_left_64(v2, -8) ^ (v2 >> 7)
|
||||
|
||||
w[i] = t1 + w[i-7] + t2 + w[i-16]
|
||||
v1 := w[i - 2]
|
||||
t1 := bits.rotate_left_64(v1, -19) ^ bits.rotate_left_64(v1, -61) ^ (v1>>6)
|
||||
v2 := w[i - 15]
|
||||
t2 := bits.rotate_left_64(v2, -1) ^ bits.rotate_left_64(v2, -8) ^ (v2>>7)
|
||||
w[i] = t1 + w[i - 7] + t2 + w[i - 16]
|
||||
}
|
||||
|
||||
mut a := h0
|
||||
mut b := h1
|
||||
mut c := h2
|
||||
@ -133,11 +124,9 @@ fn block_generic(dig mut Digest, p_ []byte) {
|
||||
mut f := h5
|
||||
mut g := h6
|
||||
mut h := h7
|
||||
|
||||
for i := 0; i < 80; i++ {
|
||||
t1 := h + (bits.rotate_left_64(e, -14) ^ bits.rotate_left_64(e, -18) ^ bits.rotate_left_64(e, -41)) + ((e & f) ^ (~e & g)) + _k[i] + w[i]
|
||||
t2 := (bits.rotate_left_64(a, -28) ^ bits.rotate_left_64(a, -34) ^ bits.rotate_left_64(a, -39)) + ((a & b) ^ (a & c) ^ (b & c))
|
||||
|
||||
h = g
|
||||
g = f
|
||||
f = e
|
||||
@ -147,7 +136,6 @@ fn block_generic(dig mut Digest, p_ []byte) {
|
||||
b = a
|
||||
a = t1 + t2
|
||||
}
|
||||
|
||||
h0 += a
|
||||
h1 += b
|
||||
h2 += c
|
||||
@ -156,14 +144,13 @@ fn block_generic(dig mut Digest, p_ []byte) {
|
||||
h5 += f
|
||||
h6 += g
|
||||
h7 += h
|
||||
|
||||
if Chunk >= p.len {
|
||||
p = []
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
p = p[Chunk..]
|
||||
}
|
||||
}
|
||||
|
||||
dig.h[0] = h0
|
||||
dig.h[1] = h1
|
||||
dig.h[2] = h2
|
||||
@ -173,3 +160,4 @@ fn block_generic(dig mut Digest, p_ []byte) {
|
||||
dig.h[6] = h6
|
||||
dig.h[7] = h7
|
||||
}
|
||||
|
||||
|
@ -1,10 +1,7 @@
|
||||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
module binary
|
||||
|
||||
|
||||
// Little Endian
|
||||
[inline]
|
||||
pub fn little_endian_endian_u16(b []byte) u16 {
|
||||
@ -16,7 +13,7 @@ pub fn little_endian_endian_u16(b []byte) u16 {
|
||||
pub fn little_endian_put_u16(b mut []byte, v u16) {
|
||||
_ = b[1] // bounds check
|
||||
b[0] = byte(v)
|
||||
b[1] = byte(v >> u16(8))
|
||||
b[1] = byte(v>>u16(8))
|
||||
}
|
||||
|
||||
[inline]
|
||||
@ -29,29 +26,28 @@ pub fn little_endian_u32(b []byte) u32 {
|
||||
pub fn little_endian_put_u32(b mut []byte, v u32) {
|
||||
_ = b[3] // bounds check
|
||||
b[0] = byte(v)
|
||||
b[1] = byte(v >> u32(8))
|
||||
b[2] = byte(v >> u32(16))
|
||||
b[3] = byte(v >> u32(24))
|
||||
b[1] = byte(v>>u32(8))
|
||||
b[2] = byte(v>>u32(16))
|
||||
b[3] = byte(v>>u32(24))
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn little_endian_u64(b []byte) u64 {
|
||||
_ = b[7] // bounds check
|
||||
return u64(b[0]) | u64(u64(b[1])<<u64(8)) | u64(u64(b[2])<<u64(16)) | u64(u64(b[3])<<u64(24)) |
|
||||
u64(u64(b[4])<<u64(32)) | u64(u64(b[5])<<u64(40)) | u64(u64(b[6])<<u64(48)) | u64(u64(b[7])<<u64(56))
|
||||
return u64(b[0]) | u64(u64(b[1])<<u64(8)) | u64(u64(b[2])<<u64(16)) | u64(u64(b[3])<<u64(24)) | u64(u64(b[4])<<u64(32)) | u64(u64(b[5])<<u64(40)) | u64(u64(b[6])<<u64(48)) | u64(u64(b[7])<<u64(56))
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn little_endian_put_u64(b mut []byte, v u64) {
|
||||
_ = b[7] // bounds check
|
||||
b[0] = byte(v)
|
||||
b[1] = byte(v >> u64(8))
|
||||
b[2] = byte(v >> u64(16))
|
||||
b[3] = byte(v >> u64(24))
|
||||
b[4] = byte(v >> u64(32))
|
||||
b[5] = byte(v >> u64(40))
|
||||
b[6] = byte(v >> u64(48))
|
||||
b[7] = byte(v >> u64(56))
|
||||
b[1] = byte(v>>u64(8))
|
||||
b[2] = byte(v>>u64(16))
|
||||
b[3] = byte(v>>u64(24))
|
||||
b[4] = byte(v>>u64(32))
|
||||
b[5] = byte(v>>u64(40))
|
||||
b[6] = byte(v>>u64(48))
|
||||
b[7] = byte(v>>u64(56))
|
||||
}
|
||||
|
||||
// Big Endian
|
||||
@ -64,7 +60,7 @@ pub fn big_endian_u16(b []byte) u16 {
|
||||
[inline]
|
||||
pub fn big_endian_put_u16(b mut []byte, v u16) {
|
||||
_ = b[1] // bounds check
|
||||
b[0] = byte(v >> u16(8))
|
||||
b[0] = byte(v>>u16(8))
|
||||
b[1] = byte(v)
|
||||
}
|
||||
|
||||
@ -77,28 +73,28 @@ pub fn big_endian_u32(b []byte) u32 {
|
||||
[inline]
|
||||
pub fn big_endian_put_u32(b mut []byte, v u32) {
|
||||
_ = b[3] // bounds check
|
||||
b[0] = byte(v >> u32(24))
|
||||
b[1] = byte(v >> u32(16))
|
||||
b[2] = byte(v >> u32(8))
|
||||
b[0] = byte(v>>u32(24))
|
||||
b[1] = byte(v>>u32(16))
|
||||
b[2] = byte(v>>u32(8))
|
||||
b[3] = byte(v)
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn big_endian_u64(b []byte) u64 {
|
||||
_ = b[7] // bounds check
|
||||
return u64(b[7]) | u64(u64(b[6])<<u64(8)) | u64(u64(b[5])<<u64(16)) | u64(u64(b[4])<<u64(24)) |
|
||||
u64(u64(b[3])<<u64(32)) | u64(u64(b[2])<<u64(40)) | u64(u64(b[1])<<u64(48)) | u64(u64(b[0])<<u64(56))
|
||||
return u64(b[7]) | u64(u64(b[6])<<u64(8)) | u64(u64(b[5])<<u64(16)) | u64(u64(b[4])<<u64(24)) | u64(u64(b[3])<<u64(32)) | u64(u64(b[2])<<u64(40)) | u64(u64(b[1])<<u64(48)) | u64(u64(b[0])<<u64(56))
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn big_endian_put_u64(b mut []byte, v u64) {
|
||||
_ = b[7] // bounds check
|
||||
b[0] = byte(v >> u64(56))
|
||||
b[1] = byte(v >> u64(48))
|
||||
b[2] = byte(v >> u64(40))
|
||||
b[3] = byte(v >> u64(32))
|
||||
b[4] = byte(v >> u64(24))
|
||||
b[5] = byte(v >> u64(16))
|
||||
b[6] = byte(v >> u64(8))
|
||||
b[0] = byte(v>>u64(56))
|
||||
b[1] = byte(v>>u64(48))
|
||||
b[2] = byte(v>>u64(40))
|
||||
b[3] = byte(v>>u64(32))
|
||||
b[4] = byte(v>>u64(24))
|
||||
b[5] = byte(v>>u64(16))
|
||||
b[6] = byte(v>>u64(8))
|
||||
b[7] = byte(v)
|
||||
}
|
||||
|
||||
|
@ -1,21 +1,19 @@
|
||||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
module math
|
||||
|
||||
const (
|
||||
uvnan = 0x7FF8000000000001
|
||||
uvinf = 0x7FF0000000000000
|
||||
uvneginf = 0xFFF0000000000000
|
||||
uvone = 0x3FF0000000000000
|
||||
mask = 0x7FF
|
||||
shift = 64 - 11 - 1
|
||||
bias = 1023
|
||||
sign_mask = (u64(1) << 63)
|
||||
uvnan = 0x7FF8000000000001
|
||||
uvinf = 0x7FF0000000000000
|
||||
uvneginf = 0xFFF0000000000000
|
||||
uvone = 0x3FF0000000000000
|
||||
mask = 0x7FF
|
||||
shift = 64 - 11 - 1
|
||||
bias = 1023
|
||||
sign_mask = (u64(1)<<63)
|
||||
frac_mask = ((u64(1)<<u64(shift)) - u64(1))
|
||||
)
|
||||
|
||||
// inf returns positive infinity if sign >= 0, negative infinity if sign < 0.
|
||||
pub fn inf(sign int) f64 {
|
||||
v := if sign >= 0 { uvinf } else { uvneginf }
|
||||
@ -23,14 +21,16 @@ pub fn inf(sign int) f64 {
|
||||
}
|
||||
|
||||
// nan returns an IEEE 754 ``not-a-number'' value.
|
||||
pub fn nan() f64 { return f64_from_bits(uvnan) }
|
||||
pub fn nan() f64 {
|
||||
return f64_from_bits(uvnan)
|
||||
}
|
||||
|
||||
// is_nan reports whether f is an IEEE 754 ``not-a-number'' value.
|
||||
pub fn is_nan(f f64) bool {
|
||||
// IEEE 754 says that only NaNs satisfy f != f.
|
||||
// To avoid the floating-point hardware, could use:
|
||||
// x := f64_bits(f);
|
||||
// return u32(x>>shift)&mask == mask && x != uvinf && x != uvneginf
|
||||
// x := f64_bits(f);
|
||||
// return u32(x>>shift)&mask == mask && x != uvinf && x != uvneginf
|
||||
return f != f
|
||||
}
|
||||
|
||||
@ -41,8 +41,8 @@ pub fn is_nan(f f64) bool {
|
||||
pub fn is_inf(f f64, sign int) bool {
|
||||
// Test for infinity by comparing against maximum float.
|
||||
// To avoid the floating-point hardware, could use:
|
||||
// x := f64_bits(f);
|
||||
// return sign >= 0 && x == uvinf || sign <= 0 && x == uvneginf;
|
||||
// x := f64_bits(f);
|
||||
// return sign >= 0 && x == uvinf || sign <= 0 && x == uvneginf;
|
||||
return (sign >= 0 && f > max_f64) || (sign <= 0 && f < -max_f64)
|
||||
}
|
||||
|
||||
@ -50,9 +50,10 @@ pub fn is_inf(f f64, sign int) bool {
|
||||
// normalize returns a normal number y and exponent exp
|
||||
// satisfying x == y × 2**exp. It assumes x is finite and non-zero.
|
||||
// pub fn normalize(x f64) (f64, int) {
|
||||
// smallest_normal := 2.2250738585072014e-308 // 2**-1022
|
||||
// if abs(x) < smallest_normal {
|
||||
// return x * (1 << 52), -52
|
||||
// }
|
||||
// return x, 0
|
||||
// smallest_normal := 2.2250738585072014e-308 // 2**-1022
|
||||
// if abs(x) < smallest_normal {
|
||||
// return x * (1 << 52), -52
|
||||
// }
|
||||
// return x, 0
|
||||
// }
|
||||
|
||||
|
@ -1,49 +1,51 @@
|
||||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
module bits
|
||||
|
||||
const(
|
||||
// See http://supertech.csail.mit.edu/papers/debruijn.pdf
|
||||
const (
|
||||
// See http://supertech.csail.mit.edu/papers/debruijn.pdf
|
||||
de_bruijn32 = u32(0x077CB531)
|
||||
de_bruijn32tab = [
|
||||
byte(0), 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
|
||||
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9,
|
||||
de_bruijn32tab = [byte(0), 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
|
||||
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9,
|
||||
]
|
||||
de_bruijn64 = u64(0x03f79d71b4ca8b09)
|
||||
de_bruijn64tab = [
|
||||
byte(0), 1, 56, 2, 57, 49, 28, 3, 61, 58, 42, 50, 38, 29, 17, 4,
|
||||
62, 47, 59, 36, 45, 43, 51, 22, 53, 39, 33, 30, 24, 18, 12, 5,
|
||||
63, 55, 48, 27, 60, 41, 37, 16, 46, 35, 44, 21, 52, 32, 23, 11,
|
||||
54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6,
|
||||
de_bruijn64tab = [byte(0), 1, 56, 2, 57, 49, 28, 3, 61, 58, 42, 50, 38, 29, 17, 4,
|
||||
62, 47, 59, 36, 45, 43, 51, 22, 53, 39, 33, 30, 24, 18, 12, 5,
|
||||
63, 55, 48, 27, 60, 41, 37, 16, 46, 35, 44, 21, 52, 32, 23, 11,
|
||||
54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6,
|
||||
]
|
||||
)
|
||||
|
||||
const(
|
||||
const (
|
||||
m0 = 0x5555555555555555 // 01010101 ...
|
||||
m1 = 0x3333333333333333 // 00110011 ...
|
||||
m2 = 0x0f0f0f0f0f0f0f0f // 00001111 ...
|
||||
m3 = 0x00ff00ff00ff00ff // etc.
|
||||
m4 = 0x0000ffff0000ffff
|
||||
)
|
||||
|
||||
// --- LeadingZeros ---
|
||||
|
||||
// leading_zeros8 returns the number of leading zero bits in x; the result is 8 for x == 0.
|
||||
pub fn leading_zeros8(x byte) int { return 8 - len8(x) }
|
||||
pub fn leading_zeros8(x byte) int {
|
||||
return 8 - len8(x)
|
||||
}
|
||||
|
||||
// leading_zeros16 returns the number of leading zero bits in x; the result is 16 for x == 0.
|
||||
pub fn leading_zeros16(x u16) int { return 16 - len16(x) }
|
||||
pub fn leading_zeros16(x u16) int {
|
||||
return 16 - len16(x)
|
||||
}
|
||||
|
||||
// leading_zeros32 returns the number of leading zero bits in x; the result is 32 for x == 0.
|
||||
pub fn leading_zeros32(x u32) int { return 32 - len32(x) }
|
||||
pub fn leading_zeros32(x u32) int {
|
||||
return 32 - len32(x)
|
||||
}
|
||||
|
||||
// leading_zeros64 returns the number of leading zero bits in x; the result is 64 for x == 0.
|
||||
pub fn leading_zeros64(x u64) int { return 64 - len64(x) }
|
||||
pub fn leading_zeros64(x u64) int {
|
||||
return 64 - len64(x)
|
||||
}
|
||||
|
||||
// --- TrailingZeros ---
|
||||
|
||||
// trailing_zeros8 returns the number of trailing zero bits in x; the result is 8 for x == 0.
|
||||
pub fn trailing_zeros8(x byte) int {
|
||||
return int(ntz8_tab[x])
|
||||
@ -55,7 +57,7 @@ pub fn trailing_zeros16(x u16) int {
|
||||
return 16
|
||||
}
|
||||
// see comment in trailing_zeros64
|
||||
return int(de_bruijn32tab[u32(x&-x)*de_bruijn32>>(32-5)])
|
||||
return int(de_bruijn32tab[u32(x & -x) * de_bruijn32>>(32 - 5)])
|
||||
}
|
||||
|
||||
// trailing_zeros32 returns the number of trailing zero bits in x; the result is 32 for x == 0.
|
||||
@ -64,7 +66,7 @@ pub fn trailing_zeros32(x u32) int {
|
||||
return 32
|
||||
}
|
||||
// see comment in trailing_zeros64
|
||||
return int(de_bruijn32tab[(x&-x)*de_bruijn32>>(32-5)])
|
||||
return int(de_bruijn32tab[(x & -x) * de_bruijn32>>(32 - 5)])
|
||||
}
|
||||
|
||||
// trailing_zeros64 returns the number of trailing zero bits in x; the result is 64 for x == 0.
|
||||
@ -83,11 +85,10 @@ pub fn trailing_zeros64(x u64) int {
|
||||
// find by how many bits it was shifted by looking at which six bit
|
||||
// substring ended up at the top of the word.
|
||||
// (Knuth, volume 4, section 7.3.1)
|
||||
return int(de_bruijn64tab[(x&-x)*de_bruijn64>>(64-6)])
|
||||
return int(de_bruijn64tab[(x & -x) * de_bruijn64>>(64 - 6)])
|
||||
}
|
||||
|
||||
// --- OnesCount ---
|
||||
|
||||
// ones_count8 returns the number of one bits ("population count") in x.
|
||||
pub fn ones_count8(x byte) int {
|
||||
return int(pop8_tab[x])
|
||||
@ -95,12 +96,12 @@ pub fn ones_count8(x byte) int {
|
||||
|
||||
// ones_count16 returns the number of one bits ("population count") in x.
|
||||
pub fn ones_count16(x u16) int {
|
||||
return int(pop8_tab[x>>8] + pop8_tab[x&u16(0xff)])
|
||||
return int(pop8_tab[x>>8] + pop8_tab[x & u16(0xff)])
|
||||
}
|
||||
|
||||
// ones_count32 returns the number of one bits ("population count") in x.
|
||||
pub fn ones_count32(x u32) int {
|
||||
return int(pop8_tab[x>>24] + pop8_tab[x>>16&0xff] + pop8_tab[x>>8&0xff] + pop8_tab[x&u32(0xff)])
|
||||
return int(pop8_tab[x>>24] + pop8_tab[x>>16 & 0xff] + pop8_tab[x>>8 & 0xff] + pop8_tab[x & u32(0xff)])
|
||||
}
|
||||
|
||||
// ones_count64 returns the number of one bits ("population count") in x.
|
||||
@ -109,13 +110,13 @@ pub fn ones_count64(x u64) int {
|
||||
// See "Hacker's Delight", Chap. 5: Counting Bits.
|
||||
// The following pattern shows the general approach:
|
||||
//
|
||||
// x = x>>1&(m0&m) + x&(m0&m)
|
||||
// x = x>>2&(m1&m) + x&(m1&m)
|
||||
// x = x>>4&(m2&m) + x&(m2&m)
|
||||
// x = x>>8&(m3&m) + x&(m3&m)
|
||||
// x = x>>16&(m4&m) + x&(m4&m)
|
||||
// x = x>>32&(m5&m) + x&(m5&m)
|
||||
// return int(x)
|
||||
// x = x>>1&(m0&m) + x&(m0&m)
|
||||
// x = x>>2&(m1&m) + x&(m1&m)
|
||||
// x = x>>4&(m2&m) + x&(m2&m)
|
||||
// x = x>>8&(m3&m) + x&(m3&m)
|
||||
// x = x>>16&(m4&m) + x&(m4&m)
|
||||
// x = x>>32&(m5&m) + x&(m5&m)
|
||||
// return int(x)
|
||||
//
|
||||
// Masking (& operations) can be left away when there's no
|
||||
// danger that a field's sum will carry over into the next
|
||||
@ -125,17 +126,16 @@ pub fn ones_count64(x u64) int {
|
||||
// more, but it saves at best one instruction, so we leave
|
||||
// it alone for clarity.
|
||||
m := u64(1<<64) - 1
|
||||
mut y := u64(x>>u64(1)&(m0&m)) + u64(x&(m0&m))
|
||||
y = u64(y>>u64(2)&(m1&m)) + u64(y&(m1&m))
|
||||
mut y := u64(x>>u64(1) & (m0 & m)) + u64(x & (m0 & m))
|
||||
y = u64(y>>u64(2) & (m1 & m)) + u64(y & (m1 & m))
|
||||
y = u64(u64(y>>4) + y) & (m2 & m)
|
||||
y += y >> 8
|
||||
y += y >> 16
|
||||
y += y >> 32
|
||||
y += y>>8
|
||||
y += y>>16
|
||||
y += y>>32
|
||||
return int(y) & ((1<<7) - 1)
|
||||
}
|
||||
|
||||
// --- RotateLeft ---
|
||||
|
||||
// rotate_left_8 returns the value of x rotated left by (k mod 8) bits.
|
||||
// To rotate x right by k bits, call rotate_left_8(x, -k).
|
||||
//
|
||||
@ -144,7 +144,7 @@ pub fn ones_count64(x u64) int {
|
||||
pub fn rotate_left_8(x byte, k int) byte {
|
||||
n := byte(8)
|
||||
s := byte(k) & byte(n - byte(1))
|
||||
return byte((x<<s) | (x>>(n-s)))
|
||||
return byte((x<<s) | (x>>(n - s)))
|
||||
}
|
||||
|
||||
// rotate_left_16 returns the value of x rotated left by (k mod 16) bits.
|
||||
@ -155,7 +155,7 @@ pub fn rotate_left_8(x byte, k int) byte {
|
||||
pub fn rotate_left_16(x u16, k int) u16 {
|
||||
n := u16(16)
|
||||
s := u16(k) & (n - u16(1))
|
||||
return u16((x<<s) | (x>>(n-s)))
|
||||
return u16((x<<s) | (x>>(n - s)))
|
||||
}
|
||||
|
||||
// rotate_left_32 returns the value of x rotated left by (k mod 32) bits.
|
||||
@ -166,7 +166,7 @@ pub fn rotate_left_16(x u16, k int) u16 {
|
||||
pub fn rotate_left_32(x u32, k int) u32 {
|
||||
n := u32(32)
|
||||
s := u32(k) & (n - u32(1))
|
||||
return u32(u32(x<<s) | u32(x>>(n-s)))
|
||||
return u32(u32(x<<s) | u32(x>>(n - s)))
|
||||
}
|
||||
|
||||
// rotate_left_64 returns the value of x rotated left by (k mod 64) bits.
|
||||
@ -177,11 +177,10 @@ pub fn rotate_left_32(x u32, k int) u32 {
|
||||
pub fn rotate_left_64(x u64, k int) u64 {
|
||||
n := u64(64)
|
||||
s := u64(k) & (n - u64(1))
|
||||
return u64(u64(x<<s) | u64(x>>(n-s)))
|
||||
return u64(u64(x<<s) | u64(x>>(n - s)))
|
||||
}
|
||||
|
||||
// --- Reverse ---
|
||||
|
||||
// reverse8 returns the value of x with its bits in reversed order.
|
||||
[inline]
|
||||
pub fn reverse8(x byte) byte {
|
||||
@ -191,16 +190,16 @@ pub fn reverse8(x byte) byte {
|
||||
// reverse16 returns the value of x with its bits in reversed order.
|
||||
[inline]
|
||||
pub fn reverse16(x u16) u16 {
|
||||
return u16(rev8_tab[x>>8]) | u16(u16(rev8_tab[x&u16(0xff)])<<8)
|
||||
return u16(rev8_tab[x>>8]) | u16(u16(rev8_tab[x & u16(0xff)])<<8)
|
||||
}
|
||||
|
||||
// reverse32 returns the value of x with its bits in reversed order.
|
||||
[inline]
|
||||
pub fn reverse32(x u32) u32 {
|
||||
m := u64(1<<32) - 1
|
||||
mut y := u32(x>>u32(1)&u32(m0&m) | u32(u32(x&u32(m0&m))<<1))
|
||||
y = u32(y>>u32(2)&u32(m1&m) | u32(u32(y&u32(m1&m))<<u32(2)))
|
||||
y = u32(y>>u32(4)&u32(m2&m) | u32(u32(y&u32(m2&m))<<u32(4)))
|
||||
mut y := u32(x>>u32(1) & u32(m0 & m) | u32(u32(x & u32(m0 & m))<<1))
|
||||
y = u32(y>>u32(2) & u32(m1 & m) | u32(u32(y & u32(m1 & m))<<u32(2)))
|
||||
y = u32(y>>u32(4) & u32(m2 & m) | u32(u32(y & u32(m2 & m))<<u32(4)))
|
||||
return reverse_bytes32(y)
|
||||
}
|
||||
|
||||
@ -208,14 +207,13 @@ pub fn reverse32(x u32) u32 {
|
||||
[inline]
|
||||
pub fn reverse64(x u64) u64 {
|
||||
m := u64(1<<64) - 1
|
||||
mut y := u64(x>>u64(1)&(m0&m) | u64(u64(x&(m0&m))<<1))
|
||||
y = u64(y>>u64(2)&(m1&m) | u64(u64(y&(m1&m))<<2))
|
||||
y = u64(y>>u64(4)&(m2&m) | u64(u64(y&(m2&m))<<4))
|
||||
mut y := u64(x>>u64(1) & (m0 & m) | u64(u64(x & (m0 & m))<<1))
|
||||
y = u64(y>>u64(2) & (m1 & m) | u64(u64(y & (m1 & m))<<2))
|
||||
y = u64(y>>u64(4) & (m2 & m) | u64(u64(y & (m2 & m))<<4))
|
||||
return reverse_bytes64(y)
|
||||
}
|
||||
|
||||
// --- ReverseBytes ---
|
||||
|
||||
// reverse_bytes16 returns the value of x with its bytes in reversed order.
|
||||
//
|
||||
// This function's execution time does not depend on the inputs.
|
||||
@ -230,7 +228,7 @@ pub fn reverse_bytes16(x u16) u16 {
|
||||
[inline]
|
||||
pub fn reverse_bytes32(x u32) u32 {
|
||||
m := u64(1<<32) - 1
|
||||
y := u32(x>>u32(8)&u32(m3&m) | u32(u32(x&u32(m3&m))<<u32(8)))
|
||||
y := u32(x>>u32(8) & u32(m3 & m) | u32(u32(x & u32(m3 & m))<<u32(8)))
|
||||
return u32(y>>16) | u32(y<<16)
|
||||
}
|
||||
|
||||
@ -240,13 +238,12 @@ pub fn reverse_bytes32(x u32) u32 {
|
||||
[inline]
|
||||
pub fn reverse_bytes64(x u64) u64 {
|
||||
m := u64(1<<64) - 1
|
||||
mut y := u64(x>>u64(8)&(m3&m) | u64(u64(x&(m3&m))<<u64(8)))
|
||||
y = u64(y>>u64(16)&(m4&m) | u64(u64(y&(m4&m))<<u64(16)))
|
||||
mut y := u64(x>>u64(8) & (m3 & m) | u64(u64(x & (m3 & m))<<u64(8)))
|
||||
y = u64(y>>u64(16) & (m4 & m) | u64(u64(y & (m4 & m))<<u64(16)))
|
||||
return u64(y>>32) | u64(y<<32)
|
||||
}
|
||||
|
||||
// --- Len ---
|
||||
|
||||
// len8 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
|
||||
pub fn len8(x byte) int {
|
||||
return int(len8_tab[x])
|
||||
@ -296,3 +293,4 @@ pub fn len64(x u64) int {
|
||||
}
|
||||
return n + int(len8_tab[y])
|
||||
}
|
||||
|
||||
|
@ -1,80 +1,76 @@
|
||||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
module bits
|
||||
|
||||
const(
|
||||
ntz8_tab = [
|
||||
byte(0x08), 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x06, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x07, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x06, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
]
|
||||
pop8_tab = [
|
||||
byte(0x00), 0x01, 0x01, 0x02, 0x01, 0x02, 0x02, 0x03, 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04,
|
||||
0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
|
||||
0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
|
||||
0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
|
||||
0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
|
||||
0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
|
||||
0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
|
||||
0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
|
||||
0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
|
||||
0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
|
||||
0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
|
||||
0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
|
||||
0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
|
||||
0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
|
||||
0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
|
||||
0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07, 0x05, 0x06, 0x06, 0x07, 0x06, 0x07, 0x07, 0x08,
|
||||
]
|
||||
rev8_tab = [
|
||||
byte(0x00), 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
|
||||
0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
|
||||
0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
|
||||
0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
|
||||
0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
|
||||
0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea, 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
|
||||
0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
|
||||
0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
|
||||
0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1, 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
|
||||
0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
|
||||
0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
|
||||
0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
|
||||
0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
|
||||
0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
|
||||
0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
|
||||
0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
|
||||
]
|
||||
len8_tab = [
|
||||
byte(0x00), 0x01, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
|
||||
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
|
||||
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
|
||||
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
|
||||
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
|
||||
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
|
||||
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
|
||||
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
|
||||
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
|
||||
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
|
||||
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
|
||||
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
|
||||
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
|
||||
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
|
||||
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
|
||||
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
|
||||
]
|
||||
const (
|
||||
ntz8_tab = [byte(0x08), 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x06, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x07, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x06, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
|
||||
]
|
||||
pop8_tab = [byte(0x00), 0x01, 0x01, 0x02, 0x01, 0x02, 0x02, 0x03, 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04,
|
||||
0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
|
||||
0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
|
||||
0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
|
||||
0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
|
||||
0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
|
||||
0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
|
||||
0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
|
||||
0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
|
||||
0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
|
||||
0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
|
||||
0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
|
||||
0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
|
||||
0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
|
||||
0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
|
||||
0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07, 0x05, 0x06, 0x06, 0x07, 0x06, 0x07, 0x07, 0x08,
|
||||
]
|
||||
rev8_tab = [byte(0x00), 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
|
||||
0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
|
||||
0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
|
||||
0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
|
||||
0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
|
||||
0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea, 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
|
||||
0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
|
||||
0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
|
||||
0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1, 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
|
||||
0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
|
||||
0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
|
||||
0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
|
||||
0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
|
||||
0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
|
||||
0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
|
||||
0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
|
||||
]
|
||||
len8_tab = [byte(0x00), 0x01, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
|
||||
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
|
||||
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
|
||||
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
|
||||
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
|
||||
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
|
||||
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
|
||||
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
|
||||
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
|
||||
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
|
||||
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
|
||||
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
|
||||
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
|
||||
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
|
||||
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
|
||||
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
|
||||
]
|
||||
)
|
||||
|
||||
|
@ -1,42 +1,36 @@
|
||||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
module math
|
||||
|
||||
pub const (
|
||||
e = 2.71828182845904523536028747135266249775724709369995957496696763
|
||||
pi = 3.14159265358979323846264338327950288419716939937510582097494459
|
||||
e = 2.71828182845904523536028747135266249775724709369995957496696763
|
||||
pi = 3.14159265358979323846264338327950288419716939937510582097494459
|
||||
phi = 1.61803398874989484820458683436563811772030917980576286213544862
|
||||
tau = 6.28318530717958647692528676655900576839433879875021164194988918
|
||||
|
||||
sqrt2 = 1.41421356237309504880168872420969807856967187537694807317667974
|
||||
sqrt_e = 1.64872127070012814684865078781416357165377610071014801157507931
|
||||
sqrt_pi = 1.77245385090551602729816748334114518279754945612238712821380779
|
||||
sqrt2 = 1.41421356237309504880168872420969807856967187537694807317667974
|
||||
sqrt_e = 1.64872127070012814684865078781416357165377610071014801157507931
|
||||
sqrt_pi = 1.77245385090551602729816748334114518279754945612238712821380779
|
||||
sqrt_tau = 2.50662827463100050241576528481104525300698674060993831662992357
|
||||
sqrt_phi = 1.27201964951406896425242246173749149171560804184009624861664038
|
||||
|
||||
ln2 = 0.693147180559945309417232121458176568075500134360255254120680009
|
||||
log2_e = 1.0 / ln2
|
||||
ln10 = 2.30258509299404568401799145468436420760110148862877297603332790
|
||||
ln2 = 0.693147180559945309417232121458176568075500134360255254120680009
|
||||
log2_e = 1.0 / ln2
|
||||
ln10 = 2.30258509299404568401799145468436420760110148862877297603332790
|
||||
log10_e = 1.0 / ln10
|
||||
)
|
||||
|
||||
// Floating-point limit values
|
||||
// max is the largest finite value representable by the type.
|
||||
// smallest_non_zero is the smallest positive, non-zero value representable by the type.
|
||||
pub const (
|
||||
max_f32 = 3.40282346638528859811704183484516925440e+38 // 2**127 * (2**24 - 1) / 2**23
|
||||
max_f32 = 3.40282346638528859811704183484516925440e+38 // 2**127 * (2**24 - 1) / 2**23
|
||||
smallest_non_zero_f32 = 1.401298464324817070923729583289916131280e-45 // 1 / 2**(127 - 1 + 23)
|
||||
|
||||
max_f64 = 1.797693134862315708145274237317043567981e+308 // 2**1023 * (2**53 - 1) / 2**52
|
||||
max_f64 = 1.797693134862315708145274237317043567981e+308 // 2**1023 * (2**53 - 1) / 2**52
|
||||
smallest_non_zero_f64 = 4.940656458412465441765687928682213723651e-324 // 1 / 2**(1023 - 1 + 52)
|
||||
)
|
||||
|
||||
// Integer limit values
|
||||
pub const (
|
||||
max_i8 = 127
|
||||
min_i8 = -128
|
||||
max_i8 = 127
|
||||
min_i8 = -128
|
||||
max_i16 = 32767
|
||||
min_i16 = -32768
|
||||
max_i32 = 2147483647
|
||||
@ -46,8 +40,9 @@ pub const (
|
||||
// consecutive subtraction by 1
|
||||
min_i64 = -9223372036854775807 - 1
|
||||
max_i64 = 9223372036854775807
|
||||
max_u8 = 255
|
||||
max_u8 = 255
|
||||
max_u16 = 65535
|
||||
max_u32 = 4294967295
|
||||
max_u64 = 18446744073709551615
|
||||
)
|
||||
|
||||
|
@ -1,177 +1,177 @@
|
||||
module math
|
||||
|
||||
const(
|
||||
factorials = [
|
||||
f64(1.000000000000000000000e+0), /* 0! */
|
||||
1.000000000000000000000e+0, /* 1! */
|
||||
2.000000000000000000000e+0, /* 2! */
|
||||
6.000000000000000000000e+0, /* 3! */
|
||||
2.400000000000000000000e+1, /* 4! */
|
||||
1.200000000000000000000e+2, /* 5! */
|
||||
7.200000000000000000000e+2, /* 6! */
|
||||
5.040000000000000000000e+3, /* 7! */
|
||||
4.032000000000000000000e+4, /* 8! */
|
||||
3.628800000000000000000e+5, /* 9! */
|
||||
3.628800000000000000000e+6, /* 10! */
|
||||
3.991680000000000000000e+7, /* 11! */
|
||||
4.790016000000000000000e+8, /* 12! */
|
||||
6.227020800000000000000e+9, /* 13! */
|
||||
8.717829120000000000000e+10, /* 14! */
|
||||
1.307674368000000000000e+12, /* 15! */
|
||||
2.092278988800000000000e+13, /* 16! */
|
||||
3.556874280960000000000e+14, /* 17! */
|
||||
6.402373705728000000000e+15, /* 18! */
|
||||
1.216451004088320000000e+17, /* 19! */
|
||||
2.432902008176640000000e+18, /* 20! */
|
||||
5.109094217170944000000e+19, /* 21! */
|
||||
1.124000727777607680000e+21, /* 22! */
|
||||
2.585201673888497664000e+22, /* 23! */
|
||||
6.204484017332394393600e+23, /* 24! */
|
||||
1.551121004333098598400e+25, /* 25! */
|
||||
4.032914611266056355840e+26, /* 26! */
|
||||
1.088886945041835216077e+28, /* 27! */
|
||||
3.048883446117138605015e+29, /* 28! */
|
||||
8.841761993739701954544e+30, /* 29! */
|
||||
2.652528598121910586363e+32, /* 30! */
|
||||
8.222838654177922817726e+33, /* 31! */
|
||||
2.631308369336935301672e+35, /* 32! */
|
||||
8.683317618811886495518e+36, /* 33! */
|
||||
2.952327990396041408476e+38, /* 34! */
|
||||
1.033314796638614492967e+40, /* 35! */
|
||||
3.719933267899012174680e+41, /* 36! */
|
||||
1.376375309122634504632e+43, /* 37! */
|
||||
5.230226174666011117600e+44, /* 38! */
|
||||
2.039788208119744335864e+46, /* 39! */
|
||||
8.159152832478977343456e+47, /* 40! */
|
||||
3.345252661316380710817e+49, /* 41! */
|
||||
1.405006117752879898543e+51, /* 42! */
|
||||
6.041526306337383563736e+52, /* 43! */
|
||||
2.658271574788448768044e+54, /* 44! */
|
||||
1.196222208654801945620e+56, /* 45! */
|
||||
5.502622159812088949850e+57, /* 46! */
|
||||
2.586232415111681806430e+59, /* 47! */
|
||||
1.241391559253607267086e+61, /* 48! */
|
||||
6.082818640342675608723e+62, /* 49! */
|
||||
3.041409320171337804361e+64, /* 50! */
|
||||
1.551118753287382280224e+66, /* 51! */
|
||||
8.065817517094387857166e+67, /* 52! */
|
||||
4.274883284060025564298e+69, /* 53! */
|
||||
2.308436973392413804721e+71, /* 54! */
|
||||
1.269640335365827592597e+73, /* 55! */
|
||||
7.109985878048634518540e+74, /* 56! */
|
||||
4.052691950487721675568e+76, /* 57! */
|
||||
2.350561331282878571829e+78, /* 58! */
|
||||
1.386831185456898357379e+80, /* 59! */
|
||||
8.320987112741390144276e+81, /* 60! */
|
||||
5.075802138772247988009e+83, /* 61! */
|
||||
3.146997326038793752565e+85, /* 62! */
|
||||
1.982608315404440064116e+87, /* 63! */
|
||||
1.268869321858841641034e+89, /* 64! */
|
||||
8.247650592082470666723e+90, /* 65! */
|
||||
5.443449390774430640037e+92, /* 66! */
|
||||
3.647111091818868528825e+94, /* 67! */
|
||||
2.480035542436830599601e+96, /* 68! */
|
||||
1.711224524281413113725e+98, /* 69! */
|
||||
1.197857166996989179607e+100, /* 70! */
|
||||
8.504785885678623175212e+101, /* 71! */
|
||||
6.123445837688608686152e+103, /* 72! */
|
||||
4.470115461512684340891e+105, /* 73! */
|
||||
3.307885441519386412260e+107, /* 74! */
|
||||
2.480914081139539809195e+109, /* 75! */
|
||||
1.885494701666050254988e+111, /* 76! */
|
||||
1.451830920282858696341e+113, /* 77! */
|
||||
1.132428117820629783146e+115, /* 78! */
|
||||
8.946182130782975286851e+116, /* 79! */
|
||||
7.156945704626380229481e+118, /* 80! */
|
||||
5.797126020747367985880e+120, /* 81! */
|
||||
4.753643337012841748421e+122, /* 82! */
|
||||
3.945523969720658651190e+124, /* 83! */
|
||||
3.314240134565353266999e+126, /* 84! */
|
||||
2.817104114380550276949e+128, /* 85! */
|
||||
2.422709538367273238177e+130, /* 86! */
|
||||
2.107757298379527717214e+132, /* 87! */
|
||||
1.854826422573984391148e+134, /* 88! */
|
||||
1.650795516090846108122e+136, /* 89! */
|
||||
1.485715964481761497310e+138, /* 90! */
|
||||
1.352001527678402962552e+140, /* 91! */
|
||||
1.243841405464130725548e+142, /* 92! */
|
||||
1.156772507081641574759e+144, /* 93! */
|
||||
1.087366156656743080274e+146, /* 94! */
|
||||
1.032997848823905926260e+148, /* 95! */
|
||||
9.916779348709496892096e+149, /* 96! */
|
||||
9.619275968248211985333e+151, /* 97! */
|
||||
9.426890448883247745626e+153, /* 98! */
|
||||
9.332621544394415268170e+155, /* 99! */
|
||||
9.332621544394415268170e+157, /* 100! */
|
||||
9.425947759838359420852e+159, /* 101! */
|
||||
9.614466715035126609269e+161, /* 102! */
|
||||
9.902900716486180407547e+163, /* 103! */
|
||||
1.029901674514562762385e+166, /* 104! */
|
||||
1.081396758240290900504e+168, /* 105! */
|
||||
1.146280563734708354534e+170, /* 106! */
|
||||
1.226520203196137939352e+172, /* 107! */
|
||||
1.324641819451828974500e+174, /* 108! */
|
||||
1.443859583202493582205e+176, /* 109! */
|
||||
1.588245541522742940425e+178, /* 110! */
|
||||
1.762952551090244663872e+180, /* 111! */
|
||||
1.974506857221074023537e+182, /* 112! */
|
||||
2.231192748659813646597e+184, /* 113! */
|
||||
2.543559733472187557120e+186, /* 114! */
|
||||
2.925093693493015690688e+188, /* 115! */
|
||||
3.393108684451898201198e+190, /* 116! */
|
||||
3.969937160808720895402e+192, /* 117! */
|
||||
4.684525849754290656574e+194, /* 118! */
|
||||
5.574585761207605881323e+196, /* 119! */
|
||||
6.689502913449127057588e+198, /* 120! */
|
||||
8.094298525273443739682e+200, /* 121! */
|
||||
9.875044200833601362412e+202, /* 122! */
|
||||
1.214630436702532967577e+205, /* 123! */
|
||||
1.506141741511140879795e+207, /* 124! */
|
||||
1.882677176888926099744e+209, /* 125! */
|
||||
2.372173242880046885677e+211, /* 126! */
|
||||
3.012660018457659544810e+213, /* 127! */
|
||||
3.856204823625804217357e+215, /* 128! */
|
||||
4.974504222477287440390e+217, /* 129! */
|
||||
6.466855489220473672507e+219, /* 130! */
|
||||
8.471580690878820510985e+221, /* 131! */
|
||||
1.118248651196004307450e+224, /* 132! */
|
||||
1.487270706090685728908e+226, /* 133! */
|
||||
1.992942746161518876737e+228, /* 134! */
|
||||
2.690472707318050483595e+230, /* 135! */
|
||||
3.659042881952548657690e+232, /* 136! */
|
||||
5.012888748274991661035e+234, /* 137! */
|
||||
6.917786472619488492228e+236, /* 138! */
|
||||
9.615723196941089004197e+238, /* 139! */
|
||||
1.346201247571752460588e+241, /* 140! */
|
||||
1.898143759076170969429e+243, /* 141! */
|
||||
2.695364137888162776589e+245, /* 142! */
|
||||
3.854370717180072770522e+247, /* 143! */
|
||||
5.550293832739304789551e+249, /* 144! */
|
||||
8.047926057471991944849e+251, /* 145! */
|
||||
1.174997204390910823948e+254, /* 146! */
|
||||
1.727245890454638911203e+256, /* 147! */
|
||||
2.556323917872865588581e+258, /* 148! */
|
||||
3.808922637630569726986e+260, /* 149! */
|
||||
5.713383956445854590479e+262, /* 150! */
|
||||
8.627209774233240431623e+264, /* 151! */
|
||||
1.311335885683452545607e+267, /* 152! */
|
||||
2.006343905095682394778e+269, /* 153! */
|
||||
3.089769613847350887959e+271, /* 154! */
|
||||
4.789142901463393876336e+273, /* 155! */
|
||||
7.471062926282894447084e+275, /* 156! */
|
||||
1.172956879426414428192e+278, /* 157! */
|
||||
1.853271869493734796544e+280, /* 158! */
|
||||
2.946702272495038326504e+282, /* 159! */
|
||||
4.714723635992061322407e+284, /* 160! */
|
||||
7.590705053947218729075e+286, /* 161! */
|
||||
1.229694218739449434110e+289, /* 162! */
|
||||
2.004401576545302577600e+291, /* 163! */
|
||||
3.287218585534296227263e+293, /* 164! */
|
||||
5.423910666131588774984e+295, /* 165! */
|
||||
9.003691705778437366474e+297, /* 166! */
|
||||
1.503616514864999040201e+300, /* 167! */
|
||||
2.526075744973198387538e+302, /* 168! */
|
||||
4.269068009004705274939e+304, /* 169! */
|
||||
7.257415615307998967397e+306 /* 170! */
|
||||
const (
|
||||
factorials = [f64(1.000000000000000000000e+0),/* 0! */
|
||||
1.000000000000000000000e+0,/* 1! */
|
||||
2.000000000000000000000e+0,/* 2! */
|
||||
6.000000000000000000000e+0,/* 3! */
|
||||
2.400000000000000000000e+1,/* 4! */
|
||||
1.200000000000000000000e+2,/* 5! */
|
||||
7.200000000000000000000e+2,/* 6! */
|
||||
5.040000000000000000000e+3,/* 7! */
|
||||
4.032000000000000000000e+4,/* 8! */
|
||||
3.628800000000000000000e+5,/* 9! */
|
||||
3.628800000000000000000e+6,/* 10! */
|
||||
3.991680000000000000000e+7,/* 11! */
|
||||
4.790016000000000000000e+8,/* 12! */
|
||||
6.227020800000000000000e+9,/* 13! */
|
||||
8.717829120000000000000e+10,/* 14! */
|
||||
1.307674368000000000000e+12,/* 15! */
|
||||
2.092278988800000000000e+13,/* 16! */
|
||||
3.556874280960000000000e+14,/* 17! */
|
||||
6.402373705728000000000e+15,/* 18! */
|
||||
1.216451004088320000000e+17,/* 19! */
|
||||
2.432902008176640000000e+18,/* 20! */
|
||||
5.109094217170944000000e+19,/* 21! */
|
||||
1.124000727777607680000e+21,/* 22! */
|
||||
2.585201673888497664000e+22,/* 23! */
|
||||
6.204484017332394393600e+23,/* 24! */
|
||||
1.551121004333098598400e+25,/* 25! */
|
||||
4.032914611266056355840e+26,/* 26! */
|
||||
1.088886945041835216077e+28,/* 27! */
|
||||
3.048883446117138605015e+29,/* 28! */
|
||||
8.841761993739701954544e+30,/* 29! */
|
||||
2.652528598121910586363e+32,/* 30! */
|
||||
8.222838654177922817726e+33,/* 31! */
|
||||
2.631308369336935301672e+35,/* 32! */
|
||||
8.683317618811886495518e+36,/* 33! */
|
||||
2.952327990396041408476e+38,/* 34! */
|
||||
1.033314796638614492967e+40,/* 35! */
|
||||
3.719933267899012174680e+41,/* 36! */
|
||||
1.376375309122634504632e+43,/* 37! */
|
||||
5.230226174666011117600e+44,/* 38! */
|
||||
2.039788208119744335864e+46,/* 39! */
|
||||
8.159152832478977343456e+47,/* 40! */
|
||||
3.345252661316380710817e+49,/* 41! */
|
||||
1.405006117752879898543e+51,/* 42! */
|
||||
6.041526306337383563736e+52,/* 43! */
|
||||
2.658271574788448768044e+54,/* 44! */
|
||||
1.196222208654801945620e+56,/* 45! */
|
||||
5.502622159812088949850e+57,/* 46! */
|
||||
2.586232415111681806430e+59,/* 47! */
|
||||
1.241391559253607267086e+61,/* 48! */
|
||||
6.082818640342675608723e+62,/* 49! */
|
||||
3.041409320171337804361e+64,/* 50! */
|
||||
1.551118753287382280224e+66,/* 51! */
|
||||
8.065817517094387857166e+67,/* 52! */
|
||||
4.274883284060025564298e+69,/* 53! */
|
||||
2.308436973392413804721e+71,/* 54! */
|
||||
1.269640335365827592597e+73,/* 55! */
|
||||
7.109985878048634518540e+74,/* 56! */
|
||||
4.052691950487721675568e+76,/* 57! */
|
||||
2.350561331282878571829e+78,/* 58! */
|
||||
1.386831185456898357379e+80,/* 59! */
|
||||
8.320987112741390144276e+81,/* 60! */
|
||||
5.075802138772247988009e+83,/* 61! */
|
||||
3.146997326038793752565e+85,/* 62! */
|
||||
1.982608315404440064116e+87,/* 63! */
|
||||
1.268869321858841641034e+89,/* 64! */
|
||||
8.247650592082470666723e+90,/* 65! */
|
||||
5.443449390774430640037e+92,/* 66! */
|
||||
3.647111091818868528825e+94,/* 67! */
|
||||
2.480035542436830599601e+96,/* 68! */
|
||||
1.711224524281413113725e+98,/* 69! */
|
||||
1.197857166996989179607e+100,/* 70! */
|
||||
8.504785885678623175212e+101,/* 71! */
|
||||
6.123445837688608686152e+103,/* 72! */
|
||||
4.470115461512684340891e+105,/* 73! */
|
||||
3.307885441519386412260e+107,/* 74! */
|
||||
2.480914081139539809195e+109,/* 75! */
|
||||
1.885494701666050254988e+111,/* 76! */
|
||||
1.451830920282858696341e+113,/* 77! */
|
||||
1.132428117820629783146e+115,/* 78! */
|
||||
8.946182130782975286851e+116,/* 79! */
|
||||
7.156945704626380229481e+118,/* 80! */
|
||||
5.797126020747367985880e+120,/* 81! */
|
||||
4.753643337012841748421e+122,/* 82! */
|
||||
3.945523969720658651190e+124,/* 83! */
|
||||
3.314240134565353266999e+126,/* 84! */
|
||||
2.817104114380550276949e+128,/* 85! */
|
||||
2.422709538367273238177e+130,/* 86! */
|
||||
2.107757298379527717214e+132,/* 87! */
|
||||
1.854826422573984391148e+134,/* 88! */
|
||||
1.650795516090846108122e+136,/* 89! */
|
||||
1.485715964481761497310e+138,/* 90! */
|
||||
1.352001527678402962552e+140,/* 91! */
|
||||
1.243841405464130725548e+142,/* 92! */
|
||||
1.156772507081641574759e+144,/* 93! */
|
||||
1.087366156656743080274e+146,/* 94! */
|
||||
1.032997848823905926260e+148,/* 95! */
|
||||
9.916779348709496892096e+149,/* 96! */
|
||||
9.619275968248211985333e+151,/* 97! */
|
||||
9.426890448883247745626e+153,/* 98! */
|
||||
9.332621544394415268170e+155,/* 99! */
|
||||
9.332621544394415268170e+157,/* 100! */
|
||||
9.425947759838359420852e+159,/* 101! */
|
||||
9.614466715035126609269e+161,/* 102! */
|
||||
9.902900716486180407547e+163,/* 103! */
|
||||
1.029901674514562762385e+166,/* 104! */
|
||||
1.081396758240290900504e+168,/* 105! */
|
||||
1.146280563734708354534e+170,/* 106! */
|
||||
1.226520203196137939352e+172,/* 107! */
|
||||
1.324641819451828974500e+174,/* 108! */
|
||||
1.443859583202493582205e+176,/* 109! */
|
||||
1.588245541522742940425e+178,/* 110! */
|
||||
1.762952551090244663872e+180,/* 111! */
|
||||
1.974506857221074023537e+182,/* 112! */
|
||||
2.231192748659813646597e+184,/* 113! */
|
||||
2.543559733472187557120e+186,/* 114! */
|
||||
2.925093693493015690688e+188,/* 115! */
|
||||
3.393108684451898201198e+190,/* 116! */
|
||||
3.969937160808720895402e+192,/* 117! */
|
||||
4.684525849754290656574e+194,/* 118! */
|
||||
5.574585761207605881323e+196,/* 119! */
|
||||
6.689502913449127057588e+198,/* 120! */
|
||||
8.094298525273443739682e+200,/* 121! */
|
||||
9.875044200833601362412e+202,/* 122! */
|
||||
1.214630436702532967577e+205,/* 123! */
|
||||
1.506141741511140879795e+207,/* 124! */
|
||||
1.882677176888926099744e+209,/* 125! */
|
||||
2.372173242880046885677e+211,/* 126! */
|
||||
3.012660018457659544810e+213,/* 127! */
|
||||
3.856204823625804217357e+215,/* 128! */
|
||||
4.974504222477287440390e+217,/* 129! */
|
||||
6.466855489220473672507e+219,/* 130! */
|
||||
8.471580690878820510985e+221,/* 131! */
|
||||
1.118248651196004307450e+224,/* 132! */
|
||||
1.487270706090685728908e+226,/* 133! */
|
||||
1.992942746161518876737e+228,/* 134! */
|
||||
2.690472707318050483595e+230,/* 135! */
|
||||
3.659042881952548657690e+232,/* 136! */
|
||||
5.012888748274991661035e+234,/* 137! */
|
||||
6.917786472619488492228e+236,/* 138! */
|
||||
9.615723196941089004197e+238,/* 139! */
|
||||
1.346201247571752460588e+241,/* 140! */
|
||||
1.898143759076170969429e+243,/* 141! */
|
||||
2.695364137888162776589e+245,/* 142! */
|
||||
3.854370717180072770522e+247,/* 143! */
|
||||
5.550293832739304789551e+249,/* 144! */
|
||||
8.047926057471991944849e+251,/* 145! */
|
||||
1.174997204390910823948e+254,/* 146! */
|
||||
1.727245890454638911203e+256,/* 147! */
|
||||
2.556323917872865588581e+258,/* 148! */
|
||||
3.808922637630569726986e+260,/* 149! */
|
||||
5.713383956445854590479e+262,/* 150! */
|
||||
8.627209774233240431623e+264,/* 151! */
|
||||
1.311335885683452545607e+267,/* 152! */
|
||||
2.006343905095682394778e+269,/* 153! */
|
||||
3.089769613847350887959e+271,/* 154! */
|
||||
4.789142901463393876336e+273,/* 155! */
|
||||
7.471062926282894447084e+275,/* 156! */
|
||||
1.172956879426414428192e+278,/* 157! */
|
||||
1.853271869493734796544e+280,/* 158! */
|
||||
2.946702272495038326504e+282,/* 159! */
|
||||
4.714723635992061322407e+284,/* 160! */
|
||||
7.590705053947218729075e+286,/* 161! */
|
||||
1.229694218739449434110e+289,/* 162! */
|
||||
2.004401576545302577600e+291,/* 163! */
|
||||
3.287218585534296227263e+293,/* 164! */
|
||||
5.423910666131588774984e+295,/* 165! */
|
||||
9.003691705778437366474e+297,/* 166! */
|
||||
1.503616514864999040201e+300,/* 167! */
|
||||
2.526075744973198387538e+302,/* 168! */
|
||||
4.269068009004705274939e+304,/* 169! */
|
||||
7.257415615307998967397e+306/* 170! */
|
||||
]
|
||||
)
|
||||
|
||||
|
@ -1,46 +1,97 @@
|
||||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
module math
|
||||
|
||||
#include <math.h>
|
||||
|
||||
fn C.acos(x f64) f64
|
||||
|
||||
|
||||
fn C.asin(x f64) f64
|
||||
|
||||
|
||||
fn C.atan(x f64) f64
|
||||
|
||||
|
||||
fn C.atan2(y f64, x f64) f64
|
||||
|
||||
|
||||
fn C.cbrt(x f64) f64
|
||||
|
||||
|
||||
fn C.ceil(x f64) f64
|
||||
|
||||
|
||||
fn C.cos(x f64) f64
|
||||
|
||||
|
||||
fn C.cosh(x f64) f64
|
||||
|
||||
|
||||
fn C.erf(x f64) f64
|
||||
|
||||
|
||||
fn C.erfc(x f64) f64
|
||||
|
||||
|
||||
fn C.exp(x f64) f64
|
||||
|
||||
|
||||
fn C.exp2(x f64) f64
|
||||
|
||||
|
||||
fn C.fabs(x f64) f64
|
||||
|
||||
|
||||
fn C.floor(x f64) f64
|
||||
|
||||
|
||||
fn C.fmod(x f64, y f64) f64
|
||||
|
||||
|
||||
fn C.hypot(x f64, y f64) f64
|
||||
|
||||
|
||||
fn C.log(x f64) f64
|
||||
|
||||
|
||||
fn C.log2(x f64) f64
|
||||
|
||||
|
||||
fn C.log10(x f64) f64
|
||||
|
||||
|
||||
fn C.lgamma(x f64) f64
|
||||
|
||||
|
||||
fn C.pow(x f64, y f64) f64
|
||||
|
||||
|
||||
fn C.round(x f64) f64
|
||||
|
||||
|
||||
fn C.sin(x f64) f64
|
||||
|
||||
|
||||
fn C.sinh(x f64) f64
|
||||
|
||||
|
||||
fn C.sqrt(x f64) f64
|
||||
|
||||
|
||||
fn C.tgamma(x f64) f64
|
||||
|
||||
|
||||
fn C.tan(x f64) f64
|
||||
|
||||
|
||||
fn C.tanh(x f64) f64
|
||||
|
||||
|
||||
fn C.trunc(x f64) f64
|
||||
|
||||
|
||||
// NOTE
|
||||
// When adding a new function, please make sure it's in the right place.
|
||||
// All functions are sorted alphabetically.
|
||||
|
||||
// Returns the absolute value.
|
||||
pub fn abs(a f64) f64 {
|
||||
return C.fabs(a)
|
||||
@ -133,16 +184,13 @@ pub fn exp2(a f64) f64 {
|
||||
// factorial calculates the factorial of the provided value.
|
||||
pub fn factorial(n f64) f64 {
|
||||
// For a large postive argument (n >= factorials.len) return max_f64
|
||||
|
||||
if n >= factorials.len {
|
||||
return max_f64
|
||||
return max_f64
|
||||
}
|
||||
|
||||
// Otherwise return n!.
|
||||
if n == f64(i64(n)) && n >= 0.0 {
|
||||
return factorials[i64(n)]
|
||||
}
|
||||
|
||||
return gamma(n + 1.0)
|
||||
}
|
||||
|
||||
@ -268,6 +316,7 @@ pub fn sinh(a f64) f64 {
|
||||
pub fn sqrt(a f64) f64 {
|
||||
return C.sqrt(a)
|
||||
}
|
||||
|
||||
// tan calculates tangent.
|
||||
pub fn tan(a f64) f64 {
|
||||
return C.tan(a)
|
||||
@ -283,3 +332,4 @@ pub fn tanh(a f64) f64 {
|
||||
pub fn trunc(a f64) f64 {
|
||||
return C.trunc(a)
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,7 @@
|
||||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
module math
|
||||
|
||||
// f32_bits returns the IEEE 754 binary representation of f,
|
||||
// with the sign bit of f and the result in the same bit position.
|
||||
// f32_bits(f32_from_bits(x)) == x.
|
||||
@ -37,3 +35,4 @@ pub fn f64_from_bits(b u64) f64 {
|
||||
p := *f64(&b)
|
||||
return *p
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user