Skip to content

Commit ccb6605

Browse files
committed
.
1 parent ac6de5f commit ccb6605

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

vecxt/src-jvm/arrays.scala

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,40 @@ object arrays:
337337
end while
338338
end +=
339339

340+
inline def minSIMD =
341+
var i = 0
342+
var acc = IntVector.broadcast(spi, Int.MaxValue)
343+
344+
while i < spi.loopBound(vec.length) do
345+
acc = acc.min(IntVector.fromArray(spi, vec, i))
346+
i += spil
347+
end while
348+
349+
var temp = acc.reduceLanes(VectorOperators.MIN)
350+
351+
while i < vec.length do
352+
temp = Math.min(temp, vec(i))
353+
i += 1
354+
end while
355+
temp
356+
357+
inline def maxSIMD =
358+
var i = 0
359+
var acc = IntVector.broadcast(spi, Int.MinValue)
360+
361+
while i < spi.loopBound(vec.length) do
362+
acc = acc.max(IntVector.fromArray(spi, vec, i))
363+
i += spil
364+
end while
365+
366+
var temp = acc.reduceLanes(VectorOperators.MAX)
367+
368+
while i < vec.length do
369+
temp = Math.max(temp, vec(i))
370+
i += 1
371+
end while
372+
temp
373+
340374
end extension
341375

342376
extension [@specialized(Double, Int) A](vec: Array[A])(using ClassTag[A])
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2023 quafadas
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package vecxt
18+
19+
import all.*
20+
import narr.*
21+
22+
class MinMaxSIMDSuite extends munit.FunSuite:
23+
24+
test("maxSIMD finds maximum in small array"):
25+
val arr = NArray[Double](1.0, 5.0, 3.0, 9.0, 2.0)
26+
val result = arr.maxSIMD
27+
assertEquals(result, 9.0)
28+
29+
test("maxSIMD finds maximum in large array with SIMD lanes"):
30+
// Create array larger than SIMD vector length to test SIMD path
31+
val arr = NArray.tabulate[Double](100)(i => (i * 2.5) % 37.0)
32+
arr(75) = 1000.0 // Insert known maximum
33+
val result = arr.maxSIMD
34+
assertEquals(result, 1000.0)
35+
36+
test("minSIMD finds minimum in array with positive integers"):
37+
val arr = Array(5, 3, 9, 1, 7, 2, 8, 4, 6)
38+
val result = arr.minSIMD
39+
assertEquals(result, 1)
40+
41+
test("minSIMD finds minimum in large array crossing SIMD boundaries"):
42+
// Create array larger than SIMD vector length to test both SIMD and scalar paths
43+
val arr = Array.tabulate(100)(i => if i == 73 then -5 else i * 2 + 10)
44+
val result = arr.minSIMD
45+
assertEquals(result, -5)

0 commit comments

Comments
 (0)