on master help sign in
Merge pull request #4147 from asandroq/fix-bignum-literals

Fix generation of bignum literals

commit852be39660c015f4b8781490cb1dff9519d8c62c authorAhmad Salim Al-Sibahi <asal@itu.dk> dateTue 24 Oct 2017, 15:34 (9 years ago) parent539186cac88f4f
3 files changed+20−4
src/IRTS/CodegenC.hs+4−2 test/bignum002/bignum002.idr+10−2 test/bignum002/expected+6
src/IRTS/CodegenC.hs+4−2
@@ -179,8 +179,10 @@ bcc i (ASSIGNCONST l c)
179179 = indent i ++ creg l ++ " = " ++ mkConst c ++ ";\n"
180180 where
181181 mkConst (I i) = "MKINT(" ++ show i ++ ")"
182− mkConst (BI i) | i > ((-2)^30) && i < (2^30) = "MKINT(" ++ show i ++ ")"
183− | otherwise = "MKBIGC(vm,\"" ++ show i ++ "\")"
182+ mkConst (BI i) = let maxInt = 2^30
183+ in if i >= -maxInt && i < maxInt
184+ then "MKINT(" ++ show i ++ ")"
185+ else "MKBIGC(vm,\"" ++ show i ++ "\")"
184186 mkConst (Fl f) = "MKFLOAT(vm, " ++ (map toUpper $ show f) ++ ")"
185187 mkConst (Ch c) = "MKINT(" ++ show (fromEnum c) ++ ")"
186188 mkConst (Str s) = "MKSTR(vm, " ++ showCStr s ++ ")"
test/bignum002/bignum002.idr+10−2
@@ -2,5 +2,13 @@ module Main
22
33main : IO ()
44main = do
5− putStrLn $ show $ divNat 1809022195644390369852458 91238741987
6− putStrLn $ show $ div (-432642342742368327462378462387) 36473264372
5+ printLn $ divNat 1809022195644390369852458 91238741987
6+ printLn $ div (-432642342742368327462378462387) 36473264372
7+
8+ -- values at the border between C ints and bignums
9+ printLn $ div 1073741822 73892
10+ printLn $ div 1073741823 73892
11+ printLn $ div 1073741824 73892
12+ printLn $ div (-1073741823) 73892
13+ printLn $ div (-1073741824) 73892
14+ printLn $ div (-1073741825) 73892
test/bignum002/expected+6
@@ -1,2 +1,8 @@
1119827346982734
22-11861903511836511836
3+14531
4+14531
5+14531
6+-14531
7+-14531
8+-14531