Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[numpy] add trunc #2316

Merged
merged 9 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ RUN(NAME elemental_09 LABELS cpython llvm c NOFAST)
RUN(NAME elemental_10 LABELS cpython llvm c NOFAST)
RUN(NAME elemental_11 LABELS cpython llvm c NOFAST)
RUN(NAME elemental_12 LABELS cpython llvm c NOFAST)
RUN(NAME elemental_13 LABELS cpython llvm c NOFAST)
RUN(NAME test_random LABELS cpython llvm NOFAST)
RUN(NAME test_os LABELS cpython llvm c NOFAST)
RUN(NAME test_builtin LABELS cpython llvm c)
Expand Down
64 changes: 64 additions & 0 deletions integration_tests/elemental_13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from lpython import f32, f64
from numpy import trunc, empty, sqrt, reshape, int32, float32, float64


def elemental_trunc64():
i: i32
j: i32
k: i32
l: i32
eps: f32
eps = f32(1e-6)

arraynd: f64[32, 16, 8, 4] = empty((32, 16, 8, 4), dtype=float64)

newshape: i32[1] = empty(1, dtype = int32)
newshape[0] = 16384

for i in range(32):
for j in range(16):
for k in range(8):
for l in range(4):
arraynd[i, j, k, l] = f64((-1)**l) * sqrt(float(i + j + j + l))

observed: f64[32, 16, 8, 4] = empty((32, 16, 8, 4), dtype=float64)
observed = trunc(arraynd)

observed1d: f64[16384] = empty(16384, dtype=float64)
observed1d = reshape(observed, newshape)

array: f64[16384] = empty(16384, dtype=float64)
array = reshape(arraynd, newshape)

for i in range(16384):
assert f32(abs(trunc(array[i]) - observed1d[i])) <= eps


def elemental_trunc32():
i: i32
j: i32
k: i32
l: i32
eps: f32
eps = f32(1e-6)

arraynd: f32[32, 16, 8, 4] = empty((32, 16, 8, 4), dtype=float32)

for i in range(32):
for j in range(16):
for k in range(8):
for l in range(4):
arraynd[i, j, k, l] = f32(f64((-1)**l) * sqrt(float(i + j + j + l)))

observed: f32[32, 16, 8, 4] = empty((32, 16, 8, 4), dtype=float32)
observed = trunc(arraynd)

for i in range(32):
for j in range(16):
for k in range(8):
for l in range(4):
assert abs(trunc(arraynd[i, j, k, l]) - observed[i, j, k, l]) <= eps


elemental_trunc64()
elemental_trunc32()
12 changes: 12 additions & 0 deletions src/libasr/runtime/lfortran_intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,18 @@ LFORTRAN_API double_complex_t _lfortran_zatanh(double_complex_t x)
return catanh(x);
}

// trunc -----------------------------------------------------------------------

LFORTRAN_API float _lfortran_strunc(float x)
{
return truncf(x);
}

LFORTRAN_API double _lfortran_dtrunc(double x)
{
return trunc(x);
}

// phase --------------------------------------------------------------------

LFORTRAN_API float _lfortran_cphase(float_complex_t x)
Expand Down
2 changes: 2 additions & 0 deletions src/libasr/runtime/lfortran_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ LFORTRAN_API float _lfortran_satanh(float x);
LFORTRAN_API double _lfortran_datanh(double x);
LFORTRAN_API float_complex_t _lfortran_catanh(float_complex_t x);
LFORTRAN_API double_complex_t _lfortran_zatanh(double_complex_t x);
LFORTRAN_API float _lfortran_strunc(float x);
LFORTRAN_API double _lfortran_dtrunc(double x);
LFORTRAN_API float _lfortran_cphase(float_complex_t x);
LFORTRAN_API double _lfortran_zphase(double_complex_t x);
LFORTRAN_API bool _lpython_str_compare_eq(char** s1, char** s2);
Expand Down
20 changes: 20 additions & 0 deletions src/runtime/lpython_intrinsic_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,23 @@ def ceil(x: f32) -> f32:
if x <= f32(0) or x == resultf:
return resultf
return resultf + f32(1)

########## trunc ##########

@ccall
def _lfortran_dtrunc(x: f64) -> f64:
pass

@overload
@vectorize
def trunc(x: f64) -> f64:
return _lfortran_dtrunc(x)

@ccall
def _lfortran_strunc(x: f32) -> f32:
pass

@overload
@vectorize
def trunc(x: f32) -> f32:
return _lfortran_strunc(x)
certik marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion tests/reference/asr-array_01_decl-39cf894.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-array_01_decl-39cf894.stdout",
"stdout_hash": "137a0c427925ba7da2e7151f2cf52bfa9a64fede11fe8d2653f20b64",
"stdout_hash": "2aa47467473392c970bb1ddde961e3007d4c157bb0ea507b5e0db4a4",
"stderr": null,
khushi-411 marked this conversation as resolved.
Show resolved Hide resolved
"stderr_hash": null,
"returncode": 0
Expand Down
11 changes: 11 additions & 0 deletions tests/reference/asr-array_01_decl-39cf894.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
warning: The module 'numpy' located in $DIR/src/bin/../runtime/lpython_intrinsic_numpy.py cannot be loaded
--> tests/../integration_tests/array_01_decl.py:2:1
|
2 | from numpy import empty, int16, int32, int64, float32, float64, complex64, complex128
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ imported here

semantic error: Decorator: vertorize is not supported
--> $DIR/src/bin/../runtime/lpython_intrinsic_numpy.py:421:2
|
421 | @vertorize
| ^^^^^^^^^
khushi-411 marked this conversation as resolved.
Show resolved Hide resolved
Loading