Debian Codegraph @

src/Rewriter/PerfTesting/StandaloneOCamlMain.v

raw
1 From Coq Require Import List.
2 Require Export Crypto.StandaloneOCamlMain.
3 Require Import Crypto.Rewriter.PerfTesting.Core.
4 Require Import Crypto.Util.Notations.
5 Import ListNotations. Local Open Scope list_scope.
6
7 (** We pull a hack to get coqchk to not report these as axioms; for
8 this, all we care about is that there exists a model. *)
9 Module Type OCamlPrimitivesT.
10 Axiom OCaml_float : Set.
11 Notation float := OCaml_float.
12 Axiom Unix_gettimeofday : unit -> float.
13 Axiom Sys_time : unit -> float.
14 Axiom fsub : float -> float -> float.
15 Axiom printf_float : float -> unit.
16 End OCamlPrimitivesT.
17
18 Module Export OCamlPrimitives : OCamlPrimitivesT.
19 Definition OCaml_float : Set := unit.
20 Notation float := OCaml_float.
21 Definition Unix_gettimeofday : unit -> float := fun 'tt => tt.
22 Definition Sys_time : unit -> float := fun 'tt => tt.
23 Definition fsub : float -> float -> float := fun _ _ => tt.
24 Definition printf_float : float -> unit := fun _ => tt.
25 End OCamlPrimitives.
26
27 (* We cannot inline these constants due to COQBUG(https://github.com/coq/coq/issues/16169) *)
28 Extract (*Inlined*) Constant float => "float".
29 Extract (*Inlined*) Constant Unix_gettimeofday => "Unix.gettimeofday".
30 Extract (*Inlined*) Constant Sys_time => "Sys.time".
31 Extract (*Inlined*) Constant fsub => "(-.)".
32 Extract Constant printf_float =>
33 "fun f -> Printf.printf ""%f%!"" f".
34
35 Local Notation "v <- x ; f" := (seq (fun _ => x) (fun v => f)).
36
37 Definition seq' : forall A B, (unit -> A) -> (unit -> B) -> unit
38 := fun A B f g => _ <- f tt; _ <- g tt; tt.
39
40 Definition time : forall A, String.string -> (unit -> A) -> unit
41 := fun _ descr f
42 => rstart <- Unix_gettimeofday tt;
43 ustart <- Sys_time tt;
44 _ <- f tt;
45 rend <- Unix_gettimeofday tt;
46 uend <- Sys_time tt;
47 _ <- printf_list_string [descr; ": real: "%string];
48 _ <- printf_float (fsub rend rstart);
49 _ <- printf_list_string [" s; user: "%string];
50 _ <- printf_float (fsub uend ustart);
51 _ <- printf_list_string [" s"%string; String.NewLine];
52 tt.
53
54 Definition error : list String.string -> unit
55 := fun msg => _ <- printf_list_string_with_newlines msg;
56 raise_Failure (string_of_Coq_string (String.concat String.NewLine msg)).
57
58 Module UnsaturatedSolinas.
59 Definition main : unit
60 := let argv := List.map string_to_Coq_string sys_argv in
61 match argv with
62 | [_; prime; bitwidth; index]
63 => UnsaturatedSolinas.ForExtraction
64 "OCaml" seq' time prime bitwidth index error
65 | _ => error ["Expected arguments: prime bitwidth index"]
66 end.
67 End UnsaturatedSolinas.
68
69 Module WordByWordMontgomery.
70 Definition main : unit
71 := let argv := List.map string_to_Coq_string sys_argv in
72 match argv with
73 | [_; prime; bitwidth]
74 => WordByWordMontgomery.ForExtraction
75 "OCaml" seq' time prime bitwidth error
76 | _ => error ["Expected arguments: prime bitwidth"]
77 end.
78 End WordByWordMontgomery.