Skip to content

Commit e432e7a

Browse files
authored
Merge pull request #1761 from Shaikh-Ubaid/fix_types
Make i32/i64 convert float to int in CPython
2 parents d2bf0f1 + df30c8b commit e432e7a

File tree

13 files changed

+77
-59
lines changed

13 files changed

+77
-59
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ RUN(NAME if_01 LABELS cpython llvm c wasm wasm_x86 wasm_x64)
335335
RUN(NAME if_02 LABELS cpython llvm c wasm wasm_x86 wasm_x64)
336336
RUN(NAME print_02 LABELS cpython llvm c)
337337
RUN(NAME test_types_01 LABELS cpython llvm c)
338+
RUN(NAME test_types_02 LABELS cpython llvm c wasm)
338339
RUN(NAME test_str_01 LABELS cpython llvm c)
339340
RUN(NAME test_str_02 LABELS cpython llvm c)
340341
RUN(NAME test_str_03 LABELS cpython llvm c)

integration_tests/elemental_03.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def elemental_sqrt64():
2020
shape[0] = 4096
2121
observed = reshape(sqrt(array), shape)
2222
for l in range(4096):
23-
i = i32(int(l/256))
23+
i = i32(l/256)
2424
j = (l - i*256)//16
2525
k = (l - i*256 - j*16)
2626
assert abs(observed[l]**2.0 - f64(i + j + k)) <= eps
@@ -42,7 +42,7 @@ def elemental_sqrt32():
4242
shape[0] = 256
4343
observed = reshape(sqrt(array), shape)
4444
for l in range(256):
45-
i = i32(int(l/16))
45+
i = i32(l/16)
4646
j = (l - i*16)
4747
assert abs(observed[l]**f32(2.0) - f32(i + j)) <= eps
4848

integration_tests/elemental_04.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def verify(observed: f32[:], base: i32, eps: f32):
2323
j: i32
2424

2525
for k in range(100):
26-
i = i32(int(k/10))
26+
i = i32(k/10)
2727
j = (k - i*10)
2828
assert abs(f32(base)**(observed[k]) - f32(i + j + 1)) <= eps
2929

integration_tests/global_syms_04.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ def test_global_symbols():
1919
def update_global_symbols():
2020
global b, c, d
2121
x: f64 = f64(c) * d
22-
b = i32(int(x))
22+
b = i32(x)
2323
y: f64 = f64(b) / 12.0
24-
c = i64(int(y))
24+
c = i64(y)
2525
z: i64 = i64(b) * c
2626
d = f64(z)
2727

integration_tests/lpdraw/draw.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def Line(H: i32, W: i32, Screen: i32[H, W], x1: i32, y1: i32, x2: i32, y2: i32)
8686
y1 += sy
8787

8888
def Circle(H: i32, W: i32, Screen: i32[H, W], x: i32, y: i32, r: f64) -> None:
89-
x0: i32 = i32(int(r))
89+
x0: i32 = i32(r)
9090
y0: i32 = 0
9191
err: i32 = 0
9292

integration_tests/test_numpy_03.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_nd_to_1d(a: f64[:, :]):
3030
newshape1[0] = 4096
3131
d = reshape(c, newshape1)
3232
for l in range(4096):
33-
i = i32(int(l/256))
33+
i = i32(l/256)
3434
j = (l - i*256)//16
3535
k = (l - i*256 - j*16)
3636
assert abs(d[l] - f64(i + j + k) - 0.5) <= eps
@@ -87,7 +87,7 @@ def test_reshape_with_argument():
8787

8888
d: f64[4096] = empty(4096)
8989
for l in range(4096):
90-
i = i32(int(l/256))
90+
i = i32(l/256)
9191
j = (l - i*256)//16
9292
k = (l - i*256 - j*16)
9393
d[l] = f64(i + j + k) + 0.5

integration_tests/test_pkg_lnn_01.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def plot_graph(p: Perceptron, input_vectors: list[list[f64]], outputs: list[i32]
3131
y2 *= scale_offset
3232

3333
# print (x1, y1, x2, y2)
34-
Line(Height, Width, Screen, i32(int(x1 + shift_offset)), i32(int(y1 + shift_offset)), i32(int(x2 + shift_offset)), i32(int(y2 + shift_offset)))
34+
Line(Height, Width, Screen, i32(x1 + shift_offset), i32(y1 + shift_offset), i32(x2 + shift_offset), i32(y2 + shift_offset))
3535

3636
i: i32
3737
point_size: i32 = 5
@@ -41,12 +41,12 @@ def plot_graph(p: Perceptron, input_vectors: list[list[f64]], outputs: list[i32]
4141
input_vectors[i][0] += shift_offset
4242
input_vectors[i][1] += shift_offset
4343
if outputs[i] == 1:
44-
x: i32 = i32(int(input_vectors[i][0]))
45-
y: i32 = i32(int(input_vectors[i][1]))
44+
x: i32 = i32(input_vectors[i][0])
45+
y: i32 = i32(input_vectors[i][1])
4646
Line(Height, Width, Screen, x - point_size, y, x + point_size, y)
4747
Line(Height, Width, Screen, x, y - point_size, x, y + point_size)
4848
else:
49-
Circle(Height, Width, Screen, i32(int(input_vectors[i][0])), i32(int(input_vectors[i][1])), f64(point_size))
49+
Circle(Height, Width, Screen, i32(input_vectors[i][0]), i32(input_vectors[i][1]), f64(point_size))
5050

5151
Display(Height, Width, Screen)
5252

integration_tests/test_pkg_lnn_02.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def plot_graph(p: Perceptron, input_vectors: list[list[f64]], outputs: list[f64]
3131
y2 *= scale_offset
3232

3333
# print (x1, y1, x2, y2)
34-
Line(Height, Width, Screen, i32(int(x1 + shift_offset)), i32(int(y1 + shift_offset)), i32(int(x2 + shift_offset)), i32(int(y2 + shift_offset)))
34+
Line(Height, Width, Screen, i32(x1 + shift_offset), i32(y1 + shift_offset), i32(x2 + shift_offset), i32(y2 + shift_offset))
3535

3636
i: i32
3737
point_size: i32 = 5
@@ -41,7 +41,7 @@ def plot_graph(p: Perceptron, input_vectors: list[list[f64]], outputs: list[f64]
4141
outputs[i] *= scale_offset
4242
outputs[i] += shift_offset
4343

44-
Circle(Height, Width, Screen, i32(int(input_vectors[i][0])), i32(int(outputs[i])), f64(point_size))
44+
Circle(Height, Width, Screen, i32(input_vectors[i][0]), i32(outputs[i]), f64(point_size))
4545

4646
Display(Height, Width, Screen)
4747

integration_tests/test_types_02.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from lpython import i32, f64
2+
3+
def main0():
4+
a: f64
5+
a = 3.25
6+
b: i32 = i32(a) * 4
7+
print(b)
8+
assert b == 12
9+
10+
main0()

src/libasr/codegen/wasm_to_x64.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ class X64Visitor : public WASMDecoder<X64Visitor>,
342342
void visit_I32Ne() { visit_I64Ne(); }
343343

344344
void visit_I32WrapI64() { } // empty, since i32's and i64's are considered similar currently.
345+
void visit_I32TruncF64S() { visit_I64TruncF64S(); }
345346

346347
void visit_I64Const(int64_t value) {
347348
m_a.asm_mov_r64_imm64(X64Reg::rax, labs((int64_t)value));

0 commit comments

Comments
 (0)