Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0006749OCamlmiddle end (typedtree to clambda)public2015-01-13 08:082017-03-03 18:03
Reporterysk 
Assigned Toshinwell 
PrioritynormalSeverityminorReproducibilityalways
StatusclosedResolutionfixed 
PlatformDarwinOSOS XOS Version14.0
Product Version4.02.1 
Target VersionFixed in Version4.02.2+dev / +rc1 
Summary0006749: ocamlopt returns n for (n mod 1) instead of 0
DescriptionThe code is "let () = for i = 1 to 3 do Printf.printf "%d\n" (i mod 1) done".

The results of the code with ocamlopt are:
  1
  2
  3

but it should be:
  0
  0
  0
Additional InformationI think that mod_int in asmcomp/cmmgen.ml may have a bug.
Tagspatch
Attached Filespatch file icon 6749.patch [^] (3,244 bytes) 2015-01-14 12:53 [Show Content]

- Relationships
related to 0006042closed Integer division by constants 
related to 0006879closedshinwell wrong optimization of 1 mod n 

-  Notes
(0013068)
gasche (administrator)
2015-01-13 10:42

Indeed this is a bug in cmmgen.ml:mod_int which believes that (c1 mod 1 = c1) instead of (c1 mod 1 = 0). This was introduced by commit trunk@14303 between 4.01 and 4.02 (3bb161216), and it seems to be a copy-pasting mistake from div_int's definition.

The whole function should be reviewed (it also seems to believe that (1 mod n = 1)), and it would also be important to understand why the testsuite that was added for PR#6042 did not catch this bug.
(0013071)
pcouderc (reporter)
2015-01-13 10:49

"List.iter (fun x -> Printf.printf "%d\n" (x mod 1)) [1; 2; 3]"

produces the same result, however unrolling it, i.e.

"Printf.printf "%d\n" (1 mod 1);
Printf.printf "%d\n" (2 mod 1);
Printf.printf "%d\n" (3 mod 1)"

returns a correct result.
(0013072)
gasche (administrator)
2015-01-13 10:52
edited on: 2015-01-13 10:52

Indeed (nice catch!), this comes from the fact that (constant mod constant) operations are fully-evaluated earlier in the compilation pipeline, in closure.ml.

(0013094)
shinwell (developer)
2015-01-14 11:15

Jeremie and I are looking at this
(0013095)
shinwell (developer)
2015-01-14 12:55
edited on: 2015-01-14 12:56

Proposed patch attached; comments welcome.

1. The case for x mod 1 with x non-constant was wrong.
2. We have added cases for x mod y when y is constant negative 1.
3. Gabriel's first comment, second paragraph doesn't seem correct: 1 mod n does equal 1 so long as n is not 0 or 1, and those cases have already been ruled out at the corresponding point in the code.

We couldn't see anything else wrong.

(0013251)
gasche (administrator)
2015-02-08 11:00

Merged in trunk and 4.02, thanks!

- Issue History
Date Modified Username Field Change
2015-01-13 08:08 ysk New Issue
2015-01-13 10:42 gasche Note Added: 0013068
2015-01-13 10:42 gasche Relationship added related to 0006042
2015-01-13 10:49 pcouderc Note Added: 0013071
2015-01-13 10:52 gasche Note Added: 0013072
2015-01-13 10:52 gasche Note Edited: 0013072 View Revisions
2015-01-14 10:56 shinwell Summary ocamlopt returns n of (n mod 1) instead of 1 => ocamlopt returns n for (n mod 1) instead of 0
2015-01-14 11:15 shinwell Note Added: 0013094
2015-01-14 11:15 shinwell Assigned To => shinwell
2015-01-14 11:15 shinwell Status new => assigned
2015-01-14 12:53 shinwell File Added: 6749.patch
2015-01-14 12:55 shinwell Note Added: 0013095
2015-01-14 12:56 shinwell Note Edited: 0013095 View Revisions
2015-01-20 00:16 doligez Tag Attached: patch
2015-01-20 00:21 doligez Note Added: 0013150
2015-01-20 00:22 doligez Note Deleted: 0013150
2015-02-08 11:00 gasche Note Added: 0013251
2015-02-08 11:00 gasche Status assigned => resolved
2015-02-08 11:00 gasche Fixed in Version => 4.02.2+dev / +rc1
2015-02-08 11:00 gasche Resolution open => fixed
2015-05-27 06:16 gasche Relationship added has duplicate 0006879
2015-05-27 10:09 gasche Relationship deleted has duplicate 0006879
2015-05-27 10:09 gasche Relationship added related to 0006879
2017-02-16 15:14 xleroy Status resolved => closed
2017-02-23 16:42 doligez Category Ocaml optimization => -Ocaml optimization
2017-03-03 18:03 doligez Category -Ocaml optimization => middle end (typedtree to clambda)


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker