Skip to content

Commit 9c0f244

Browse files
committed
fix: mod 0
1 parent 6430ae5 commit 9c0f244

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

src/Init/Data/SInt/Basic.lean

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def Int8.mul (a b : Int8) : Int8 := ⟨⟨a.toBitVec * b.toBitVec⟩⟩
8787
@[extern "lean_int8_div"]
8888
def Int8.div (a b : Int8) : Int8 := ⟨⟨BitVec.sdiv a.toBitVec b.toBitVec⟩⟩
8989
@[extern "lean_int8_mod"]
90-
def Int8.mod (a b : Int8) : Int8 := ⟨⟨BitVec.smod a.toBitVec b.toBitVec⟩⟩
90+
def Int8.mod (a b : Int8) : Int8 := ⟨⟨BitVec.srem a.toBitVec b.toBitVec⟩⟩
9191
@[extern "lean_int8_land"]
9292
def Int8.land (a b : Int8) : Int8 := ⟨⟨a.toBitVec &&& b.toBitVec⟩⟩
9393
@[extern "lean_int8_lor"]
@@ -184,7 +184,7 @@ def Int16.mul (a b : Int16) : Int16 := ⟨⟨a.toBitVec * b.toBitVec⟩⟩
184184
@[extern "lean_int16_div"]
185185
def Int16.div (a b : Int16) : Int16 := ⟨⟨BitVec.sdiv a.toBitVec b.toBitVec⟩⟩
186186
@[extern "lean_int16_mod"]
187-
def Int16.mod (a b : Int16) : Int16 := ⟨⟨BitVec.smod a.toBitVec b.toBitVec⟩⟩
187+
def Int16.mod (a b : Int16) : Int16 := ⟨⟨BitVec.srem a.toBitVec b.toBitVec⟩⟩
188188
@[extern "lean_int16_land"]
189189
def Int16.land (a b : Int16) : Int16 := ⟨⟨a.toBitVec &&& b.toBitVec⟩⟩
190190
@[extern "lean_int16_lor"]
@@ -285,7 +285,7 @@ def Int32.mul (a b : Int32) : Int32 := ⟨⟨a.toBitVec * b.toBitVec⟩⟩
285285
@[extern "lean_int32_div"]
286286
def Int32.div (a b : Int32) : Int32 := ⟨⟨BitVec.sdiv a.toBitVec b.toBitVec⟩⟩
287287
@[extern "lean_int32_mod"]
288-
def Int32.mod (a b : Int32) : Int32 := ⟨⟨BitVec.smod a.toBitVec b.toBitVec⟩⟩
288+
def Int32.mod (a b : Int32) : Int32 := ⟨⟨BitVec.srem a.toBitVec b.toBitVec⟩⟩
289289
@[extern "lean_int32_land"]
290290
def Int32.land (a b : Int32) : Int32 := ⟨⟨a.toBitVec &&& b.toBitVec⟩⟩
291291
@[extern "lean_int32_lor"]
@@ -390,7 +390,7 @@ def Int64.mul (a b : Int64) : Int64 := ⟨⟨a.toBitVec * b.toBitVec⟩⟩
390390
@[extern "lean_int64_div"]
391391
def Int64.div (a b : Int64) : Int64 := ⟨⟨BitVec.sdiv a.toBitVec b.toBitVec⟩⟩
392392
@[extern "lean_int64_mod"]
393-
def Int64.mod (a b : Int64) : Int64 := ⟨⟨BitVec.smod a.toBitVec b.toBitVec⟩⟩
393+
def Int64.mod (a b : Int64) : Int64 := ⟨⟨BitVec.srem a.toBitVec b.toBitVec⟩⟩
394394
@[extern "lean_int64_land"]
395395
def Int64.land (a b : Int64) : Int64 := ⟨⟨a.toBitVec &&& b.toBitVec⟩⟩
396396
@[extern "lean_int64_lor"]

src/include/lean/lean.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,7 +1928,7 @@ static inline uint8_t lean_int8_mod(uint8_t a1, uint8_t a2) {
19281928
int8_t lhs = (int8_t) a1;
19291929
int8_t rhs = (int8_t) a2;
19301930

1931-
return (uint8_t)(rhs == 0 ? 0 : lhs % rhs);
1931+
return (uint8_t)(rhs == 0 ? lhs : lhs % rhs);
19321932
}
19331933

19341934
static inline uint8_t lean_int8_land(uint8_t a1, uint8_t a2) {
@@ -2068,7 +2068,7 @@ static inline uint16_t lean_int16_mod(uint16_t a1, uint16_t a2) {
20682068
int16_t lhs = (int16_t) a1;
20692069
int16_t rhs = (int16_t) a2;
20702070

2071-
return (uint16_t)(rhs == 0 ? 0 : lhs % rhs);
2071+
return (uint16_t)(rhs == 0 ? lhs : lhs % rhs);
20722072
}
20732073

20742074
static inline uint16_t lean_int16_land(uint16_t a1, uint16_t a2) {
@@ -2207,7 +2207,7 @@ static inline uint32_t lean_int32_mod(uint32_t a1, uint32_t a2) {
22072207
int32_t lhs = (int32_t) a1;
22082208
int32_t rhs = (int32_t) a2;
22092209

2210-
return (uint32_t)(rhs == 0 ? 0 : lhs % rhs);
2210+
return (uint32_t)(rhs == 0 ? lhs : lhs % rhs);
22112211
}
22122212

22132213
static inline uint32_t lean_int32_land(uint32_t a1, uint32_t a2) {
@@ -2346,7 +2346,7 @@ static inline uint64_t lean_int64_mod(uint64_t a1, uint64_t a2) {
23462346
int64_t lhs = (int64_t) a1;
23472347
int64_t rhs = (int64_t) a2;
23482348

2349-
return (uint64_t)(rhs == 0 ? 0 : lhs % rhs);
2349+
return (uint64_t)(rhs == 0 ? lhs : lhs % rhs);
23502350
}
23512351

23522352
static inline uint64_t lean_int64_land(uint64_t a1, uint64_t a2) {

tests/lean/sint_basic.lean

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
#eval (10 : Int8) / -2 = -5
3535
#eval (10 : Int8) / 0 = 0
3636
#eval (10 : Int8) % 1 = 0
37-
#eval (10 : Int8) % 0 = 0
37+
#eval (10 : Int8) % 0 = 10
38+
#eval (-10 : Int8) % 0 = -10
3839
#eval (10 : Int8) % 3 = 1
3940
#eval (-10 : Int8) % 3 = -1
4041
#eval (-10 : Int8) % -3 = -1
@@ -139,7 +140,8 @@ def myId8 (x : Int8) : Int8 := x
139140
#eval (10 : Int16) / -2 = -5
140141
#eval (10 : Int16) / 0 = 0
141142
#eval (10 : Int16) % 1 = 0
142-
#eval (10 : Int16) % 0 = 0
143+
#eval (10 : Int16) % 0 = 10
144+
#eval (-10 : Int16) % 0 = -10
143145
#eval (10 : Int16) % 3 = 1
144146
#eval (-10 : Int16) % 3 = -1
145147
#eval (-10 : Int16) % -3 = -1
@@ -220,7 +222,8 @@ def myId16 (x : Int16) : Int16 := x
220222
#eval (10 : Int32) / -2 = -5
221223
#eval (10 : Int32) / 0 = 0
222224
#eval (10 : Int32) % 1 = 0
223-
#eval (10 : Int32) % 0 = 0
225+
#eval (10 : Int32) % 0 = 10
226+
#eval (-10 : Int32) % 0 = -10
224227
#eval (10 : Int32) % 3 = 1
225228
#eval (-10 : Int32) % 3 = -1
226229
#eval (-10 : Int32) % -3 = -1
@@ -300,7 +303,8 @@ def myId32 (x : Int32) : Int32 := x
300303
#eval (10 : Int64) / -2 = -5
301304
#eval (10 : Int64) / 0 = 0
302305
#eval (10 : Int64) % 1 = 0
303-
#eval (10 : Int64) % 0 = 0
306+
#eval (10 : Int64) % 0 = 10
307+
#eval (-10 : Int64) % 0 = -10
304308
#eval (10 : Int64) % 3 = 1
305309
#eval (-10 : Int64) % 3 = -1
306310
#eval (-10 : Int64) % -3 = -1

tests/lean/sint_basic.lean.expected.out

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ true
7171
true
7272
true
7373
true
74+
true
7475

7576
[result]
7677
def myId8 (x_1 : u8) : u8 :=
@@ -154,6 +155,7 @@ true
154155
true
155156
true
156157
true
158+
true
157159

158160
[result]
159161
def myId16 (x_1 : u16) : u16 :=
@@ -237,6 +239,7 @@ true
237239
true
238240
true
239241
true
242+
true
240243

241244
[result]
242245
def myId32 (x_1 : u32) : u32 :=
@@ -320,6 +323,7 @@ true
320323
true
321324
true
322325
true
326+
true
323327

324328
[result]
325329
def myId64 (x_1 : u64) : u64 :=

0 commit comments

Comments
 (0)