Skip to content

Commit cbdf2e5

Browse files
certikubaidsk
andauthored
Merge pull request #1547 from faze-geek/sum
* Add builtin sum for runtime lists * TEST: Enable test for builtin sum * TEST: Update reference tests --------- Co-authored-by: Shaikh Ubaid <[email protected]>
2 parents ba525e6 + 036e486 commit cbdf2e5

File tree

75 files changed

+158
-74
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+158
-74
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ RUN(NAME test_builtin_float LABELS cpython llvm c)
323323
RUN(NAME test_builtin_str_02 LABELS cpython llvm c)
324324
RUN(NAME test_builtin_round LABELS cpython llvm c)
325325
RUN(NAME test_builtin_divmod LABELS cpython llvm c)
326+
RUN(NAME test_builtin_sum LABELS cpython llvm c)
326327
RUN(NAME test_math1 LABELS cpython llvm c)
327328
RUN(NAME test_math_02 LABELS cpython llvm)
328329
RUN(NAME test_pass_compare LABELS cpython llvm c)

integration_tests/test_builtin_sum.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from ltypes import f32, f64, i32, i64
2+
3+
def test_sum():
4+
arr_i32 :list[i32]
5+
arr_i32 = [6, 12]
6+
res_i32 :i32 = sum(arr_i32)
7+
assert res_i32 == 18
8+
9+
arr_i64 :list[i64] = [i64(600000000000), i64(1200000000000)]
10+
res_i64 :i64 = sum(arr_i64)
11+
assert res_i64 == i64(1800000000000)
12+
13+
eps1 :f32 = f32(1e-12)
14+
x :f32 = f32(12.5)
15+
y :f32 = f32(6.5)
16+
arr_f32 :list[f32]
17+
arr_f32 = [x, y]
18+
res_f32 :f32 = sum(arr_f32)
19+
assert abs(res_f32 - f32(19.0)) < eps1
20+
21+
eps2: f64 = 1e-12
22+
arr_f64: list[f64]
23+
arr_f64 = [12.6, 6.4]
24+
res_f64 :f64 = sum(arr_f64)
25+
assert abs(res_f64 - 19.0) < eps2
26+
27+
28+
test_sum()

src/lpython/semantics/python_comptime_eval.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ struct PythonIntrinsicProcedures {
6767
{"_mod", {m_builtin, &eval__mod}},
6868
{"max" , {m_builtin , &eval_max}},
6969
{"min" , {m_builtin , &eval_min}},
70+
{"sum" , {m_builtin , &not_implemented}},
7071
// The following functions for string methods are not used
7172
// for evaluation.
7273
{"_lpython_str_capitalize", {m_builtin, &not_implemented}},

src/runtime/lpython_builtin.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,60 @@ def pow(x: bool, y: bool) -> i32:
161161
def pow(c: c32, y: i32) -> c32:
162162
return c**c32(y)
163163

164+
# sum
165+
# supported data types: i32, i64, f32, f64
166+
167+
@overload
168+
def sum(arr: list[i32]) -> i32:
169+
"""
170+
Sum of the elements of `arr`.
171+
"""
172+
sum: i32
173+
sum = 0
174+
175+
i: i32
176+
for i in range(len(arr)):
177+
sum += arr[i]
178+
return sum
179+
180+
@overload
181+
def sum(arr: list[i64]) -> i64:
182+
"""
183+
Sum of the elements of `arr`.
184+
"""
185+
sum: i64
186+
sum = i64(0)
187+
188+
i: i32
189+
for i in range(len(arr)):
190+
sum += arr[i]
191+
return sum
192+
193+
@overload
194+
def sum(arr: list[f32]) -> f32:
195+
"""
196+
Sum of the elements of `arr`.
197+
"""
198+
sum: f32
199+
sum = f32(0.0)
200+
201+
i: i32
202+
for i in range(len(arr)):
203+
sum += arr[i]
204+
return sum
205+
206+
@overload
207+
def sum(arr: list[f64]) -> f64:
208+
"""
209+
Sum of the elements of `arr`.
210+
"""
211+
sum: f64
212+
sum = 0.0
213+
214+
i: i32
215+
for i in range(len(arr)):
216+
sum += arr[i]
217+
return sum
164218

165219
def bin(n: i32) -> str:
166220
"""

src/runtime/math.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def ceil(x: f32) -> i32:
102102
@overload
103103
def fsum(arr: list[i32]) -> f64:
104104
"""
105-
Sum of the elements of `arr`.
105+
Floating-point sum of the elements of `arr`.
106106
"""
107107
sum: f64
108108
sum = 0.0
@@ -115,7 +115,7 @@ def fsum(arr: list[i32]) -> f64:
115115
@overload
116116
def fsum(arr: list[i64]) -> f64:
117117
"""
118-
Sum of the elements of `arr`.
118+
Floating-point sum of the elements of `arr`.
119119
"""
120120
sum: f64
121121
sum = 0.0
@@ -128,7 +128,7 @@ def fsum(arr: list[i64]) -> f64:
128128
@overload
129129
def fsum(arr: list[f32]) -> f64:
130130
"""
131-
Sum of the elements of `arr`.
131+
Floating-point sum of the elements of `arr`.
132132
"""
133133
sum: f64
134134
sum = 0.0
@@ -141,7 +141,7 @@ def fsum(arr: list[f32]) -> f64:
141141
@overload
142142
def fsum(arr: list[f64]) -> f64:
143143
"""
144-
Sum of the elements of `arr`.
144+
Floating-point sum of the elements of `arr`.
145145
"""
146146
sum: f64
147147
sum = 0.0

tests/reference/asr-array_01_decl-39cf894.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-array_01_decl-39cf894.stdout",
9-
"stdout_hash": "c2477224b2c01309d0c62db9e4d2168abc779fd2fb0c4874a4232f14",
9+
"stdout_hash": "4409e567b0abf42b56cf4c70a66bf2e24bbeb61bc52338b26cb4840c",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-array_01_decl-39cf894.stdout

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/reference/asr-array_02_decl-e8f6874.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-array_02_decl-e8f6874.stdout",
9-
"stdout_hash": "768f04d435a32db40ba224093967cf0acaca792a73c732728e66eee8",
9+
"stdout_hash": "b2be9582bdf29f9c7dd1d480998ef8c7b5dbefba08d4f430e2316a7d",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-array_02_decl-e8f6874.stdout

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/reference/asr-bindc_02-bc1a7ea.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-bindc_02-bc1a7ea.stdout",
9-
"stdout_hash": "143c305a40ef34791bab7054403d254c11bd7f357a55e774177f5e1d",
9+
"stdout_hash": "ba2fbb9012c13e01cc2b596c7f706c8078bd1b285a11c861350cc358",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)