Skip to content

Commit

Permalink
Merge branch 'main' into move-to-math
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyfettes authored and qazxcdswe123 committed Oct 8, 2024
2 parents 57e7073 + e4752ed commit 86310f8
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 31 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
cd hello
echo """fn main {
println([1, 2, 3].rev())
}""" > main/main.mbt
moon run main
}""" > src/main/main.mbt
moon run src/main
# Output: [3, 2, 1]
```

Expand Down
28 changes: 27 additions & 1 deletion NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,30 @@ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

File `float/exp.mbt` is adapted from [musl](https://www.musl-libc.org/),
Specifically, it is adapted from `src/math/logf.c`, `src/math/logf_data.h`, and
`src/math/logf_data.c`. These files are Copyright (c) 2017-2018 Arm Limited and
licensed under the MIT license.

Here is a copy of MIT license:

Copyright (c) 2017-2018 Arm Limited.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the “Software”), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 changes: 6 additions & 13 deletions builtin/arraycore_js.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,9 @@ pub fn unsafe_pop[T](self : Array[T]) -> T {
/// ```
/// @alert unsafe "Panic if index is out of bounds."
pub fn remove[T](self : Array[T], index : Int) -> T {
if index < 0 || index >= self.length() {
let len = self.length()
guard index >= 0 && index < self.length() else {
abort(
"index out of bounds: the len is from 0 to \{len} but the index is \{index}",
"index out of bounds: the len is from 0 to \{self.length()} but the index is \{index}",
)
}
let value = self.buffer()[index]
Expand All @@ -255,14 +254,9 @@ pub fn remove[T](self : Array[T], index : Int) -> T {
/// ```
/// @alert unsafe "Panic if index is out of bounds."
pub fn drain[T](self : Array[T], begin : Int, end : Int) -> Array[T] {
if begin < 0 ||
begin >= self.length() ||
end < 0 ||
end > self.length() ||
begin > end {
let len = self.length()
guard begin >= 0 && end <= self.length() && begin <= end else {
abort(
"index out of bounds: the len is \{len} but the index is (\{begin}, \{end})",
"index out of bounds: the len is \{self.length()} but the index is (\{begin}, \{end})",
)
}
JSArray::ofAnyArray(self).splice(begin, end - begin).toAnyArray()
Expand All @@ -276,10 +270,9 @@ pub fn drain[T](self : Array[T], begin : Int, end : Int) -> Array[T] {
/// ```
/// @alert unsafe "Panic if index is out of bounds."
pub fn insert[T](self : Array[T], index : Int, value : T) -> Unit {
if index < 0 || index > self.length() {
let len = self.length()
guard index >= 0 && index <= self.length() else {
abort(
"index out of bounds: the len is from 0 to \{len} but the index is \{index}",
"index out of bounds: the len is from 0 to \{self.length()} but the index is \{index}",
)
}
let _ = JSArray::ofAnyArray(self).splice1(index, 0, JSValue::ofAny(value))
Expand Down
10 changes: 4 additions & 6 deletions builtin/arraycore_nonjs.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,9 @@ pub fn unsafe_pop[T](self : Array[T]) -> T {
/// ```
/// @alert unsafe "Panic if index is out of bounds."
pub fn remove[T](self : Array[T], index : Int) -> T {
if index < 0 || index >= self.length() {
let len = self.length()
guard index >= 0 && index < self.length() else {
abort(
"index out of bounds: the len is from 0 to \{len} but the index is \{index}",
"index out of bounds: the len is from 0 to \{self.length()} but the index is \{index}",
)
}
let value = self.buffer()[index]
Expand Down Expand Up @@ -338,10 +337,9 @@ pub fn drain[T](self : Array[T], begin : Int, end : Int) -> Array[T] {
/// ```
/// @alert unsafe "Panic if index is out of bounds."
pub fn insert[T](self : Array[T], index : Int, value : T) -> Unit {
if index < 0 || index > self.length() {
let len = self.length()
guard index >= 0 && index <= self.length() else {
abort(
"index out of bounds: the len is from 0 to \{len} but the index is \{index}",
"index out of bounds: the len is from 0 to \{self.length()} but the index is \{index}",
)
}
if self.length() == self.buffer()._.length() {
Expand Down
15 changes: 6 additions & 9 deletions builtin/arrayview.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,27 @@ pub fn length[T](self : ArrayView[T]) -> Int {
}

pub fn op_get[T](self : ArrayView[T], index : Int) -> T {
if index < 0 || index >= self.len {
let len = self.len
guard index >= 0 && index < self.len else {
abort(
"index out of bounds: the len is from 0 to \{len} but the index is \{index}",
"index out of bounds: the len is from 0 to \{self.len} but the index is \{index}",
)
}
self.buf[self.start + index]
}

pub fn op_set[T](self : ArrayView[T], index : Int, value : T) -> Unit {
if index < 0 || index >= self.len {
let len = self.len
guard index >= 0 && index < self.len else {
abort(
"index out of bounds: the len is from 0 to \{len} but the index is \{index}",
"index out of bounds: the len is from 0 to \{self.len} but the index is \{index}",
)
}
self.buf[self.start + index] = value
}

pub fn swap[T](self : ArrayView[T], i : Int, j : Int) -> Unit {
if i >= self.len || j >= self.len || i < 0 || j < 0 {
let len = self.len
guard i >= 0 && i < self.len && j >= 0 && j < self.len else {
abort(
"index out of bounds: the len is from 0 to \{len} but the index is (\{i}, \{j})",
"index out of bounds: the len is from 0 to \{self.len} but the index is (\{i}, \{j})",
)
}
let temp = self.buf[self.start + i]
Expand Down
1 change: 1 addition & 0 deletions float/float.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ let not_a_number : Float
// Types and methods
impl Float {
abs(Float) -> Float
ln(Float) -> Float
to_string(Float) -> String
}

Expand Down
77 changes: 77 additions & 0 deletions float/log.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2024 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

let logf_off = 0x3f330000U

let logf_table_bits = 4

let logf_n : UInt = 1U << logf_table_bits

priv struct LogfData {
invc : Array[Double]
logc : Array[Double]
ln2 : Double
poly : Array[Double]
}

let logf_data : LogfData = {
invc: [
0x1.661ec79f8f3bep+0, 0x1.571ed4aaf883dp+0, 0x1.49539f0f010bp+0, 0x1.3c995b0b80385p+0,
0x1.30d190c8864a5p+0, 0x1.25e227b0b8eap+0, 0x1.1bb4a4a1a343fp+0, 0x1.12358f08ae5bap+0,
0x1.0953f419900a7p+0, 0x1.0p+0, 0x1.e608cfd9a47acp-1, 0x1.ca4b31f026aap-1, 0x1.b2036576afce6p-1,
0x1.9c2d163a1aa2dp-1, 0x1.886e6037841edp-1, 0x1.767dcf5534862p-1,
],
logc: [
-0x1.57bf7808caadep-2, -0x1.2bef0a7c06ddbp-2, -0x1.01eae7f513a67p-2, -0x1.b31d8a68224e9p-3,
-0x1.6574f0ac07758p-3, -0x1.1aa2bc79c81p-3, -0x1.a4e76ce8c0e5ep-4, -0x1.1973c5a611cccp-4,
-0x1.252f438e10c1ep-5, 0x0.0p+0, 0x1.aa5aa5df25984p-5, 0x1.c5e53aa362eb4p-4,
0x1.526e57720db08p-3, 0x1.bc2860d22477p-3, 0x1.1058bc8a07ee1p-2, 0x1.4043057b6ee09p-2,
],
ln2: 0x1.62e42fefa39efp-1,
poly: [-0x1.00ea348b88334p-2, 0x1.5575b0be00b6ap-2, -0x1.ffffef20a4123p-2],
}

pub fn ln(self : Float) -> Float {
let mut ix : UInt = self.reinterpret_as_int().reinterpret_as_uint()
if ix == 0x3f800000U {
return 0.0
}
if ix - 0x00800000U >= 0x7f800000U - 0x00800000U {
if ix * 2 == 0 {
return neg_infinity
}
if ix == 0x7f800000U {
return self
}
if (ix & 0x80000000U) != 0 || ix * 2 >= 0xff000000U {
return not_a_number
}
ix = (self * 0x1.0p23).reinterpret_as_int().reinterpret_as_uint()
ix -= (23 << 23).reinterpret_as_uint()
}
let tmp = ix - logf_off
let i = ((tmp >> (23 - logf_table_bits)) % logf_n).reinterpret_as_int()
let k = tmp.reinterpret_as_int() >> 23
let iz = ix - (tmp & 0xff800000U)
let invc = logf_data.invc[i]
let logc = logf_data.logc[i]
let z = iz.reinterpret_as_int().reinterpret_as_float().to_double()
let r = z * invc - 1
let y0 = logc + k.to_double() * logf_data.ln2
let r2 = r * r
let y = logf_data.poly[1] * r + logf_data.poly[2]
let y = logf_data.poly[0] * r2 + y
let y = y * r2 + (y0 + r)
y.to_float()
}
23 changes: 23 additions & 0 deletions float/log_test.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2024 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

test "log" {
inspect!((1.0 : Float).ln(), content="0")
inspect!((0.0 : Float).ln(), content="-Infinity")
inspect!(@float.infinity.ln(), content="Infinity")
inspect!((2.0 : Float).ln(), content="0.6931471824645996")
inspect!((2.718 : Float).ln(), content="0.9998962879180908")
inspect!((1.0e37 : Float).ln(), content="85.19564819335938")
inspect!((-1.0 : Float).ln(), content="NaN")
}

0 comments on commit 86310f8

Please sign in to comment.