Source

compiler-libs-hack / ocaml / testsuite / tests / asmcomp / tagged-quicksort.cmm

(***********************************************************************)
(*                                                                     *)
(*                                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 Q Public License version 1.0.               *)
(*                                                                     *)
(***********************************************************************)

(function "quick" (lo: int hi: int a: addr)
  (if (< lo hi)
      (let (i lo
            j hi
            pivot (addraref a (>>s hi 1)))
        (while (< i j)
          (catch
              (while 1
                (if (>= i hi) exit [])
                (if (> (addraref a (>>s i 1)) pivot) exit [])
                (assign i (+ i 2)))
           with [])
          (catch
              (while 1
                (if (<= j lo) exit [])
                (if (< (addraref a (>>s j 1)) pivot) exit [])
                (assign j (- j 2)))
           with [])
          (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: addr)
   (app "quick" (+ (<< lo 1) 1) (+ (<< hi 1) 1) a unit))