summaryrefslogtreecommitdiff
path: root/testsuite/tests/asmgen/tagged-quicksort.cmm
blob: 401856b9636fa0972f3ac9456c33390a8f06a38f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
(* TEST
 readonly_files = "main.c";
 arguments = "-DSORT -DFUN=quicksort main.c";
 asmgen;
*)

(**************************************************************************)
(*                                                                        *)
(*                                OCaml                                   *)
(*                                                                        *)
(*             Xavier Leroy, projet Cristal, INRIA Rocquencourt           *)
(*                                                                        *)
(*   Copyright 1996 Institut National de Recherche en Informatique et     *)
(*     en Automatique.                                                    *)
(*                                                                        *)
(*   All rights reserved.  This file is distributed under the terms of    *)
(*   the GNU Lesser General Public License version 2.1, with the          *)
(*   special exception on linking described in the file LICENSE.          *)
(*                                                                        *)
(**************************************************************************)

(function "quick" (lo: int hi: int a: val)
  (if (< lo hi)
      (letmut (i int lo
               j int hi
               pivot int (addraref a (>>s hi 1)))
        (while (< i j)
          (catch
              (while 1
                (if (>= i hi) (exit n25) [])
                (if (> (addraref a (>>s i 1)) pivot) (exit n25) [])
                (assign i (+ i 2)))
           with (n25) [])
          (catch
              (while 1
                (if (<= j lo) (exit n35) [])
                (if (< (addraref a (>>s j 1)) pivot) (exit n35) [])
                (assign j (- j 2)))
           with (n35) [])
          (if (< i j)
              (let temp (addraref a (>>s i 1))
                   (addraset a (>>s i 1) (addraref a (>>s j 1)))
                   (addraset a (>>s j 1) temp))
            []))
        (let temp (addraref a (>>s i 1))
             (addraset a (>>s i 1) (addraref a (>>s hi 1)))
             (addraset a (>>s hi 1) temp))
        (app "quick" lo (- i 2) a unit)
        (app "quick" (+ i 2) hi a unit))
    []))

(function "quicksort" (lo: int hi: int a: val)
   (app "quick" (+ (<< lo 1) 1) (+ (<< hi 1) 1) a unit))