1
0
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:
Alexander Medvednikov
2019-12-22 02:22:32 +03:00
parent 9198285688
commit a251db068f
17 changed files with 721 additions and 659 deletions

View File

@ -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
// }

View File

@ -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])
}

View File

@ -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,
]
)

View File

@ -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
)

View File

@ -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! */
]
)

View File

@ -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)
}

View File

@ -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
}