aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDelphine Demange <delphine.demange@irisa.fr>2020-11-09 15:46:11 +0100
committerDelphine Demange <delphine.demange@irisa.fr>2020-11-09 15:46:11 +0100
commitd40e8c3684d1864aee11988b957171321ebbd7fa (patch)
tree125735e630335cc27e5eb57a60bd3ac8ea96c63f
parentb98274e122d95a3643a0ef119a1e17e300d45c93 (diff)
parent00a17c975f68a4dd30e6a322bcd7511a7da7b801 (diff)
downloadcompcert-kvx-d40e8c3684d1864aee11988b957171321ebbd7fa.tar.gz
compcert-kvx-d40e8c3684d1864aee11988b957171321ebbd7fa.zip
Merge branch 'issue1' into 'ssa'
Generalize RTL normalization checker This MR generalizes the RTL normalization checker. The entry point of the initial function is not necessarily reached after just a single goto. On CFG where the entry point is a junction point, there will be a tunnel of 2 gotos from the entry point of the normalized function and the entry point of the initial function. Closes #1 See merge request compcertssa/compcertssa!1
-rw-r--r--.gitignore6
-rw-r--r--midend/CSSAgen.v4
-rw-r--r--midend/RTLnorm.v23
-rw-r--r--midend/RTLnormspec.v80
-rw-r--r--midend/SSAvalid.v14
-rw-r--r--test/Makefile8
-rw-r--r--test/extra/Makefile60
-rw-r--r--test/extra/self-loop.c4
-rw-r--r--test/picosat-965/LICENSE20
-rw-r--r--test/picosat-965/Makefile.local62
-rw-r--r--test/picosat-965/NEWS162
-rw-r--r--test/picosat-965/README5
-rw-r--r--test/picosat-965/VERSION1
-rw-r--r--test/picosat-965/app.c1192
-rwxr-xr-xtest/picosat-965/configure.sh150
-rw-r--r--test/picosat-965/main.c7
-rw-r--r--test/picosat-965/makefile.in59
-rwxr-xr-xtest/picosat-965/mkconfig.sh35
-rw-r--r--test/picosat-965/picogcnf.c165
-rw-r--r--test/picosat-965/picomcs.c334
-rw-r--r--test/picosat-965/picomus.c407
-rw-r--r--test/picosat-965/picosat.c8502
-rw-r--r--test/picosat-965/picosat.h658
-rw-r--r--test/picosat-965/version.c14
24 files changed, 11929 insertions, 43 deletions
diff --git a/.gitignore b/.gitignore
index e437c762..719507e5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -74,6 +74,12 @@
.DS_Store
# Test generated data
/test/clightgen/*.v
+/test/picosat-965/picogcnf
+/test/picosat-965/picomcs
+/test/picosat-965/picomus
+/test/picosat-965/picosat
+/test/picosat-965/config.h
+/test/picosat-965/makefile
# Coq caches
.lia.cache
.nia.cache
diff --git a/midend/CSSAgen.v b/midend/CSSAgen.v
index 968195c6..1e67c810 100644
--- a/midend/CSSAgen.v
+++ b/midend/CSSAgen.v
@@ -149,7 +149,7 @@ Qed.
Definition add_parcopy (parcopy: CSSA.parcopy) (pc: node) : mon unit :=
fun s =>
match PTree.get pc (st_parcopycode s) with
- | None => Error (Errors.msg "")
+ | None => Error (Errors.msg "CSSAgen - 1")
| Some parcopies =>
let new_parcb := parcopy :: parcopies
in
@@ -188,7 +188,7 @@ Definition add_new_phi (dst': reg ) (args': list reg)
(pc: node) :=
fun s =>
match PTree.get pc (st_phicode s) with
- | None => Error (Errors.msg "")
+ | None => Error (Errors.msg "CSSAgen - 2")
| Some phib' =>
OK tt
(mkstate
diff --git a/midend/RTLnorm.v b/midend/RTLnorm.v
index a1b36d0f..0a026286 100644
--- a/midend/RTLnorm.v
+++ b/midend/RTLnorm.v
@@ -87,7 +87,7 @@ Definition add_nop (pc':node) : mon node :=
fun s =>
let pc_new := s.(st_nextnode) in
if peq pc' s.(st_entry)
- then Error (Errors.msg "")
+ then Error (Errors.msg "RTLnorm - 1")
else OK pc_new
(mkstate (Pos.succ pc_new)
(st_entry s)
@@ -126,7 +126,7 @@ Definition upd_pred (preds_pc : list node) (pc pcnew: node) : mon unit :=
OK tt (mkstate (st_nextnode s)
(st_entry s)
(PTree.set pred (ch_successors i pc pcnew) (st_code s)))
- | None => Error (Errors.msg "")
+ | None => Error (Errors.msg "RTLnorm - 2")
end)
preds_pc.
@@ -189,7 +189,7 @@ Definition upd_succ (pc: node) (newsucc:node) (k: nat): mon nat :=
(mkstate (st_nextnode s)
(st_entry s)
(PTree.set pc (ins_newsucc i newsucc k) s.(st_code)))
- | None => Error (Errors.msg "")
+ | None => Error (Errors.msg "RTLnorm - 3")
end.
(** [modif_ksucc] changes the instruction at [pc] concerning its
@@ -200,7 +200,7 @@ Definition modif_ksucc (is_jp:node->bool) (pc: node) (succ:node) (k: nat) : mon
match (st_code s) ! pc with
| None
| Some (Ireturn _)
- | Some (Itailcall _ _ _) => (Error (Errors.msg ""))
+ | Some (Itailcall _ _ _) => (Error (Errors.msg "RTLnorm - 4"))
| _ =>
if is_jp succ
then
@@ -361,9 +361,10 @@ Definition checker (c: code) (tc: code) : bool :=
PTree.fold (fun res pc i => res && (check_pc c tc pc i)) c true.
Definition check_entry (f tf: function) : bool :=
- match (fn_code tf) ! (fn_entrypoint tf) with
- | Some (Inop ts) => peq ts (fn_entrypoint f)
- | _ => false
+ match (fn_code tf) ! (fn_entrypoint tf) with
+ | Some (Inop s) =>
+ (peq s (fn_entrypoint f)) || inop_tunel 3 (fn_code tf) (fn_entrypoint f) s
+ | _ => false
end.
Definition transl_function (f: RTL.function) : Errors.res RTL.function :=
@@ -387,9 +388,11 @@ Definition transl_function (f: RTL.function) : Errors.res RTL.function :=
f.(RTL.fn_stacksize)
s''.(st_code)
s''.(st_entry)) in
- if (checker (fn_code f) (fn_code tf) && check_entry f tf) then
- Errors.OK tf
- else Errors.Error (Errors.msg "")
+ if checker (fn_code f) (fn_code tf) then
+ if (check_entry f tf)
+ then Errors.OK tf
+ else Errors.Error (Errors.msg "RTLnorm - 5")
+ else Errors.Error (Errors.msg "RTLnorm - 6")
end
end
end.
diff --git a/midend/RTLnormspec.v b/midend/RTLnormspec.v
index 3f975c4b..a04174f6 100644
--- a/midend/RTLnormspec.v
+++ b/midend/RTLnormspec.v
@@ -71,7 +71,55 @@ Proof.
destruct i, i'; go.
simpl. intros ; boolInv; auto.
Qed.
-
+
+Lemma inop_tunel_reach_nops : forall code n s1 s2,
+ inop_tunel n code s1 s2 = true ->
+ (s2 = s1) \/ exists aux, reach_nops code s2 s1 aux.
+Proof.
+ unfold inop_tunel.
+ induction n; intros.
+ - rewrite orb_false_r in H.
+ destruct peq in H.
+ + subst; auto.
+ + inv H.
+ - fold inop_tunel in *.
+ destruct peq in H.
+ + subst. auto.
+ + rewrite orb_false_l in H.
+ flatten H; try congruence.
+ subst.
+ eapply IHn in H; eauto.
+ inv H.
+ * right.
+ exists nil.
+ econstructor; eauto.
+ * right.
+ destruct H0 as [aux Haux].
+ exists (n1::aux).
+ econstructor; eauto.
+Qed.
+
+Lemma check_entry_correct : forall f tf,
+ check_entry f tf = true ->
+ exists aux, reach_nops (fn_code tf) (fn_entrypoint tf) (fn_entrypoint f) aux.
+Proof.
+ unfold check_entry; intros.
+ flatten H. subst.
+ apply orb_true_iff in H. inv H.
+ - destruct peq.
+ + subst.
+ exists nil.
+ constructor; auto.
+ + inv H0.
+ - eapply inop_tunel_reach_nops in H0; eauto.
+ inv H0.
+ + exists nil.
+ constructor; auto.
+ + destruct H as [aux Haux].
+ exists (n::aux).
+ econstructor; eauto.
+Qed.
+
Lemma check_mks_spec_correct: forall code tcode pc i ti,
code ! pc = Some i ->
tcode ! pc = Some ti ->
@@ -84,21 +132,8 @@ Proof.
econstructor ; eauto.
intros k succ succ' SUCC SUCC'.
exploit ch_succ_same_length; eauto. intros LENGTH.
- exploit @forall_list2_nth_error; eauto. simpl; intros.
- destruct (peq succ succ') eqn: EQ ; subst.
- + auto.
- + right.
- unfold inop_tunel in H.
- flatten H; subst;
- (repeat (match goal with
- | id : _ || _ = true |- _ =>
- eapply orb_prop in id; invh or
- end);
- repeat (match goal with
- | id : _ = true |- _ =>
- apply proj_sumbool_true in id ; subst
- end); go).
- exists (n0::n1::nil); go.
+ exploit @forall_list2_nth_error; eauto.
+ eapply inop_tunel_reach_nops; eauto.
Qed.
Lemma checker_correct : forall code tcode pc i,
@@ -112,17 +147,16 @@ Proof.
flatten CHECK; try congruence. boolInv.
eapply check_mks_spec_correct ; eauto ; go.
Qed.
-
+
Lemma transl_function_spec_ok : forall f tf,
transl_function f = Errors.OK tf ->
transl_function_spec f tf.
Proof.
unfold transl_function ; intros f tf TRANS.
flatten TRANS. boolInv. simpl in *.
- eapply transl_function_spec_intro ; eauto.
- - intros. simpl in *.
- eapply checker_correct; eauto.
- - unfold check_entry in *.
- flatten H5; try congruence.
- destruct peq; go.
+ eapply check_entry_correct in Eq3; eauto.
+ destruct Eq3 as [aux Hentry].
+ eapply transl_function_spec_intro; eauto.
+ intros.
+ eapply checker_correct; eauto.
Qed.
diff --git a/midend/SSAvalid.v b/midend/SSAvalid.v
index d334cb24..2bc6d470 100644
--- a/midend/SSAvalid.v
+++ b/midend/SSAvalid.v
@@ -239,7 +239,7 @@ Definition update_ctx (size: nat) (preds: PTree.t (list positive))
OK (G,
PTree.set pc (Inop l) new_code,
juncpoints)
- else Error (msg "SSAvalid 1")
+ else Error (msg "SSAvalid - 1")
end
else
OK (PTree.set l g G,
@@ -264,7 +264,7 @@ Definition update_ctx (size: nat) (preds: PTree.t (list positive))
OK (PTree.set succ (PTree.set dest i g) G,
PTree.set pc (Iop op (map use args) dest' succ) new_code,
juncpoints)
- else Error (msg "SSAvalid 2")
+ else Error (msg "SSAvalid - 2")
| RTL.Iload chunk addr args dest succ =>
do i <- get_option (def ! pc) "" ;
let dest' := Bij.pamr size (dest,i) in
@@ -272,7 +272,7 @@ Definition update_ctx (size: nat) (preds: PTree.t (list positive))
OK (PTree.set succ (PTree.set dest i g) G,
PTree.set pc (Iload chunk addr (map use args) dest' succ) new_code,
juncpoints)
- else Error (msg "SSAvalid 3")
+ else Error (msg "SSAvalid - 3")
| RTL.Icall sig fn args dest succ =>
do i <- get_option (def ! pc) "" ;
let dest' := Bij.pamr size (dest,i) in
@@ -280,7 +280,7 @@ Definition update_ctx (size: nat) (preds: PTree.t (list positive))
OK (PTree.set succ (PTree.set dest i g) G,
PTree.set pc (Icall sig (map_os use fn) (map use args) dest' succ) new_code,
juncpoints)
- else Error (msg "SSAvalid 4")
+ else Error (msg "SSAvalid - 4")
| RTL.Itailcall sig fn args =>
OK (G,
PTree.set pc (Itailcall sig (map_os use fn) (map use args)) new_code,
@@ -348,7 +348,7 @@ Definition build_phi_block (size:nat) (preds: PTree.t (list positive))
| Error msg => Error msg
| OK acc => OK (PTree.set pc new_phi_block acc)
end
- else Error (msg "SSAvalid 7").
+ else Error (msg "SSAvalid - 7").
(** Computing dominator-test and so-called external parameters of an SSA function. *)
@@ -428,11 +428,11 @@ Definition typecheck_function (f: RTLdfs.function) (max_index:nat)
) in
if check_unique_def fwo && check_code_at_phipoints fwo
then OK fwo
- else Errors.Error (msg "SSAvalid 8") (* Function is not in SSA *)
+ else Errors.Error (msg "Code not in SSA") (* Function is not in SSA *)
| None => Error (msg "Could not compute dominators")
end
end)
- else Error (msg "SSAvalid 9") (* Program code is not well normalized *)
+ else Error (msg "Code should be normalized")
else Error (msg "Wrong index for definition")
else Error (msg "Wrong maximum index")
.
diff --git a/test/Makefile b/test/Makefile
index 504e4c53..e5546824 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -1,12 +1,17 @@
include ../Makefile.config
-DIRS=c compression raytracer spass regression
+DIRS=c compression raytracer spass regression \
+ extra
+PICOSAT=picosat-965
+
ifeq ($(CLIGHTGEN),true)
DIRS+=clightgen
endif
all:
set -e; for i in $(DIRS); do $(MAKE) CCOMPOPTS='$(CCOMPOPTS)' -C $$i all; done
+ cd $(PICOSAT) && ./configure.sh && cd ..
+ $(MAKE) CCOMPOPTS='$(CCOMPOPTS)' -C $(PICOSAT) -f Makefile.local all
test:
set -e; for i in $(DIRS); do $(MAKE) SIMU='$(SIMU)' -C $$i test; done
@@ -19,3 +24,4 @@ bench:
clean:
for i in $(DIRS); do $(MAKE) -C $$i clean; done
+ $(MAKE) CCOMPOPTS='$(CCOMPOPTS)' -C $(PICOSAT) -f Makefile.local clean
diff --git a/test/extra/Makefile b/test/extra/Makefile
new file mode 100644
index 00000000..3dd08820
--- /dev/null
+++ b/test/extra/Makefile
@@ -0,0 +1,60 @@
+include ../../Makefile.config
+
+CCOMP=../../ccomp
+CCOMPFLAGS=$(CCOMPOPTS) -stdlib ../../runtime -dc -dclight -dasm $(SSA_MODE)
+
+CFLAGS=-O1 -Wall
+
+LIBS=$(LIBMATH)
+
+TIME=ocaml unix.cma ../../tools/xtime.ml -o /dev/null -mintime 2.0 -minruns 4
+
+PROGS=self-loop
+
+BENCHPROGS=
+
+all: $(PROGS:%=%.compcert)
+
+all_s: $(PROGS:%=%.s)
+
+all_gcc: $(PROGS:%=%.gcc)
+
+%.compcert: %.c
+ $(CCOMP) $(CCOMPFLAGS) -o $*.compcert $*.c $(LIBS)
+
+%.s: %.c
+ $(CCOMP) $(CCOMPFLAGS) -S $*.c
+
+%.gcc: %.c
+ $(CC) $(CFLAGS) -o $*.gcc $*.c $(LIBS)
+
+test:
+ @for i in $(BENCHPROGS); do \
+ if $(SIMU) ./$$i.compcert | cmp -s - Results/$$i; \
+ then echo "$$i: passed"; \
+ else echo "$$i: FAILED"; exit 2; \
+ fi; \
+ done
+
+test_gcc:
+ @for i in $(BENCHPROGS); do \
+ if ./$$i.gcc | cmp -s - Results/$$i; \
+ then echo "$$i: passed"; \
+ else echo "$$i: FAILED"; \
+ fi; \
+ done
+
+bench_gcc:
+ @for i in $(BENCHPROGS); do \
+ $(TIME) -name $$i -- ./$$i.gcc; \
+ done
+
+bench:
+ @for i in $(BENCHPROGS); do \
+ $(TIME) -name $$i -- ./$$i.compcert; \
+ done
+
+clean:
+ rm -f *.compcert *.gcc
+ rm -f *.compcert.c *.light.c *.parsed.c *.s *.o *.sdump *~
+ rm -f *.rtl.[0-9] *.ssa.[0-9] *.rtl.norm
diff --git a/test/extra/self-loop.c b/test/extra/self-loop.c
new file mode 100644
index 00000000..d810da1f
--- /dev/null
+++ b/test/extra/self-loop.c
@@ -0,0 +1,4 @@
+int main() {
+a:
+ goto a;
+}
diff --git a/test/picosat-965/LICENSE b/test/picosat-965/LICENSE
new file mode 100644
index 00000000..96739de2
--- /dev/null
+++ b/test/picosat-965/LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2006 - 2014, Armin Biere, Johannes Kepler University.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
+
diff --git a/test/picosat-965/Makefile.local b/test/picosat-965/Makefile.local
new file mode 100644
index 00000000..9db5dfac
--- /dev/null
+++ b/test/picosat-965/Makefile.local
@@ -0,0 +1,62 @@
+include ../../Makefile.config
+
+CC=../../ccomp
+CFLAGS=$(CCOMPOPTS) -stdlib ../../runtime -dparse -dclight -dasm -fstruct-return -fbitfields $(SSA_MODE)
+
+all: picosat picomcs picomus picogcnf libpicosat.a
+
+clean:
+ rm -f picosat picomcs picomus picogcnf
+ rm -f *.exe *.s *.o *.a *.so *.plist
+ rm -f makefile config.h
+ rm -f *.o *.s *.parsed.c *.light.c *.sdump
+ rm -f gmon.out *~
+
+analyze:
+ clang --analyze $(CFLAGS) *.c *.h
+
+picosat: libpicosat.a app.o main.o
+ $(CC) $(CFLAGS) -o $@ main.o app.o -L. -lpicosat
+
+picomcs: libpicosat.a picomcs.o
+ $(CC) $(CFLAGS) -o $@ picomcs.o -L. -lpicosat
+
+picomus: libpicosat.a picomus.o
+ $(CC) $(CFLAGS) -o $@ picomus.o -L. -lpicosat
+
+picogcnf: libpicosat.a picogcnf.o
+ $(CC) $(CFLAGS) -o $@ picogcnf.o -L. -lpicosat
+
+app.o: app.c picosat.h makefile
+ $(CC) $(CFLAGS) -c $<
+
+picomcs.o: picomcs.c picosat.h makefile
+ $(CC) $(CFLAGS) -c $<
+
+picomus.o: picomus.c picosat.h makefile
+ $(CC) $(CFLAGS) -c $<
+
+picogcnf.o: picogcnf.c picosat.h makefile
+ $(CC) $(CFLAGS) -c $<
+
+main.o: main.c picosat.h makefile
+ $(CC) $(CFLAGS) -c $<
+
+picosat.o: picosat.c picosat.h makefile
+ $(CC) $(CFLAGS) -c $<
+
+version.o: version.c config.h makefile
+ $(CC) $(CFLAGS) -c $<
+
+config.h: makefile VERSION mkconfig.sh # and actually picosat.c
+ rm -f $@; ./mkconfig.sh > $@
+
+libpicosat.a: picosat.o version.o
+ ar rc $@ picosat.o version.o
+ ranlib $@
+
+SONAME=-Xlinker -soname -Xlinker libpicosat.so
+libpicosat.so: picosat.o version.o
+ $(CC) $(CFLAGS) -shared -o $@ picosat.o version.o $(SONAME)
+
+.PHONY: all clean
diff --git a/test/picosat-965/NEWS b/test/picosat-965/NEWS
new file mode 100644
index 00000000..7ad60b53
--- /dev/null
+++ b/test/picosat-965/NEWS
@@ -0,0 +1,162 @@
+news for release 965 since 959
+------------------------------
+
+* ADC code works again (spotted by Himanshu Jain)
+* include into R projects (with Christoph Muessel) (--rcode)
+* fixed 'undefined' + 'ptrdiff_' issues (thanks to Christoph Muessel)
+* added 'picosat_set_interrupt' and '-a <alarm>' command line option
+* fixed various issues pointed out by Stefan Hengelein:
+ - fixed incremental usage of 'picosat_adjust'
+ - added CPP fixes (STATS, NO_BINARY_CLAUSE versus TRACE mix-ups)
+ - removed redundant explicit set to zero on reset
+* fixed various usage bugs with 'picomus' (thanks to Stefan Hengelein)
+* removed '-fno-strict-aliasing' (thanks to Jerry James)
+
+news for release 959 since 953
+------------------------------
+
+* fixed header comments
+
+* fixed minor compilation issues
+
+* fixed unitialized memory access problem for 'picosat_deref_partial'
+ and another issue with partial models
+
+* added 'picosat_add_arg' and 'picosat_add_lits'
+
+* '--plain' and 'picosat_set_plain' to disable failed literal probing
+
+* new '#define PICOSAT_REENTRANT_API' in 'picosat.h'
+
+* added manager so no more global variables
+ (allows multiple instances, requires manager object)
+
+news for release 951 since 941
+------------------------------
+
+* cleaned up code (based on comments by Donald Knuth)
+
+* lreduce=O(conflicts^.5)
+
+* added 'picosat_visits' and 'picosat_decisions'
+
+* added '--partial' command line option
+
+* added 'picosat_deref_partial' and 'picosat_save_original_clauses'
+
+* added 'picomcs' as example for MSS computation
+
+news for release 941 since 936
+------------------------------
+
+* added 'picogcnf'
+
+* added All-SAT mode ('--all' command line option)
+
+* statistics include time spent in failed literal preprocessing (probing)
+
+* 'picosat_failed_context' for 'push & pop'
+ (and tested failed assumptions for 'push & pop')
+
+* 'picosat_simplify' for forced garbage collection
+
+* undefined NFL, defined NADC (= failed literals on, ADC's off)
+
+* 'picosat_push' and 'picosat_pop' (beta version)
+
+* fixed some issues related to binary clause handling and
+ generating list of failed assumptions
+
+news for release 936 since 935
+------------------------------
+
+* simple minimal unsatisfiable core (MUS) extractor 'picomus'
+ (example for using 'picosat_mus_assumptions' and 'picosat_coreclause')
+
+* added 'picosat_mus_assumptions'
+
+* added 'picosat_{set_}propagations'
+
+* new 'int' return value for 'picosat_enable_trace_generation' to
+ check for trace code being compiled
+
+news for release 935 since 926
+------------------------------
+
+* added 'picosat_failed_assumptions' (plural)
+
+* new '-A <failedlits>' command line option
+
+* fixed failed assumption issues
+
+* added 'picosat_remove_learned'
+
+* added 'picosat_reset_{phases,scores}'
+
+* added 'picosat_set_less_important_lit'
+
+* added 'picosat_res'
+
+news for release 926 since 846
+------------------------------
+
+* random initial phase (API of 'picosat_set_default_phase' changed)
+
+* fixed accumulative failed assumption (multiple times)
+
+* fixed missing original clause in core generation with assumptions
+
+* fixed debugging code for memory allocation
+
+* shared library in addition to static library
+
+* removed potential UNKNOWN result without decision limit
+
+* added picosat_set_more_important_lit
+
+* added picosat_coreclause
+
+* propagation of binary clauses until completion
+
+* fixed API usage 'assume;sat;sat'
+
+* literals move to front (LMTF) during traversal of visited clauses
+
+* switched from inner/outer to Luby style restart scheduling
+
+* less agressive reduce schedule
+
+* replaced watched literals with head and tail pointers
+
+* add 'picosat_failed_assumption', which allows to avoid tracing and core
+ generation, if one is only interested in assumptions in the core
+
+* fixed a BUG in the generic iterator code of clauses
+ (should rarely happen unless you use a very sophisticated malloc lib)
+
+news for release 846 since 632
+------------------------------
+
+* cleaned up assumption handling (actually removed buggy optimization)
+
+* incremental core generation
+
+* experimental 'all different constraint' handling as in our FMCAD'08 paper
+
+* new API calls:
+
+ - picosat_add_ado_lit (add all different object literal)
+ - picosat_deref_top_level (deref top level assignment)
+ - picosat_changed (check whether extension was possible)
+ - picosat_measure_all_calls (per default do not measure adding time)
+ - picosat_set_prefix (set prefix for messages)
+
+* 64 bit port (and compilation options)
+
+* optional NVSIDS visualization code
+
+* resource controlled failed literal implementation
+
+* disconnect long clauses satisfied at lower decision level
+
+* controlling restarts
diff --git a/test/picosat-965/README b/test/picosat-965/README
new file mode 100644
index 00000000..05b84396
--- /dev/null
+++ b/test/picosat-965/README
@@ -0,0 +1,5 @@
+These are the sources of the PicoSAT solver.
+The preprocessor is not included.
+To compile run './configure.sh && make'.
+The API is document in 'picosat.h'.
+See also 'NEWS' and 'LICENSE'.
diff --git a/test/picosat-965/VERSION b/test/picosat-965/VERSION
new file mode 100644
index 00000000..aa5bea4e
--- /dev/null
+++ b/test/picosat-965/VERSION
@@ -0,0 +1 @@
+965
diff --git a/test/picosat-965/app.c b/test/picosat-965/app.c
new file mode 100644
index 00000000..8c907374
--- /dev/null
+++ b/test/picosat-965/app.c
@@ -0,0 +1,1192 @@
+#include "picosat.h"
+
+#include <assert.h>
+#include <ctype.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define GUNZIP "gunzip -c %s"
+#define BUNZIP2 "bzcat %s"
+#define GZIP "gzip -c -f > %s"
+
+FILE * popen (const char *, const char*);
+int pclose (FILE *);
+
+static PicoSAT * picosat;
+
+static int lineno;
+static FILE *input;
+static int inputid;
+static FILE *output;
+static int verbose;
+static int sargc;
+static char ** sargv;
+static char buffer[100];
+static char *bhead = buffer;
+static const char *eob = buffer + 80;
+static FILE * incremental_rup_file;
+static signed char * sol;
+
+extern void picosat_enter (PicoSAT *);
+extern void picosat_leave (PicoSAT *);
+
+static int
+next (void)
+{
+ int res = getc (input);
+ if (res == '\n')
+ lineno++;
+
+ return res;
+}
+
+static const char *
+parse (PicoSAT * picosat, int force)
+{
+ int ch, sign, lit, vars, clauses;
+
+ lineno = 1;
+ inputid = fileno (input);
+
+SKIP_COMMENTS:
+ ch = next ();
+ if (ch == 'c')
+ {
+ while ((ch = next ()) != EOF && ch != '\n')
+ ;
+ goto SKIP_COMMENTS;
+ }
+
+ if (isspace (ch))
+ goto SKIP_COMMENTS;
+
+ if (ch != 'p')
+INVALID_HEADER:
+ return "missing or invalid 'p cnf <variables> <clauses>' header";
+
+ if (!isspace (next ()))
+ goto INVALID_HEADER;
+
+ while (isspace (ch = next ()))
+ ;
+
+ if (ch != 'c' || next () != 'n' || next () != 'f' || !isspace (next ()))
+ goto INVALID_HEADER;
+
+ while (isspace (ch = next ()))
+ ;
+
+ if (!isdigit (ch))
+ goto INVALID_HEADER;
+
+ vars = ch - '0';
+ while (isdigit (ch = next ()))
+ vars = 10 * vars + (ch - '0');
+
+ if (!isspace (ch))
+ goto INVALID_HEADER;
+
+ while (isspace (ch = next ()))
+ ;
+
+ if (!isdigit (ch))
+ goto INVALID_HEADER;
+
+ clauses = ch - '0';
+ while (isdigit (ch = next ()))
+ clauses = 10 * clauses + (ch - '0');
+
+ if (!isspace (ch) && ch != '\n' )
+ goto INVALID_HEADER;
+
+ if (verbose)
+ {
+ fprintf (output, "c parsed header 'p cnf %d %d'\n", vars, clauses);
+ fflush (output);
+ }
+
+ picosat_adjust (picosat, vars);
+
+ if (incremental_rup_file)
+ picosat_set_incremental_rup_file (picosat, incremental_rup_file, vars, clauses);
+
+ lit = 0;
+READ_LITERAL:
+ ch = next ();
+
+ if (ch == 'c')
+ {
+ while ((ch = next ()) != EOF && ch != '\n')
+ ;
+ goto READ_LITERAL;
+ }
+
+ if (ch == EOF)
+ {
+ if (lit)
+ return "trailing 0 missing";
+
+ if (clauses && !force)
+ return "clause missing";
+
+ return 0;
+ }
+
+ if (isspace (ch))
+ goto READ_LITERAL;
+
+ sign = 1;
+ if (ch == '-')
+ {
+ sign = -1;
+ ch = next ();
+ }
+
+ if (!isdigit (ch))
+ return "expected number";
+
+ lit = ch - '0';
+ while (isdigit (ch = next ()))
+ lit = 10 * lit + (ch - '0');
+
+ if (!clauses && !force)
+ return "too many clauses";
+
+ if (lit)
+ {
+ if (lit > vars && !force)
+ return "maximal variable index exceeded";
+
+ lit *= sign;
+ }
+ else
+ clauses--;
+
+ picosat_add (picosat, lit);
+
+ goto READ_LITERAL;
+}
+
+static void
+bflush (void)
+{
+ *bhead = 0;
+ fputs (buffer, output);
+ fputc ('\n', output);
+ bhead = buffer;
+}
+
+static void
+printi (int i)
+{
+ char *next;
+ int l;
+
+REENTER:
+ if (bhead == buffer)
+ *bhead++ = 'v';
+
+ l = sprintf (bhead, " %d", i);
+ next = bhead + l;
+
+ if (next >= eob)
+ {
+ bflush ();
+ goto REENTER;
+ }
+ else
+ bhead = next;
+}
+
+static void
+printa (PicoSAT * picosat, int partial)
+{
+ int max_idx = picosat_variables (picosat), i, lit, val;
+
+ assert (bhead == buffer);
+
+ for (i = 1; i <= max_idx; i++)
+ {
+ if (partial)
+ {
+ val = picosat_deref_partial (picosat, i);
+ if (!val)
+ continue;
+ }
+ else
+ val = picosat_deref (picosat, i);
+ lit = (val > 0) ? i : -i;
+ printi (lit);
+ }
+
+ printi (0);
+ if (bhead > buffer)
+ bflush ();
+}
+
+static void
+blocksol (PicoSAT * picosat)
+{
+ int max_idx = picosat_variables (picosat), i;
+
+ if (!sol)
+ {
+ sol = malloc (max_idx + 1);
+ memset (sol, 0, max_idx + 1);
+ }
+
+ for (i = 1; i <= max_idx; i++)
+ sol[i] = (picosat_deref (picosat, i) > 0) ? 1 : -1;
+
+ for (i = 1; i <= max_idx; i++)
+ picosat_add (picosat, (sol[i] < 0) ? i : -i);
+
+ picosat_add (picosat, 0);
+}
+
+static int
+has_suffix (const char *str, const char *suffix)
+{
+ const char *tmp = strstr (str, suffix);
+ if (!tmp)
+ return 0;
+
+ return str + strlen (str) - strlen (suffix) == tmp;
+}
+
+static void
+write_core_variables (PicoSAT * picosat, FILE * file)
+{
+ int i, max_idx = picosat_variables (picosat), count = 0;
+ for (i = 1; i <= max_idx; i++)
+ if (picosat_corelit (picosat, i))
+ {
+ fprintf (file, "%d\n", i);
+ count++;
+ }
+
+ if (verbose)
+ fprintf (output, "c found and wrote %d core variables\n", count);
+}
+
+static int
+next_assumption (int start)
+{
+ char * arg, c;
+ int res;
+ res = start + 1;
+ while (res < sargc)
+ {
+ arg = sargv[res++];
+ if (!strcmp (arg, "-a"))
+ {
+ assert (res < sargc);
+ break;
+ }
+
+ if (arg[0] == '-') {
+ c = arg[1];
+ if (c == 'l' || c == 'i' || c == 's' || c == 'o' || c == 't' ||
+ c == 'T' || c == 'r' || c == 'R' || c == 'c' || c == 'V' ||
+ c == 'U' || c == 'A') res++;
+ }
+ }
+ if (res >= sargc) res = 0;
+ return res;
+}
+
+static void
+write_failed_assumptions (PicoSAT * picosat, FILE * file)
+{
+ int i, lit, count = 0;
+#ifndef NDEBUG
+ int max_idx = picosat_variables (picosat);
+#endif
+ i = 0;
+ while ((i = next_assumption (i))) {
+ lit = atoi (sargv[i]);
+ if (!picosat_failed_assumption (picosat, lit)) continue;
+ fprintf (file, "%d\n", lit);
+ count++;
+ }
+ if (verbose)
+ fprintf (output, "c found and wrote %d failed assumptions\n", count);
+#ifndef NDEBUG
+ for (i = 1; i <= max_idx; i++)
+ if (picosat_failed_assumption (picosat, i))
+ count--;
+#endif
+ assert (!count);
+}
+
+static void
+write_to_file (PicoSAT * picosat,
+ const char *name,
+ const char *type,
+ void (*writer) (PicoSAT *, FILE *))
+{
+ int pclose_file, zipped = has_suffix (name, ".gz");
+ FILE *file;
+ char *cmd;
+
+ if (zipped)
+ {
+ cmd = malloc (strlen (GZIP) + strlen (name));
+ sprintf (cmd, GZIP, name);
+ file = popen (cmd, "w");
+ free (cmd);
+ pclose_file = 1;
+ }
+ else
+ {
+ file = fopen (name, "w");
+ pclose_file = 0;
+ }
+
+ if (file)
+ {
+ if (verbose)
+ fprintf (output,
+ "c\nc writing %s%s to '%s'\n",
+ zipped ? "gzipped " : "", type, name);
+
+ writer (picosat, file);
+
+ if (pclose_file)
+ pclose (file);
+ else
+ fclose (file);
+ }
+ else
+ fprintf (output, "*** picosat: can not write to '%s'\n", name);
+}
+
+static int catched;
+
+static void (*sig_int_handler);
+static void (*sig_segv_handler);
+static void (*sig_abrt_handler);
+static void (*sig_term_handler);
+#ifndef NALLSIGNALS
+static void (*sig_kill_handler);
+static void (*sig_xcpu_handler);
+static void (*sig_xfsz_handler);
+#endif
+
+static void
+resetsighandlers (void)
+{
+ (void) signal (SIGINT, sig_int_handler);
+ (void) signal (SIGSEGV, sig_segv_handler);
+ (void) signal (SIGABRT, sig_abrt_handler);
+ (void) signal (SIGTERM, sig_term_handler);
+#ifndef NALLSIGNALS
+ (void) signal (SIGKILL, sig_kill_handler);
+ (void) signal (SIGXCPU, sig_xcpu_handler);
+ (void) signal (SIGXFSZ, sig_xfsz_handler);
+#endif
+}
+
+static int time_limit_in_seconds;
+static void (*sig_alarm_handler);
+static int ought_to_be_interrupted, interrupt_notified;
+
+static void
+alarm_triggered (int sig)
+{
+ (void) sig;
+ assert (sig == SIGALRM);
+ assert (time_limit_in_seconds);
+ assert (!ought_to_be_interrupted);
+ ought_to_be_interrupted = 1;
+ assert (!interrupt_notified);
+}
+
+static int
+interrupt_call_back (void * dummy)
+{
+ (void) dummy;
+ if (!ought_to_be_interrupted)
+ return 0;
+ if (!interrupt_notified)
+ {
+ if (verbose)
+ {
+ picosat_message (picosat, 1, "");
+ picosat_message (picosat, 1,
+ "*** TIME LIMIT OF %d SECONDS REACHED ***",
+ time_limit_in_seconds);
+ picosat_message (picosat, 1, "");
+ }
+ interrupt_notified = 1;
+ }
+ return 1;
+}
+
+static void
+setalarm ()
+{
+ assert (time_limit_in_seconds > 0);
+ sig_alarm_handler = signal (SIGALRM, alarm_triggered);
+ alarm (time_limit_in_seconds);
+ assert (picosat);
+ picosat_set_interrupt (picosat, 0, interrupt_call_back);
+}
+
+static void
+resetalarm ()
+{
+ assert (time_limit_in_seconds > 0);
+ (void) signal (SIGALRM, sig_term_handler);
+}
+
+static void
+message (int sig)
+{
+ picosat_message (picosat, 1, "");
+ picosat_message (picosat, 1, "*** CAUGHT SIGNAL %d ***", sig);
+ picosat_message (picosat, 1, "");
+}
+
+static void
+catch (int sig)
+{
+ if (!catched)
+ {
+ message (sig);
+ catched = 1;
+ picosat_stats (picosat);
+ message (sig);
+ }
+
+ resetsighandlers ();
+ raise (sig);
+}
+
+static void
+setsighandlers (void)
+{
+ sig_int_handler = signal (SIGINT, catch);
+ sig_segv_handler = signal (SIGSEGV, catch);
+ sig_abrt_handler = signal (SIGABRT, catch);
+ sig_term_handler = signal (SIGTERM, catch);
+#ifndef NALLSIGNALS
+ sig_kill_handler = signal (SIGKILL, catch);
+ sig_xcpu_handler = signal (SIGXCPU, catch);
+ sig_xfsz_handler = signal (SIGXFSZ, catch);
+#endif
+}
+
+#define USAGE \
+"usage: picosat [ <option> ... ] [ <input> ]\n" \
+"\n" \
+"where <option> is one of the following\n" \
+"\n" \
+" -h print this command line option summary and exit\n" \
+" --version print version and exit\n" \
+" --config print build configuration and exit\n" \
+"\n" \
+" -v enable verbose output\n" \
+" -f ignore invalid header\n" \
+" -n do not print satisfying assignment\n" \
+" -p print formula in DIMACS format and exit\n" \
+" --plain disable preprocessing (failed literal probing)\n" \
+" -a <lit> start with an assumption\n" \
+" -l <limit> set decision limit (no limit per default)\n" \
+" -L <limit> set time limit in seconds (no limit per default)\n" \
+" -P <limit> set propagation limit (no limit per default)\n" \
+" -i [0-3] [0-3]=[FALSE,TRUE,JWH,RAND] initial phase (default 2=JWH)\n" \
+" -s <seed> set random number generator seed (default 0)\n" \
+" -o <output> set output file (<stdout> per default)\n" \
+" -t <trace> generate compact proof trace file\n" \
+" -T <trace> generate extended proof trace file\n" \
+" -r <trace> generate reverse unit propagation proof file\n" \
+" -R <trace> generate reverse unit propagation proof file incrementally\n" \
+" -c <core> generate clausal core file in DIMACS format\n" \
+" -V <core> generate file listing core variables\n" \
+" -U <core> generate file listing used variables\n" \
+" -A <core> generate file listing failed assumptions\n" \
+"\n" \
+" --all enumerate all solutions\n" \
+" --partial generate and print only partial assignment\n" \
+"\n" \
+"and <input> is an optional input file in DIMACS format.\n"
+
+int
+picosat_main (int argc, char **argv)
+{
+ int res, done, err, print_satisfying_assignment, force, print_formula;
+ const char *compact_trace_name, *extended_trace_name, * rup_trace_name;
+ int assumption, assumptions, defaultphase, allsat, partial, plain;
+ const char * clausal_core_name, * variable_core_name;
+ const char *input_name, *output_name;
+ const char * failed_assumptions_name;
+ int close_input, pclose_input;
+ long long propagation_limit;
+ int i, decision_limit;
+ double start_time;
+ long long sols;
+ unsigned seed;
+ FILE *file;
+ int trace;
+
+ start_time = picosat_time_stamp ();
+
+ sargc = argc;
+ sargv = argv;
+
+ clausal_core_name = 0;
+ variable_core_name = 0;
+ failed_assumptions_name = 0;
+ output_name = 0;
+ compact_trace_name = 0;
+ extended_trace_name = 0;
+ rup_trace_name = 0;
+ incremental_rup_file = 0;
+ close_input = 0;
+ pclose_input = 0;
+ input_name = "<stdin>";
+ input = stdin;
+ output = stdout;
+ verbose = 0;
+ done = err = 0;
+ decision_limit = -1;
+ propagation_limit = -1;
+ defaultphase = 2;
+ assumptions = 0;
+ force = 0;
+ allsat = 0;
+ partial = 0;
+ trace = 0;
+ plain = 0;
+ seed = 0;
+ sols= 0;
+
+ picosat = 0;
+
+ print_satisfying_assignment = 1;
+ print_formula = 0;
+
+ for (i = 1; !done && !err && i < argc; i++)
+ {
+ if (!strcmp (argv[i], "-h"))
+ {
+ fputs (USAGE, output);
+ done = 1;
+ }
+ else if (!strcmp (argv[i], "--version"))
+ {
+ fprintf (output, "%s\n", picosat_version ());
+ done = 1;
+ }
+ else if (!strcmp (argv[i], "--config"))
+ {
+ fprintf (output, "%s\n", picosat_config ());
+ done = 1;
+ }
+ else if (!strcmp (argv[i], "-v"))
+ {
+ verbose++;
+ }
+ else if (!strcmp (argv[i], "--plain"))
+ {
+ plain = 1;
+ }
+ else if (!strcmp (argv[i], "-f"))
+ {
+ force = 1;
+ }
+ else if (!strcmp (argv[i], "-n"))
+ {
+ print_satisfying_assignment = 0;
+ }
+ else if (!strcmp (argv[i], "--partial"))
+ {
+ partial = 1;
+ }
+ else if (!strcmp (argv[i], "-p"))
+ {
+ print_formula = 1;
+ }
+ else if (!strcmp (argv[i], "-l"))
+ {
+ if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument to '-l' missing\n");
+ err = 1;
+ }
+ else
+ decision_limit = atoi (argv[i]);
+ }
+ else if (!strcmp (argv[i], "-L"))
+ {
+ if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument to '-L' missing\n");
+ err = 1;
+ }
+ else
+ {
+ time_limit_in_seconds = atoi (argv[i]);
+ if (time_limit_in_seconds <= 0)
+ {
+ fprintf (output, "*** picosat: invalid '-L' argument\n");
+ err = 1;
+ }
+ }
+ }
+ else if (!strcmp (argv[i], "-P"))
+ {
+ if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument to '-P' missing\n");
+ err = 1;
+ }
+ else
+ propagation_limit = atoll (argv[i]);
+ }
+ else if (!strcmp (argv[i], "-i"))
+ {
+ if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument to '-i' missing\n");
+ err = 1;
+ }
+ else if (!argv[i][1] && ('0' <= argv[i][0] && argv[i][0] <= '3'))
+ {
+ defaultphase = argv[i][0] - '0';
+ }
+ else
+ {
+ fprintf (output, "*** picosat: invalid argument to '-i'\n");
+ err = 1;
+ }
+ }
+ else if (!strcmp (argv[i], "-a"))
+ {
+ if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument to '-a' missing\n");
+ err = 1;
+ }
+ else if (!atoi (argv[i]))
+ {
+ fprintf (output, "*** picosat: argument to '-a' zero\n");
+ err = 1;
+ }
+ else
+ {
+ /* Handle assumptions further down
+ */
+ assumptions++;
+ }
+ }
+ else if (!strcmp (argv[i], "--all"))
+ {
+ allsat = 1;
+ }
+ else if (!strcmp (argv[i], "-s"))
+ {
+ if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument to '-s' missing\n");
+ err = 1;
+ }
+ else
+ seed = atoi (argv[i]);
+ }
+ else if (!strcmp (argv[i], "-o"))
+ {
+ if (output_name)
+ {
+ fprintf (output,
+ "*** picosat: "
+ "multiple output files '%s' and '%s'\n",
+ output_name, argv[i]);
+ err = 1;
+ }
+ else if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument ot '-o' missing\n");
+ err = 1;
+ }
+ else if (!(file = fopen (argv[i], "w")))
+ {
+ fprintf (output,
+ "*** picosat: "
+ "can not write output file '%s'\n", argv[i]);
+ err = 1;
+ }
+ else
+ {
+ output_name = argv[i];
+ output = file;
+ }
+ }
+ else if (!strcmp (argv[i], "-t"))
+ {
+ if (compact_trace_name)
+ {
+ fprintf (output,
+ "*** picosat: "
+ "multiple compact trace files '%s' and '%s'\n",
+ compact_trace_name, argv[i]);
+ err = 1;
+ }
+ else if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument ot '-t' missing\n");
+ err = 1;
+ }
+ else
+ {
+ compact_trace_name = argv[i];
+ trace = 1;
+ }
+ }
+ else if (!strcmp (argv[i], "-T"))
+ {
+ if (extended_trace_name)
+ {
+ fprintf (output,
+ "*** picosat: "
+ "multiple extended trace files '%s' and '%s'\n",
+ extended_trace_name, argv[i]);
+ err = 1;
+ }
+ else if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument ot '-T' missing\n");
+ err = 1;
+ }
+ else
+ {
+ extended_trace_name = argv[i];
+ trace = 1;
+ }
+ }
+ else if (!strcmp (argv[i], "-r"))
+ {
+ if (rup_trace_name)
+ {
+ fprintf (output,
+ "*** picosat: "
+ "multiple RUP trace files '%s' and '%s'\n",
+ rup_trace_name, argv[i]);
+ err = 1;
+ }
+ else if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument ot '-r' missing\n");
+ err = 1;
+ }
+ else
+ {
+ rup_trace_name = argv[i];
+ trace = 1;
+ }
+ }
+ else if (!strcmp (argv[i], "-R"))
+ {
+ if (rup_trace_name)
+ {
+ fprintf (output,
+ "*** picosat: "
+ "multiple RUP trace files '%s' and '%s'\n",
+ rup_trace_name, argv[i]);
+ err = 1;
+ }
+ else if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument ot '-R' missing\n");
+ err = 1;
+ }
+ else if (!(file = fopen (argv[i], "w")))
+ {
+ fprintf (output,
+ "*** picosat: can not write to '%s'\n", argv[i]);
+ err = 1;
+ }
+ else
+ {
+ rup_trace_name = argv[i];
+ incremental_rup_file = file;
+ }
+ }
+ else if (!strcmp (argv[i], "-c"))
+ {
+ if (clausal_core_name)
+ {
+ fprintf (output,
+ "*** picosat: "
+ "multiple clausal core files '%s' and '%s'\n",
+ clausal_core_name, argv[i]);
+ err = 1;
+ }
+ else if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument ot '-c' missing\n");
+ err = 1;
+ }
+ else
+ {
+ clausal_core_name = argv[i];
+ trace = 1;
+ }
+ }
+ else if (!strcmp (argv[i], "-V"))
+ {
+ if (variable_core_name)
+ {
+ fprintf (output,
+ "*** picosat: "
+ "multiple variable core files '%s' and '%s'\n",
+ variable_core_name, argv[i]);
+ err = 1;
+ }
+ else if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument ot '-V' missing\n");
+ err = 1;
+ }
+ else
+ {
+ variable_core_name = argv[i];
+ trace = 1;
+ }
+ }
+ else if (!strcmp (argv[i], "-A"))
+ {
+ if (failed_assumptions_name)
+ {
+ fprintf (output,
+ "*** picosat: "
+ "multiple failed assumptions files '%s' and '%s'\n",
+ failed_assumptions_name, argv[i]);
+ err = 1;
+ }
+ else if (++i == argc)
+ {
+ fprintf (output, "*** picosat: argument ot '-A' missing\n");
+ err = 1;
+ }
+ else
+ failed_assumptions_name = argv[i];
+ }
+ else if (argv[i][0] == '-')
+ {
+ fprintf (output,
+ "*** picosat: "
+ "unknown command line option '%s' (try '-h')\n", argv[i]);
+ err = 1;
+ }
+ else if (close_input || pclose_input)
+ {
+ fprintf (output,
+ "*** picosat: "
+ "multiple input files '%s' and '%s'\n",
+ input_name, argv[i]);
+ err = 1;
+ }
+ else if (has_suffix (argv[i], ".gz"))
+ {
+ char *cmd = malloc (strlen (GUNZIP) + strlen (argv[i]));
+ sprintf (cmd, GUNZIP, argv[i]);
+ if ((file = popen (cmd, "r")))
+ {
+ input_name = argv[i];
+ pclose_input = 1;
+ input = file;
+ }
+ else
+ {
+ fprintf (output,
+ "*** picosat: "
+ "can not read compressed input file '%s'\n", argv[i]);
+ err = 1;
+ }
+ free (cmd);
+ }
+ else if (has_suffix (argv[i], ".bz2"))
+ {
+ char *cmd = malloc (strlen (BUNZIP2) + strlen (argv[i]));
+ sprintf (cmd, BUNZIP2, argv[i]);
+ if ((file = popen (cmd, "r")))
+ {
+ input_name = argv[i];
+ pclose_input = 1;
+ input = file;
+ }
+ else
+ {
+ fprintf (output,
+ "*** picosat: "
+ "can not read compressed input file '%s'\n", argv[i]);
+ err = 1;
+ }
+ free (cmd);
+ }
+ else if (!(file = fopen (argv[i], "r"))) /* TODO .gz ? */
+ {
+ fprintf (output,
+ "*** picosat: can not read input file '%s'\n", argv[i]);
+ err = 1;
+ }
+ else
+ {
+ input_name = argv[i];
+ close_input = 1;
+ input = file;
+ }
+ }
+
+ if (allsat && partial)
+ {
+ fprintf (output,
+ "*** picosat: can not combine '--all' and '--partial'");
+ err = 1;
+ }
+
+ res = PICOSAT_UNKNOWN;
+
+ if (!done && !err)
+ {
+ const char *err_msg;
+
+ if (verbose)
+ {
+ fprintf (output,
+ "c PicoSAT SAT Solver Version %s\n",
+ picosat_version ());
+
+ fprintf (output, "c %s\n", picosat_copyright ());
+ fprintf (output, "c %s\n", picosat_config ());
+ }
+
+ picosat = picosat_init ();
+
+ if (verbose)
+ setsighandlers ();
+
+ if (time_limit_in_seconds)
+ setalarm ();
+
+ picosat_enter (picosat);
+
+ if (output_name)
+ picosat_set_output (picosat, output);
+
+ picosat_set_verbosity (picosat, verbose);
+ picosat_set_plain (picosat, plain);
+
+ if (verbose) fputs ("c\n", output);
+
+ if (trace)
+ {
+ if (verbose)
+ fprintf (output, "c tracing proof\n");
+ picosat_enable_trace_generation (picosat);
+ }
+
+ if (defaultphase)
+ {
+ if (verbose)
+ fprintf (output, "c using %d as default phase\n", defaultphase);
+ picosat_set_global_default_phase (picosat, defaultphase);
+ }
+
+ if (propagation_limit >= 0)
+ {
+ if (verbose)
+ fprintf (output, "c propagation limit of %lld propagations\n",
+ propagation_limit);
+ picosat_set_propagation_limit (picosat,
+ (unsigned long long) propagation_limit);
+ }
+
+ if (partial)
+ {
+ if (verbose)
+ fprintf (output,
+ "c saving original clauses for partial assignment\n");
+
+ picosat_save_original_clauses (picosat);
+ }
+
+ if (verbose)
+ fprintf (output, "c\nc parsing %s\n", input_name);
+
+ if (verbose)
+ fflush (output);
+
+ if ((err_msg = parse (picosat, force)))
+ {
+ fprintf (output, "%s:%d: %s\n", input_name, lineno, err_msg);
+ err = 1;
+ }
+ else
+ {
+NEXT_SOLUTION:
+ if (assumptions)
+ {
+ i = 0;
+ while ((i = next_assumption (i)))
+ {
+ assert (i < argc);
+ assumption = atoi (argv[i]);
+ assert (assumption);
+
+ picosat_assume (picosat, assumption);
+
+ if (verbose)
+ fprintf (output, "c assumption %d\n", assumption);
+ }
+ }
+
+ if (print_formula)
+ {
+ picosat_print (picosat, output);
+ }
+ else
+ {
+ if (verbose)
+ fprintf (output,
+ "c initialized %u variables\n"
+ "c found %u non trivial clauses\n",
+ picosat_variables (picosat),
+ picosat_added_original_clauses (picosat));
+
+ picosat_set_seed (picosat, seed);
+ if (verbose)
+ fprintf (output,
+ "c\nc random number generator seed %u\n",
+ seed);
+
+ res = picosat_sat (picosat, decision_limit);
+
+ if (res == PICOSAT_UNSATISFIABLE)
+ {
+
+ if (allsat)
+ fprintf (output, "s SOLUTIONS %lld\n", sols);
+ else
+ fputs ("s UNSATISFIABLE\n", output);
+
+ fflush (output);
+
+ if (compact_trace_name)
+ write_to_file (picosat,
+ compact_trace_name,
+ "compact trace",
+ picosat_write_compact_trace);
+
+ if (extended_trace_name)
+ write_to_file (picosat,
+ extended_trace_name,
+ "extended trace",
+ picosat_write_extended_trace);
+
+ if (!incremental_rup_file && rup_trace_name)
+ write_to_file (picosat,
+ rup_trace_name,
+ "rup trace",
+ picosat_write_rup_trace);
+
+ if (clausal_core_name)
+ write_to_file (picosat,
+ clausal_core_name,
+ "clausal core",
+ picosat_write_clausal_core);
+
+ if (variable_core_name)
+ write_to_file (picosat,
+ variable_core_name,
+ "variable core",
+ write_core_variables);
+
+ if (failed_assumptions_name)
+ write_to_file (picosat,
+ failed_assumptions_name,
+ "failed assumptions",
+ write_failed_assumptions);
+ }
+ else if (res == PICOSAT_SATISFIABLE)
+ {
+ if (allsat)
+ {
+ sols++;
+ if (verbose)
+ fprintf (output, "c\nc solution %lld\nc\n", sols);
+ }
+
+ if (!allsat || print_satisfying_assignment)
+ fputs ("s SATISFIABLE\n", output);
+
+ if (!allsat || verbose || print_satisfying_assignment)
+ fflush (output);
+
+ if (print_satisfying_assignment)
+ printa (picosat, partial);
+
+ if (allsat)
+ {
+ blocksol (picosat);
+ goto NEXT_SOLUTION;
+ }
+ }
+ else
+ {
+ fputs ("s UNKNOWN\n", output);
+
+ if (allsat && verbose)
+ fprintf (output,
+ "c\nc limit reached after %lld solutions\n",
+ sols);
+ fflush (output);
+ }
+ }
+ }
+
+ if (!err && verbose)
+ {
+ fputs ("c\n", output);
+ picosat_stats (picosat);
+ fprintf (output,
+ "c %.1f seconds total run time\n",
+ picosat_time_stamp () - start_time);
+ }
+
+ if (sol)
+ {
+ free (sol);
+ sol = 0;
+ }
+
+ picosat_leave (picosat);
+
+ if (time_limit_in_seconds)
+ resetalarm ();
+
+ if (verbose)
+ resetsighandlers ();
+
+ picosat_reset (picosat);
+ }
+
+ if (incremental_rup_file)
+ fclose (incremental_rup_file);
+
+ if (close_input)
+ fclose (input);
+
+ if (pclose_input)
+ pclose (input);
+
+ if (output_name)
+ fclose (output);
+
+ return res;
+}
diff --git a/test/picosat-965/configure.sh b/test/picosat-965/configure.sh
new file mode 100755
index 00000000..a82cfe87
--- /dev/null
+++ b/test/picosat-965/configure.sh
@@ -0,0 +1,150 @@
+#!/bin/sh
+
+satcompetition=no
+
+log=no
+debug=no
+stats=undefined
+trace=undefined
+static=yes
+shared=no
+thirtytwobit=no
+static=no
+rcode=no
+
+while [ $# -gt 0 ]
+do
+ case $1 in
+ -g|--debug) debug=yes;;
+ -O|--optimize) debug=no;;
+ -l|--log) log=yes;;
+ -s|--stats) stats=yes;;
+ -t|--trace) trace=yes;;
+ --no-stats) stats=no;;
+ --no-trace) trace=no;;
+ --no-rcode) rcode=no;;
+ --rcode) rcode=yes;;
+ -32|--32|-m32) thirtytwobit=yes;;
+ -static|--static) static=yes;;
+ -shared|--shared) shared=yes;;
+ *) cat <<EOF
+usage: ./configure.sh [<option> ...]
+
+where <option> is one of the following:
+
+ -g|--debug include debugging code and symbols
+ -O|--optimize optimized compilation (default)
+ -l|--log add low level logging code (default with '-g')
+ -s|--stats more expensive statististcs (default with '-g')
+ -t|--trace trace generation (more memory, default with '-g')
+ --no-stats disable expensive stats
+ --no-trace enable trace generation
+ -32|--32|-m32 compile for 32 bit machine even on 64 bit host
+ -rcode|--no-rcode enable/disable compatibility for used in R exension
+ -static|--static produce static binary
+ -shared|--shared produce shared library
+EOF
+exit 1
+;;
+ esac
+shift
+done
+
+echo "version ... `cat VERSION`"
+
+if [ $satcompetition = yes ]
+then
+ debug=no
+ stats=no
+ trace=no
+ thirtytwobit=yes
+ static=yes
+ shared=no
+fi
+
+echo "debug ... $debug"
+echo "log ... $log"
+
+[ $stats = undefined ] && stats=$debug
+echo "stats ... $stats"
+
+[ $trace = undefined ] && trace=$debug
+echo "trace ... $trace"
+
+echo "static ... $static"
+
+echo "shared ... $shared"
+
+[ "X$CC" = X ] && CC=gcc
+
+if [ X"$CFLAGS" = X ]
+then
+ case X"$CC" in
+ *wine*|*mingw*) CFLAGS="-DNGETRUSAGE -DNALLSIGNALS";;
+ *);;
+ esac
+ [ $log = yes ] && CFLAGS="$CFLAGS -DLOGGING"
+ [ $stats = yes ] && CFLAGS="$CFLAGS -DSTATS"
+ [ $trace = yes ] && CFLAGS="$CFLAGS -DTRACE"
+ [ $static = yes ] && CFLAGS="$CFLAGS -static"
+ [ $rcode = yes ] && CFLAGS="$CFLAGS -DRCODE"
+ case X"$CC" in
+ X*gcc*)
+ CFLAGS="$CFLAGS -Wall -Wextra"
+ [ $thirtytwobit = yes ] && CFLAGS="$CFLAGS -m32"
+ if [ $debug = yes ]
+ then
+ CFLAGS="$CFLAGS -g3 -ggdb"
+ else
+ CFLAGS="$CFLAGS -DNDEBUG -O3"
+ fi
+ ;;
+ *)
+ if [ $debug = yes ]
+ then
+ CFLAGS="$CFLAGS -g"
+ else
+ CFLAGS="$CFLAGS -O"
+ fi
+ ;;
+ esac
+fi
+
+if [ $rcode = yes ]
+then
+ for rdoth in /usr/share/R/include/R.h $RINC undefined
+ do
+ [ -f $rdoth ] && break
+ done
+ if [ $rdoth = undefined ]
+ then
+ echo "R.h ... not found (add '-I' manually or 'RHEADER=... ./configure')"
+ else
+ RINC="-I`dirname $rdoth`"
+ CFLAGS="$CFLAGS $RINC"
+ echo "R.h ... added '$RINC' include directive"
+ fi
+ TARGETS="libpicosat.a"
+else
+ TARGETS="picosat picomcs picomus picogcnf libpicosat.a"
+fi
+
+if [ $shared = yes ]
+then
+ TARGETS="$TARGETS libpicosat.so"
+ CFLAGS="$CFLAGS -fPIC"
+fi
+echo "targets ... $TARGETS"
+
+echo "cc ... $CC"
+
+echo "cflags ... $CFLAGS"
+
+printf "makefile ..."
+rm -f makefile
+sed \
+ -e "s,@CC@,$CC," \
+ -e "s,@CFLAGS@,$CFLAGS," \
+ -e "s,@TARGETS@,$TARGETS," \
+makefile.in > makefile
+echo " done"
diff --git a/test/picosat-965/main.c b/test/picosat-965/main.c
new file mode 100644
index 00000000..03fad79f
--- /dev/null
+++ b/test/picosat-965/main.c
@@ -0,0 +1,7 @@
+int picosat_main (int, char **);
+
+int
+main (int argc, char **argv)
+{
+ return picosat_main (argc, argv);
+}
diff --git a/test/picosat-965/makefile.in b/test/picosat-965/makefile.in
new file mode 100644
index 00000000..8e0e7403
--- /dev/null
+++ b/test/picosat-965/makefile.in
@@ -0,0 +1,59 @@
+CC=@CC@
+CFLAGS=@CFLAGS@
+
+all: @TARGETS@
+
+clean:
+ rm -f picosat picomcs picomus picogcnf
+ rm -f *.exe *.s *.o *.a *.so *.plist
+ rm -f makefile config.h
+ rm -f gmon.out *~
+
+analyze:
+ clang --analyze $(CFLAGS) *.c *.h
+
+picosat: libpicosat.a app.o main.o
+ $(CC) $(CFLAGS) -o $@ main.o app.o -L. -lpicosat
+
+picomcs: libpicosat.a picomcs.o
+ $(CC) $(CFLAGS) -o $@ picomcs.o -L. -lpicosat
+
+picomus: libpicosat.a picomus.o
+ $(CC) $(CFLAGS) -o $@ picomus.o -L. -lpicosat
+
+picogcnf: libpicosat.a picogcnf.o
+ $(CC) $(CFLAGS) -o $@ picogcnf.o -L. -lpicosat
+
+app.o: app.c picosat.h makefile
+ $(CC) $(CFLAGS) -c $<
+
+picomcs.o: picomcs.c picosat.h makefile
+ $(CC) $(CFLAGS) -c $<
+
+picomus.o: picomus.c picosat.h makefile
+ $(CC) $(CFLAGS) -c $<
+
+picogcnf.o: picogcnf.c picosat.h makefile
+ $(CC) $(CFLAGS) -c $<
+
+main.o: main.c picosat.h makefile
+ $(CC) $(CFLAGS) -c $<
+
+picosat.o: picosat.c picosat.h makefile
+ $(CC) $(CFLAGS) -c $<
+
+version.o: version.c config.h makefile
+ $(CC) $(CFLAGS) -c $<
+
+config.h: makefile VERSION mkconfig.sh # and actually picosat.c
+ rm -f $@; ./mkconfig.sh > $@
+
+libpicosat.a: picosat.o version.o
+ ar rc $@ picosat.o version.o
+ ranlib $@
+
+SONAME=-Xlinker -soname -Xlinker libpicosat.so
+libpicosat.so: picosat.o version.o
+ $(CC) $(CFLAGS) -shared -o $@ picosat.o version.o $(SONAME)
+
+.PHONY: all clean
diff --git a/test/picosat-965/mkconfig.sh b/test/picosat-965/mkconfig.sh
new file mode 100755
index 00000000..621210b9
--- /dev/null
+++ b/test/picosat-965/mkconfig.sh
@@ -0,0 +1,35 @@
+#!/bin/sh
+
+die () {
+ echo "*** mkconfig.sh: $*" 1>&2
+ exit 1
+}
+
+[ -f makefile ] || die "can not find 'makefile'"
+
+sed \
+ -e '/^C[A-Z]*=/!d' \
+ -e 's,^,#define PICOSAT_,' \
+ -e 's,= *, ",' \
+ -e 's,$,",' \
+ makefile
+
+id=""
+if [ -d .git -a -f .git/HEAD ]
+then
+ head="`awk 'NF == 1' .git/HEAD`"
+ if [ x"$head" = x ]
+ then
+ head="`awk '{print $2}' .git/HEAD`"
+ if [ ! x"$head" = x -a -f ".git/$head" ]
+ then
+ id=" `cat .git/$head`"
+ fi
+ else
+ id=" $head"
+ fi
+fi
+
+echo "#define PICOSAT_VERSION \"`cat VERSION`$id\""
+
+exit 0
diff --git a/test/picosat-965/picogcnf.c b/test/picosat-965/picogcnf.c
new file mode 100644
index 00000000..4d91bfae
--- /dev/null
+++ b/test/picosat-965/picogcnf.c
@@ -0,0 +1,165 @@
+/****************************************************************************
+Copyright (c) 2011-2012, Armin Biere, Johannes Kepler University.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
+****************************************************************************/
+
+#include "picosat.h"
+
+#include <stdio.h>
+#include <assert.h>
+#include <stdarg.h>
+#include <ctype.h>
+#include <limits.h>
+
+#if 1
+#define LOG(ARGS...) do { } while (0)
+#else
+#define LOG(ARGS...) do { printf (##ARGS); } while (0)
+#endif
+
+static int reductions, ngroups;
+
+static PicoSAT * ps;
+
+static void die (const char * fmt, ...) {
+ va_list ap;
+ fprintf (stderr, "*** picogcnf: ");
+ va_start (ap, fmt);
+ vfprintf (stderr, fmt, ap);
+ va_end (ap);
+ fputc ('\n', stderr);
+ exit (1);
+}
+
+static void msg (const char * fmt, ...) {
+ va_list ap;
+ printf ("c [picogcnf] %.2f seconds: ", picosat_time_stamp ());
+ va_start (ap, fmt);
+ vprintf (fmt, ap);
+ va_end (ap);
+ fputc ('\n', stdout);
+ fflush (stdout);
+}
+
+static double percent (double a, double b) { return b?100.0*a/b:0.0; }
+
+static void callback (void * dummy, const int * mus) {
+ int remaining;
+ const int * p;
+ (void) dummy;
+ remaining = 0;
+ for (p = mus; *p; p++) remaining++;
+ assert (remaining <= ngroups);
+ msg ("<%d> reduction to %d out of %d (%.0f%%)",
+ ++reductions, remaining, ngroups, percent (remaining, ngroups));
+}
+
+int main (int argc, char ** argv) {
+ int ch, nvars, sclauses, nclauses, sign, lit, group, res;
+ const int * mus, * p;
+ FILE * file;
+ if (argc != 2) die ("usage: picogcnf <gcnf-file>");
+ if (!(file = fopen (argv[1], "r"))) die ("can not read '%s'", argv[1]);
+ ps = picosat_init ();
+HEADER:
+ ch = getc (file);
+ if (ch == 'c') {
+ while ((ch = getc (file)) != '\n')
+ if (ch == EOF) die ("unexpected EOF");
+ goto HEADER;
+ }
+ if (ch != 'p' ||
+ getc (file) != ' ' ||
+ fscanf (file, "gcnf %d %d %d", &nvars, &sclauses, &ngroups) != 3)
+ die ("invalid header");
+ nclauses = lit = 0;
+ group = INT_MAX;
+ LOG ("p gcnf %d %d %d\n", nvars, sclauses, ngroups);
+LIT:
+ ch = getc (file);
+ if (ch == EOF) {
+ if (lit) die ("zero missing");
+ if (nclauses < sclauses) die ("clauses missing");
+ goto DONE;
+ }
+ if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') goto LIT;
+ if (lit) {
+ if (ch == '-') {
+ sign = -1;
+ ch = getc (file);
+ } else sign = 1;
+ lit = ch - '0';
+ while (isdigit (ch = getc (file)))
+ lit = 10 * lit + ch - '0';
+ if (lit > nvars) die ("maximum variable exceeded");
+ lit *= sign;
+ if (lit) {
+ LOG ("%d ", lit);
+ } else {
+ LOG ("0\n");
+ group = INT_MAX;
+ nclauses++;
+ }
+ picosat_add (ps, lit);
+ } else if (ch == '{') {
+ if (nclauses == sclauses) die ("too many clauses");
+ if (group < INT_MAX) die ("multiple groups per clause");
+GROUP:
+ ch = getc (file);
+ if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') goto GROUP;
+ if (!isdigit (ch)) die ("group does not start with digit");
+ group = ch - '0';
+ while (isdigit (ch = getc (file)))
+ group = 10 * group + (ch - '0');
+ if (group > ngroups) die ("maximal group exceeded");
+ while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
+ ch = getc (file);
+ if (ch != '}') die ("expected '}'");
+ LOG ("{%d} ", group);
+ if (group) picosat_add (ps, -(nvars + group));
+ lit = INT_MAX;
+ } else die ("expected '{'");
+ goto LIT;
+DONE:
+ fclose (file);
+ for (lit = nvars + 1; lit <= nvars + ngroups; lit++) picosat_assume (ps, lit);
+ res = picosat_sat (ps, -1);
+ msg ("first call to SAT solver returned");
+ if (res == 10) printf ("s SATISFIABLE\n");
+ else if (res == 20) printf ("s UNSATISFIABLE\n");
+ else printf ("s UNKNOWN\n");
+ fflush (stdout);
+ if (res == 20) {
+ mus = picosat_mus_assumptions (ps, 0, callback, 1);
+ assert (mus);
+ printf ("v");
+ for (p = mus; (lit = *p); p++) {
+ assert (nvars + 1 <= lit && lit <= nvars + ngroups);
+ printf (" %d", lit - nvars);
+ }
+ printf (" 0\n");
+ fflush (stdout);
+ }
+ msg ("max memory %.1f MB",
+ picosat_max_bytes_allocated (ps) / (double)(1<<20));
+ picosat_reset (ps);
+ msg ("%d reductions", reductions);
+ return res;
+}
diff --git a/test/picosat-965/picomcs.c b/test/picosat-965/picomcs.c
new file mode 100644
index 00000000..3acf7bd7
--- /dev/null
+++ b/test/picosat-965/picomcs.c
@@ -0,0 +1,334 @@
+#include <assert.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <ctype.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "picosat.h"
+
+typedef struct Clause { int cid, * lits; struct Clause * next; } Clause;
+typedef struct MCS { int mid, * clauses; struct MCS * next; } MCS;
+
+static int nvars;
+static char * marked;
+
+static Clause * first_clause, * last_clause;
+static int nclauses, first_cid, last_cid;
+
+static MCS * first_mcs, * last_mcs;
+static int nmcs;
+
+static int * stk, szstk, nstk;
+
+static int verbose, join, noprint;
+
+static int lineno = 1, close_input;
+static const char * input_name;
+static FILE * input;
+
+static PicoSAT * ps;
+
+static void release_clauses (void) {
+ Clause * p, * next;
+ for (p = first_clause; p; p = next) {
+ next = p->next;
+ free (p->lits);
+ free (p);
+ }
+}
+
+static void release_mss (void) {
+ MCS * p, * next;
+ for (p = first_mcs; p; p = next) {
+ next = p->next;
+ free (p->clauses);
+ free (p);
+ }
+}
+
+static void release (void) {
+ release_clauses ();
+ release_mss ();
+ free (marked);
+ free (stk);
+}
+
+static void push_stack (int n) {
+ if (nstk == szstk)
+ stk = realloc (stk, (szstk = szstk ? 2*szstk : 1) * sizeof *stk);
+ stk[nstk++] = n;
+}
+
+static void push_clause (void) {
+ Clause * clause;
+ size_t bytes;
+ clause = malloc (sizeof *clause);
+ clause->cid = ++nclauses;
+ clause->next = 0;
+ push_stack (0);
+ bytes = nstk * sizeof *clause->lits;
+ clause->lits = malloc (bytes);
+ memcpy (clause->lits, stk, bytes);
+ if (last_clause) last_clause->next = clause;
+ else first_clause = clause;
+ last_clause = clause;
+ nstk = 0;
+}
+
+static void push_mcs (void) {
+ MCS * mcs;
+ size_t bytes;
+ mcs = malloc (sizeof *mcs);
+ mcs->mid = ++nmcs;
+ mcs->next = 0;
+ push_stack (0);
+ bytes = nstk * sizeof *mcs->clauses;
+ mcs->clauses = malloc (bytes);
+ memcpy (mcs->clauses, stk, bytes);
+ if (last_mcs) last_mcs->next = mcs;
+ else first_mcs = mcs;
+ last_mcs = mcs;
+ nstk = 0;
+}
+
+static int nextch (void) {
+ int res = getc (input);
+ if (res == '\n') lineno++;
+ return res;
+}
+
+static void msg (int level, const char * fmt, ...) {
+ va_list ap;
+ if (level > verbose) return;
+ printf ("c [picomcs] ");
+ va_start (ap, fmt);
+ vprintf (fmt, ap);
+ va_end (ap);
+ fputc ('\n', stdout);
+ fflush (stdout);
+}
+
+static const char * parse (void) {
+ int ch, expclauses, lit, sign;
+ size_t bytes;
+ msg (1, "parsing %s", input_name);
+COMMENTS:
+ ch = nextch ();
+ if (ch == 'c') {
+ while ((ch = nextch ()) != '\n')
+ if (ch == EOF) return "out of file in comment";
+ goto COMMENTS;
+ }
+ if (ch != 'p')
+INVALID_HEADER:
+ return "invalid header";
+ ungetc (ch, input);
+ if (fscanf (input, "p cnf %d %d", &nvars, &expclauses) != 2)
+ goto INVALID_HEADER;
+ msg (1, "found 'p cnf %d %d' header", nvars, expclauses);
+ bytes = (1 + nvars + expclauses) * sizeof *marked;
+ marked = malloc (bytes);
+ memset (marked, 0, bytes);
+LIT:
+ ch = nextch ();
+ if (ch == ' ' || ch == '\n' || ch == '\t' || ch == '\r') goto LIT;
+ if (ch == EOF) {
+ assert (nclauses <= expclauses);
+ if (nclauses < expclauses) return "clauses missing";
+ return 0;
+ }
+ if (ch == '-') {
+ ch = nextch ();
+ if (!isdigit (ch)) return "expected digit after '-'";
+ if (ch == '0') return "expected positive digit after '-'";
+ sign = -1;
+ } else if (!isdigit (ch)) return "expected '-' or digit";
+ else sign = 1;
+ lit = ch - '0';
+ while (isdigit (ch = nextch ()))
+ lit = 10*lit + (ch - '0');
+ if (lit) {
+ if (lit > nvars) return "maximum variable index exceeded";
+ if (nclauses == expclauses) return "too many clauses";
+ push_stack (sign * lit);
+ } else {
+ assert (nclauses < expclauses);
+ push_clause ();
+ }
+ goto LIT;
+}
+
+#ifndef NDEBUG
+static void dump_clause (Clause * c) {
+ int * p, lit;
+ for (p = c->lits; (lit = *p); p++)
+ printf ("%d ", lit);
+ printf ("0\n");
+}
+
+void dump (void) {
+ Clause * p;
+ printf ("p cnf %d %d\n", nvars, nclauses);
+ for (p = first_clause; p; p = p->next)
+ dump_clause (p);
+}
+#endif
+
+static int clause2selvar (Clause * c) {
+ int res = c->cid + nvars;
+ assert (first_cid <= res && res <= last_cid);
+ return res;
+}
+
+static void encode_clause (Clause * c) {
+ int * p, lit;
+ if (verbose >= 2) {
+ printf ("c [picomcs] encode clause %d :", c->cid);
+ printf (" %d", -clause2selvar (c));
+ for (p = c->lits; (lit = *p); p++) printf (" %d", lit);
+ fputc ('\n', stdout), fflush (stdout);
+ }
+ picosat_add (ps, -clause2selvar (c));
+ for (p = c->lits; (lit = *p); p++) picosat_add (ps, lit);
+ picosat_add (ps, 0);
+}
+
+static void encode (void) {
+ Clause * p;
+ first_cid = nvars + 1;
+ last_cid = nvars + nclauses;
+ msg (2, "selector variables range %d to %d", first_cid, last_cid);
+ for (p = first_clause; p; p = p->next)
+ encode_clause (p);
+ msg (1, "encoded %d clauses", nclauses);
+}
+
+static void camcs (void) {
+ int cid, i;
+ const int * mcs, * p;
+ msg (1, "starting to compute all minimal correcting sets");
+ while ((mcs = picosat_next_minimal_correcting_subset_of_assumptions (ps))) {
+ for (p = mcs; (cid = *p); p++)
+ push_stack (cid);
+ if (verbose >= 2) {
+ printf ("c [picomcs] mcs %d :", nmcs);
+ for (i = 0; i < nstk; i++) printf (" %d", stk[i] - nvars);
+ fputc ('\n', stdout);
+ fflush (stdout);
+ } else if (verbose && isatty (1)) {
+ printf ("\rc [picomcs] mcs %d", nmcs);
+ fflush (stdout);
+ }
+ push_mcs ();
+ }
+ if (verbose && isatty (1)) fputc ('\r', stdout);
+ msg (1, "found %d minimal correcting sets", nmcs);
+}
+
+static void cumcscb (void * state, int nmcs, int nhumus) {
+ int * ptr = state;
+ *ptr = nmcs;
+ ptr[0] = nmcs, ptr[1] = nhumus;
+ if (!verbose || (!isatty (1) && verbose == 1)) return;
+ if (verbose == 1) fputc ('\r', stdout);
+ printf ("c [picomcs] mcs %d humus %d", nmcs, nhumus);
+ if (verbose >= 2) fputc ('\n', stdout);
+ fflush (stdout);
+}
+
+static void cumcs (void) {
+ int stats[2], count, cid;
+ const int * humus, * p;
+ stats[0] = stats[1] = 0;
+ humus = picosat_humus (ps, cumcscb, stats);
+ if (isatty (1) && verbose == 1) fputc ('\n', stdout);
+ count = 0;
+ for (p = humus; (cid = *p); p++) {
+ if (marked[cid]) continue;
+ marked[cid] = 1;
+ count++;
+ }
+ assert (count == stats[1]);
+ msg (1,
+ "computed union of minimal correcting sets of size %d with %d mcs",
+ stats[1], stats[0]);
+}
+
+static void
+print_umcs (void) {
+ int cid;
+ printf ("v");
+ for (cid = first_cid; cid <= last_cid; cid++)
+ if (marked[cid])
+ printf (" %d", cid - nvars);
+ printf (" 0\n");
+}
+
+static void
+print_mcs (MCS * mcs)
+{
+ const int * p;
+ int cid;
+ printf ("v");
+ for (p = mcs->clauses; (cid = *p); p++)
+ printf (" %d", cid - nvars);
+ printf (" 0\n");
+}
+
+static void
+print_all_mcs (void)
+{
+ MCS * p;
+ for (p = first_mcs; p; p = p->next)
+ print_mcs (p);
+}
+
+int main (int argc, char ** argv) {
+ const char * perr;
+ int i, res;
+ for (i = 1; i < argc; i++) {
+ if (!strcmp (argv[i], "-h")) {
+ printf ("usage: picomcs [-h][-v][-j][-n][<input>]\n");
+ exit (0);
+ }
+ else if (!strcmp (argv[i], "-v")) verbose++;
+ else if (!strcmp (argv[i], "-j")) join = 1;
+ else if (!strcmp (argv[i], "-n")) noprint = 1;
+ else if (argv[i][0] == '-') {
+ fprintf (stderr, "*** picomcs: invalid option '%s'\n", argv[i]);
+ exit (1);
+ } else if (input_name) {
+ fprintf (stderr, "*** picomcs: two input files specified\n");
+ exit (1);
+ } else if (!(input = fopen ((input_name = argv[i]), "r"))) {
+ fprintf (stderr, "*** picomcs: can not read '%s'\n", argv[i]);
+ exit (1);
+ } else close_input = 1;
+ }
+ if (!input_name) input_name = "<stdin>", input = stdin;
+ if ((perr = parse ())) {
+ fprintf (stderr, "%s:%d: parse error: %s\n", input_name, lineno, perr);
+ exit (1);
+ }
+ if (close_input) fclose (input);
+ ps = picosat_init ();
+ picosat_set_prefix (ps, "c [picosat] ");
+ encode ();
+ for (i = first_cid; i <= last_cid; i++)
+ picosat_set_default_phase_lit (ps, i, 1);
+ for (i = first_cid; i <= last_cid; i++) picosat_assume (ps, i);
+ res = picosat_sat (ps, -1);
+ if (res == 10) printf ("s SATISFIABLE\n");
+ else printf ("s UNSATISFIABLE\n");
+ fflush (stdout);
+ if (join) cumcs (); else camcs ();
+ if (verbose) picosat_stats (ps);
+ picosat_reset (ps);
+ if (!noprint) {
+ if (join) print_umcs (); else print_all_mcs ();
+ }
+ release ();
+ return res;
+}
diff --git a/test/picosat-965/picomus.c b/test/picosat-965/picomus.c
new file mode 100644
index 00000000..f92a2d58
--- /dev/null
+++ b/test/picosat-965/picomus.c
@@ -0,0 +1,407 @@
+/****************************************************************************
+Copyright (c) 2011-2014, Armin Biere, Johannes Kepler University.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
+****************************************************************************/
+
+#include "picosat.h"
+
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+#include <stdarg.h>
+#include <ctype.h>
+
+#define MAXNONREDROUNDS 3
+#define MINCOREROUNDS 5
+#define MAXCOREROUNDS 100
+
+typedef struct Cls { int lit, red, * lits; } Cls;
+
+static int verbose, nowitness;
+static int fclose_input, pclose_input, close_output;
+static FILE * input_file, * output_file;
+static const char * input_name, * output_name;
+static int lineno = 1;
+static int nvars, nclauses;
+static Cls * clauses;
+static int * lits, nlits, szlits;
+static double start;
+static int reductions;
+
+static PicoSAT * ps;
+
+static int next (void) {
+ int res = fgetc (input_file);
+ if (res == '\n') lineno++;
+ return res;
+}
+
+static void msg (int level, const char * fmt, ...) {
+ va_list ap;
+ if (verbose < level) return;
+ fputs ("c [picomus] ", stdout);
+ va_start (ap, fmt);
+ vfprintf (stdout, fmt, ap);
+ va_end (ap);
+ fputc ('\n', stdout);
+ fflush (stdout);
+}
+
+static void warn (const char * fmt, ...) {
+ va_list ap;
+ if (verbose < 0) return;
+ fputs ("c [picomus] WARNING: ", stdout);
+ va_start (ap, fmt);
+ vfprintf (stdout, fmt, ap);
+ va_end (ap);
+ fputc ('\n', stdout);
+ fflush (stdout);
+}
+
+static const char * parse (void) {
+ int ch, n, lit, sign, i;
+ Cls * c;
+HEADER:
+ ch = next ();
+ if (ch == 'c') {
+ while ((ch = next ()) != '\n')
+ if (ch == EOF) return "EOF after 'c'";
+ goto HEADER;
+ }
+ if (ch == '\r') goto HEADER;
+ if (ch != 'p') return "expected 'c' or 'p'";
+ if (fscanf (input_file, " cnf %d %d", &nvars, &nclauses) != 2)
+ return "invalid header";
+ msg (1, "p cnf %d %d", nvars, nclauses);
+ clauses = calloc (nclauses, sizeof *clauses);
+ lit = n = 0;
+LIT:
+ ch = next ();
+ if (ch == ' ' || ch == '\n' || ch == '\t' || ch == '\r') goto LIT;
+ if (ch == 'c') {
+ while ((ch = next ()) != '\n')
+ if (ch == EOF) return "EOF after 'c'";
+ goto LIT;
+ }
+ if (ch == EOF) {
+ if (lit) return "zero missing";
+ if (n < nclauses) return "clauses missing";
+ return 0;
+ }
+ if (n == nclauses) return "too many clauses";
+ if (ch == '-') {
+ sign = -1;
+ ch = next ();
+ if (!isdigit (ch)) return "expected digit after '-'";
+ } else sign = 1;
+ if (!isdigit (ch)) return "expected digit";
+ lit = ch - '0';
+ while (isdigit (ch = next ()))
+ lit = 10 * lit + (ch - '0');
+ if (lit > nvars) return "maximum variable index exceeded";
+ if (lit) {
+ lit *= sign;
+ if (nlits == szlits) {
+ szlits = szlits ? 2 * szlits : 1;
+ lits = realloc (lits, szlits * sizeof *lits);
+ }
+ lits[nlits++] = lit;
+ } else {
+ c = clauses + n++;
+ c->lits = malloc ((nlits + 1) * sizeof *c->lits);
+ for (i = 0; i < nlits; i++)
+ c->lits[i] = lits[i];
+ c->lits[i] = 0;
+ nlits = 0;
+ }
+ goto LIT;
+}
+
+static void die (const char * fmt, ...) {
+ va_list ap;
+ fputs ("*** picomus: ", stdout);
+ va_start (ap, fmt);
+ vfprintf (stdout, fmt, ap);
+ va_end (ap);
+ fputc ('\n', stdout);
+ fflush (stdout);
+ exit (1);
+}
+
+static double percent (double a, double b) { return b?100.0*a/b:0.0; }
+
+static void callback (void * dummy, const int * mus) {
+ int remaining;
+ const int * p;
+ (void) dummy;
+ if (verbose <= 0) return;
+ remaining = 0;
+ for (p = mus; *p; p++) remaining++;
+ assert (remaining <= nclauses);
+ reductions++;
+ msg (1, "reduction %d to %d = %.0f%% out of %d after %.1f sec",
+ reductions,
+ remaining, percent (remaining, nclauses), nclauses,
+ picosat_time_stamp () - start);
+}
+
+static const char * USAGE =
+"picomus [-h][-v][-q] [ <input> [ <output> ] ]\n"
+"\n"
+" -h print this command line option summary\n"
+" -v increase verbosity level (default 0 = no messages)\n"
+" -q be quiet (no warnings nor messages)\n"
+"\n"
+"This tool is a SAT solver that uses the PicoSAT library to\n"
+"generate a 'minimal unsatisfiable core' also known as 'minimal\n"
+"unsatisfiable set' (MUS) of a CNF in DIMACS format.\n"
+"\n"
+"Both file arguments can be \"-\" and then denote <stdin> and\n"
+"<stdout> respectively. If no input file is given <stdin> is used.\n"
+"If no output file is specified the MUS is computed and only printed\n"
+"to <stdout> in the format of the SAT competition 2011 MUS track.\n"
+"\n"
+"Note, that the 's ...' lines and in case the instance is satisfiable\n"
+"also the 'v ...' lines for the satisfying assignment are always\n"
+"printed to <stdout> (or not printed at all with '-q').\n"
+"\n"
+"If '-n' is specified satisfying assignment and MUS printing\n"
+"on <stdout> (using the 'v ...' format) is suppressed.\n"
+"The 's ...' line is still printed unless '-q' is specified.\n"
+"If <output> is specified an MUS is written to this file,\n"
+"even if '-n' or '-q' is used.\n"
+"\n"
+#ifndef TRACE
+"WARNING: PicosSAT is compiled without trace support.\n"
+"\n"
+"This typically slows down this MUS extractor, since\n"
+"it only relies on clause selector variables and\n"
+"can not make use of core extraction. To enable\n"
+"trace generation use './configure.sh --trace' or\n"
+"'./configure.sh -O --trace' when building PicoSAT.\n"
+#else
+"Since trace generation code is included, this binary\n"
+"uses also core extraction in addition to clause selector\n"
+"variables.\n"
+#endif
+;
+
+int main (int argc, char ** argv) {
+ int i, * p, n, oldn, red, nonred, res, round, printed, len;
+ const char * err;
+ const int * q;
+ char * cmd;
+ Cls * c;
+#ifndef NDEBUG
+ int tmp;
+#endif
+ start = picosat_time_stamp ();
+ for (i = 1; i < argc; i++) {
+ if (!strcmp (argv[i], "-h")) {
+ fputs (USAGE, stdout);
+ exit (0);
+ } else if (!strcmp (argv[i], "-v")) {
+ if (verbose < 0) die ("'-v' option after '-q'");
+ verbose++;
+ } else if (!strcmp (argv[i], "-q")) {
+ if (verbose < 0) die ("two '-q' options");
+ if (verbose > 0) die ("'-q' option after '-v'");
+ verbose = -1;
+ } else if (!strcmp (argv[i], "-n")) nowitness = 1;
+ else if (argv[i][0] == '-' && argv[i][1])
+ die ("invalid command line option '%s'", argv[i]);
+ else if (output_name) die ("too many arguments");
+ else if (!input_name) input_name = argv[i];
+ else output_name = argv[i];
+ }
+ if (!output_name) warn ("no output file given");
+ if (input_name && strcmp (input_name, "-")) {
+ len = strlen (input_name);
+ if (len >= 3 && !strcmp (input_name + len - 3, ".gz")) {
+ cmd = malloc (len + 20);
+ sprintf (cmd, "gunzip -c %s 2>/dev/null", input_name);
+ input_file = popen (cmd, "r");
+ pclose_input = 1;
+ free (cmd);
+ } else input_file = fopen (input_name, "r"), fclose_input = 1;
+ if (!input_file) die ("can not read '%s'", input_name);
+ } else input_file = stdin, input_name = "-";
+ if ((err = parse ())) {
+ fprintf (stdout, "%s:%d: %s\n", input_name, lineno, err);
+ fflush (stdout);
+ exit (1);
+ }
+ if (fclose_input) fclose (input_file);
+ if (pclose_input) pclose (input_file);
+ ps = picosat_init ();
+ picosat_set_prefix (ps, "c [picosat] ");
+ picosat_set_output (ps, stdout);
+ if (verbose > 1) picosat_set_verbosity (ps, verbose - 1);
+ printed = 0;
+ if (!picosat_enable_trace_generation (ps))
+ warn ("PicoSAT compiled without trace generation"),
+ warn ("core extraction disabled");
+ else {
+ n = nclauses;
+ nonred = 0;
+ for (round = 1; round <= MAXCOREROUNDS; round++) {
+ if (verbose > 1)
+ msg (1, "starting core extraction round %d", round);
+ picosat_set_seed (ps, round);
+ for (i = 0; i < nclauses; i++) {
+ c = clauses + i;
+ if (c->red) {
+ picosat_add (ps, 1);
+ picosat_add (ps, -1);
+ } else {
+ for (p = c->lits; *p; p++)
+ picosat_add (ps, *p);
+ }
+#ifndef NDEBUG
+ tmp =
+#endif
+ picosat_add (ps, 0);
+ assert (tmp == i);
+ }
+ res = picosat_sat (ps, -1);
+ if (res == 10) { assert (round == 1); goto SAT; }
+ assert (res == 20);
+ if (!printed) {
+ assert (round == 1);
+ printed = 1;
+ if (verbose >= 0)
+ printf ("s UNSATISFIABLE\n"),
+ fflush (stdout);
+ }
+ for (i = 0; i < nclauses; i++) {
+ c = clauses + i;
+ if (c->red) { assert (!picosat_coreclause (ps, i)); continue; }
+ if (picosat_coreclause (ps, i)) continue;
+ c->red = 1;
+ }
+ oldn = n;
+ n = 0;
+ for (i = 0; i < nclauses; i++) if (!clauses[i].red) n++;
+ msg (1, "extracted core %d of size %d = %0.f%% out of %d after %.1f sec",
+ round, n, percent (n, nclauses), nclauses,
+ picosat_time_stamp () - start);
+ assert (oldn >= n);
+ picosat_reset (ps);
+ ps = picosat_init ();
+ picosat_set_prefix (ps, "c [picosat] ");
+ picosat_set_output (ps, stdout);
+ if (round >= MINCOREROUNDS) {
+ red = oldn - n;
+ if (red < 10 && (100*red + 99)/oldn < 2) {
+ nonred++;
+ if (nonred > MAXNONREDROUNDS) break;
+ }
+ }
+ if (round < MAXCOREROUNDS) picosat_enable_trace_generation (ps);
+ }
+ }
+ for (i = 0; i < nclauses; i++) {
+ c = clauses + i;
+ if (c->red) {
+ picosat_add (ps, 1);
+ picosat_add (ps, -1);
+#ifndef NDEBUG
+ tmp =
+#endif
+ picosat_add (ps, 0);
+ assert (tmp == i);
+ continue;
+ }
+ c->lit = nvars + i + 1;
+ picosat_add (ps, -c->lit);
+ for (p = c->lits; *p; p++)
+ (void) picosat_add (ps, *p);
+#ifndef NDEBUG
+ tmp =
+#endif
+ picosat_add (ps, 0);
+ assert (tmp == i);
+ }
+ for (i = 0; i < nclauses; i++) {
+ c = clauses + i;
+ if (c->red) continue;
+ picosat_assume (ps, c->lit);
+ }
+ res = picosat_sat (ps, -1);
+ if (res == 20) {
+ if (!printed && verbose >= 0)
+ printf ("s UNSATISFIABLE\n"), fflush (stdout);
+ for (i = 0; i < nclauses; i++) clauses[i].red = 1;
+ q = picosat_mus_assumptions (ps, 0, callback, 1);
+ while ((i = *q++)) {
+ i -= nvars + 1;
+ assert (0 <= i && i < nclauses);
+ clauses[i].red = 0;
+ }
+ } else {
+SAT:
+ assert (res == 10);
+ if (!printed && verbose >= 0)
+ printf ("s SATISFIABLE\n"), fflush (stdout);
+ if (!nowitness && verbose >= 0) {
+ for (i = 1; i <= nvars; i++)
+ printf ("v %d\n", ((picosat_deref (ps, i) < 0) ? -1 : 1) * i);
+ printf ("v 0\n");
+ }
+ }
+ if (verbose > 0) picosat_stats (ps);
+ picosat_reset (ps);
+ n = 0;
+ for (i = 0; i < nclauses; i++) if (!clauses[i].red) n++;
+ red = nclauses - n;
+ msg (1, "found %d redundant clauses %.0f%%", red, percent (red, nclauses));
+ if (res == 20)
+ msg (0, "computed MUS of size %d out of %d (%.0f%%)",
+ n, nclauses, percent (n, nclauses));
+ if (output_name && strcmp (output_name, "-")) {
+ output_file = fopen (output_name, "w");
+ if (!output_file) die ("can not write '%s'", output_name);
+ close_output = 1;
+ } else if (output_name && !strcmp (output_name, "-")) output_file = stdout;
+ if (output_file) {
+ fprintf (output_file, "p cnf %d %d\n", nvars, n);
+ for (i = 0; i < nclauses; i++)
+ if (!clauses[i].red) {
+ for (p = clauses[i].lits; *p; p++) fprintf (output_file, "%d ", *p);
+ fprintf (output_file, "0\n");
+ }
+ if (close_output) fclose (output_file);
+ }
+ if (res == 20) {
+ if (!nowitness && verbose >= 0) {
+ for (i = 0; i < nclauses; i++)
+ if (!clauses[i].red) printf ("v %d\n", i+1);
+ printf ("v 0\n");
+ }
+ }
+ msg (1, "%s %d irredundant clauses %.0f%%",
+ output_file ? "printed" : "computed", n, percent (n, nclauses));
+ for (i = 0; i < nclauses; i++) free (clauses[i].lits);
+ free (clauses);
+ free (lits);
+ msg (1, "%d reductions in %.1f seconds",
+ reductions, picosat_time_stamp () - start);
+ return res;
+}
diff --git a/test/picosat-965/picosat.c b/test/picosat-965/picosat.c
new file mode 100644
index 00000000..76db7ae0
--- /dev/null
+++ b/test/picosat-965/picosat.c
@@ -0,0 +1,8502 @@
+/****************************************************************************
+Copyright (c) 2006 - 2015, Armin Biere, Johannes Kepler University.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
+****************************************************************************/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <limits.h>
+#include <ctype.h>
+#include <stdarg.h>
+#include <stdint.h>
+
+#include "picosat.h"
+
+/* By default code for 'all different constraints' is disabled, since 'NADC'
+ * is defined.
+ */
+#define NADC
+
+/* By default we enable failed literals, since 'NFL' is undefined.
+ *
+#define NFL
+ */
+
+/* By default we 'detach satisfied (large) clauses', e.g. NDSC undefined.
+ *
+#define NDSC
+ */
+
+/* Do not use luby restart schedule instead of inner/outer.
+ *
+#define NLUBY
+ */
+
+/* Enabling this define, will use gnuplot to visualize how the scores evolve.
+ *
+#define VISCORES
+ */
+#ifdef VISCORES
+// #define WRITEGIF /* ... to generate a video */
+#endif
+
+#ifdef VISCORES
+#ifndef WRITEGIF
+#include <unistd.h> /* for 'usleep' */
+#endif
+#endif
+
+#ifdef RCODE
+#include <R.h>
+#endif
+
+#define MINRESTART 100 /* minimum restart interval */
+#define MAXRESTART 1000000 /* maximum restart interval */
+#define RDECIDE 1000 /* interval of random decisions */
+#define FRESTART 110 /* restart increase factor in percent */
+#define FREDUCE 110 /* reduce increase factor in percent */
+#define FREDADJ 121 /* reduce increase adjustment factor */
+#define MAXCILS 10 /* maximal number of unrecycled internals */
+#define FFLIPPED 10000 /* flipped reduce factor */
+#define FFLIPPEDPREC 10000000/* flipped reduce factor precision */
+#define INTERRUPTLIM 1024 /* check interrupt after that many decisions */
+
+#ifndef TRACE
+#define NO_BINARY_CLAUSES /* store binary clauses more compactly */
+#endif
+
+/* For debugging purposes you may want to define 'LOGGING', which actually
+ * can be enforced by using './configure.sh --log'.
+ */
+#ifdef LOGGING
+#define LOG(code) do { code; } while (0)
+#else
+#define LOG(code) do { } while (0)
+#endif
+#define NOLOG(code) do { } while (0) /* log exception */
+#define ONLYLOG(code) do { code; } while (0) /* force logging */
+
+#define FALSE ((Val)-1)
+#define UNDEF ((Val)0)
+#define TRUE ((Val)1)
+
+#define COMPACT_TRACECHECK_TRACE_FMT 0
+#define EXTENDED_TRACECHECK_TRACE_FMT 1
+#define RUP_TRACE_FMT 2
+
+#define NEWN(p,n) do { (p) = new (ps, sizeof (*(p)) * (n)); } while (0)
+#define CLRN(p,n) do { memset ((p), 0, sizeof (*(p)) * (n)); } while (0)
+#define CLR(p) CLRN(p,1)
+#define DELETEN(p,n) \
+ do { delete (ps, p, sizeof (*(p)) * (n)); (p) = 0; } while (0)
+
+#define RESIZEN(p,old_num,new_num) \
+ do { \
+ size_t old_size = sizeof (*(p)) * (old_num); \
+ size_t new_size = sizeof (*(p)) * (new_num); \
+ (p) = resize (ps, (p), old_size, new_size) ; \
+ } while (0)
+
+#define ENLARGE(start,head,end) \
+ do { \
+ unsigned old_num = (ptrdiff_t)((end) - (start)); \
+ size_t new_num = old_num ? (2 * old_num) : 1; \
+ unsigned count = (head) - (start); \
+ assert ((start) <= (end)); \
+ RESIZEN((start),old_num,new_num); \
+ (head) = (start) + count; \
+ (end) = (start) + new_num; \
+ } while (0)
+
+#define NOTLIT(l) (ps->lits + (1 ^ ((l) - ps->lits)))
+
+#define LIT2IDX(l) ((ptrdiff_t)((l) - ps->lits) / 2)
+#define LIT2IMPLS(l) (ps->impls + (ptrdiff_t)((l) - ps->lits))
+#define LIT2INT(l) ((int)(LIT2SGN(l) * LIT2IDX(l)))
+#define LIT2SGN(l) (((ptrdiff_t)((l) - ps->lits) & 1) ? -1 : 1)
+#define LIT2VAR(l) (ps->vars + LIT2IDX(l))
+#define LIT2HTPS(l) (ps->htps + (ptrdiff_t)((l) - ps->lits))
+#define LIT2JWH(l) (ps->jwh + ((l) - ps->lits))
+
+#ifndef NDSC
+#define LIT2DHTPS(l) (ps->dhtps + (ptrdiff_t)((l) - ps->lits))
+#endif
+
+#ifdef NO_BINARY_CLAUSES
+typedef uintptr_t Wrd;
+#define ISLITREASON(C) (1&(Wrd)C)
+#define LIT2REASON(L) \
+ (assert (L->val==TRUE), ((Cls*)(1 + (2*(L - ps->lits)))))
+#define REASON2LIT(C) ((Lit*)(ps->lits + ((Wrd)C)/2))
+#endif
+
+#define ENDOFCLS(c) ((void*)((Lit**)(c)->lits + (c)->size))
+
+#define SOC ((ps->oclauses == ps->ohead) ? ps->lclauses : ps->oclauses)
+#define EOC (ps->lhead)
+#define NXC(p) (((p) + 1 == ps->ohead) ? ps->lclauses : (p) + 1)
+
+#define OIDX2IDX(idx) (2 * ((idx) + 1))
+#define LIDX2IDX(idx) (2 * (idx) + 1)
+
+#define ISLIDX(idx) ((idx)&1)
+
+#define IDX2OIDX(idx) (assert(!ISLIDX(idx)), (idx)/2 - 1)
+#define IDX2LIDX(idx) (assert(ISLIDX(idx)), (idx)/2)
+
+#define EXPORTIDX(idx) \
+ ((ISLIDX(idx) ? (IDX2LIDX (idx) + (ps->ohead - ps->oclauses)) : IDX2OIDX(idx)) + 1)
+
+#define IDX2CLS(i) \
+ (assert(i), (ISLIDX(i) ? ps->lclauses : ps->oclauses)[(i)/2 - !ISLIDX(i)])
+
+#define IDX2ZHN(i) (assert(i), (ISLIDX(i) ? ps->zhains[(i)/2] : 0))
+
+#define CLS2TRD(c) (((Trd*)(c)) - 1)
+#define CLS2IDX(c) ((((Trd*)(c)) - 1)->idx)
+
+#define CLS2ACT(c) \
+ ((Act*)((assert((c)->learned)),assert((c)->size>2),ENDOFCLS(c)))
+
+#define VAR2LIT(v) (ps->lits + 2 * ((v) - ps->vars))
+#define VAR2RNK(v) (ps->rnks + ((v) - ps->vars))
+
+#define RNK2LIT(r) (ps->lits + 2 * ((r) - ps->rnks))
+#define RNK2VAR(r) (ps->vars + ((r) - ps->rnks))
+
+#define BLK_FILL_BYTES 8
+#define SIZE_OF_BLK (sizeof (Blk) - BLK_FILL_BYTES)
+
+#define PTR2BLK(void_ptr) \
+ ((void_ptr) ? (Blk*)(((char*)(void_ptr)) - SIZE_OF_BLK) : 0)
+
+#define AVERAGE(a,b) ((b) ? (((double)a) / (double)(b)) : 0.0)
+#define PERCENT(a,b) (100.0 * AVERAGE(a,b))
+
+#ifndef RCODE
+#define ABORT(msg) \
+ do { \
+ fputs ("*** picosat: " msg "\n", stderr); \
+ abort (); \
+ } while (0)
+#else
+#define ABORT(msg) \
+ do { \
+ Rf_error (msg); \
+ } while (0)
+#endif
+
+#define ABORTIF(cond,msg) \
+ do { \
+ if (!(cond)) break; \
+ ABORT (msg); \
+ } while (0)
+
+#define ZEROFLT (0x00000000u)
+#define EPSFLT (0x00000001u)
+#define INFFLT (0xffffffffu)
+
+#define FLTCARRY (1u << 25)
+#define FLTMSB (1u << 24)
+#define FLTMAXMANTISSA (FLTMSB - 1)
+
+#define FLTMANTISSA(d) ((d) & FLTMAXMANTISSA)
+#define FLTEXPONENT(d) ((int)((d) >> 24) - 128)
+
+#define FLTMINEXPONENT (-128)
+#define FLTMAXEXPONENT (127)
+
+#define CMPSWAPFLT(a,b) \
+ do { \
+ Flt tmp; \
+ if (((a) < (b))) \
+ { \
+ tmp = (a); \
+ (a) = (b); \
+ (b) = tmp; \
+ } \
+ } while (0)
+
+#define UNPACKFLT(u,m,e) \
+ do { \
+ (m) = FLTMANTISSA(u); \
+ (e) = FLTEXPONENT(u); \
+ (m) |= FLTMSB; \
+ } while (0)
+
+#define INSERTION_SORT_LIMIT 10
+
+#define SORTING_SWAP(T,p,q) \
+do { \
+ T tmp = *(q); \
+ *(q) = *(p); \
+ *(p) = tmp; \
+} while (0)
+
+#define SORTING_CMP_SWAP(T,cmp,p,q) \
+do { \
+ if ((cmp) (ps, *(p), *(q)) > 0) \
+ SORTING_SWAP (T, p, q); \
+} while(0)
+
+#define QUICKSORT_PARTITION(T,cmp,a,l,r) \
+do { \
+ T pivot; \
+ int j; \
+ i = (l) - 1; /* result in 'i' */ \
+ j = (r); \
+ pivot = (a)[j]; \
+ for (;;) \
+ { \
+ while ((cmp) (ps, (a)[++i], pivot) < 0) \
+ ; \
+ while ((cmp) (ps, pivot, (a)[--j]) < 0) \
+ if (j == (l)) \
+ break; \
+ if (i >= j) \
+ break; \
+ SORTING_SWAP (T, (a) + i, (a) + j); \
+ } \
+ SORTING_SWAP (T, (a) + i, (a) + (r)); \
+} while(0)
+
+#define QUICKSORT(T,cmp,a,n) \
+do { \
+ int l = 0, r = (n) - 1, m, ll, rr, i; \
+ assert (ps->ihead == ps->indices); \
+ if (r - l <= INSERTION_SORT_LIMIT) \
+ break; \
+ for (;;) \
+ { \
+ m = (l + r) / 2; \
+ SORTING_SWAP (T, (a) + m, (a) + r - 1); \
+ SORTING_CMP_SWAP (T, cmp, (a) + l, (a) + r - 1); \
+ SORTING_CMP_SWAP (T, cmp, (a) + l, (a) + r); \
+ SORTING_CMP_SWAP (T, cmp, (a) + r - 1, (a) + r); \
+ QUICKSORT_PARTITION (T, cmp, (a), l + 1, r - 1); \
+ if (i - l < r - i) \
+ { \
+ ll = i + 1; \
+ rr = r; \
+ r = i - 1; \
+ } \
+ else \
+ { \
+ ll = l; \
+ rr = i - 1; \
+ l = i + 1; \
+ } \
+ if (r - l > INSERTION_SORT_LIMIT) \
+ { \
+ assert (rr - ll > INSERTION_SORT_LIMIT); \
+ if (ps->ihead == ps->eoi) \
+ ENLARGE (ps->indices, ps->ihead, ps->eoi); \
+ *ps->ihead++ = ll; \
+ if (ps->ihead == ps->eoi) \
+ ENLARGE (ps->indices, ps->ihead, ps->eoi); \
+ *ps->ihead++ = rr; \
+ } \
+ else if (rr - ll > INSERTION_SORT_LIMIT) \
+ { \
+ l = ll; \
+ r = rr; \
+ } \
+ else if (ps->ihead > ps->indices) \
+ { \
+ r = *--ps->ihead; \
+ l = *--ps->ihead; \
+ } \
+ else \
+ break; \
+ } \
+} while (0)
+
+#define INSERTION_SORT(T,cmp,a,n) \
+do { \
+ T pivot; \
+ int l = 0, r = (n) - 1, i, j; \
+ for (i = r; i > l; i--) \
+ SORTING_CMP_SWAP (T, cmp, (a) + i - 1, (a) + i); \
+ for (i = l + 2; i <= r; i++) \
+ { \
+ j = i; \
+ pivot = (a)[i]; \
+ while ((cmp) (ps, pivot, (a)[j - 1]) < 0) \
+ { \
+ (a)[j] = (a)[j - 1]; \
+ j--; \
+ } \
+ (a)[j] = pivot; \
+ } \
+} while (0)
+
+#ifdef NDEBUG
+#define CHECK_SORTED(cmp,a,n) do { } while(0)
+#else
+#define CHECK_SORTED(cmp,a,n) \
+do { \
+ int i; \
+ for (i = 0; i < (n) - 1; i++) \
+ assert ((cmp) (ps, (a)[i], (a)[i + 1]) <= 0); \
+} while(0)
+#endif
+
+#define SORT(T,cmp,a,n) \
+do { \
+ T * aa = (a); \
+ int nn = (n); \
+ QUICKSORT (T, cmp, aa, nn); \
+ INSERTION_SORT (T, cmp, aa, nn); \
+ assert (ps->ihead == ps->indices); \
+ CHECK_SORTED (cmp, aa, nn); \
+} while (0)
+
+#define WRDSZ (sizeof (long) * 8)
+
+#ifdef RCODE
+#define fprintf(...) do { } while (0)
+#define vfprintf(...) do { } while (0)
+#define fputs(...) do { } while (0)
+#define fputc(...) do { } while (0)
+#endif
+
+typedef unsigned Flt; /* 32 bit deterministic soft float */
+typedef Flt Act; /* clause and variable activity */
+typedef struct Blk Blk; /* allocated memory block */
+typedef struct Cls Cls; /* clause */
+typedef struct Lit Lit; /* literal */
+typedef struct Rnk Rnk; /* variable to score mapping */
+typedef signed char Val; /* TRUE, UNDEF, FALSE */
+typedef struct Var Var; /* variable */
+#ifdef TRACE
+typedef struct Trd Trd; /* trace data for clauses */
+typedef struct Zhn Zhn; /* compressed chain (=zain) data */
+typedef unsigned char Znt; /* compressed antecedent data */
+#endif
+
+#ifdef NO_BINARY_CLAUSES
+typedef struct Ltk Ltk;
+
+struct Ltk
+{
+ Lit ** start;
+ unsigned count : WRDSZ == 32 ? 27 : 32;
+ unsigned ldsize : WRDSZ == 32 ? 5 : 32;
+};
+#endif
+
+struct Lit
+{
+ Val val;
+};
+
+struct Var
+{
+ unsigned mark : 1; /*bit 1*/
+ unsigned resolved : 1; /*bit 2*/
+ unsigned phase : 1; /*bit 3*/
+ unsigned assigned : 1; /*bit 4*/
+ unsigned used : 1; /*bit 5*/
+ unsigned failed : 1; /*bit 6*/
+ unsigned internal : 1; /*bit 7*/
+ unsigned usedefphase : 1; /*bit 8*/
+ unsigned defphase : 1; /*bit 9*/
+ unsigned msspos : 1; /*bit 10*/
+ unsigned mssneg : 1; /*bit 11*/
+ unsigned humuspos : 1; /*bit 12*/
+ unsigned humusneg : 1; /*bit 13*/
+ unsigned partial : 1; /*bit 14*/
+#ifdef TRACE
+ unsigned core : 1; /*bit 15*/
+#endif
+ unsigned level;
+ Cls *reason;
+#ifndef NADC
+ Lit ** inado;
+ Lit ** ado;
+ Lit *** adotabpos;
+#endif
+};
+
+struct Rnk
+{
+ Act score;
+ unsigned pos : 30; /* 0 iff not on heap */
+ unsigned moreimportant : 1;
+ unsigned lessimportant : 1;
+};
+
+struct Cls
+{
+ unsigned size;
+
+ unsigned collect:1; /* bit 1 */
+ unsigned learned:1; /* bit 2 */
+ unsigned locked:1; /* bit 3 */
+ unsigned used:1; /* bit 4 */
+#ifndef NDEBUG
+ unsigned connected:1; /* bit 5 */
+#endif
+#ifdef TRACE
+ unsigned collected:1; /* bit 6 */
+ unsigned core:1; /* bit 7 */
+#endif
+
+#define LDMAXGLUE 25 /* 32 - 7 */
+#define MAXGLUE ((1<<LDMAXGLUE)-1)
+
+ unsigned glue:LDMAXGLUE;
+
+ Cls *next[2];
+ Lit *lits[2];
+};
+
+#ifdef TRACE
+struct Zhn
+{
+ unsigned ref:31;
+ unsigned core:1;
+ Znt * liz;
+ Znt znt[0];
+};
+
+struct Trd
+{
+ unsigned idx;
+ Cls cls[0];
+};
+#endif
+
+struct Blk
+{
+#ifndef NDEBUG
+ union
+ {
+ size_t size; /* this is what we really use */
+ void *as_two_ptrs[2]; /* 2 * sizeof (void*) alignment of data */
+ }
+ header;
+#endif
+ char data[BLK_FILL_BYTES];
+};
+
+enum State
+{
+ RESET = 0,
+ READY = 1,
+ SAT = 2,
+ UNSAT = 3,
+ UNKNOWN = 4,
+};
+
+enum Phase
+{
+ POSPHASE,
+ NEGPHASE,
+ JWLPHASE,
+ RNDPHASE,
+};
+
+struct PicoSAT
+{
+ enum State state;
+ enum Phase defaultphase;
+ int last_sat_call_result;
+
+ FILE *out;
+ char * prefix;
+ int verbosity;
+ int plain;
+ unsigned LEVEL;
+ unsigned max_var;
+ unsigned size_vars;
+
+ Lit *lits;
+ Var *vars;
+ Rnk *rnks;
+ Flt *jwh;
+ Cls **htps;
+#ifndef NDSC
+ Cls **dhtps;
+#endif
+#ifdef NO_BINARY_CLAUSES
+ Ltk *impls;
+ Cls impl, cimpl;
+ int implvalid, cimplvalid;
+#else
+ Cls **impls;
+#endif
+ Lit **trail, **thead, **eot, **ttail, ** ttail2;
+#ifndef NADC
+ Lit **ttailado;
+#endif
+ unsigned adecidelevel;
+ Lit **als, **alshead, **alstail, **eoals;
+ Lit **CLS, **clshead, **eocls;
+ int *rils, *rilshead, *eorils;
+ int *cils, *cilshead, *eocils;
+ int *fals, *falshead, *eofals;
+ int *mass, szmass;
+ int *mssass, szmssass;
+ int *mcsass, nmcsass, szmcsass;
+ int *humus, szhumus;
+ Lit *failed_assumption;
+ int extracted_all_failed_assumptions;
+ Rnk **heap, **hhead, **eoh;
+ Cls **oclauses, **ohead, **eoo; /* original clauses */
+ Cls **lclauses, **lhead, ** EOL; /* learned clauses */
+ int * soclauses, * sohead, * eoso; /* saved original clauses */
+ int saveorig;
+ int partial;
+#ifdef TRACE
+ int trace;
+ Zhn **zhains, **zhead, **eoz;
+ int ocore;
+#endif
+ FILE * rup;
+ int rupstarted;
+ int rupvariables;
+ int rupclauses;
+ Cls *mtcls;
+ Cls *conflict;
+ Lit **added, **ahead, **eoa;
+ Var **marked, **mhead, **eom;
+ Var **dfs, **dhead, **eod;
+ Cls **resolved, **rhead, **eor;
+ unsigned char *levels, *levelshead, *eolevels;
+ unsigned *dused, *dusedhead, *eodused;
+ unsigned char *buffer, *bhead, *eob;
+ Act vinc, lscore, ilvinc, ifvinc;
+#ifdef VISCORES
+ Act fvinc, nvinc;
+#endif
+ Act cinc, lcinc, ilcinc, fcinc;
+ unsigned srng;
+ size_t current_bytes;
+ size_t max_bytes;
+ size_t recycled;
+ double seconds, flseconds;
+ double entered;
+ unsigned nentered;
+ int measurealltimeinlib;
+ char *rline[2];
+ int szrline, RCOUNT;
+ double levelsum;
+ unsigned iterations;
+ int reports;
+ int lastrheader;
+ unsigned calls;
+ unsigned decisions;
+ unsigned restarts;
+ unsigned simps;
+ unsigned fsimplify;
+ unsigned isimplify;
+ unsigned reductions;
+ unsigned lreduce;
+ unsigned lreduceadjustcnt;
+ unsigned lreduceadjustinc;
+ unsigned lastreduceconflicts;
+ unsigned llocked; /* locked large learned clauses */
+ unsigned lrestart;
+#ifdef NLUBY
+ unsigned drestart;
+ unsigned ddrestart;
+#else
+ unsigned lubycnt;
+ unsigned lubymaxdelta;
+ int waslubymaxdelta;
+#endif
+ unsigned long long lsimplify;
+ unsigned long long propagations;
+ unsigned long long lpropagations;
+ unsigned fixed; /* top level assignments */
+#ifndef NFL
+ unsigned failedlits;
+ unsigned ifailedlits;
+ unsigned efailedlits;
+ unsigned flcalls;
+#ifdef STATS
+ unsigned flrounds;
+ unsigned long long flprops;
+ unsigned long long floopsed, fltried, flskipped;
+#endif
+ unsigned long long fllimit;
+ int simplifying;
+ Lit ** saved;
+ unsigned saved_size;
+#endif
+ unsigned conflicts;
+ unsigned contexts;
+ unsigned internals;
+ unsigned noclauses; /* current number large original clauses */
+ unsigned nlclauses; /* current number large learned clauses */
+ unsigned olits; /* current literals in large original clauses */
+ unsigned llits; /* current literals in large learned clauses */
+ unsigned oadded; /* added original clauses */
+ unsigned ladded; /* added learned clauses */
+ unsigned loadded; /* added original large clauses */
+ unsigned lladded; /* added learned large clauses */
+ unsigned addedclauses; /* oadded + ladded */
+ unsigned vused; /* used variables */
+ unsigned llitsadded; /* added learned literals */
+ unsigned long long visits;
+#ifdef STATS
+ unsigned loused; /* used large original clauses */
+ unsigned llused; /* used large learned clauses */
+ unsigned long long bvisits;
+ unsigned long long tvisits;
+ unsigned long long lvisits;
+ unsigned long long othertrue;
+ unsigned long long othertrue2;
+ unsigned long long othertruel;
+ unsigned long long othertrue2u;
+ unsigned long long othertruelu;
+ unsigned long long ltraversals;
+ unsigned long long traversals;
+#ifdef TRACE
+ unsigned long long antecedents;
+#endif
+ unsigned uips;
+ unsigned znts;
+ unsigned assumptions;
+ unsigned rdecisions;
+ unsigned sdecisions;
+ size_t srecycled;
+ size_t rrecycled;
+ unsigned long long derefs;
+#endif
+ unsigned minimizedllits;
+ unsigned nonminimizedllits;
+#ifndef NADC
+ Lit *** ados, *** hados, *** eados;
+ Lit *** adotab;
+ unsigned nadotab;
+ unsigned szadotab;
+ Cls * adoconflict;
+ unsigned adoconflicts;
+ unsigned adoconflictlimit;
+ int addingtoado;
+ int adodisabled;
+#endif
+ unsigned long long flips;
+#ifdef STATS
+ unsigned long long FORCED;
+ unsigned long long assignments;
+ unsigned inclreduces;
+ unsigned staticphasedecisions;
+ unsigned skippedrestarts;
+#endif
+ int * indices, * ihead, *eoi;
+ unsigned sdflips;
+
+ unsigned long long saved_flips;
+ unsigned saved_max_var;
+ unsigned min_flipped;
+
+ void * emgr;
+ picosat_malloc enew;
+ picosat_realloc eresize;
+ picosat_free edelete;
+
+ struct {
+ void * state;
+ int (*function) (void *);
+ } interrupt;
+
+#ifdef VISCORES
+ FILE * fviscores;
+#endif
+};
+
+typedef PicoSAT PS;
+
+static Flt
+packflt (unsigned m, int e)
+{
+ Flt res;
+ assert (m < FLTMSB);
+ assert (FLTMINEXPONENT <= e);
+ assert (e <= FLTMAXEXPONENT);
+ res = m | ((unsigned)(e + 128) << 24);
+ return res;
+}
+
+static Flt
+base2flt (unsigned m, int e)
+{
+ if (!m)
+ return ZEROFLT;
+
+ if (m < FLTMSB)
+ {
+ do
+ {
+ if (e <= FLTMINEXPONENT)
+ return EPSFLT;
+
+ e--;
+ m <<= 1;
+
+ }
+ while (m < FLTMSB);
+ }
+ else
+ {
+ while (m >= FLTCARRY)
+ {
+ if (e >= FLTMAXEXPONENT)
+ return INFFLT;
+
+ e++;
+ m >>= 1;
+ }
+ }
+
+ m &= ~FLTMSB;
+ return packflt (m, e);
+}
+
+static Flt
+addflt (Flt a, Flt b)
+{
+ unsigned ma, mb, delta;
+ int ea, eb;
+
+ CMPSWAPFLT (a, b);
+ if (!b)
+ return a;
+
+ UNPACKFLT (a, ma, ea);
+ UNPACKFLT (b, mb, eb);
+
+ assert (ea >= eb);
+ delta = ea - eb;
+ if (delta < 32) mb >>= delta; else mb = 0;
+ if (!mb)
+ return a;
+
+ ma += mb;
+ if (ma & FLTCARRY)
+ {
+ if (ea == FLTMAXEXPONENT)
+ return INFFLT;
+
+ ea++;
+ ma >>= 1;
+ }
+
+ assert (ma < FLTCARRY);
+ ma &= FLTMAXMANTISSA;
+
+ return packflt (ma, ea);
+}
+
+static Flt
+mulflt (Flt a, Flt b)
+{
+ unsigned ma, mb;
+ unsigned long long accu;
+ int ea, eb;
+
+ CMPSWAPFLT (a, b);
+ if (!b)
+ return ZEROFLT;
+
+ UNPACKFLT (a, ma, ea);
+ UNPACKFLT (b, mb, eb);
+
+ ea += eb;
+ ea += 24;
+ if (ea > FLTMAXEXPONENT)
+ return INFFLT;
+
+ if (ea < FLTMINEXPONENT)
+ return EPSFLT;
+
+ accu = ma;
+ accu *= mb;
+ accu >>= 24;
+
+ if (accu >= FLTCARRY)
+ {
+ if (ea == FLTMAXEXPONENT)
+ return INFFLT;
+
+ ea++;
+ accu >>= 1;
+
+ if (accu >= FLTCARRY)
+ return INFFLT;
+ }
+
+ assert (accu < FLTCARRY);
+ assert (accu & FLTMSB);
+
+ ma = accu;
+ ma &= ~FLTMSB;
+
+ return packflt (ma, ea);
+}
+
+static Flt
+ascii2flt (const char *str)
+{
+ Flt ten = base2flt (10, 0);
+ Flt onetenth = base2flt (26843546, -28);
+ Flt res = ZEROFLT, tmp, base;
+ const char *p = str;
+ int ch;
+
+ ch = *p++;
+
+ if (ch != '.')
+ {
+ if (!isdigit (ch))
+ return INFFLT; /* better abort ? */
+
+ res = base2flt (ch - '0', 0);
+
+ while ((ch = *p++))
+ {
+ if (ch == '.')
+ break;
+
+ if (!isdigit (ch))
+ return INFFLT; /* better abort? */
+
+ res = mulflt (res, ten);
+ tmp = base2flt (ch - '0', 0);
+ res = addflt (res, tmp);
+ }
+ }
+
+ if (ch == '.')
+ {
+ ch = *p++;
+ if (!isdigit (ch))
+ return INFFLT; /* better abort ? */
+
+ base = onetenth;
+ tmp = mulflt (base2flt (ch - '0', 0), base);
+ res = addflt (res, tmp);
+
+ while ((ch = *p++))
+ {
+ if (!isdigit (ch))
+ return INFFLT; /* better abort? */
+
+ base = mulflt (base, onetenth);
+ tmp = mulflt (base2flt (ch - '0', 0), base);
+ res = addflt (res, tmp);
+ }
+ }
+
+ return res;
+}
+
+#if defined(VISCORES)
+
+static double
+flt2double (Flt f)
+{
+ double res;
+ unsigned m;
+ int e, i;
+
+ UNPACKFLT (f, m, e);
+ res = m;
+
+ if (e < 0)
+ {
+ for (i = e; i < 0; i++)
+ res *= 0.5;
+ }
+ else
+ {
+ for (i = 0; i < e; i++)
+ res *= 2.0;
+ }
+
+ return res;
+}
+
+#endif
+
+static int
+log2flt (Flt a)
+{
+ return FLTEXPONENT (a) + 24;
+}
+
+static int
+cmpflt (Flt a, Flt b)
+{
+ if (a < b)
+ return -1;
+
+ if (a > b)
+ return 1;
+
+ return 0;
+}
+
+static void *
+new (PS * ps, size_t size)
+{
+ size_t bytes;
+ Blk *b;
+
+ if (!size)
+ return 0;
+
+ bytes = size + SIZE_OF_BLK;
+
+ if (ps->enew)
+ b = ps->enew (ps->emgr, bytes);
+ else
+ b = malloc (bytes);
+
+ ABORTIF (!b, "out of memory in 'new'");
+#ifndef NDEBUG
+ b->header.size = size;
+#endif
+ ps->current_bytes += size;
+ if (ps->current_bytes > ps->max_bytes)
+ ps->max_bytes = ps->current_bytes;
+ return b->data;
+}
+
+static void
+delete (PS * ps, void *void_ptr, size_t size)
+{
+ size_t bytes;
+ Blk *b;
+
+ if (!void_ptr)
+ {
+ assert (!size);
+ return;
+ }
+
+ assert (size);
+ b = PTR2BLK (void_ptr);
+
+ assert (size <= ps->current_bytes);
+ ps->current_bytes -= size;
+
+ assert (b->header.size == size);
+
+ bytes = size + SIZE_OF_BLK;
+ if (ps->edelete)
+ ps->edelete (ps->emgr, b, bytes);
+ else
+ free (b);
+}
+
+static void *
+resize (PS * ps, void *void_ptr, size_t old_size, size_t new_size)
+{
+ size_t old_bytes, new_bytes;
+ Blk *b;
+
+ b = PTR2BLK (void_ptr);
+
+ assert (old_size <= ps->current_bytes);
+ ps->current_bytes -= old_size;
+
+ if ((old_bytes = old_size))
+ {
+ assert (old_size && b && b->header.size == old_size);
+ old_bytes += SIZE_OF_BLK;
+ }
+ else
+ assert (!b);
+
+ if ((new_bytes = new_size))
+ new_bytes += SIZE_OF_BLK;
+
+ if (ps->eresize)
+ b = ps->eresize (ps->emgr, b, old_bytes, new_bytes);
+ else
+ b = realloc (b, new_bytes);
+
+ if (!new_size)
+ {
+ assert (!b);
+ return 0;
+ }
+
+ ABORTIF (!b, "out of memory in 'resize'");
+#ifndef NDEBUG
+ b->header.size = new_size;
+#endif
+
+ ps->current_bytes += new_size;
+ if (ps->current_bytes > ps->max_bytes)
+ ps->max_bytes = ps->current_bytes;
+
+ return b->data;
+}
+
+static unsigned
+int2unsigned (int l)
+{
+ return (l < 0) ? 1 + 2 * -l : 2 * l;
+}
+
+static Lit *
+int2lit (PS * ps, int l)
+{
+ return ps->lits + int2unsigned (l);
+}
+
+static Lit **
+end_of_lits (Cls * c)
+{
+ return (Lit**)c->lits + c->size;
+}
+
+#if !defined(NDEBUG) || defined(LOGGING)
+
+static void
+dumplits (PS * ps, Lit ** l, Lit ** end)
+{
+ int first;
+ Lit ** p;
+
+ if (l == end)
+ {
+ /* empty clause */
+ }
+ else if (l + 1 == end)
+ {
+ fprintf (ps->out, "%d ", LIT2INT (l[0]));
+ }
+ else
+ {
+ assert (l + 2 <= end);
+ first = (abs (LIT2INT (l[0])) > abs (LIT2INT (l[1])));
+ fprintf (ps->out, "%d ", LIT2INT (l[first]));
+ fprintf (ps->out, "%d ", LIT2INT (l[!first]));
+ for (p = l + 2; p < end; p++)
+ fprintf (ps->out, "%d ", LIT2INT (*p));
+ }
+
+ fputc ('0', ps->out);
+}
+
+static void
+dumpcls (PS * ps, Cls * c)
+{
+ Lit **end;
+
+ if (c)
+ {
+ end = end_of_lits (c);
+ dumplits (ps, c->lits, end);
+#ifdef TRACE
+ if (ps->trace)
+ fprintf (ps->out, " clause(%u)", CLS2IDX (c));
+#endif
+ }
+ else
+ fputs ("DECISION", ps->out);
+}
+
+static void
+dumpclsnl (PS * ps, Cls * c)
+{
+ dumpcls (ps, c);
+ fputc ('\n', ps->out);
+}
+
+void
+dumpcnf (PS * ps)
+{
+ Cls **p, *c;
+
+ for (p = SOC; p != EOC; p = NXC (p))
+ {
+ c = *p;
+
+ if (!c)
+ continue;
+
+#ifdef TRACE
+ if (c->collected)
+ continue;
+#endif
+
+ dumpclsnl (ps, *p);
+ }
+}
+
+#endif
+
+static void
+delete_prefix (PS * ps)
+{
+ if (!ps->prefix)
+ return;
+
+ delete (ps, ps->prefix, strlen (ps->prefix) + 1);
+ ps->prefix = 0;
+}
+
+static void
+new_prefix (PS * ps, const char * str)
+{
+ delete_prefix (ps);
+ assert (str);
+ ps->prefix = new (ps, strlen (str) + 1);
+ strcpy (ps->prefix, str);
+}
+
+static PS *
+init (void * pmgr,
+ picosat_malloc pnew, picosat_realloc presize, picosat_free pdelete)
+{
+ PS * ps;
+
+#if 0
+ int count = 3 - !pnew - !presize - !pdelete;
+
+ ABORTIF (count && !pnew, "API usage: missing 'picosat_set_new'");
+ ABORTIF (count && !presize, "API usage: missing 'picosat_set_resize'");
+ ABORTIF (count && !pdelete, "API usage: missing 'picosat_set_delete'");
+#endif
+
+ ps = pnew ? pnew (pmgr, sizeof *ps) : malloc (sizeof *ps);
+ ABORTIF (!ps, "failed to allocate memory for PicoSAT manager");
+ memset (ps, 0, sizeof *ps);
+
+ ps->emgr = pmgr;
+ ps->enew = pnew;
+ ps->eresize = presize;
+ ps->edelete = pdelete;
+
+ ps->size_vars = 1;
+ ps->state = RESET;
+ ps->defaultphase = JWLPHASE;
+#ifdef TRACE
+ ps->ocore = -1;
+#endif
+ ps->lastrheader = -2;
+#ifndef NADC
+ ps->adoconflictlimit = UINT_MAX;
+#endif
+ ps->min_flipped = UINT_MAX;
+
+ NEWN (ps->lits, 2 * ps->size_vars);
+ NEWN (ps->jwh, 2 * ps->size_vars);
+ NEWN (ps->htps, 2 * ps->size_vars);
+#ifndef NDSC
+ NEWN (ps->dhtps, 2 * ps->size_vars);
+#endif
+ NEWN (ps->impls, 2 * ps->size_vars);
+ NEWN (ps->vars, ps->size_vars);
+ NEWN (ps->rnks, ps->size_vars);
+
+ /* because '0' pos denotes not on heap
+ */
+ ENLARGE (ps->heap, ps->hhead, ps->eoh);
+ ps->hhead = ps->heap + 1;
+
+ ps->vinc = base2flt (1, 0); /* initial var activity */
+ ps->ifvinc = ascii2flt ("1.05"); /* var score rescore factor */
+#ifdef VISCORES
+ ps->fvinc = ascii2flt ("0.9523809"); /* 1/f = 1/1.05 */
+ ps->nvinc = ascii2flt ("0.0476191"); /* 1 - 1/f = 1 - 1/1.05 */
+#endif
+ ps->lscore = base2flt (1, 90); /* var activity rescore limit */
+ ps->ilvinc = base2flt (1, -90); /* inverse of 'lscore' */
+
+ ps->cinc = base2flt (1, 0); /* initial clause activity */
+ ps->fcinc = ascii2flt ("1.001"); /* cls activity rescore factor */
+ ps->lcinc = base2flt (1, 90); /* cls activity rescore limit */
+ ps->ilcinc = base2flt (1, -90); /* inverse of 'ilcinc' */
+
+ ps->lreduceadjustcnt = ps->lreduceadjustinc = 100;
+ ps->lpropagations = ~0ull;
+
+#ifndef RCODE
+ ps->out = stdout;
+#else
+ ps->out = 0;
+#endif
+ new_prefix (ps, "c ");
+ ps->verbosity = 0;
+ ps->plain = 0;
+
+#ifdef NO_BINARY_CLAUSES
+ memset (&ps->impl, 0, sizeof (ps->impl));
+ ps->impl.size = 2;
+
+ memset (&ps->cimpl, 0, sizeof (ps->impl));
+ ps->cimpl.size = 2;
+#endif
+
+#ifdef VISCORES
+ ps->fviscores = popen (
+ "/usr/bin/gnuplot -background black"
+ " -xrm 'gnuplot*textColor:white'"
+ " -xrm 'gnuplot*borderColor:white'"
+ " -xrm 'gnuplot*axisColor:white'"
+ , "w");
+ fprintf (ps->fviscores, "unset key\n");
+ // fprintf (ps->fviscores, "set log y\n");
+ fflush (ps->fviscores);
+ system ("rm -rf /tmp/picosat-viscores");
+ system ("mkdir /tmp/picosat-viscores");
+ system ("mkdir /tmp/picosat-viscores/data");
+#ifdef WRITEGIF
+ system ("mkdir /tmp/picosat-viscores/gif");
+ fprintf (ps->fviscores,
+ "set terminal gif giant animate opt size 1024,768 x000000 xffffff"
+ "\n");
+
+ fprintf (ps->fviscores,
+ "set output \"/tmp/picosat-viscores/gif/animated.gif\"\n");
+#endif
+#endif
+ ps->defaultphase = JWLPHASE;
+ ps->state = READY;
+ ps->last_sat_call_result = 0;
+
+ return ps;
+}
+
+static size_t
+bytes_clause (PS * ps, unsigned size, unsigned learned)
+{
+ size_t res;
+
+ res = sizeof (Cls);
+ res += size * sizeof (Lit *);
+ res -= 2 * sizeof (Lit *);
+
+ if (learned && size > 2)
+ res += sizeof (Act); /* add activity */
+
+#ifdef TRACE
+ if (ps->trace)
+ res += sizeof (Trd); /* add trace data */
+#else
+ (void) ps;
+#endif
+
+ return res;
+}
+
+static Cls *
+new_clause (PS * ps, unsigned size, unsigned learned)
+{
+ size_t bytes;
+ void * tmp;
+#ifdef TRACE
+ Trd *trd;
+#endif
+ Cls *res;
+
+ bytes = bytes_clause (ps, size, learned);
+ tmp = new (ps, bytes);
+
+#ifdef TRACE
+ if (ps->trace)
+ {
+ trd = tmp;
+
+ if (learned)
+ trd->idx = LIDX2IDX (ps->lhead - ps->lclauses);
+ else
+ trd->idx = OIDX2IDX (ps->ohead - ps->oclauses);
+
+ res = trd->cls;
+ }
+ else
+#endif
+ res = tmp;
+
+ res->size = size;
+ res->learned = learned;
+
+ res->collect = 0;
+#ifndef NDEBUG
+ res->connected = 0;
+#endif
+ res->locked = 0;
+ res->used = 0;
+#ifdef TRACE
+ res->core = 0;
+ res->collected = 0;
+#endif
+
+ if (learned && size > 2)
+ {
+ Act * p = CLS2ACT (res);
+ *p = ps->cinc;
+ }
+
+ return res;
+}
+
+static void
+delete_clause (PS * ps, Cls * c)
+{
+ size_t bytes;
+#ifdef TRACE
+ Trd *trd;
+#endif
+
+ bytes = bytes_clause (ps, c->size, c->learned);
+
+#ifdef TRACE
+ if (ps->trace)
+ {
+ trd = CLS2TRD (c);
+ delete (ps, trd, bytes);
+ }
+ else
+#endif
+ delete (ps, c, bytes);
+}
+
+static void
+delete_clauses (PS * ps)
+{
+ Cls **p;
+ for (p = SOC; p != EOC; p = NXC (p))
+ if (*p)
+ delete_clause (ps, *p);
+
+ DELETEN (ps->oclauses, ps->eoo - ps->oclauses);
+ DELETEN (ps->lclauses, ps->EOL - ps->lclauses);
+
+ ps->ohead = ps->eoo = ps->lhead = ps->EOL = 0;
+}
+
+#ifdef TRACE
+
+static void
+delete_zhain (PS * ps, Zhn * zhain)
+{
+ const Znt *p, *znt;
+
+ assert (zhain);
+
+ znt = zhain->znt;
+ for (p = znt; *p; p++)
+ ;
+
+ delete (ps, zhain, sizeof (Zhn) + (p - znt) + 1);
+}
+
+static void
+delete_zhains (PS * ps)
+{
+ Zhn **p, *z;
+ for (p = ps->zhains; p < ps->zhead; p++)
+ if ((z = *p))
+ delete_zhain (ps, z);
+
+ DELETEN (ps->zhains, ps->eoz - ps->zhains);
+ ps->eoz = ps->zhead = 0;
+}
+
+#endif
+
+#ifdef NO_BINARY_CLAUSES
+static void
+lrelease (PS * ps, Ltk * stk)
+{
+ if (stk->start)
+ DELETEN (stk->start, (1 << (stk->ldsize)));
+ memset (stk, 0, sizeof (*stk));
+}
+#endif
+
+#ifndef NADC
+
+static unsigned
+llength (Lit ** a)
+{
+ Lit ** p;
+ for (p = a; *p; p++)
+ ;
+ return p - a;
+}
+
+static void
+resetadoconflict (PS * ps)
+{
+ assert (ps->adoconflict);
+ delete_clause (ps, ps->adoconflict);
+ ps->adoconflict = 0;
+}
+
+static void
+reset_ados (PS * ps)
+{
+ Lit *** p;
+
+ for (p = ps->ados; p < ps->hados; p++)
+ DELETEN (*p, llength (*p) + 1);
+
+ DELETEN (ps->ados, ps->eados - ps->ados);
+ ps->hados = ps->eados = 0;
+
+ DELETEN (ps->adotab, ps->szadotab);
+ ps->szadotab = ps->nadotab = 0;
+
+ if (ps->adoconflict)
+ resetadoconflict (ps);
+
+ ps->adoconflicts = 0;
+ ps->adoconflictlimit = UINT_MAX;
+ ps->adodisabled = 0;
+}
+
+#endif
+
+static void
+reset (PS * ps)
+{
+ ABORTIF (!ps ||
+ ps->state == RESET, "API usage: reset without initialization");
+
+ delete_clauses (ps);
+#ifdef TRACE
+ delete_zhains (ps);
+#endif
+#ifdef NO_BINARY_CLAUSES
+ {
+ unsigned i;
+ for (i = 2; i <= 2 * ps->max_var + 1; i++)
+ lrelease (ps, ps->impls + i);
+ }
+#endif
+#ifndef NADC
+ reset_ados (ps);
+#endif
+#ifndef NFL
+ DELETEN (ps->saved, ps->saved_size);
+#endif
+ DELETEN (ps->htps, 2 * ps->size_vars);
+#ifndef NDSC
+ DELETEN (ps->dhtps, 2 * ps->size_vars);
+#endif
+ DELETEN (ps->impls, 2 * ps->size_vars);
+ DELETEN (ps->lits, 2 * ps->size_vars);
+ DELETEN (ps->jwh, 2 * ps->size_vars);
+ DELETEN (ps->vars, ps->size_vars);
+ DELETEN (ps->rnks, ps->size_vars);
+ DELETEN (ps->trail, ps->eot - ps->trail);
+ DELETEN (ps->heap, ps->eoh - ps->heap);
+ DELETEN (ps->als, ps->eoals - ps->als);
+ DELETEN (ps->CLS, ps->eocls - ps->CLS);
+ DELETEN (ps->rils, ps->eorils - ps->rils);
+ DELETEN (ps->cils, ps->eocils - ps->cils);
+ DELETEN (ps->fals, ps->eofals - ps->fals);
+ DELETEN (ps->mass, ps->szmass);
+ DELETEN (ps->mssass, ps->szmssass);
+ DELETEN (ps->mcsass, ps->szmcsass);
+ DELETEN (ps->humus, ps->szhumus);
+ DELETEN (ps->added, ps->eoa - ps->added);
+ DELETEN (ps->marked, ps->eom - ps->marked);
+ DELETEN (ps->dfs, ps->eod - ps->dfs);
+ DELETEN (ps->resolved, ps->eor - ps->resolved);
+ DELETEN (ps->levels, ps->eolevels - ps->levels);
+ DELETEN (ps->dused, ps->eodused - ps->dused);
+ DELETEN (ps->buffer, ps->eob - ps->buffer);
+ DELETEN (ps->indices, ps->eoi - ps->indices);
+ DELETEN (ps->soclauses, ps->eoso - ps->soclauses);
+ delete_prefix (ps);
+ delete (ps, ps->rline[0], ps->szrline);
+ delete (ps, ps->rline[1], ps->szrline);
+ assert (getenv ("LEAK") || !ps->current_bytes); /* found leak if failing */
+#ifdef VISCORES
+ pclose (ps->fviscores);
+#endif
+ if (ps->edelete)
+ ps->edelete (ps->emgr, ps, sizeof *ps);
+ else
+ free (ps);
+}
+
+inline static void
+tpush (PS * ps, Lit * lit)
+{
+ assert (ps->lits < lit && lit <= ps->lits + 2* ps->max_var + 1);
+ if (ps->thead == ps->eot)
+ {
+ unsigned ttail2count = ps->ttail2 - ps->trail;
+ unsigned ttailcount = ps->ttail - ps->trail;
+#ifndef NADC
+ unsigned ttailadocount = ps->ttailado - ps->trail;
+#endif
+ ENLARGE (ps->trail, ps->thead, ps->eot);
+ ps->ttail = ps->trail + ttailcount;
+ ps->ttail2 = ps->trail + ttail2count;
+#ifndef NADC
+ ps->ttailado = ps->trail + ttailadocount;
+#endif
+ }
+
+ *ps->thead++ = lit;
+}
+
+static void
+assign_reason (PS * ps, Var * v, Cls * reason)
+{
+#if defined(NO_BINARY_CLAUSES) && !defined(NDEBUG)
+ assert (reason != &ps->impl);
+#else
+ (void) ps;
+#endif
+ v->reason = reason;
+}
+
+static void
+assign_phase (PS * ps, Lit * lit)
+{
+ unsigned new_phase, idx;
+ Var * v = LIT2VAR (lit);
+
+#ifndef NFL
+ /* In 'simplifying' mode we only need to keep 'min_flipped' up to date if
+ * we force assignments on the top level. The other assignments will be
+ * undone and thus we can keep the old saved value of the phase.
+ */
+ if (!ps->LEVEL || !ps->simplifying)
+#endif
+ {
+ new_phase = (LIT2SGN (lit) > 0);
+
+ if (v->assigned)
+ {
+ ps->sdflips -= ps->sdflips/FFLIPPED;
+
+ if (new_phase != v->phase)
+ {
+ assert (FFLIPPEDPREC >= FFLIPPED);
+ ps->sdflips += FFLIPPEDPREC / FFLIPPED;
+ ps->flips++;
+
+ idx = LIT2IDX (lit);
+ if (idx < ps->min_flipped)
+ ps->min_flipped = idx;
+
+ NOLOG (fprintf (ps->out,
+ "%sflipped %d\n",
+ ps->prefix, LIT2INT (lit)));
+ }
+ }
+
+ v->phase = new_phase;
+ v->assigned = 1;
+ }
+
+ lit->val = TRUE;
+ NOTLIT (lit)->val = FALSE;
+}
+
+inline static void
+assign (PS * ps, Lit * lit, Cls * reason)
+{
+ Var * v = LIT2VAR (lit);
+ assert (lit->val == UNDEF);
+#ifdef STATS
+ ps->assignments++;
+#endif
+ v->level = ps->LEVEL;
+ assign_phase (ps, lit);
+ assign_reason (ps, v, reason);
+ tpush (ps, lit);
+}
+
+inline static int
+cmp_added (PS * ps, Lit * k, Lit * l)
+{
+ Val a = k->val, b = l->val;
+ Var *u, *v;
+ int res;
+
+ if (a == UNDEF && b != UNDEF)
+ return -1;
+
+ if (a != UNDEF && b == UNDEF)
+ return 1;
+
+ u = LIT2VAR (k);
+ v = LIT2VAR (l);
+
+ if (a != UNDEF)
+ {
+ assert (b != UNDEF);
+ res = v->level - u->level;
+ if (res)
+ return res; /* larger level first */
+ }
+
+ res = cmpflt (VAR2RNK (u)->score, VAR2RNK (v)->score);
+ if (res)
+ return res; /* smaller activity first */
+
+ return u - v; /* smaller index first */
+}
+
+static void
+sorttwolits (Lit ** v)
+{
+ Lit * a = v[0], * b = v[1];
+
+ assert (a != b);
+
+ if (a < b)
+ return;
+
+ v[0] = b;
+ v[1] = a;
+}
+
+inline static void
+sortlits (PS * ps, Lit ** v, unsigned size)
+{
+ if (size == 2)
+ sorttwolits (v); /* same order with and with out 'NO_BINARY_CLAUSES' */
+ else
+ SORT (Lit *, cmp_added, v, size);
+}
+
+#ifdef NO_BINARY_CLAUSES
+static Cls *
+setimpl (PS * ps, Lit * a, Lit * b)
+{
+ assert (!ps->implvalid);
+ assert (ps->impl.size == 2);
+
+ ps->impl.lits[0] = a;
+ ps->impl.lits[1] = b;
+
+ sorttwolits (ps->impl.lits);
+ ps->implvalid = 1;
+
+ return &ps->impl;
+}
+
+static void
+resetimpl (PS * ps)
+{
+ ps->implvalid = 0;
+}
+
+static Cls *
+setcimpl (PS * ps, Lit * a, Lit * b)
+{
+ assert (!ps->cimplvalid);
+ assert (ps->cimpl.size == 2);
+
+ ps->cimpl.lits[0] = a;
+ ps->cimpl.lits[1] = b;
+
+ sorttwolits (ps->cimpl.lits);
+ ps->cimplvalid = 1;
+
+ return &ps->cimpl;
+}
+
+static void
+resetcimpl (PS * ps)
+{
+ assert (ps->cimplvalid);
+ ps->cimplvalid = 0;
+}
+
+#endif
+
+static int
+cmp_ptr (PS * ps, void *l, void *k)
+{
+ (void) ps;
+ return ((char*)l) - (char*)k; /* arbitrarily already reverse */
+}
+
+static int
+cmp_rnk (Rnk * r, Rnk * s)
+{
+ if (!r->moreimportant && s->moreimportant)
+ return -1;
+
+ if (r->moreimportant && !s->moreimportant)
+ return 1;
+
+ if (!r->lessimportant && s->lessimportant)
+ return 1;
+
+ if (r->lessimportant && !s->lessimportant)
+ return -1;
+
+ if (r->score < s->score)
+ return -1;
+
+ if (r->score > s->score)
+ return 1;
+
+ return -cmp_ptr (0, r, s);
+}
+
+static void
+hup (PS * ps, Rnk * v)
+{
+ int upos, vpos;
+ Rnk *u;
+
+#ifndef NFL
+ assert (!ps->simplifying);
+#endif
+
+ vpos = v->pos;
+
+ assert (0 < vpos);
+ assert (vpos < ps->hhead - ps->heap);
+ assert (ps->heap[vpos] == v);
+
+ while (vpos > 1)
+ {
+ upos = vpos / 2;
+
+ u = ps->heap[upos];
+
+ if (cmp_rnk (u, v) > 0)
+ break;
+
+ ps->heap[vpos] = u;
+ u->pos = vpos;
+
+ vpos = upos;
+ }
+
+ ps->heap[vpos] = v;
+ v->pos = vpos;
+}
+
+static Cls *add_simplified_clause (PS *, int);
+
+inline static void
+add_antecedent (PS * ps, Cls * c)
+{
+ assert (c);
+
+#ifdef NO_BINARY_CLAUSES
+ if (ISLITREASON (c))
+ return;
+
+ if (c == &ps->impl)
+ return;
+#elif defined(STATS) && defined(TRACE)
+ ps->antecedents++;
+#endif
+ if (ps->rhead == ps->eor)
+ ENLARGE (ps->resolved, ps->rhead, ps->eor);
+
+ assert (ps->rhead < ps->eor);
+ *ps->rhead++ = c;
+}
+
+#ifdef TRACE
+
+#ifdef NO_BINARY_CLAUSES
+#error "can not combine TRACE and NO_BINARY_CLAUSES"
+#endif
+
+#endif /* TRACE */
+
+static void
+add_lit (PS * ps, Lit * lit)
+{
+ assert (lit);
+
+ if (ps->ahead == ps->eoa)
+ ENLARGE (ps->added, ps->ahead, ps->eoa);
+
+ *ps->ahead++ = lit;
+}
+
+static void
+push_var_as_marked (PS * ps, Var * v)
+{
+ if (ps->mhead == ps->eom)
+ ENLARGE (ps->marked, ps->mhead, ps->eom);
+
+ *ps->mhead++ = v;
+}
+
+static void
+mark_var (PS * ps, Var * v)
+{
+ assert (!v->mark);
+ v->mark = 1;
+ push_var_as_marked (ps, v);
+}
+
+#ifdef NO_BINARY_CLAUSES
+
+static Cls *
+impl2reason (PS * ps, Lit * lit)
+{
+ Lit * other;
+ Cls * res;
+ other = ps->impl.lits[0];
+ if (lit == other)
+ other = ps->impl.lits[1];
+ assert (other->val == FALSE);
+ res = LIT2REASON (NOTLIT (other));
+ resetimpl (ps);
+ return res;
+}
+
+#endif
+
+/* Whenever we have a top level derived unit we really should derive a unit
+ * clause otherwise the resolutions in 'add_simplified_clause' become
+ * incorrect.
+ */
+static Cls *
+resolve_top_level_unit (PS * ps, Lit * lit, Cls * reason)
+{
+ unsigned count_resolved;
+ Lit **p, **eol, *other;
+ Var *u, *v;
+
+ assert (ps->rhead == ps->resolved);
+ assert (ps->ahead == ps->added);
+
+ add_lit (ps, lit);
+ add_antecedent (ps, reason);
+ count_resolved = 1;
+ v = LIT2VAR (lit);
+
+ eol = end_of_lits (reason);
+ for (p = reason->lits; p < eol; p++)
+ {
+ other = *p;
+ u = LIT2VAR (other);
+ if (u == v)
+ continue;
+
+ add_antecedent (ps, u->reason);
+ count_resolved++;
+ }
+
+ /* Some of the literals could be assumptions. If at least one
+ * variable is not an assumption, we should resolve.
+ */
+ if (count_resolved >= 2)
+ {
+#ifdef NO_BINARY_CLAUSES
+ if (reason == &ps->impl)
+ resetimpl (ps);
+#endif
+ reason = add_simplified_clause (ps, 1);
+#ifdef NO_BINARY_CLAUSES
+ if (reason->size == 2)
+ {
+ assert (reason == &ps->impl);
+ reason = impl2reason (ps, lit);
+ }
+#endif
+ assign_reason (ps, v, reason);
+ }
+ else
+ {
+ ps->ahead = ps->added;
+ ps->rhead = ps->resolved;
+ }
+
+ return reason;
+}
+
+static void
+fixvar (PS * ps, Var * v)
+{
+ Rnk * r;
+
+ assert (VAR2LIT (v) != UNDEF);
+ assert (!v->level);
+
+ ps->fixed++;
+
+ r = VAR2RNK (v);
+ r->score = INFFLT;
+
+#ifndef NFL
+ if (ps->simplifying)
+ return;
+#endif
+
+ if (!r->pos)
+ return;
+
+ hup (ps, r);
+}
+
+static void
+use_var (PS * ps, Var * v)
+{
+ if (v->used)
+ return;
+
+ v->used = 1;
+ ps->vused++;
+}
+
+static void
+assign_forced (PS * ps, Lit * lit, Cls * reason)
+{
+ Var *v;
+
+ assert (reason);
+ assert (lit->val == UNDEF);
+
+#ifdef STATS
+ ps->FORCED++;
+#endif
+ assign (ps, lit, reason);
+
+#ifdef NO_BINARY_CLAUSES
+ assert (reason != &ps->impl);
+ if (ISLITREASON (reason))
+ {
+ reason = setimpl (ps, lit, NOTLIT (REASON2LIT (reason)));
+ assert (reason);
+ }
+#endif
+ LOG ( fprintf (ps->out,
+ "%sassign %d at level %d by ",
+ ps->prefix, LIT2INT (lit), ps->LEVEL);
+ dumpclsnl (ps, reason));
+
+ v = LIT2VAR (lit);
+ if (!ps->LEVEL)
+ use_var (ps, v);
+
+ if (!ps->LEVEL && reason->size > 1)
+ {
+ reason = resolve_top_level_unit (ps, lit, reason);
+ assert (reason);
+ }
+
+#ifdef NO_BINARY_CLAUSES
+ if (ISLITREASON (reason) || reason == &ps->impl)
+ {
+ /* DO NOTHING */
+ }
+ else
+#endif
+ {
+ assert (!reason->locked);
+ reason->locked = 1;
+ if (reason->learned && reason->size > 2)
+ ps->llocked++;
+ }
+
+#ifdef NO_BINARY_CLAUSES
+ if (reason == &ps->impl)
+ resetimpl (ps);
+#endif
+
+ if (!ps->LEVEL)
+ fixvar (ps, v);
+}
+
+#ifdef NO_BINARY_CLAUSES
+
+static void
+lpush (PS * ps, Lit * lit, Cls * c)
+{
+ int pos = (c->lits[0] == lit);
+ Ltk * s = LIT2IMPLS (lit);
+ unsigned oldsize, newsize;
+
+ assert (c->size == 2);
+
+ if (!s->start)
+ {
+ assert (!s->count);
+ assert (!s->ldsize);
+ NEWN (s->start, 1);
+ }
+ else
+ {
+ oldsize = (1 << (s->ldsize));
+ assert (s->count <= oldsize);
+ if (s->count == oldsize)
+ {
+ newsize = 2 * oldsize;
+ RESIZEN (s->start, oldsize, newsize);
+ s->ldsize++;
+ }
+ }
+
+ s->start[s->count++] = c->lits[pos];
+}
+
+#endif
+
+static void
+connect_head_tail (PS * ps, Lit * lit, Cls * c)
+{
+ Cls ** s;
+ assert (c->size >= 1);
+ if (c->size == 2)
+ {
+#ifdef NO_BINARY_CLAUSES
+ lpush (ps, lit, c);
+ return;
+#else
+ s = LIT2IMPLS (lit);
+#endif
+ }
+ else
+ s = LIT2HTPS (lit);
+
+ if (c->lits[0] != lit)
+ {
+ assert (c->size >= 2);
+ assert (c->lits[1] == lit);
+ c->next[1] = *s;
+ }
+ else
+ c->next[0] = *s;
+
+ *s = c;
+}
+
+#ifdef TRACE
+static void
+zpush (PS * ps, Zhn * zhain)
+{
+ assert (ps->trace);
+
+ if (ps->zhead == ps->eoz)
+ ENLARGE (ps->zhains, ps->zhead, ps->eoz);
+
+ *ps->zhead++ = zhain;
+}
+
+static int
+cmp_resolved (PS * ps, Cls * c, Cls * d)
+{
+#ifndef NDEBUG
+ assert (ps->trace);
+#else
+ (void) ps;
+#endif
+ return CLS2IDX (c) - CLS2IDX (d);
+}
+
+static void
+bpushc (PS * ps, unsigned char ch)
+{
+ if (ps->bhead == ps->eob)
+ ENLARGE (ps->buffer, ps->bhead, ps->eob);
+
+ *ps->bhead++ = ch;
+}
+
+static void
+bpushu (PS * ps, unsigned u)
+{
+ while (u & ~0x7f)
+ {
+ bpushc (ps, u | 0x80);
+ u >>= 7;
+ }
+
+ bpushc (ps, u);
+}
+
+static void
+bpushd (PS * ps, unsigned prev, unsigned this)
+{
+ unsigned delta;
+ assert (prev < this);
+ delta = this - prev;
+ bpushu (ps, delta);
+}
+
+static void
+add_zhain (PS * ps)
+{
+ unsigned prev, this, count, rcount;
+ Cls **p, *c;
+ Zhn *res;
+
+ assert (ps->trace);
+ assert (ps->bhead == ps->buffer);
+ assert (ps->rhead > ps->resolved);
+
+ rcount = ps->rhead - ps->resolved;
+ SORT (Cls *, cmp_resolved, ps->resolved, rcount);
+
+ prev = 0;
+ for (p = ps->resolved; p < ps->rhead; p++)
+ {
+ c = *p;
+ this = CLS2TRD (c)->idx;
+ bpushd (ps, prev, this);
+ prev = this;
+ }
+ bpushc (ps, 0);
+
+ count = ps->bhead - ps->buffer;
+
+ res = new (ps, sizeof (Zhn) + count);
+ res->core = 0;
+ res->ref = 0;
+ memcpy (res->znt, ps->buffer, count);
+
+ ps->bhead = ps->buffer;
+#ifdef STATS
+ ps->znts += count - 1;
+#endif
+ zpush (ps, res);
+}
+
+#endif
+
+static void
+add_resolved (PS * ps, int learned)
+{
+#if defined(STATS) || defined(TRACE)
+ Cls **p, *c;
+
+ for (p = ps->resolved; p < ps->rhead; p++)
+ {
+ c = *p;
+ if (c->used)
+ continue;
+
+ c->used = 1;
+
+ if (c->size <= 2)
+ continue;
+
+#ifdef STATS
+ if (c->learned)
+ ps->llused++;
+ else
+ ps->loused++;
+#endif
+ }
+#endif
+
+#ifdef TRACE
+ if (learned && ps->trace)
+ add_zhain (ps);
+#else
+ (void) learned;
+#endif
+ ps->rhead = ps->resolved;
+}
+
+static void
+incjwh (PS * ps, Cls * c)
+{
+ Lit **p, *lit, ** eol;
+ Flt * f, inc, sum;
+ unsigned size = 0;
+ Var * v;
+ Val val;
+
+ eol = end_of_lits (c);
+
+ for (p = c->lits; p < eol; p++)
+ {
+ lit = *p;
+ val = lit->val;
+
+ if (val && ps->LEVEL > 0)
+ {
+ v = LIT2VAR (lit);
+ if (v->level > 0)
+ val = UNDEF;
+ }
+
+ if (val == TRUE)
+ return;
+
+ if (val != FALSE)
+ size++;
+ }
+
+ inc = base2flt (1, -size);
+
+ for (p = c->lits; p < eol; p++)
+ {
+ lit = *p;
+ f = LIT2JWH (lit);
+ sum = addflt (*f, inc);
+ *f = sum;
+ }
+}
+
+static void
+write_rup_header (PS * ps, FILE * file)
+{
+ char line[80];
+ int i;
+
+ sprintf (line, "%%RUPD32 %u %u", ps->rupvariables, ps->rupclauses);
+
+ fputs (line, file);
+ for (i = 255 - strlen (line); i >= 0; i--)
+ fputc (' ', file);
+
+ fputc ('\n', file);
+ fflush (file);
+}
+
+static Cls *
+add_simplified_clause (PS * ps, int learned)
+{
+ unsigned num_true, num_undef, num_false, size, count_resolved;
+ Lit **p, **q, *lit, ** end;
+ unsigned litlevel, glue;
+ Cls *res, * reason;
+ int reentered;
+ Val val;
+ Var *v;
+#if !defined(NDEBUG) && defined(TRACE)
+ unsigned idx;
+#endif
+
+ reentered = 0;
+
+REENTER:
+
+ size = ps->ahead - ps->added;
+
+ add_resolved (ps, learned);
+
+ if (learned)
+ {
+ ps->ladded++;
+ ps->llitsadded += size;
+ if (size > 2)
+ {
+ ps->lladded++;
+ ps->nlclauses++;
+ ps->llits += size;
+ }
+ }
+ else
+ {
+ ps->oadded++;
+ if (size > 2)
+ {
+ ps->loadded++;
+ ps->noclauses++;
+ ps->olits += size;
+ }
+ }
+
+ ps->addedclauses++;
+ assert (ps->addedclauses == ps->ladded + ps->oadded);
+
+#ifdef NO_BINARY_CLAUSES
+ if (size == 2)
+ res = setimpl (ps, ps->added[0], ps->added[1]);
+ else
+#endif
+ {
+ sortlits (ps, ps->added, size);
+
+ if (learned)
+ {
+ if (ps->lhead == ps->EOL)
+ {
+ ENLARGE (ps->lclauses, ps->lhead, ps->EOL);
+
+ /* A very difficult to find bug, which only occurs if the
+ * learned clauses stack is immediately allocated before the
+ * original clauses stack without padding. In this case, we
+ * have 'SOC == EOC', which terminates all loops using the
+ * idiom 'for (p = SOC; p != EOC; p = NXC(p))' immediately.
+ * Unfortunately this occurred in 'fix_clause_lits' after
+ * using a recent version of the memory allocator of 'Google'
+ * perftools in the context of one large benchmark for
+ * our SMT solver 'Boolector'.
+ */
+ if (ps->EOL == ps->oclauses)
+ ENLARGE (ps->lclauses, ps->lhead, ps->EOL);
+ }
+
+#if !defined(NDEBUG) && defined(TRACE)
+ idx = LIDX2IDX (ps->lhead - ps->lclauses);
+#endif
+ }
+ else
+ {
+ if (ps->ohead == ps->eoo)
+ {
+ ENLARGE (ps->oclauses, ps->ohead, ps->eoo);
+ if (ps->EOL == ps->oclauses)
+ ENLARGE (ps->oclauses, ps->ohead, ps->eoo); /* ditto */
+ }
+
+#if !defined(NDEBUG) && defined(TRACE)
+ idx = OIDX2IDX (ps->ohead - ps->oclauses);
+#endif
+ }
+
+ assert (ps->EOL != ps->oclauses); /* ditto */
+
+ res = new_clause (ps, size, learned);
+
+ glue = 0;
+
+ if (learned)
+ {
+ assert (ps->dusedhead == ps->dused);
+
+ for (p = ps->added; p < ps->ahead; p++)
+ {
+ lit = *p;
+ if (lit->val)
+ {
+ litlevel = LIT2VAR (lit)->level;
+ assert (litlevel <= ps->LEVEL);
+ while (ps->levels + litlevel >= ps->levelshead)
+ {
+ if (ps->levelshead >= ps->eolevels)
+ ENLARGE (ps->levels, ps->levelshead, ps->eolevels);
+ assert (ps->levelshead < ps->eolevels);
+ *ps->levelshead++ = 0;
+ }
+ if (!ps->levels[litlevel])
+ {
+ if (ps->dusedhead >= ps->eodused)
+ ENLARGE (ps->dused, ps->dusedhead, ps->eodused);
+ assert (ps->dusedhead < ps->eodused);
+ *ps->dusedhead++ = litlevel;
+ ps->levels[litlevel] = 1;
+ glue++;
+ }
+ }
+ else
+ glue++;
+ }
+
+ while (ps->dusedhead > ps->dused)
+ {
+ litlevel = *--ps->dusedhead;
+ assert (ps->levels + litlevel < ps->levelshead);
+ assert (ps->levels[litlevel]);
+ ps->levels[litlevel] = 0;
+ }
+ }
+
+ assert (glue <= MAXGLUE);
+ res->glue = glue;
+
+#if !defined(NDEBUG) && defined(TRACE)
+ if (ps->trace)
+ assert (CLS2IDX (res) == idx);
+#endif
+ if (learned)
+ *ps->lhead++ = res;
+ else
+ *ps->ohead++ = res;
+
+#if !defined(NDEBUG) && defined(TRACE)
+ if (ps->trace && learned)
+ assert (ps->zhead - ps->zhains == ps->lhead - ps->lclauses);
+#endif
+ assert (ps->lhead != ps->oclauses); /* ditto */
+ }
+
+ if (learned && ps->rup)
+ {
+ if (!ps->rupstarted)
+ {
+ write_rup_header (ps, ps->rup);
+ ps->rupstarted = 1;
+ }
+ }
+
+ num_true = num_undef = num_false = 0;
+
+ q = res->lits;
+ for (p = ps->added; p < ps->ahead; p++)
+ {
+ lit = *p;
+ *q++ = lit;
+
+ if (learned && ps->rup)
+ fprintf (ps->rup, "%d ", LIT2INT (lit));
+
+ val = lit->val;
+
+ num_true += (val == TRUE);
+ num_undef += (val == UNDEF);
+ num_false += (val == FALSE);
+ }
+ assert (num_false + num_true + num_undef == size);
+
+ if (learned && ps->rup)
+ fputs ("0\n", ps->rup);
+
+ ps->ahead = ps->added; /* reset */
+
+ if (!reentered) // TODO merge
+ if (size > 0)
+ {
+ assert (size <= 2 || !reentered); // TODO remove
+ connect_head_tail (ps, res->lits[0], res);
+ if (size > 1)
+ connect_head_tail (ps, res->lits[1], res);
+ }
+
+ if (size == 0)
+ {
+ if (!ps->mtcls)
+ ps->mtcls = res;
+ }
+
+#ifdef NO_BINARY_CLAUSES
+ if (size != 2)
+#endif
+#ifndef NDEBUG
+ res->connected = 1;
+#endif
+
+ LOG ( fprintf (ps->out, "%s%s ", ps->prefix, learned ? "learned" : "original");
+ dumpclsnl (ps, res));
+
+ /* Shrink clause by resolving it against top level assignments.
+ */
+ if (!ps->LEVEL && num_false > 0)
+ {
+ assert (ps->ahead == ps->added);
+ assert (ps->rhead == ps->resolved);
+
+ count_resolved = 1;
+ add_antecedent (ps, res);
+
+ end = end_of_lits (res);
+ for (p = res->lits; p < end; p++)
+ {
+ lit = *p;
+ v = LIT2VAR (lit);
+ use_var (ps, v);
+
+ if (lit->val == FALSE)
+ {
+ add_antecedent (ps, v->reason);
+ count_resolved++;
+ }
+ else
+ add_lit (ps, lit);
+ }
+
+ assert (count_resolved >= 2);
+
+ learned = 1;
+#ifdef NO_BINARY_CLAUSES
+ if (res == &ps->impl)
+ resetimpl (ps);
+#endif
+ reentered = 1;
+ goto REENTER; /* and return simplified clause */
+ }
+
+ if (!num_true && num_undef == 1) /* unit clause */
+ {
+ lit = 0;
+ for (p = res->lits; p < res->lits + size; p++)
+ {
+ if ((*p)->val == UNDEF)
+ lit = *p;
+
+ v = LIT2VAR (*p);
+ use_var (ps, v);
+ }
+ assert (lit);
+
+ reason = res;
+#ifdef NO_BINARY_CLAUSES
+ if (size == 2)
+ {
+ Lit * other = res->lits[0];
+ if (other == lit)
+ other = res->lits[1];
+
+ assert (other->val == FALSE);
+ reason = LIT2REASON (NOTLIT (other));
+ }
+#endif
+ assign_forced (ps, lit, reason);
+ num_true++;
+ }
+
+ if (num_false == size && !ps->conflict)
+ {
+#ifdef NO_BINARY_CLAUSES
+ if (res == &ps->impl)
+ ps->conflict = setcimpl (ps, res->lits[0], res->lits[1]);
+ else
+#endif
+ ps->conflict = res;
+ }
+
+ if (!learned && !num_true && num_undef)
+ incjwh (ps, res);
+
+#ifdef NO_BINARY_CLAUSES
+ if (res == &ps->impl)
+ resetimpl (ps);
+#endif
+ return res;
+}
+
+static int
+trivial_clause (PS * ps)
+{
+ Lit **p, **q, *prev;
+ Var *v;
+
+ SORT (Lit *, cmp_ptr, ps->added, ps->ahead - ps->added);
+
+ prev = 0;
+ q = ps->added;
+ for (p = q; p < ps->ahead; p++)
+ {
+ Lit *this = *p;
+
+ v = LIT2VAR (this);
+
+ if (prev == this) /* skip repeated literals */
+ continue;
+
+ /* Top level satisfied ?
+ */
+ if (this->val == TRUE && !v->level)
+ return 1;
+
+ if (prev == NOTLIT (this))/* found pair of dual literals */
+ return 1;
+
+ *q++ = prev = this;
+ }
+
+ ps->ahead = q; /* shrink */
+
+ return 0;
+}
+
+static void
+simplify_and_add_original_clause (PS * ps)
+{
+#ifdef NO_BINARY_CLAUSES
+ Cls * c;
+#endif
+ if (trivial_clause (ps))
+ {
+ ps->ahead = ps->added;
+
+ if (ps->ohead == ps->eoo)
+ ENLARGE (ps->oclauses, ps->ohead, ps->eoo);
+
+ *ps->ohead++ = 0;
+
+ ps->addedclauses++;
+ ps->oadded++;
+ }
+ else
+ {
+ if (ps->CLS != ps->clshead)
+ add_lit (ps, NOTLIT (ps->clshead[-1]));
+
+#ifdef NO_BINARY_CLAUSES
+ c =
+#endif
+ add_simplified_clause (ps, 0);
+#ifdef NO_BINARY_CLAUSES
+ if (c == &ps->impl) assert (!ps->implvalid);
+#endif
+ }
+}
+
+#ifndef NADC
+
+static void
+add_ado (PS * ps)
+{
+ unsigned len = ps->ahead - ps->added;
+ Lit ** ado, ** p, ** q, *lit;
+ Var * v, * u;
+
+#ifdef TRACE
+ assert (!ps->trace);
+#endif
+
+ ABORTIF (ps->ados < ps->hados && llength (ps->ados[0]) != len,
+ "internal: non matching all different constraint object lengths");
+
+ if (ps->hados == ps->eados)
+ ENLARGE (ps->ados, ps->hados, ps->eados);
+
+ NEWN (ado, len + 1);
+ *ps->hados++ = ado;
+
+ p = ps->added;
+ q = ado;
+ u = 0;
+ while (p < ps->ahead)
+ {
+ lit = *p++;
+ v = LIT2VAR (lit);
+ ABORTIF (v->inado,
+ "internal: variable in multiple all different objects");
+ v->inado = ado;
+ if (!u && !lit->val)
+ u = v;
+ *q++ = lit;
+ }
+
+ assert (q == ado + len);
+ *q++ = 0;
+
+ /* TODO simply do a conflict test as in propado */
+
+ ABORTIF (!u,
+ "internal: "
+ "adding fully instantiated all different object not implemented yet");
+
+ assert (u);
+ assert (u->inado == ado);
+ assert (!u->ado);
+ u->ado = ado;
+
+ ps->ahead = ps->added;
+}
+
+#endif
+
+static void
+hdown (PS * ps, Rnk * r)
+{
+ unsigned end, rpos, cpos, opos;
+ Rnk *child, *other;
+
+ assert (r->pos > 0);
+ assert (ps->heap[r->pos] == r);
+
+ end = ps->hhead - ps->heap;
+ rpos = r->pos;
+
+ for (;;)
+ {
+ cpos = 2 * rpos;
+ if (cpos >= end)
+ break;
+
+ opos = cpos + 1;
+ child = ps->heap[cpos];
+
+ if (cmp_rnk (r, child) < 0)
+ {
+ if (opos < end)
+ {
+ other = ps->heap[opos];
+
+ if (cmp_rnk (child, other) < 0)
+ {
+ child = other;
+ cpos = opos;
+ }
+ }
+ }
+ else if (opos < end)
+ {
+ child = ps->heap[opos];
+
+ if (cmp_rnk (r, child) >= 0)
+ break;
+
+ cpos = opos;
+ }
+ else
+ break;
+
+ ps->heap[rpos] = child;
+ child->pos = rpos;
+ rpos = cpos;
+ }
+
+ r->pos = rpos;
+ ps->heap[rpos] = r;
+}
+
+static Rnk *
+htop (PS * ps)
+{
+ assert (ps->hhead > ps->heap + 1);
+ return ps->heap[1];
+}
+
+static Rnk *
+hpop (PS * ps)
+{
+ Rnk *res, *last;
+ unsigned end;
+
+ assert (ps->hhead > ps->heap + 1);
+
+ res = ps->heap[1];
+ res->pos = 0;
+
+ end = --ps->hhead - ps->heap;
+ if (end == 1)
+ return res;
+
+ last = ps->heap[end];
+
+ ps->heap[last->pos = 1] = last;
+ hdown (ps, last);
+
+ return res;
+}
+
+inline static void
+hpush (PS * ps, Rnk * r)
+{
+ assert (!r->pos);
+
+ if (ps->hhead == ps->eoh)
+ ENLARGE (ps->heap, ps->hhead, ps->eoh);
+
+ r->pos = ps->hhead++ - ps->heap;
+ ps->heap[r->pos] = r;
+ hup (ps, r);
+}
+
+static void
+fix_trail_lits (PS * ps, long delta)
+{
+ Lit **p;
+ for (p = ps->trail; p < ps->thead; p++)
+ *p += delta;
+}
+
+#ifdef NO_BINARY_CLAUSES
+static void
+fix_impl_lits (PS * ps, long delta)
+{
+ Ltk * s;
+ Lit ** p;
+
+ for (s = ps->impls + 2; s <= ps->impls + 2 * ps->max_var + 1; s++)
+ for (p = s->start; p < s->start + s->count; p++)
+ *p += delta;
+}
+#endif
+
+static void
+fix_clause_lits (PS * ps, long delta)
+{
+ Cls **p, *clause;
+ Lit **q, *lit, **eol;
+
+ for (p = SOC; p != EOC; p = NXC (p))
+ {
+ clause = *p;
+ if (!clause)
+ continue;
+
+ q = clause->lits;
+ eol = end_of_lits (clause);
+ while (q < eol)
+ {
+ assert (q - clause->lits <= (int) clause->size);
+ lit = *q;
+ lit += delta;
+ *q++ = lit;
+ }
+ }
+}
+
+static void
+fix_added_lits (PS * ps, long delta)
+{
+ Lit **p;
+ for (p = ps->added; p < ps->ahead; p++)
+ *p += delta;
+}
+
+static void
+fix_assumed_lits (PS * ps, long delta)
+{
+ Lit **p;
+ for (p = ps->als; p < ps->alshead; p++)
+ *p += delta;
+}
+
+static void
+fix_cls_lits (PS * ps, long delta)
+{
+ Lit **p;
+ for (p = ps->CLS; p < ps->clshead; p++)
+ *p += delta;
+}
+
+static void
+fix_heap_rnks (PS * ps, long delta)
+{
+ Rnk **p;
+
+ for (p = ps->heap + 1; p < ps->hhead; p++)
+ *p += delta;
+}
+
+#ifndef NADC
+
+static void
+fix_ado (long delta, Lit ** ado)
+{
+ Lit ** p;
+ for (p = ado; *p; p++)
+ *p += delta;
+}
+
+static void
+fix_ados (PS * ps, long delta)
+{
+ Lit *** p;
+
+ for (p = ps->ados; p < ps->hados; p++)
+ fix_ado (delta, *p);
+}
+
+#endif
+
+static void
+enlarge (PS * ps, unsigned new_size_vars)
+{
+ long rnks_delta, lits_delta;
+ Lit *old_lits = ps->lits;
+ Rnk *old_rnks = ps->rnks;
+
+ RESIZEN (ps->lits, 2 * ps->size_vars, 2 * new_size_vars);
+ RESIZEN (ps->jwh, 2 * ps->size_vars, 2 * new_size_vars);
+ RESIZEN (ps->htps, 2 * ps->size_vars, 2 * new_size_vars);
+#ifndef NDSC
+ RESIZEN (ps->dhtps, 2 * ps->size_vars, 2 * new_size_vars);
+#endif
+ RESIZEN (ps->impls, 2 * ps->size_vars, 2 * new_size_vars);
+ RESIZEN (ps->vars, ps->size_vars, new_size_vars);
+ RESIZEN (ps->rnks, ps->size_vars, new_size_vars);
+
+ if ((lits_delta = ps->lits - old_lits))
+ {
+ fix_trail_lits (ps, lits_delta);
+ fix_clause_lits (ps, lits_delta);
+ fix_added_lits (ps, lits_delta);
+ fix_assumed_lits (ps, lits_delta);
+ fix_cls_lits (ps, lits_delta);
+#ifdef NO_BINARY_CLAUSES
+ fix_impl_lits (ps, lits_delta);
+#endif
+#ifndef NADC
+ fix_ados (ps, lits_delta);
+#endif
+ }
+
+ if ((rnks_delta = ps->rnks - old_rnks))
+ {
+ fix_heap_rnks (ps, rnks_delta);
+ }
+
+ assert (ps->mhead == ps->marked);
+
+ ps->size_vars = new_size_vars;
+}
+
+static void
+unassign (PS * ps, Lit * lit)
+{
+ Cls *reason;
+ Var *v;
+ Rnk *r;
+
+ assert (lit->val == TRUE);
+
+ LOG ( fprintf (ps->out, "%sunassign %d\n", ps->prefix, LIT2INT (lit)));
+
+ v = LIT2VAR (lit);
+ reason = v->reason;
+
+#ifdef NO_BINARY_CLAUSES
+ assert (reason != &ps->impl);
+ if (ISLITREASON (reason))
+ {
+ /* DO NOTHING */
+ }
+ else
+#endif
+ if (reason)
+ {
+ assert (reason->locked);
+ reason->locked = 0;
+ if (reason->learned && reason->size > 2)
+ {
+ assert (ps->llocked > 0);
+ ps->llocked--;
+ }
+ }
+
+ lit->val = UNDEF;
+ NOTLIT (lit)->val = UNDEF;
+
+ r = VAR2RNK (v);
+ if (!r->pos)
+ hpush (ps, r);
+
+#ifndef NDSC
+ {
+ Cls * p, * next, ** q;
+
+ q = LIT2DHTPS (lit);
+ p = *q;
+ *q = 0;
+
+ while (p)
+ {
+ Lit * other = p->lits[0];
+
+ if (other == lit)
+ {
+ other = p->lits[1];
+ q = p->next + 1;
+ }
+ else
+ {
+ assert (p->lits[1] == lit);
+ q = p->next;
+ }
+
+ next = *q;
+ *q = *LIT2HTPS (other);
+ *LIT2HTPS (other) = p;
+ p = next;
+ }
+ }
+#endif
+
+#ifndef NADC
+ if (v->adotabpos)
+ {
+ assert (ps->nadotab);
+ assert (*v->adotabpos == v->ado);
+
+ *v->adotabpos = 0;
+ v->adotabpos = 0;
+
+ ps->nadotab--;
+ }
+#endif
+}
+
+static Cls *
+var2reason (PS * ps, Var * var)
+{
+ Cls * res = var->reason;
+#ifdef NO_BINARY_CLAUSES
+ Lit * this, * other;
+ if (ISLITREASON (res))
+ {
+ this = VAR2LIT (var);
+ if (this->val == FALSE)
+ this = NOTLIT (this);
+
+ other = REASON2LIT (res);
+ assert (other->val == TRUE);
+ assert (this->val == TRUE);
+ res = setimpl (ps, NOTLIT (other), this);
+ }
+#else
+ (void) ps;
+#endif
+ return res;
+}
+
+static void
+mark_clause_to_be_collected (Cls * c)
+{
+ assert (!c->collect);
+ c->collect = 1;
+}
+
+static void
+undo (PS * ps, unsigned new_level)
+{
+ Lit *lit;
+ Var *v;
+
+ while (ps->thead > ps->trail)
+ {
+ lit = *--ps->thead;
+ v = LIT2VAR (lit);
+ if (v->level == new_level)
+ {
+ ps->thead++; /* fix pre decrement */
+ break;
+ }
+
+ unassign (ps, lit);
+ }
+
+ ps->LEVEL = new_level;
+ ps->ttail = ps->thead;
+ ps->ttail2 = ps->thead;
+#ifndef NADC
+ ps->ttailado = ps->thead;
+#endif
+
+#ifdef NO_BINARY_CLAUSES
+ if (ps->conflict == &ps->cimpl)
+ resetcimpl (ps);
+#endif
+#ifndef NADC
+ if (ps->conflict && ps->conflict == ps->adoconflict)
+ resetadoconflict (ps);
+#endif
+ ps->conflict = ps->mtcls;
+ if (ps->LEVEL < ps->adecidelevel)
+ {
+ assert (ps->als < ps->alshead);
+ ps->adecidelevel = 0;
+ ps->alstail = ps->als;
+ }
+ LOG ( fprintf (ps->out, "%sback to level %u\n", ps->prefix, ps->LEVEL));
+}
+
+#ifndef NDEBUG
+
+static int
+clause_satisfied (Cls * c)
+{
+ Lit **p, **eol, *lit;
+
+ eol = end_of_lits (c);
+ for (p = c->lits; p < eol; p++)
+ {
+ lit = *p;
+ if (lit->val == TRUE)
+ return 1;
+ }
+
+ return 0;
+}
+
+static void
+original_clauses_satisfied (PS * ps)
+{
+ Cls **p, *c;
+
+ for (p = ps->oclauses; p < ps->ohead; p++)
+ {
+ c = *p;
+
+ if (!c)
+ continue;
+
+ if (c->learned)
+ continue;
+
+ assert (clause_satisfied (c));
+ }
+}
+
+static void
+assumptions_satisfied (PS * ps)
+{
+ Lit *lit, ** p;
+
+ for (p = ps->als; p < ps->alshead; p++)
+ {
+ lit = *p;
+ assert (lit->val == TRUE);
+ }
+}
+
+#endif
+
+static void
+sflush (PS * ps)
+{
+ double now = picosat_time_stamp ();
+ double delta = now - ps->entered;
+ delta = (delta < 0) ? 0 : delta;
+ ps->seconds += delta;
+ ps->entered = now;
+}
+
+static double
+mb (PS * ps)
+{
+ return ps->current_bytes / (double) (1 << 20);
+}
+
+static double
+avglevel (PS * ps)
+{
+ return ps->decisions ? ps->levelsum / ps->decisions : 0.0;
+}
+
+static void
+rheader (PS * ps)
+{
+ assert (ps->lastrheader <= ps->reports);
+
+ if (ps->lastrheader == ps->reports)
+ return;
+
+ ps->lastrheader = ps->reports;
+
+ fprintf (ps->out, "%s\n", ps->prefix);
+ fprintf (ps->out, "%s %s\n", ps->prefix, ps->rline[0]);
+ fprintf (ps->out, "%s %s\n", ps->prefix, ps->rline[1]);
+ fprintf (ps->out, "%s\n", ps->prefix);
+}
+
+static unsigned
+dynamic_flips_per_assignment_per_mille (PS * ps)
+{
+ assert (FFLIPPEDPREC >= 1000);
+ return ps->sdflips / (FFLIPPEDPREC / 1000);
+}
+
+#ifdef NLUBY
+
+static int
+high_agility (PS * ps)
+{
+ return dynamic_flips_per_assignment_per_mille (ps) >= 200;
+}
+
+static int
+very_high_agility (PS * ps)
+{
+ return dynamic_flips_per_assignment_per_mille (ps) >= 250;
+}
+
+#else
+
+static int
+medium_agility (PS * ps)
+{
+ return dynamic_flips_per_assignment_per_mille (ps) >= 230;
+}
+
+#endif
+
+static void
+relemdata (PS * ps)
+{
+ char *p;
+ int x;
+
+ if (ps->reports < 0)
+ {
+ /* strip trailing white space
+ */
+ for (x = 0; x <= 1; x++)
+ {
+ p = ps->rline[x] + strlen (ps->rline[x]);
+ while (p-- > ps->rline[x])
+ {
+ if (*p != ' ')
+ break;
+
+ *p = 0;
+ }
+ }
+
+ rheader (ps);
+ }
+ else
+ fputc ('\n', ps->out);
+
+ ps->RCOUNT = 0;
+}
+
+static void
+relemhead (PS * ps, const char * name, int fp, double val)
+{
+ int x, y, len, size;
+ const char *fmt;
+ unsigned tmp, e;
+
+ if (ps->reports < 0)
+ {
+ x = ps->RCOUNT & 1;
+ y = (ps->RCOUNT / 2) * 12 + x * 6;
+
+ if (ps->RCOUNT == 1)
+ sprintf (ps->rline[1], "%6s", "");
+
+ len = strlen (name);
+ while (ps->szrline <= len + y + 1)
+ {
+ size = ps->szrline ? 2 * ps->szrline : 128;
+ ps->rline[0] = resize (ps, ps->rline[0], ps->szrline, size);
+ ps->rline[1] = resize (ps, ps->rline[1], ps->szrline, size);
+ ps->szrline = size;
+ }
+
+ fmt = (len <= 6) ? "%6s%10s" : "%-10s%4s";
+ sprintf (ps->rline[x] + y, fmt, name, "");
+ }
+ else if (val < 0)
+ {
+ assert (fp);
+
+ if (val > -100 && (tmp = val * 10.0 - 0.5) > -1000.0)
+ {
+ fprintf (ps->out, "-%4.1f ", -tmp / 10.0);
+ }
+ else
+ {
+ tmp = -val / 10.0 + 0.5;
+ e = 1;
+ while (tmp >= 100)
+ {
+ tmp /= 10;
+ e++;
+ }
+
+ fprintf (ps->out, "-%2ue%u ", tmp, e);
+ }
+ }
+ else
+ {
+ if (fp && val < 1000 && (tmp = val * 10.0 + 0.5) < 10000)
+ {
+ fprintf (ps->out, "%5.1f ", tmp / 10.0);
+ }
+ else if (!fp && (tmp = val) < 100000)
+ {
+ fprintf (ps->out, "%5u ", tmp);
+ }
+ else
+ {
+ tmp = val / 10.0 + 0.5;
+ e = 1;
+
+ while (tmp >= 1000)
+ {
+ tmp /= 10;
+ e++;
+ }
+
+ fprintf (ps->out, "%3ue%u ", tmp, e);
+ }
+ }
+
+ ps->RCOUNT++;
+}
+
+inline static void
+relem (PS * ps, const char *name, int fp, double val)
+{
+ if (name)
+ relemhead (ps, name, fp, val);
+ else
+ relemdata (ps);
+}
+
+static unsigned
+reduce_limit_on_lclauses (PS * ps)
+{
+ unsigned res = ps->lreduce;
+ res += ps->llocked;
+ return res;
+}
+
+static void
+report (PS * ps, int replevel, char type)
+{
+ int rounds;
+
+#ifdef RCODE
+ (void) type;
+#endif
+
+ if (ps->verbosity < replevel)
+ return;
+
+ sflush (ps);
+
+ if (!ps->reports)
+ ps->reports = -1;
+
+ for (rounds = (ps->reports < 0) ? 2 : 1; rounds; rounds--)
+ {
+ if (ps->reports >= 0)
+ fprintf (ps->out, "%s%c ", ps->prefix, type);
+
+ relem (ps, "seconds", 1, ps->seconds);
+ relem (ps, "level", 1, avglevel (ps));
+ assert (ps->fixed <= ps->max_var);
+ relem (ps, "variables", 0, ps->max_var - ps->fixed);
+ relem (ps, "used", 1, PERCENT (ps->vused, ps->max_var));
+ relem (ps, "original", 0, ps->noclauses);
+ relem (ps, "conflicts", 0, ps->conflicts);
+ // relem (ps, "decisions", 0, ps->decisions);
+ // relem (ps, "conf/dec", 1, PERCENT(ps->conflicts,ps->decisions));
+ // relem (ps, "limit", 0, reduce_limit_on_lclauses (ps));
+ relem (ps, "learned", 0, ps->nlclauses);
+ // relem (ps, "limit", 1, PERCENT (ps->nlclauses, reduce_limit_on_lclauses (ps)));
+ relem (ps, "limit", 0, ps->lreduce);
+#ifdef STATS
+ relem (ps, "learning", 1, PERCENT (ps->llused, ps->lladded));
+#endif
+ relem (ps, "agility", 1, dynamic_flips_per_assignment_per_mille (ps) / 10.0);
+ // relem (ps, "original", 0, ps->noclauses);
+ relem (ps, "MB", 1, mb (ps));
+ // relem (ps, "lladded", 0, ps->lladded);
+ // relem (ps, "llused", 0, ps->llused);
+
+ relem (ps, 0, 0, 0);
+
+ ps->reports++;
+ }
+
+ /* Adapt this to the number of rows in your terminal.
+ */
+ #define ROWS 25
+
+ if (ps->reports % (ROWS - 3) == (ROWS - 4))
+ rheader (ps);
+
+ fflush (ps->out);
+}
+
+static int
+bcp_queue_is_empty (PS * ps)
+{
+ if (ps->ttail != ps->thead)
+ return 0;
+
+ if (ps->ttail2 != ps->thead)
+ return 0;
+
+#ifndef NADC
+ if (ps->ttailado != ps->thead)
+ return 0;
+#endif
+
+ return 1;
+}
+
+static int
+satisfied (PS * ps)
+{
+ assert (!ps->mtcls);
+ assert (!ps->failed_assumption);
+ if (ps->alstail < ps->alshead)
+ return 0;
+ assert (!ps->conflict);
+ assert (bcp_queue_is_empty (ps));
+ return ps->thead == ps->trail + ps->max_var; /* all assigned */
+}
+
+static void
+vrescore (PS * ps)
+{
+ Rnk *p, *eor = ps->rnks + ps->max_var;
+ for (p = ps->rnks + 1; p <= eor; p++)
+ if (p->score != INFFLT)
+ p->score = mulflt (p->score, ps->ilvinc);
+ ps->vinc = mulflt (ps->vinc, ps->ilvinc);;
+#ifdef VISCORES
+ ps->nvinc = mulflt (ps->nvinc, ps->lscore);;
+#endif
+}
+
+static void
+inc_score (PS * ps, Var * v)
+{
+ Flt score;
+ Rnk *r;
+
+#ifndef NFL
+ if (ps->simplifying)
+ return;
+#endif
+
+ if (!v->level)
+ return;
+
+ if (v->internal)
+ return;
+
+ r = VAR2RNK (v);
+ score = r->score;
+
+ assert (score != INFFLT);
+
+ score = addflt (score, ps->vinc);
+ assert (score < INFFLT);
+ r->score = score;
+ if (r->pos > 0)
+ hup (ps, r);
+
+ if (score > ps->lscore)
+ vrescore (ps);
+}
+
+static void
+inc_activity (PS * ps, Cls * c)
+{
+ Act *p;
+
+ if (!c->learned)
+ return;
+
+ if (c->size <= 2)
+ return;
+
+ p = CLS2ACT (c);
+ *p = addflt (*p, ps->cinc);
+}
+
+static unsigned
+hashlevel (unsigned l)
+{
+ return 1u << (l & 31);
+}
+
+static void
+push (PS * ps, Var * v)
+{
+ if (ps->dhead == ps->eod)
+ ENLARGE (ps->dfs, ps->dhead, ps->eod);
+
+ *ps->dhead++ = v;
+}
+
+static Var *
+pop (PS * ps)
+{
+ assert (ps->dfs < ps->dhead);
+ return *--ps->dhead;
+}
+
+static void
+analyze (PS * ps)
+{
+ unsigned open, minlevel, siglevels, l, old, i, orig;
+ Lit *this, *other, **p, **q, **eol;
+ Var *v, *u, **m, *start, *uip;
+ Cls *c;
+
+ assert (ps->conflict);
+
+ assert (ps->ahead == ps->added);
+ assert (ps->mhead == ps->marked);
+ assert (ps->rhead == ps->resolved);
+
+ /* First, search for First UIP variable and mark all resolved variables.
+ * At the same time determine the minimum decision level involved.
+ * Increase activities of resolved variables.
+ */
+ q = ps->thead;
+ open = 0;
+ minlevel = ps->LEVEL;
+ siglevels = 0;
+ uip = 0;
+
+ c = ps->conflict;
+
+ for (;;)
+ {
+ add_antecedent (ps, c);
+ inc_activity (ps, c);
+ eol = end_of_lits (c);
+ for (p = c->lits; p < eol; p++)
+ {
+ other = *p;
+
+ if (other->val == TRUE)
+ continue;
+
+ assert (other->val == FALSE);
+
+ u = LIT2VAR (other);
+ if (u->mark)
+ continue;
+
+ u->mark = 1;
+ inc_score (ps, u);
+ use_var (ps, u);
+
+ if (u->level == ps->LEVEL)
+ {
+ open++;
+ }
+ else
+ {
+ push_var_as_marked (ps, u);
+
+ if (u->level)
+ {
+ /* The statistics counter 'nonminimizedllits' sums up the
+ * number of literals that would be added if only the
+ * 'first UIP' scheme for learned clauses would be used
+ * and no clause minimization.
+ */
+ ps->nonminimizedllits++;
+
+ if (u->level < minlevel)
+ minlevel = u->level;
+
+ siglevels |= hashlevel (u->level);
+ }
+ else
+ {
+ assert (!u->level);
+ assert (u->reason);
+ }
+ }
+ }
+
+ do
+ {
+ if (q == ps->trail)
+ {
+ uip = 0;
+ goto DONE_FIRST_UIP;
+ }
+
+ this = *--q;
+ uip = LIT2VAR (this);
+ }
+ while (!uip->mark);
+
+ uip->mark = 0;
+
+ c = var2reason (ps, uip);
+#ifdef NO_BINARY_CLAUSES
+ if (c == &ps->impl)
+ resetimpl (ps);
+#endif
+ open--;
+ if ((!open && ps->LEVEL) || !c)
+ break;
+
+ assert (c);
+ }
+
+DONE_FIRST_UIP:
+
+ if (uip)
+ {
+ assert (ps->LEVEL);
+ this = VAR2LIT (uip);
+ this += (this->val == TRUE);
+ ps->nonminimizedllits++;
+ ps->minimizedllits++;
+ add_lit (ps, this);
+#ifdef STATS
+ if (uip->reason)
+ ps->uips++;
+#endif
+ }
+ else
+ assert (!ps->LEVEL);
+
+ /* Second, try to mark more intermediate variables, with the goal to
+ * minimize the conflict clause. This is a DFS from already marked
+ * variables backward through the implication graph. It tries to reach
+ * other marked variables. If the search reaches an unmarked decision
+ * variable or a variable assigned below the minimum level of variables in
+ * the first uip learned clause or a level on which no variable has been
+ * marked, then the variable from which the DFS is started is not
+ * redundant. Otherwise the start variable is redundant and will
+ * eventually be removed from the learned clause in step 4. We initially
+ * implemented BFS, but then profiling revelead that this step is a bottle
+ * neck for certain incremental applications. After switching to DFS this
+ * hot spot went away.
+ */
+ orig = ps->mhead - ps->marked;
+ for (i = 0; i < orig; i++)
+ {
+ start = ps->marked[i];
+
+ assert (start->mark);
+ assert (start != uip);
+ assert (start->level < ps->LEVEL);
+
+ if (!start->reason)
+ continue;
+
+ old = ps->mhead - ps->marked;
+ assert (ps->dhead == ps->dfs);
+ push (ps, start);
+
+ while (ps->dhead > ps->dfs)
+ {
+ u = pop (ps);
+ assert (u->mark);
+
+ c = var2reason (ps, u);
+#ifdef NO_BINARY_CLAUSES
+ if (c == &ps->impl)
+ resetimpl (ps);
+#endif
+ if (!c ||
+ ((l = u->level) &&
+ (l < minlevel || ((hashlevel (l) & ~siglevels)))))
+ {
+ while (ps->mhead > ps->marked + old) /* reset all marked */
+ (*--ps->mhead)->mark = 0;
+
+ ps->dhead = ps->dfs; /* and DFS stack */
+ break;
+ }
+
+ eol = end_of_lits (c);
+ for (p = c->lits; p < eol; p++)
+ {
+ v = LIT2VAR (*p);
+ if (v->mark)
+ continue;
+
+ mark_var (ps, v);
+ push (ps, v);
+ }
+ }
+ }
+
+ for (m = ps->marked; m < ps->mhead; m++)
+ {
+ v = *m;
+
+ assert (v->mark);
+ assert (!v->resolved);
+
+ use_var (ps, v);
+
+ c = var2reason (ps, v);
+ if (!c)
+ continue;
+
+#ifdef NO_BINARY_CLAUSES
+ if (c == &ps->impl)
+ resetimpl (ps);
+#endif
+ eol = end_of_lits (c);
+ for (p = c->lits; p < eol; p++)
+ {
+ other = *p;
+
+ u = LIT2VAR (other);
+ if (!u->level)
+ continue;
+
+ if (!u->mark) /* 'MARKTEST' */
+ break;
+ }
+
+ if (p != eol)
+ continue;
+
+ add_antecedent (ps, c);
+ v->resolved = 1;
+ }
+
+ for (m = ps->marked; m < ps->mhead; m++)
+ {
+ v = *m;
+
+ assert (v->mark);
+ v->mark = 0;
+
+ if (v->resolved)
+ {
+ v->resolved = 0;
+ continue;
+ }
+
+ this = VAR2LIT (v);
+ if (this->val == TRUE)
+ this++; /* actually NOTLIT */
+
+ add_lit (ps, this);
+ ps->minimizedllits++;
+ }
+
+ assert (ps->ahead <= ps->eoa);
+ assert (ps->rhead <= ps->eor);
+
+ ps->mhead = ps->marked;
+}
+
+static void
+fanalyze (PS * ps)
+{
+ Lit ** eol, ** p, * lit;
+ Cls * c, * reason;
+ Var * v, * u;
+ int next;
+
+#ifndef RCODE
+ double start = picosat_time_stamp ();
+#endif
+
+ assert (ps->failed_assumption);
+ assert (ps->failed_assumption->val == FALSE);
+
+ v = LIT2VAR (ps->failed_assumption);
+ reason = var2reason (ps, v);
+ if (!reason) return;
+#ifdef NO_BINARY_CLAUSES
+ if (reason == &ps->impl)
+ resetimpl (ps);
+#endif
+
+ eol = end_of_lits (reason);
+ for (p = reason->lits; p != eol; p++)
+ {
+ lit = *p;
+ u = LIT2VAR (lit);
+ if (u == v) continue;
+ if (u->reason) break;
+ }
+ if (p == eol) return;
+
+ assert (ps->ahead == ps->added);
+ assert (ps->mhead == ps->marked);
+ assert (ps->rhead == ps->resolved);
+
+ next = 0;
+ mark_var (ps, v);
+ add_lit (ps, NOTLIT (ps->failed_assumption));
+
+ do
+ {
+ v = ps->marked[next++];
+ use_var (ps, v);
+ if (v->reason)
+ {
+ reason = var2reason (ps, v);
+#ifdef NO_BINARY_CLAUSES
+ if (reason == &ps->impl)
+ resetimpl (ps);
+#endif
+ add_antecedent (ps, reason);
+ eol = end_of_lits (reason);
+ for (p = reason->lits; p != eol; p++)
+ {
+ lit = *p;
+ u = LIT2VAR (lit);
+ if (u == v) continue;
+ if (u->mark) continue;
+ mark_var (ps, u);
+ }
+ }
+ else
+ {
+ lit = VAR2LIT (v);
+ if (lit->val == TRUE) lit = NOTLIT (lit);
+ add_lit (ps, lit);
+ }
+ }
+ while (ps->marked + next < ps->mhead);
+
+ c = add_simplified_clause (ps, 1);
+ v = LIT2VAR (ps->failed_assumption);
+ reason = v->reason;
+#ifdef NO_BINARY_CLAUSES
+ if (!ISLITREASON (reason))
+#endif
+ {
+ assert (reason->locked);
+ reason->locked = 0;
+ if (reason->learned && reason->size > 2)
+ {
+ assert (ps->llocked > 0);
+ ps->llocked--;
+ }
+ }
+
+#ifdef NO_BINARY_CLAUSES
+ if (c == &ps->impl)
+ {
+ c = impl2reason (ps, NOTLIT (ps->failed_assumption));
+ }
+ else
+#endif
+ {
+ assert (c->learned);
+ assert (!c->locked);
+ c->locked = 1;
+ if (c->size > 2)
+ {
+ ps->llocked++;
+ assert (ps->llocked > 0);
+ }
+ }
+
+ v->reason = c;
+
+ while (ps->mhead > ps->marked)
+ (*--ps->mhead)->mark = 0;
+
+ if (ps->verbosity)
+ fprintf (ps->out, "%sfanalyze took %.1f seconds\n",
+ ps->prefix, picosat_time_stamp () - start);
+}
+
+/* Propagate assignment of 'this' to 'FALSE' by visiting all binary clauses in
+ * which 'this' occurs.
+ */
+inline static void
+prop2 (PS * ps, Lit * this)
+{
+#ifdef NO_BINARY_CLAUSES
+ Lit ** l, ** start;
+ Ltk * lstk;
+#else
+ Cls * c, ** p;
+ Cls * next;
+#endif
+ Lit * other;
+ Val tmp;
+
+ assert (this->val == FALSE);
+
+#ifdef NO_BINARY_CLAUSES
+ lstk = LIT2IMPLS (this);
+ start = lstk->start;
+ l = start + lstk->count;
+ while (l != start)
+ {
+ /* The counter 'visits' is the number of clauses that are
+ * visited during propagations of assignments.
+ */
+ ps->visits++;
+#ifdef STATS
+ ps->bvisits++;
+#endif
+ other = *--l;
+ tmp = other->val;
+
+ if (tmp == TRUE)
+ {
+#ifdef STATS
+ ps->othertrue++;
+ ps->othertrue2++;
+ if (LIT2VAR (other)->level < ps->LEVEL)
+ ps->othertrue2u++;
+#endif
+ continue;
+ }
+
+ if (tmp != FALSE)
+ {
+ assign_forced (ps, other, LIT2REASON (NOTLIT(this)));
+ continue;
+ }
+
+ if (ps->conflict == &ps->cimpl)
+ resetcimpl (ps);
+ ps->conflict = setcimpl (ps, this, other);
+ }
+#else
+ /* Traverse all binary clauses with 'this'. Head/Tail pointers for binary
+ * clauses do not have to be modified here.
+ */
+ p = LIT2IMPLS (this);
+ for (c = *p; c; c = next)
+ {
+ ps->visits++;
+#ifdef STATS
+ ps->bvisits++;
+#endif
+ assert (!c->collect);
+#ifdef TRACE
+ assert (!c->collected);
+#endif
+ assert (c->size == 2);
+
+ other = c->lits[0];
+ if (other == this)
+ {
+ next = c->next[0];
+ other = c->lits[1];
+ }
+ else
+ next = c->next[1];
+
+ tmp = other->val;
+
+ if (tmp == TRUE)
+ {
+#ifdef STATS
+ ps->othertrue++;
+ ps->othertrue2++;
+ if (LIT2VAR (other)->level < ps->LEVEL)
+ ps->othertrue2u++;
+#endif
+ continue;
+ }
+
+ if (tmp == FALSE)
+ ps->conflict = c;
+ else
+ assign_forced (ps, other, c); /* unit clause */
+ }
+#endif /* !defined(NO_BINARY_CLAUSES) */
+}
+
+#ifndef NDSC
+static int
+should_disconnect_head_tail (PS * ps, Lit * lit)
+{
+ unsigned litlevel;
+ Var * v;
+
+ assert (lit->val == TRUE);
+
+ v = LIT2VAR (lit);
+ litlevel = v->level;
+
+ if (!litlevel)
+ return 1;
+
+#ifndef NFL
+ if (ps->simplifying)
+ return 0;
+#endif
+
+ return litlevel < ps->LEVEL;
+}
+#endif
+
+inline static void
+propl (PS * ps, Lit * this)
+{
+ Lit **l, *other, *prev, *new_lit, **eol;
+ Cls *next, **htp_ptr, **new_htp_ptr;
+ Cls *c;
+#ifdef STATS
+ unsigned size;
+#endif
+
+ htp_ptr = LIT2HTPS (this);
+ assert (this->val == FALSE);
+
+ /* Traverse all non binary clauses with 'this'. Head/Tail pointers are
+ * updated as well.
+ */
+ for (c = *htp_ptr; c; c = next)
+ {
+ ps->visits++;
+#ifdef STATS
+ size = c->size;
+ assert (size >= 3);
+ ps->traversals++; /* other is dereferenced at least */
+
+ if (size == 3)
+ ps->tvisits++;
+ else if (size >= 4)
+ {
+ ps->lvisits++;
+ ps->ltraversals++;
+ }
+#endif
+#ifdef TRACE
+ assert (!c->collected);
+#endif
+ assert (c->size > 0);
+
+ other = c->lits[0];
+ if (other != this)
+ {
+ assert (c->size != 1);
+ c->lits[0] = this;
+ c->lits[1] = other;
+ next = c->next[1];
+ c->next[1] = c->next[0];
+ c->next[0] = next;
+ }
+ else if (c->size == 1) /* With assumptions we need to
+ * traverse unit clauses as well.
+ */
+ {
+ assert (!ps->conflict);
+ ps->conflict = c;
+ break;
+ }
+ else
+ {
+ assert (other == this && c->size > 1);
+ other = c->lits[1];
+ next = c->next[0];
+ }
+ assert (other == c->lits[1]);
+ assert (this == c->lits[0]);
+ assert (next == c->next[0]);
+ assert (!c->collect);
+
+ if (other->val == TRUE)
+ {
+#ifdef STATS
+ ps->othertrue++;
+ ps->othertruel++;
+#endif
+#ifndef NDSC
+ if (should_disconnect_head_tail (ps, other))
+ {
+ new_htp_ptr = LIT2DHTPS (other);
+ c->next[0] = *new_htp_ptr;
+ *new_htp_ptr = c;
+#ifdef STATS
+ ps->othertruelu++;
+#endif
+ *htp_ptr = next;
+ continue;
+ }
+#endif
+ htp_ptr = c->next;
+ continue;
+ }
+
+ l = c->lits + 1;
+ eol = (Lit**) c->lits + c->size;
+ prev = this;
+
+ while (++l != eol)
+ {
+#ifdef STATS
+ if (size >= 3)
+ {
+ ps->traversals++;
+ if (size > 3)
+ ps->ltraversals++;
+ }
+#endif
+ new_lit = *l;
+ *l = prev;
+ prev = new_lit;
+ if (new_lit->val != FALSE) break;
+ }
+
+ if (l == eol)
+ {
+ while (l > c->lits + 2)
+ {
+ new_lit = *--l;
+ *l = prev;
+ prev = new_lit;
+ }
+ assert (c->lits[0] == this);
+
+ assert (other == c->lits[1]);
+ if (other->val == FALSE) /* found conflict */
+ {
+ assert (!ps->conflict);
+ ps->conflict = c;
+ return;
+ }
+
+ assign_forced (ps, other, c); /* unit clause */
+ htp_ptr = c->next;
+ }
+ else
+ {
+ assert (new_lit->val == TRUE || new_lit->val == UNDEF);
+ c->lits[0] = new_lit;
+ // *l = this;
+ new_htp_ptr = LIT2HTPS (new_lit);
+ c->next[0] = *new_htp_ptr;
+ *new_htp_ptr = c;
+ *htp_ptr = next;
+ }
+ }
+}
+
+#ifndef NADC
+
+static unsigned primes[] = { 996293, 330643, 753947, 500873 };
+
+#define PRIMES ((sizeof primes)/sizeof *primes)
+
+static unsigned
+hash_ado (PS * ps, Lit ** ado, unsigned salt)
+{
+ unsigned i, res, tmp;
+ Lit ** p, * lit;
+
+ assert (salt < PRIMES);
+
+ i = salt;
+ res = 0;
+
+ for (p = ado; (lit = *p); p++)
+ {
+ assert (lit->val);
+
+ tmp = res >> 31;
+ res <<= 1;
+
+ if (lit->val > 0)
+ res |= 1;
+
+ assert (i < PRIMES);
+ res *= primes[i++];
+ if (i == PRIMES)
+ i = 0;
+
+ res += tmp;
+ }
+
+ return res & (ps->szadotab - 1);
+}
+
+static unsigned
+cmp_ado (Lit ** a, Lit ** b)
+{
+ Lit ** p, ** q, * l, * k;
+ int res;
+
+ for (p = a, q = b; (l = *p); p++, q++)
+ {
+ k = *q;
+ assert (k);
+ if ((res = (l->val - k->val)))
+ return res;
+ }
+
+ assert (!*q);
+
+ return 0;
+}
+
+static Lit ***
+find_ado (PS * ps, Lit ** ado)
+{
+ Lit *** res, ** other;
+ unsigned pos, delta;
+
+ pos = hash_ado (ps, ado, 0);
+ assert (pos < ps->szadotab);
+ res = ps->adotab + pos;
+
+ other = *res;
+ if (!other || !cmp_ado (other, ado))
+ return res;
+
+ delta = hash_ado (ps, ado, 1);
+ if (!(delta & 1))
+ delta++;
+
+ assert (delta & 1);
+ assert (delta < ps->szadotab);
+
+ for (;;)
+ {
+ pos += delta;
+ if (pos >= ps->szadotab)
+ pos -= ps->szadotab;
+
+ assert (pos < ps->szadotab);
+ res = ps->adotab + pos;
+ other = *res;
+ if (!other || !cmp_ado (other, ado))
+ return res;
+ }
+}
+
+static void
+enlarge_adotab (PS * ps)
+{
+ /* TODO make this generic */
+
+ ABORTIF (ps->szadotab,
+ "internal: all different objects table needs larger initial size");
+ assert (!ps->nadotab);
+ ps->szadotab = 10000;
+ NEWN (ps->adotab, ps->szadotab);
+ CLRN (ps->adotab, ps->szadotab);
+}
+
+static int
+propado (PS * ps, Var * v)
+{
+ Lit ** p, ** q, *** adotabpos, **ado, * lit;
+ Var * u;
+
+ if (ps->LEVEL && ps->adodisabled)
+ return 1;
+
+ assert (!ps->conflict);
+ assert (!ps->adoconflict);
+ assert (VAR2LIT (v)->val != UNDEF);
+ assert (!v->adotabpos);
+
+ if (!v->ado)
+ return 1;
+
+ assert (v->inado);
+
+ for (p = v->ado; (lit = *p); p++)
+ if (lit->val == UNDEF)
+ {
+ u = LIT2VAR (lit);
+ assert (!u->ado);
+ u->ado = v->ado;
+ v->ado = 0;
+
+ return 1;
+ }
+
+ if (4 * ps->nadotab >= 3 * ps->szadotab) /* at least 75% filled */
+ enlarge_adotab (ps);
+
+ adotabpos = find_ado (ps, v->ado);
+ ado = *adotabpos;
+
+ if (!ado)
+ {
+ ps->nadotab++;
+ v->adotabpos = adotabpos;
+ *adotabpos = v->ado;
+ return 1;
+ }
+
+ assert (ado != v->ado);
+
+ ps->adoconflict = new_clause (ps, 2 * llength (ado), 1);
+ q = ps->adoconflict->lits;
+
+ for (p = ado; (lit = *p); p++)
+ *q++ = lit->val == FALSE ? lit : NOTLIT (lit);
+
+ for (p = v->ado; (lit = *p); p++)
+ *q++ = lit->val == FALSE ? lit : NOTLIT (lit);
+
+ assert (q == ENDOFCLS (ps->adoconflict));
+ ps->conflict = ps->adoconflict;
+ ps->adoconflicts++;
+ return 0;
+}
+
+#endif
+
+static void
+bcp (PS * ps)
+{
+ int props = 0;
+ assert (!ps->conflict);
+
+ if (ps->mtcls)
+ return;
+
+ for (;;)
+ {
+ if (ps->ttail2 < ps->thead) /* prioritize implications */
+ {
+ props++;
+ prop2 (ps, NOTLIT (*ps->ttail2++));
+ }
+ else if (ps->ttail < ps->thead) /* unit clauses or clauses with length > 2 */
+ {
+ if (ps->conflict) break;
+ propl (ps, NOTLIT (*ps->ttail++));
+ if (ps->conflict) break;
+ }
+#ifndef NADC
+ else if (ps->ttailado < ps->thead)
+ {
+ if (ps->conflict) break;
+ propado (ps, LIT2VAR (*ps->ttailado++));
+ if (ps->conflict) break;
+ }
+#endif
+ else
+ break; /* all assignments propagated, so break */
+ }
+
+ ps->propagations += props;
+}
+
+static unsigned
+drive (PS * ps)
+{
+ unsigned res, vlevel;
+ Lit **p;
+ Var *v;
+
+ res = 0;
+ for (p = ps->added; p < ps->ahead; p++)
+ {
+ v = LIT2VAR (*p);
+ vlevel = v->level;
+ assert (vlevel <= ps->LEVEL);
+ if (vlevel < ps->LEVEL && vlevel > res)
+ res = vlevel;
+ }
+
+ return res;
+}
+
+#ifdef VISCORES
+
+static void
+viscores (PS * ps)
+{
+ Rnk *p, *eor = ps->rnks + ps->max_var;
+ char name[100], cmd[200];
+ FILE * data;
+ Flt s;
+ int i;
+
+ for (p = ps->rnks + 1; p <= ps->eor; p++)
+ {
+ s = p->score;
+ if (s == INFFLT)
+ continue;
+ s = mulflt (s, ps->nvinc);
+ assert (flt2double (s) <= 1.0);
+ }
+
+ sprintf (name, "/tmp/picosat-viscores/data/%08u", ps->conflicts);
+ sprintf (cmd, "sort -n|nl>%s", name);
+
+ data = popen (cmd, "w");
+ for (p = ps->rnks + 1; p <= ps->eor; p++)
+ {
+ s = p->score;
+ if (s == INFFLT)
+ continue;
+ s = mulflt (s, ps->nvinc);
+ fprintf (data, "%lf %d\n", 100.0 * flt2double (s), (int)(p - ps->rnks));
+ }
+ fflush (data);
+ pclose (data);
+
+ for (i = 0; i < 8; i++)
+ {
+ sprintf (cmd, "awk '$3%%8==%d' %s>%s.%d", i, name, name, i);
+ system (cmd);
+ }
+
+ fprintf (ps->fviscores, "set title \"%u\"\n", ps->conflicts);
+ fprintf (ps->fviscores, "plot [0:%u] 0, 100 * (1 - 1/1.1), 100", ps->max_var);
+
+ for (i = 0; i < 8; i++)
+ fprintf (ps->fviscores,
+ ", \"%s.%d\" using 1:2:3 with labels tc lt %d",
+ name, i, i + 1);
+
+ fputc ('\n', ps->fviscores);
+ fflush (ps->fviscores);
+#ifndef WRITEGIF
+ usleep (50000); /* refresh rate of 20 Hz */
+#endif
+}
+
+#endif
+
+static void
+crescore (PS * ps)
+{
+ Cls **p, *c;
+ Act *a;
+ Flt factor;
+ int l = log2flt (ps->cinc);
+ assert (l > 0);
+ factor = base2flt (1, -l);
+
+ for (p = ps->lclauses; p != ps->lhead; p++)
+ {
+ c = *p;
+
+ if (!c)
+ continue;
+
+#ifdef TRACE
+ if (c->collected)
+ continue;
+#endif
+ assert (c->learned);
+
+ if (c->size <= 2)
+ continue;
+
+ a = CLS2ACT (c);
+ *a = mulflt (*a, factor);
+ }
+
+ ps->cinc = mulflt (ps->cinc, factor);
+}
+
+static void
+inc_vinc (PS * ps)
+{
+#ifdef VISCORES
+ ps->nvinc = mulflt (ps->nvinc, ps->fvinc);
+#endif
+ ps->vinc = mulflt (ps->vinc, ps->ifvinc);
+}
+
+inline static void
+inc_max_var (PS * ps)
+{
+ Lit *lit;
+ Rnk *r;
+ Var *v;
+
+ assert (ps->max_var < ps->size_vars);
+
+ if (ps->max_var + 1 == ps->size_vars)
+ enlarge (ps, ps->size_vars + 2*(ps->size_vars + 3) / 4); /* +25% */
+
+ ps->max_var++; /* new index of variable */
+ assert (ps->max_var); /* no unsigned overflow */
+
+ assert (ps->max_var < ps->size_vars);
+
+ lit = ps->lits + 2 * ps->max_var;
+ lit[0].val = lit[1].val = UNDEF;
+
+ memset (ps->htps + 2 * ps->max_var, 0, 2 * sizeof *ps->htps);
+#ifndef NDSC
+ memset (ps->dhtps + 2 * ps->max_var, 0, 2 * sizeof *ps->dhtps);
+#endif
+ memset (ps->impls + 2 * ps->max_var, 0, 2 * sizeof *ps->impls);
+ memset (ps->jwh + 2 * ps->max_var, 0, 2 * sizeof *ps->jwh);
+
+ v = ps->vars + ps->max_var; /* initialize variable components */
+ CLR (v);
+
+ r = ps->rnks + ps->max_var; /* initialize rank */
+ CLR (r);
+
+ hpush (ps, r);
+}
+
+static void
+force (PS * ps, Cls * c)
+{
+ Lit ** p, ** eol, * lit, * forced;
+ Cls * reason;
+
+ forced = 0;
+ reason = c;
+
+ eol = end_of_lits (c);
+ for (p = c->lits; p < eol; p++)
+ {
+ lit = *p;
+ if (lit->val == UNDEF)
+ {
+ assert (!forced);
+ forced = lit;
+#ifdef NO_BINARY_CLAUSES
+ if (c == &ps->impl)
+ reason = LIT2REASON (NOTLIT (p[p == c->lits ? 1 : -1]));
+#endif
+ }
+ else
+ assert (lit->val == FALSE);
+ }
+
+#ifdef NO_BINARY_CLAUSES
+ if (c == &ps->impl)
+ resetimpl (ps);
+#endif
+ if (!forced)
+ return;
+
+ assign_forced (ps, forced, reason);
+}
+
+static void
+inc_lreduce (PS * ps)
+{
+#ifdef STATS
+ ps->inclreduces++;
+#endif
+ ps->lreduce *= FREDUCE;
+ ps->lreduce /= 100;
+ report (ps, 1, '+');
+}
+
+static void
+backtrack (PS * ps)
+{
+ unsigned new_level;
+ Cls * c;
+
+ ps->conflicts++;
+ LOG ( fprintf (ps->out, "%sconflict ", ps->prefix); dumpclsnl (ps, ps->conflict));
+
+ analyze (ps);
+ new_level = drive (ps);
+ // TODO: why not? assert (new_level != 1 || (ps->ahead - ps->added) == 2);
+ c = add_simplified_clause (ps, 1);
+ undo (ps, new_level);
+ force (ps, c);
+
+ if (
+#ifndef NFL
+ !ps->simplifying &&
+#endif
+ !--ps->lreduceadjustcnt)
+ {
+ /* With FREDUCE==110 and FREDADJ=121 we stir 'lreduce' to be
+ * proportional to 'sqrt(conflicts)'. In earlier version we actually
+ * used 'FREDADJ=150', which results in 'lreduce' to approximate
+ * 'conflicts^(log(1.1)/log(1.5))' which is close to the fourth root
+ * of 'conflicts', since log(1.1)/log(1.5)=0.235 (as observed by
+ * Donald Knuth). The square root is the same we get by a Glucose
+ * style increase, which simply adds a constant at every reduction.
+ * This would be way simpler to implement but for now we keep the more
+ * complicated code using the adjust increments and counters.
+ */
+ ps->lreduceadjustinc *= FREDADJ; ps->lreduceadjustinc /= 100; ps->lreduceadjustcnt
+ = ps->lreduceadjustinc;
+ inc_lreduce (ps);
+ }
+
+ if (ps->verbosity >= 4 && !(ps->conflicts % 1000))
+ report (ps, 4, 'C');
+}
+
+static void
+inc_cinc (PS * ps)
+{
+ ps->cinc = mulflt (ps->cinc, ps->fcinc);
+ if (ps->lcinc < ps->cinc)
+ crescore (ps);
+}
+
+static void
+incincs (PS * ps)
+{
+ inc_vinc (ps);
+ inc_cinc (ps);
+#ifdef VISCORES
+ viscores (ps);
+#endif
+}
+
+static void
+disconnect_clause (PS * ps, Cls * c)
+{
+ assert (c->connected);
+
+ if (c->size > 2)
+ {
+ if (c->learned)
+ {
+ assert (ps->nlclauses > 0);
+ ps->nlclauses--;
+
+ assert (ps->llits >= c->size);
+ ps->llits -= c->size;
+ }
+ else
+ {
+ assert (ps->noclauses > 0);
+ ps->noclauses--;
+
+ assert (ps->olits >= c->size);
+ ps->olits -= c->size;
+ }
+ }
+
+#ifndef NDEBUG
+ c->connected = 0;
+#endif
+}
+
+static int
+clause_is_toplevel_satisfied (PS * ps, Cls * c)
+{
+ Lit *lit, **p, **eol = end_of_lits (c);
+ Var *v;
+
+ for (p = c->lits; p < eol; p++)
+ {
+ lit = *p;
+ if (lit->val == TRUE)
+ {
+ v = LIT2VAR (lit);
+ if (!v->level)
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+static int
+collect_clause (PS * ps, Cls * c)
+{
+ assert (c->collect);
+ c->collect = 0;
+
+#ifdef TRACE
+ assert (!c->collected);
+ c->collected = 1;
+#endif
+ disconnect_clause (ps, c);
+
+#ifdef TRACE
+ if (ps->trace && (!c->learned || c->used))
+ return 0;
+#endif
+ delete_clause (ps, c);
+
+ return 1;
+}
+
+static size_t
+collect_clauses (PS * ps)
+{
+ Cls *c, **p, **q, * next;
+ Lit * lit, * eol;
+ size_t res;
+ int i;
+
+ res = ps->current_bytes;
+
+ eol = ps->lits + 2 * ps->max_var + 1;
+ for (lit = ps->lits + 2; lit <= eol; lit++)
+ {
+ for (i = 0; i <= 1; i++)
+ {
+ if (i)
+ {
+#ifdef NO_BINARY_CLAUSES
+ Ltk * lstk = LIT2IMPLS (lit);
+ Lit ** r, ** s;
+ r = lstk->start;
+ if (lit->val != TRUE || LIT2VAR (lit)->level)
+ for (s = r; s < lstk->start + lstk->count; s++)
+ {
+ Lit * other = *s;
+ Var *v = LIT2VAR (other);
+ if (v->level ||
+ other->val != TRUE)
+ *r++ = other;
+ }
+ lstk->count = r - lstk->start;
+ continue;
+#else
+ p = LIT2IMPLS (lit);
+#endif
+ }
+ else
+ p = LIT2HTPS (lit);
+
+ for (c = *p; c; c = next)
+ {
+ q = c->next;
+ if (c->lits[0] != lit)
+ q++;
+
+ next = *q;
+ if (c->collect)
+ *p = next;
+ else
+ p = q;
+ }
+ }
+ }
+
+#ifndef NDSC
+ for (lit = ps->lits + 2; lit <= eol; lit++)
+ {
+ p = LIT2DHTPS (lit);
+ while ((c = *p))
+ {
+ Lit * other = c->lits[0];
+ if (other == lit)
+ {
+ q = c->next + 1;
+ }
+ else
+ {
+ assert (c->lits[1] == lit);
+ q = c->next;
+ }
+
+ if (c->collect)
+ *p = *q;
+ else
+ p = q;
+ }
+ }
+#endif
+
+ for (p = SOC; p != EOC; p = NXC (p))
+ {
+ c = *p;
+
+ if (!c)
+ continue;
+
+ if (!c->collect)
+ continue;
+
+ if (collect_clause (ps, c))
+ *p = 0;
+ }
+
+#ifdef TRACE
+ if (!ps->trace)
+#endif
+ {
+ q = ps->oclauses;
+ for (p = q; p < ps->ohead; p++)
+ if ((c = *p))
+ *q++ = c;
+ ps->ohead = q;
+
+ q = ps->lclauses;
+ for (p = q; p < ps->lhead; p++)
+ if ((c = *p))
+ *q++ = c;
+ ps->lhead = q;
+ }
+
+ assert (ps->current_bytes <= res);
+ res -= ps->current_bytes;
+ ps->recycled += res;
+
+ LOG ( fprintf (ps->out, "%scollected %ld bytes\n", ps->prefix, (long)res));
+
+ return res;
+}
+
+static int
+need_to_reduce (PS * ps)
+{
+ return ps->nlclauses >= reduce_limit_on_lclauses (ps);
+}
+
+#ifdef NLUBY
+
+static void
+inc_drestart (PS * ps)
+{
+ ps->drestart *= FRESTART;
+ ps->drestart /= 100;
+
+ if (ps->drestart >= MAXRESTART)
+ ps->drestart = MAXRESTART;
+}
+
+static void
+inc_ddrestart (PS * ps)
+{
+ ps->ddrestart *= FRESTART;
+ ps->ddrestart /= 100;
+
+ if (ps->ddrestart >= MAXRESTART)
+ ps->ddrestart = MAXRESTART;
+}
+
+#else
+
+static unsigned
+luby (unsigned i)
+{
+ unsigned k;
+ for (k = 1; k < 32; k++)
+ if (i == (1u << k) - 1)
+ return 1u << (k - 1);
+
+ for (k = 1;; k++)
+ if ((1u << (k - 1)) <= i && i < (1u << k) - 1)
+ return luby (i - (1u << (k-1)) + 1);
+}
+
+#endif
+
+#ifndef NLUBY
+static void
+inc_lrestart (PS * ps, int skip)
+{
+ unsigned delta;
+
+ delta = 100 * luby (++ps->lubycnt);
+ ps->lrestart = ps->conflicts + delta;
+
+ if (ps->waslubymaxdelta)
+ report (ps, 1, skip ? 'N' : 'R');
+ else
+ report (ps, 2, skip ? 'n' : 'r');
+
+ if (delta > ps->lubymaxdelta)
+ {
+ ps->lubymaxdelta = delta;
+ ps->waslubymaxdelta = 1;
+ }
+ else
+ ps->waslubymaxdelta = 0;
+}
+#endif
+
+static void
+init_restart (PS * ps)
+{
+#ifdef NLUBY
+ /* TODO: why is it better in incremental usage to have smaller initial
+ * outer restart interval?
+ */
+ ps->ddrestart = ps->calls > 1 ? MINRESTART : 1000;
+ ps->drestart = MINRESTART;
+ ps->lrestart = ps->conflicts + ps->drestart;
+#else
+ ps->lubycnt = 0;
+ ps->lubymaxdelta = 0;
+ ps->waslubymaxdelta = 0;
+ inc_lrestart (ps, 0);
+#endif
+}
+
+static void
+restart (PS * ps)
+{
+ int skip;
+#ifdef NLUBY
+ char kind;
+ int outer;
+
+ inc_drestart (ps);
+ outer = (ps->drestart >= ps->ddrestart);
+
+ if (outer)
+ skip = very_high_agility (ps);
+ else
+ skip = high_agility (ps);
+#else
+ skip = medium_agility (ps);
+#endif
+
+#ifdef STATS
+ if (skip)
+ ps->skippedrestarts++;
+#endif
+
+ assert (ps->conflicts >= ps->lrestart);
+
+ if (!skip)
+ {
+ ps->restarts++;
+ assert (ps->LEVEL > 1);
+ LOG ( fprintf (ps->out, "%srestart %u\n", ps->prefix, ps->restarts));
+ undo (ps, 0);
+ }
+
+#ifdef NLUBY
+ if (outer)
+ {
+ kind = skip ? 'N' : 'R';
+ inc_ddrestart (ps);
+ ps->drestart = MINRESTART;
+ }
+ else if (skip)
+ {
+ kind = 'n';
+ }
+ else
+ {
+ kind = 'r';
+ }
+
+ assert (ps->drestart <= MAXRESTART);
+ ps->lrestart = ps->conflicts + ps->drestart;
+ assert (ps->lrestart > ps->conflicts);
+
+ report (outer ? 1 : 2, kind);
+#else
+ inc_lrestart (ps, skip);
+#endif
+}
+
+inline static void
+assign_decision (PS * ps, Lit * lit)
+{
+ assert (!ps->conflict);
+
+ ps->LEVEL++;
+
+ LOG ( fprintf (ps->out, "%snew level %u\n", ps->prefix, ps->LEVEL));
+ LOG ( fprintf (ps->out,
+ "%sassign %d at level %d <= DECISION\n",
+ ps->prefix, LIT2INT (lit), ps->LEVEL));
+
+ assign (ps, lit, 0);
+}
+
+#ifndef NFL
+
+static int
+lit_has_binary_clauses (PS * ps, Lit * lit)
+{
+#ifdef NO_BINARY_CLAUSES
+ Ltk* lstk = LIT2IMPLS (lit);
+ return lstk->count != 0;
+#else
+ return *LIT2IMPLS (lit) != 0;
+#endif
+}
+
+static void
+flbcp (PS * ps)
+{
+#ifdef STATS
+ unsigned long long propagaions_before_bcp = ps->propagations;
+#endif
+ bcp (ps);
+#ifdef STATS
+ ps->flprops += ps->propagations - propagaions_before_bcp;
+#endif
+}
+
+inline static int
+cmp_inverse_rnk (PS * ps, Rnk * a, Rnk * b)
+{
+ (void) ps;
+ return -cmp_rnk (a, b);
+}
+
+inline static Flt
+rnk2jwh (PS * ps, Rnk * r)
+{
+ Flt res, sum, pjwh, njwh;
+ Lit * plit, * nlit;
+
+ plit = RNK2LIT (r);
+ nlit = plit + 1;
+
+ pjwh = *LIT2JWH (plit);
+ njwh = *LIT2JWH (nlit);
+
+ res = mulflt (pjwh, njwh);
+
+ sum = addflt (pjwh, njwh);
+ sum = mulflt (sum, base2flt (1, -10));
+ res = addflt (res, sum);
+
+ return res;
+}
+
+static int
+cmp_inverse_jwh_rnk (PS * ps, Rnk * r, Rnk * s)
+{
+ Flt a = rnk2jwh (ps, r);
+ Flt b = rnk2jwh (ps, s);
+ int res = cmpflt (a, b);
+
+ if (res)
+ return -res;
+
+ return cmp_inverse_rnk (ps, r, s);
+}
+
+static void
+faillits (PS * ps)
+{
+ unsigned i, j, old_trail_count, common, saved_count;
+ unsigned new_saved_size, oldladded = ps->ladded;
+ unsigned long long limit, delta;
+ Lit * lit, * other, * pivot;
+ Rnk * r, ** p, ** q;
+ int new_trail_count;
+ double started;
+
+ if (ps->plain)
+ return;
+
+ if (ps->heap + 1 >= ps->hhead)
+ return;
+
+ if (ps->propagations < ps->fllimit)
+ return;
+
+ sflush (ps);
+ started = ps->seconds;
+
+ ps->flcalls++;
+#ifdef STATSA
+ ps->flrounds++;
+#endif
+ delta = ps->propagations/10;
+ if (delta >= 100*1000*1000) delta = 100*1000*1000;
+ else if (delta <= 100*1000) delta = 100*1000;
+
+ limit = ps->propagations + delta;
+ ps->fllimit = ps->propagations;
+
+ assert (!ps->LEVEL);
+ assert (ps->simplifying);
+
+ if (ps->flcalls <= 1)
+ SORT (Rnk *, cmp_inverse_jwh_rnk, ps->heap + 1, ps->hhead - (ps->heap + 1));
+ else
+ SORT (Rnk *, cmp_inverse_rnk, ps->heap + 1, ps->hhead - (ps->heap + 1));
+
+ i = 1; /* NOTE: heap starts at position '1' */
+
+ while (ps->propagations < limit)
+ {
+ if (ps->heap + i == ps->hhead)
+ {
+ if (ps->ladded == oldladded)
+ break;
+
+ i = 1;
+#ifdef STATS
+ ps->flrounds++;
+#endif
+ oldladded = ps->ladded;
+ }
+
+ assert (ps->heap + i < ps->hhead);
+
+ r = ps->heap[i++];
+ lit = RNK2LIT (r);
+
+ if (lit->val)
+ continue;
+
+ if (!lit_has_binary_clauses (ps, NOTLIT (lit)))
+ {
+#ifdef STATS
+ ps->flskipped++;
+#endif
+ continue;
+ }
+
+#ifdef STATS
+ ps->fltried++;
+#endif
+ LOG ( fprintf (ps->out, "%strying %d as failed literal\n",
+ ps->prefix, LIT2INT (lit)));
+
+ assign_decision (ps, lit);
+ old_trail_count = ps->thead - ps->trail;
+ flbcp (ps);
+
+ if (ps->conflict)
+ {
+EXPLICITLY_FAILED_LITERAL:
+ LOG ( fprintf (ps->out, "%sfound explicitly failed literal %d\n",
+ ps->prefix, LIT2INT (lit)));
+
+ ps->failedlits++;
+ ps->efailedlits++;
+
+ backtrack (ps);
+ flbcp (ps);
+
+ if (!ps->conflict)
+ continue;
+
+CONTRADICTION:
+ assert (!ps->LEVEL);
+ backtrack (ps);
+ assert (ps->mtcls);
+
+ goto RETURN;
+ }
+
+ if (ps->propagations >= limit)
+ {
+ undo (ps, 0);
+ break;
+ }
+
+ lit = NOTLIT (lit);
+
+ if (!lit_has_binary_clauses (ps, NOTLIT (lit)))
+ {
+#ifdef STATS
+ ps->flskipped++;
+#endif
+ undo (ps, 0);
+ continue;
+ }
+
+#ifdef STATS
+ ps->fltried++;
+#endif
+ LOG ( fprintf (ps->out, "%strying %d as failed literals\n",
+ ps->prefix, LIT2INT (lit)));
+
+ new_trail_count = ps->thead - ps->trail;
+ saved_count = new_trail_count - old_trail_count;
+
+ if (saved_count > ps->saved_size)
+ {
+ new_saved_size = ps->saved_size ? 2 * ps->saved_size : 1;
+ while (saved_count > new_saved_size)
+ new_saved_size *= 2;
+
+ RESIZEN (ps->saved, ps->saved_size, new_saved_size);
+ ps->saved_size = new_saved_size;
+ }
+
+ for (j = 0; j < saved_count; j++)
+ ps->saved[j] = ps->trail[old_trail_count + j];
+
+ undo (ps, 0);
+
+ assign_decision (ps, lit);
+ flbcp (ps);
+
+ if (ps->conflict)
+ goto EXPLICITLY_FAILED_LITERAL;
+
+ pivot = (ps->thead - ps->trail <= new_trail_count) ? lit : NOTLIT (lit);
+
+ common = 0;
+ for (j = 0; j < saved_count; j++)
+ if ((other = ps->saved[j])->val == TRUE)
+ ps->saved[common++] = other;
+
+ undo (ps, 0);
+
+ LOG (if (common)
+ fprintf (ps->out,
+ "%sfound %d literals implied by %d and %d\n",
+ ps->prefix, common,
+ LIT2INT (NOTLIT (lit)), LIT2INT (lit)));
+
+#if 1 // set to zero to disable 'lifting'
+ for (j = 0;
+ j < common
+ /* TODO: For some Velev benchmarks, extracting the common implicit
+ * failed literals took quite some time. This needs to be fixed by
+ * a dedicated analyzer. Up to then we bound the number of
+ * propagations in this loop as well.
+ */
+ && ps->propagations < limit + delta
+ ; j++)
+ {
+ other = ps->saved[j];
+
+ if (other->val == TRUE)
+ continue;
+
+ assert (!other->val);
+
+ LOG ( fprintf (ps->out,
+ "%sforcing %d as forced implicitly failed literal\n",
+ ps->prefix, LIT2INT (other)));
+
+ assert (pivot != NOTLIT (other));
+ assert (pivot != other);
+
+ assign_decision (ps, NOTLIT (other));
+ flbcp (ps);
+
+ assert (ps->LEVEL == 1);
+
+ if (ps->conflict)
+ {
+ backtrack (ps);
+ assert (!ps->LEVEL);
+ }
+ else
+ {
+ assign_decision (ps, pivot);
+ flbcp (ps);
+
+ backtrack (ps);
+
+ if (ps->LEVEL)
+ {
+ assert (ps->LEVEL == 1);
+
+ flbcp (ps);
+
+ if (ps->conflict)
+ {
+ backtrack (ps);
+ assert (!ps->LEVEL);
+ }
+ else
+ {
+ assign_decision (ps, NOTLIT (pivot));
+ flbcp (ps);
+ backtrack (ps);
+
+ if (ps->LEVEL)
+ {
+ assert (ps->LEVEL == 1);
+ flbcp (ps);
+
+ if (!ps->conflict)
+ {
+#ifdef STATS
+ ps->floopsed++;
+#endif
+ undo (ps, 0);
+ continue;
+ }
+
+ backtrack (ps);
+ }
+
+ assert (!ps->LEVEL);
+ }
+
+ assert (!ps->LEVEL);
+ }
+ }
+ assert (!ps->LEVEL);
+ flbcp (ps);
+
+ ps->failedlits++;
+ ps->ifailedlits++;
+
+ if (ps->conflict)
+ goto CONTRADICTION;
+ }
+#endif
+ }
+
+ ps->fllimit += 9 * (ps->propagations - ps->fllimit); /* 10% for failed literals */
+
+RETURN:
+
+ /* First flush top level assigned literals. Those are prohibited from
+ * being pushed up the heap during 'faillits' since 'simplifying' is set.
+ */
+ assert (ps->heap < ps->hhead);
+ for (p = q = ps->heap + 1; p < ps->hhead; p++)
+ {
+ r = *p;
+ lit = RNK2LIT (r);
+ if (lit->val)
+ r->pos = 0;
+ else
+ *q++ = r;
+ }
+
+ /* Then resort with respect to EVSIDS score and fix positions.
+ */
+ SORT (Rnk *, cmp_inverse_rnk, ps->heap + 1, ps->hhead - (ps->heap + 1));
+ for (p = ps->heap + 1; p < ps->hhead; p++)
+ (*p)->pos = p - ps->heap;
+
+ sflush (ps);
+ ps->flseconds += ps->seconds - started;
+}
+
+#endif
+
+static void
+simplify (PS * ps, int forced)
+{
+ Lit * lit, * notlit, ** t;
+ unsigned collect, delta;
+#ifdef STATS
+ size_t bytes_collected;
+#endif
+ int * q, ilit;
+ Cls **p, *c;
+ Var * v;
+
+#ifndef NDEDBUG
+ (void) forced;
+#endif
+
+ assert (!ps->mtcls);
+ assert (!satisfied (ps));
+ assert (forced || ps->lsimplify <= ps->propagations);
+ assert (forced || ps->fsimplify <= ps->fixed);
+
+ if (ps->LEVEL)
+ undo (ps, 0);
+#ifndef NFL
+ ps->simplifying = 1;
+ faillits (ps);
+ ps->simplifying = 0;
+
+ if (ps->mtcls)
+ return;
+#endif
+
+ if (ps->cils != ps->cilshead)
+ {
+ assert (ps->ttail == ps->thead);
+ assert (ps->ttail2 == ps->thead);
+ ps->ttail = ps->trail;
+ for (t = ps->trail; t < ps->thead; t++)
+ {
+ lit = *t;
+ v = LIT2VAR (lit);
+ if (v->internal)
+ {
+ assert (LIT2INT (lit) < 0);
+ assert (lit->val == TRUE);
+ unassign (ps, lit);
+ }
+ else
+ *ps->ttail++ = lit;
+ }
+ ps->ttail2 = ps->thead = ps->ttail;
+
+ for (q = ps->cils; q != ps->cilshead; q++)
+ {
+ ilit = *q;
+ assert (0 < ilit && ilit <= (int) ps->max_var);
+ v = ps->vars + ilit;
+ assert (v->internal);
+ v->level = 0;
+ v->reason = 0;
+ lit = int2lit (ps, -ilit);
+ assert (lit->val == UNDEF);
+ lit->val = TRUE;
+ notlit = NOTLIT (lit);
+ assert (notlit->val == UNDEF);
+ notlit->val = FALSE;
+ }
+ }
+
+ collect = 0;
+ for (p = SOC; p != EOC; p = NXC (p))
+ {
+ c = *p;
+ if (!c)
+ continue;
+
+#ifdef TRACE
+ if (c->collected)
+ continue;
+#endif
+
+ if (c->locked)
+ continue;
+
+ assert (!c->collect);
+ if (clause_is_toplevel_satisfied (ps, c))
+ {
+ mark_clause_to_be_collected (c);
+ collect++;
+ }
+ }
+
+ LOG ( fprintf (ps->out, "%scollecting %d clauses\n", ps->prefix, collect));
+#ifdef STATS
+ bytes_collected =
+#endif
+ collect_clauses (ps);
+#ifdef STATS
+ ps->srecycled += bytes_collected;
+#endif
+
+ if (ps->cils != ps->cilshead)
+ {
+ for (q = ps->cils; q != ps->cilshead; q++)
+ {
+ ilit = *q;
+ assert (0 < ilit && ilit <= (int) ps->max_var);
+ assert (ps->vars[ilit].internal);
+ if (ps->rilshead == ps->eorils)
+ ENLARGE (ps->rils, ps->rilshead, ps->eorils);
+ *ps->rilshead++ = ilit;
+ lit = int2lit (ps, -ilit);
+ assert (lit->val == TRUE);
+ lit->val = UNDEF;
+ notlit = NOTLIT (lit);
+ assert (notlit->val == FALSE);
+ notlit->val = UNDEF;
+ }
+ ps->cilshead = ps->cils;
+ }
+
+ delta = 10 * (ps->olits + ps->llits) + 100000;
+ if (delta > 2000000)
+ delta = 2000000;
+ ps->lsimplify = ps->propagations + delta;
+ ps->fsimplify = ps->fixed;
+ ps->simps++;
+
+ report (ps, 1, 's');
+}
+
+static void
+iteration (PS * ps)
+{
+ assert (!ps->LEVEL);
+ assert (bcp_queue_is_empty (ps));
+ assert (ps->isimplify < ps->fixed);
+
+ ps->iterations++;
+ report (ps, 2, 'i');
+#ifdef NLUBY
+ ps->drestart = MINRESTART;
+ ps->lrestart = ps->conflicts + ps->drestart;
+#else
+ init_restart (ps);
+#endif
+ ps->isimplify = ps->fixed;
+}
+
+static int
+cmp_glue_activity_size (PS * ps, Cls * c, Cls * d)
+{
+ Act a, b, * p, * q;
+
+ (void) ps;
+
+ assert (c->learned);
+ assert (d->learned);
+
+ if (c->glue < d->glue) // smaller glue preferred
+ return 1;
+
+ if (c->glue > d->glue)
+ return -1;
+
+ p = CLS2ACT (c);
+ q = CLS2ACT (d);
+ a = *p;
+ b = *q;
+
+ if (a < b) // then higher activity
+ return -1;
+
+ if (b < a)
+ return 1;
+
+ if (c->size < d->size) // then smaller size
+ return 1;
+
+ if (c->size > d->size)
+ return -1;
+
+ return 0;
+}
+
+static void
+reduce (PS * ps, unsigned percentage)
+{
+ unsigned redcount, lcollect, collect, target;
+#ifdef STATS
+ size_t bytes_collected;
+#endif
+ Cls **p, *c;
+
+ assert (ps->rhead == ps->resolved);
+
+ ps->lastreduceconflicts = ps->conflicts;
+
+ assert (percentage <= 100);
+ LOG ( fprintf (ps->out,
+ "%sreducing %u%% learned clauses\n",
+ ps->prefix, percentage));
+
+ while (ps->nlclauses - ps->llocked > (unsigned)(ps->eor - ps->resolved))
+ ENLARGE (ps->resolved, ps->rhead, ps->eor);
+
+ collect = 0;
+ lcollect = 0;
+
+ for (p = ((ps->fsimplify < ps->fixed) ? SOC : ps->lclauses); p != EOC; p = NXC (p))
+ {
+ c = *p;
+ if (!c)
+ continue;
+
+#ifdef TRACE
+ if (c->collected)
+ continue;
+#endif
+
+ if (c->locked)
+ continue;
+
+ assert (!c->collect);
+ if (ps->fsimplify < ps->fixed && clause_is_toplevel_satisfied (ps, c))
+ {
+ mark_clause_to_be_collected (c);
+ collect++;
+
+ if (c->learned && c->size > 2)
+ lcollect++;
+
+ continue;
+ }
+
+ if (!c->learned)
+ continue;
+
+ if (c->size <= 2)
+ continue;
+
+ assert (ps->rhead < ps->eor);
+ *ps->rhead++ = c;
+ }
+ assert (ps->rhead <= ps->eor);
+
+ ps->fsimplify = ps->fixed;
+
+ redcount = ps->rhead - ps->resolved;
+ SORT (Cls *, cmp_glue_activity_size, ps->resolved, redcount);
+
+ assert (ps->nlclauses >= lcollect);
+ target = ps->nlclauses - lcollect + 1;
+
+ target = (percentage * target + 99) / 100;
+
+ if (target >= redcount)
+ target = redcount;
+
+ ps->rhead = ps->resolved + target;
+ while (ps->rhead > ps->resolved)
+ {
+ c = *--ps->rhead;
+ mark_clause_to_be_collected (c);
+
+ collect++;
+ if (c->learned && c->size > 2) /* just for consistency */
+ lcollect++;
+ }
+
+ if (collect)
+ {
+ ps->reductions++;
+#ifdef STATS
+ bytes_collected =
+#endif
+ collect_clauses (ps);
+#ifdef STATS
+ ps->rrecycled += bytes_collected;
+#endif
+ report (ps, 2, '-');
+ }
+
+ if (!lcollect)
+ inc_lreduce (ps); /* avoid dead lock */
+
+ assert (ps->rhead == ps->resolved);
+}
+
+static void
+init_reduce (PS * ps)
+{
+ // lreduce = loadded / 2;
+ ps->lreduce = 1000;
+
+ if (ps->lreduce < 100)
+ ps->lreduce = 100;
+
+ if (ps->verbosity)
+ fprintf (ps->out,
+ "%s\n%sinitial reduction limit %u clauses\n%s\n",
+ ps->prefix, ps->prefix, ps->lreduce, ps->prefix);
+}
+
+static unsigned
+rng (PS * ps)
+{
+ unsigned res = ps->srng;
+ ps->srng *= 1664525u;
+ ps->srng += 1013904223u;
+ NOLOG ( fprintf (ps->out, "%srng () = %u\n", ps->prefix, res));
+ return res;
+}
+
+static unsigned
+rrng (PS * ps, unsigned low, unsigned high)
+{
+ unsigned long long tmp;
+ unsigned res, elements;
+ assert (low <= high);
+ elements = high - low + 1;
+ tmp = rng (ps);
+ tmp *= elements;
+ tmp >>= 32;
+ tmp += low;
+ res = tmp;
+ NOLOG ( fprintf (ps->out, "%srrng (ps, %u, %u) = %u\n", ps->prefix, low, high, res));
+ assert (low <= res);
+ assert (res <= high);
+ return res;
+}
+
+static Lit *
+decide_phase (PS * ps, Lit * lit)
+{
+ Lit * not_lit = NOTLIT (lit);
+ Var *v = LIT2VAR (lit);
+
+ assert (LIT2SGN (lit) > 0);
+ if (v->usedefphase)
+ {
+ if (v->defphase)
+ {
+ /* assign to TRUE */
+ }
+ else
+ {
+ /* assign to FALSE */
+ lit = not_lit;
+ }
+ }
+ else if (!v->assigned)
+ {
+#ifdef STATS
+ ps->staticphasedecisions++;
+#endif
+ if (ps->defaultphase == POSPHASE)
+ {
+ /* assign to TRUE */
+ }
+ else if (ps->defaultphase == NEGPHASE)
+ {
+ /* assign to FALSE */
+ lit = not_lit;
+ }
+ else if (ps->defaultphase == RNDPHASE)
+ {
+ /* randomly assign default phase */
+ if (rrng (ps, 1, 2) != 2)
+ lit = not_lit;
+ }
+ else if (*LIT2JWH(lit) <= *LIT2JWH (not_lit))
+ {
+ /* assign to FALSE (Jeroslow-Wang says there are more short
+ * clauses with negative occurence of this variable, so satisfy
+ * those, to minimize BCP)
+ */
+ lit = not_lit;
+ }
+ else
+ {
+ /* assign to TRUE (... but strictly more positive occurrences) */
+ }
+ }
+ else
+ {
+ /* repeat last phase: phase saving heuristic */
+
+ if (v->phase)
+ {
+ /* assign to TRUE (last phase was TRUE as well) */
+ }
+ else
+ {
+ /* assign to FALSE (last phase was FALSE as well) */
+ lit = not_lit;
+ }
+ }
+
+ return lit;
+}
+
+static unsigned
+gcd (unsigned a, unsigned b)
+{
+ unsigned tmp;
+
+ assert (a);
+ assert (b);
+
+ if (a < b)
+ {
+ tmp = a;
+ a = b;
+ b = tmp;
+ }
+
+ while (b)
+ {
+ assert (a >= b);
+ tmp = b;
+ b = a % b;
+ a = tmp;
+ }
+
+ return a;
+}
+
+static Lit *
+rdecide (PS * ps)
+{
+ unsigned idx, delta, spread;
+ Lit * res;
+
+ spread = RDECIDE;
+ if (rrng (ps, 1, spread) != 2)
+ return 0;
+
+ assert (1 <= ps->max_var);
+ idx = rrng (ps, 1, ps->max_var);
+ res = int2lit (ps, idx);
+
+ if (res->val != UNDEF)
+ {
+ delta = rrng (ps, 1, ps->max_var);
+ while (gcd (delta, ps->max_var) != 1)
+ delta--;
+
+ assert (1 <= delta);
+ assert (delta <= ps->max_var);
+
+ do {
+ idx += delta;
+ if (idx > ps->max_var)
+ idx -= ps->max_var;
+ res = int2lit (ps, idx);
+ } while (res->val != UNDEF);
+ }
+
+#ifdef STATS
+ ps->rdecisions++;
+#endif
+ res = decide_phase (ps, res);
+ LOG ( fprintf (ps->out, "%srdecide %d\n", ps->prefix, LIT2INT (res)));
+
+ return res;
+}
+
+static Lit *
+sdecide (PS * ps)
+{
+ Lit *res;
+ Rnk *r;
+
+ for (;;)
+ {
+ r = htop (ps);
+ res = RNK2LIT (r);
+ if (res->val == UNDEF) break;
+ (void) hpop (ps);
+ NOLOG ( fprintf (ps->out,
+ "%shpop %u %u %u\n",
+ ps->prefix, r - ps->rnks,
+ FLTMANTISSA(r->score),
+ FLTEXPONENT(r->score)));
+ }
+
+#ifdef STATS
+ ps->sdecisions++;
+#endif
+ res = decide_phase (ps, res);
+
+ LOG ( fprintf (ps->out, "%ssdecide %d\n", ps->prefix, LIT2INT (res)));
+
+ return res;
+}
+
+static Lit *
+adecide (PS * ps)
+{
+ Lit *lit;
+ Var * v;
+
+ assert (ps->als < ps->alshead);
+ assert (!ps->failed_assumption);
+
+ while (ps->alstail < ps->alshead)
+ {
+ lit = *ps->alstail++;
+
+ if (lit->val == FALSE)
+ {
+ ps->failed_assumption = lit;
+ v = LIT2VAR (lit);
+
+ use_var (ps, v);
+
+ LOG ( fprintf (ps->out, "%sfirst failed assumption %d\n",
+ ps->prefix, LIT2INT (ps->failed_assumption)));
+ fanalyze (ps);
+ return 0;
+ }
+
+ if (lit->val == TRUE)
+ {
+ v = LIT2VAR (lit);
+ if (v->level > ps->adecidelevel)
+ ps->adecidelevel = v->level;
+ continue;
+ }
+
+#ifdef STATS
+ ps->assumptions++;
+#endif
+ LOG ( fprintf (ps->out, "%sadecide %d\n", ps->prefix, LIT2INT (lit)));
+ ps->adecidelevel = ps->LEVEL + 1;
+
+ return lit;
+ }
+
+ return 0;
+}
+
+static void
+decide (PS * ps)
+{
+ Lit * lit;
+
+ assert (!satisfied (ps));
+ assert (!ps->conflict);
+
+ if (ps->alstail < ps->alshead && (lit = adecide (ps)))
+ ;
+ else if (ps->failed_assumption)
+ return;
+ else if (satisfied (ps))
+ return;
+ else if (!(lit = rdecide (ps)))
+ lit = sdecide (ps);
+
+ assert (lit);
+ assign_decision (ps, lit);
+
+ ps->levelsum += ps->LEVEL;
+ ps->decisions++;
+}
+
+static int
+sat (PS * ps, int l)
+{
+ int count = 0, backtracked;
+
+ if (!ps->conflict)
+ bcp (ps);
+
+ if (ps->conflict)
+ backtrack (ps);
+
+ if (ps->mtcls)
+ return PICOSAT_UNSATISFIABLE;
+
+ if (satisfied (ps))
+ goto SATISFIED;
+
+ if (ps->lsimplify <= ps->propagations)
+ simplify (ps, 0);
+
+ if (ps->mtcls)
+ return PICOSAT_UNSATISFIABLE;
+
+ if (satisfied (ps))
+ goto SATISFIED;
+
+ init_restart (ps);
+
+ if (!ps->lreduce)
+ init_reduce (ps);
+
+ ps->isimplify = ps->fixed;
+ backtracked = 0;
+
+ for (;;)
+ {
+ if (!ps->conflict)
+ bcp (ps);
+
+ if (ps->conflict)
+ {
+ incincs (ps);
+ backtrack (ps);
+
+ if (ps->mtcls)
+ return PICOSAT_UNSATISFIABLE;
+ backtracked = 1;
+ continue;
+ }
+
+ if (satisfied (ps))
+ {
+SATISFIED:
+#ifndef NDEBUG
+ original_clauses_satisfied (ps);
+ assumptions_satisfied (ps);
+#endif
+ return PICOSAT_SATISFIABLE;
+ }
+
+ if (backtracked)
+ {
+ backtracked = 0;
+ if (!ps->LEVEL && ps->isimplify < ps->fixed)
+ iteration (ps);
+ }
+
+ if (l >= 0 && count >= l) /* decision limit reached ? */
+ return PICOSAT_UNKNOWN;
+
+ if (ps->interrupt.function && /* external interrupt */
+ count > 0 && !(count % INTERRUPTLIM) &&
+ ps->interrupt.function (ps->interrupt.state))
+ return PICOSAT_UNKNOWN;
+
+ if (ps->propagations >= ps->lpropagations)/* propagation limit reached ? */
+ return PICOSAT_UNKNOWN;
+
+#ifndef NADC
+ if (!ps->adodisabled && ps->adoconflicts >= ps->adoconflictlimit)
+ {
+ assert (bcp_queue_is_empty (ps));
+ return PICOSAT_UNKNOWN;
+ }
+#endif
+
+ if (ps->fsimplify < ps->fixed && ps->lsimplify <= ps->propagations)
+ {
+ simplify (ps, 0);
+ if (!bcp_queue_is_empty (ps))
+ continue;
+#ifndef NFL
+ if (ps->mtcls)
+ return PICOSAT_UNSATISFIABLE;
+
+ if (satisfied (ps))
+ return PICOSAT_SATISFIABLE;
+
+ assert (!ps->LEVEL);
+#endif
+ }
+
+ if (need_to_reduce (ps))
+ reduce (ps, 50);
+
+ if (ps->conflicts >= ps->lrestart && ps->LEVEL > 2)
+ restart (ps);
+
+ decide (ps);
+ if (ps->failed_assumption)
+ return PICOSAT_UNSATISFIABLE;
+ count++;
+ }
+}
+
+static void
+rebias (PS * ps)
+{
+ Cls ** p, * c;
+ Var * v;
+
+ for (v = ps->vars + 1; v <= ps->vars + ps->max_var; v++)
+ v->assigned = 0;
+
+ memset (ps->jwh, 0, 2 * (ps->max_var + 1) * sizeof *ps->jwh);
+
+ for (p = ps->oclauses; p < ps->ohead; p++)
+ {
+ c = *p;
+
+ if (!c)
+ continue;
+
+ if (c->learned)
+ continue;
+
+ incjwh (ps, c);
+ }
+}
+
+#ifdef TRACE
+
+static unsigned
+core (PS * ps)
+{
+ unsigned idx, prev, this, delta, i, lcore, vcore;
+ unsigned *stack, *shead, *eos;
+ Lit **q, **eol, *lit;
+ Cls *c, *reason;
+ Znt *p, byte;
+ Zhn *zhain;
+ Var *v;
+
+ assert (ps->trace);
+
+ assert (ps->mtcls || ps->failed_assumption);
+ if (ps->ocore >= 0)
+ return ps->ocore;
+
+ lcore = ps->ocore = vcore = 0;
+
+ stack = shead = eos = 0;
+ ENLARGE (stack, shead, eos);
+
+ if (ps->mtcls)
+ {
+ idx = CLS2IDX (ps->mtcls);
+ *shead++ = idx;
+ }
+ else
+ {
+ assert (ps->failed_assumption);
+ v = LIT2VAR (ps->failed_assumption);
+ reason = v->reason;
+ assert (reason);
+ idx = CLS2IDX (reason);
+ *shead++ = idx;
+ }
+
+ while (shead > stack)
+ {
+ idx = *--shead;
+ zhain = IDX2ZHN (idx);
+
+ if (zhain)
+ {
+ if (zhain->core)
+ continue;
+
+ zhain->core = 1;
+ lcore++;
+
+ c = IDX2CLS (idx);
+ if (c)
+ {
+ assert (!c->core);
+ c->core = 1;
+ }
+
+ i = 0;
+ delta = 0;
+ prev = 0;
+ for (p = zhain->znt; (byte = *p); p++, i += 7)
+ {
+ delta |= (byte & 0x7f) << i;
+ if (byte & 0x80)
+ continue;
+
+ this = prev + delta;
+ assert (prev < this); /* no overflow */
+
+ if (shead == eos)
+ ENLARGE (stack, shead, eos);
+ *shead++ = this;
+
+ prev = this;
+ delta = 0;
+ i = -7;
+ }
+ }
+ else
+ {
+ c = IDX2CLS (idx);
+
+ assert (c);
+ assert (!c->learned);
+
+ if (c->core)
+ continue;
+
+ c->core = 1;
+ ps->ocore++;
+
+ eol = end_of_lits (c);
+ for (q = c->lits; q < eol; q++)
+ {
+ lit = *q;
+ v = LIT2VAR (lit);
+ if (v->core)
+ continue;
+
+ v->core = 1;
+ vcore++;
+
+ if (!ps->failed_assumption) continue;
+ if (lit != ps->failed_assumption) continue;
+
+ reason = v->reason;
+ if (!reason) continue;
+ if (reason->core) continue;
+
+ idx = CLS2IDX (reason);
+ if (shead == eos)
+ ENLARGE (stack, shead, eos);
+ *shead++ = idx;
+ }
+ }
+ }
+
+ DELETEN (stack, eos - stack);
+
+ if (ps->verbosity)
+ fprintf (ps->out,
+ "%s%u core variables out of %u (%.1f%%)\n"
+ "%s%u core original clauses out of %u (%.1f%%)\n"
+ "%s%u core learned clauses out of %u (%.1f%%)\n",
+ ps->prefix, vcore, ps->max_var, PERCENT (vcore, ps->max_var),
+ ps->prefix, ps->ocore, ps->oadded, PERCENT (ps->ocore, ps->oadded),
+ ps->prefix, lcore, ps->ladded, PERCENT (lcore, ps->ladded));
+
+ return ps->ocore;
+}
+
+static void
+trace_lits (PS * ps, Cls * c, FILE * file)
+{
+ Lit **p, **eol = end_of_lits (c);
+
+ assert (c);
+ assert (c->core);
+
+ for (p = c->lits; p < eol; p++)
+ fprintf (file, "%d ", LIT2INT (*p));
+
+ fputc ('0', file);
+}
+
+static void
+write_idx (PS * ps, unsigned idx, FILE * file)
+{
+ fprintf (file, "%ld", EXPORTIDX (idx));
+}
+
+static void
+trace_clause (PS * ps, unsigned idx, Cls * c, FILE * file, int fmt)
+{
+ assert (c);
+ assert (c->core);
+ assert (fmt == RUP_TRACE_FMT || !c->learned);
+ assert (CLS2IDX (c) == idx);
+
+ if (fmt != RUP_TRACE_FMT)
+ {
+ write_idx (ps, idx, file);
+ fputc (' ', file);
+ }
+
+ trace_lits (ps, c, file);
+
+ if (fmt != RUP_TRACE_FMT)
+ fputs (" 0", file);
+
+ fputc ('\n', file);
+}
+
+static void
+trace_zhain (PS * ps, unsigned idx, Zhn * zhain, FILE * file, int fmt)
+{
+ unsigned prev, this, delta, i;
+ Znt *p, byte;
+ Cls * c;
+
+ assert (zhain);
+ assert (zhain->core);
+
+ write_idx (ps, idx, file);
+ fputc (' ', file);
+
+ if (fmt == EXTENDED_TRACECHECK_TRACE_FMT)
+ {
+ c = IDX2CLS (idx);
+ assert (c);
+ trace_lits (ps, c, file);
+ }
+ else
+ {
+ assert (fmt == COMPACT_TRACECHECK_TRACE_FMT);
+ putc ('*', file);
+ }
+
+ i = 0;
+ delta = 0;
+ prev = 0;
+
+ for (p = zhain->znt; (byte = *p); p++, i += 7)
+ {
+ delta |= (byte & 0x7f) << i;
+ if (byte & 0x80)
+ continue;
+
+ this = prev + delta;
+
+ putc (' ', file);
+ write_idx (ps, this, file);
+
+ prev = this;
+ delta = 0;
+ i = -7;
+ }
+
+ fputs (" 0\n", file);
+}
+
+static void
+write_core (PS * ps, FILE * file)
+{
+ Lit **q, **eol;
+ Cls **p, *c;
+
+ fprintf (file, "p cnf %u %u\n", ps->max_var, core (ps));
+
+ for (p = SOC; p != EOC; p = NXC (p))
+ {
+ c = *p;
+
+ if (!c || c->learned || !c->core)
+ continue;
+
+ eol = end_of_lits (c);
+ for (q = c->lits; q < eol; q++)
+ fprintf (file, "%d ", LIT2INT (*q));
+
+ fputs ("0\n", file);
+ }
+}
+
+#endif
+
+static void
+write_trace (PS * ps, FILE * file, int fmt)
+{
+#ifdef TRACE
+ Cls *c, ** p;
+ Zhn *zhain;
+ unsigned i;
+
+ core (ps);
+
+ if (fmt == RUP_TRACE_FMT)
+ {
+ ps->rupvariables = picosat_variables (ps),
+ ps->rupclauses = picosat_added_original_clauses (ps);
+ write_rup_header (ps, file);
+ }
+
+ for (p = SOC; p != EOC; p = NXC (p))
+ {
+ c = *p;
+
+ if (ps->oclauses <= p && p < ps->eoo)
+ {
+ i = OIDX2IDX (p - ps->oclauses);
+ assert (!c || CLS2IDX (c) == i);
+ }
+ else
+ {
+ assert (ps->lclauses <= p && p < ps->EOL);
+ i = LIDX2IDX (p - ps->lclauses);
+ }
+
+ zhain = IDX2ZHN (i);
+
+ if (zhain)
+ {
+ if (zhain->core)
+ {
+ if (fmt == RUP_TRACE_FMT)
+ trace_clause (ps,i, c, file, fmt);
+ else
+ trace_zhain (ps, i, zhain, file, fmt);
+ }
+ }
+ else if (c)
+ {
+ if (fmt != RUP_TRACE_FMT && c)
+ {
+ if (c->core)
+ trace_clause (ps, i, c, file, fmt);
+ }
+ }
+ }
+#else
+ (void) file;
+ (void) fmt;
+ (void) ps;
+#endif
+}
+
+static void
+write_core_wrapper (PS * ps, FILE * file, int fmt)
+{
+ (void) fmt;
+#ifdef TRACE
+ write_core (ps, file);
+#else
+ (void) ps;
+ (void) file;
+#endif
+}
+
+static Lit *
+import_lit (PS * ps, int lit, int nointernal)
+{
+ Lit * res;
+ Var * v;
+
+ ABORTIF (lit == INT_MIN, "API usage: INT_MIN literal");
+ ABORTIF (abs (lit) > (int) ps->max_var && ps->CLS != ps->clshead,
+ "API usage: new variable index after 'picosat_push'");
+
+ if (abs (lit) <= (int) ps->max_var)
+ {
+ res = int2lit (ps, lit);
+ v = LIT2VAR (res);
+ if (nointernal && v->internal)
+ ABORT ("API usage: trying to import invalid literal");
+ else if (!nointernal && !v->internal)
+ ABORT ("API usage: trying to import invalid context");
+ }
+ else
+ {
+ while (abs (lit) > (int) ps->max_var)
+ inc_max_var (ps);
+ res = int2lit (ps, lit);
+ }
+
+ return res;
+}
+
+#ifdef TRACE
+static void
+reset_core (PS * ps)
+{
+ Cls ** p, * c;
+ Zhn ** q, * z;
+ unsigned i;
+
+ for (i = 1; i <= ps->max_var; i++)
+ ps->vars[i].core = 0;
+
+ for (p = SOC; p != EOC; p = NXC (p))
+ if ((c = *p))
+ c->core = 0;
+
+ for (q = ps->zhains; q != ps->zhead; q++)
+ if ((z = *q))
+ z->core = 0;
+
+ ps->ocore = -1;
+}
+#endif
+
+static void
+reset_assumptions (PS * ps)
+{
+ Lit ** p;
+
+ ps->failed_assumption = 0;
+
+ if (ps->extracted_all_failed_assumptions)
+ {
+ for (p = ps->als; p < ps->alshead; p++)
+ LIT2VAR (*p)->failed = 0;
+
+ ps->extracted_all_failed_assumptions = 0;
+ }
+
+ ps->alstail = ps->alshead = ps->als;
+ ps->adecidelevel = 0;
+}
+
+static void
+check_ready (PS * ps)
+{
+ ABORTIF (!ps || ps->state == RESET, "API usage: uninitialized");
+}
+
+static void
+check_sat_state (PS * ps)
+{
+ ABORTIF (ps->state != SAT, "API usage: expected to be in SAT state");
+}
+
+static void
+check_unsat_state (PS * ps)
+{
+ ABORTIF (ps->state != UNSAT, "API usage: expected to be in UNSAT state");
+}
+
+static void
+check_sat_or_unsat_or_unknown_state (PS * ps)
+{
+ ABORTIF (ps->state != SAT && ps->state != UNSAT && ps->state != UNKNOWN,
+ "API usage: expected to be in SAT, UNSAT, or UNKNOWN state");
+}
+
+static void
+reset_partial (PS * ps)
+{
+ unsigned idx;
+ if (!ps->partial)
+ return;
+ for (idx = 1; idx <= ps->max_var; idx++)
+ ps->vars[idx].partial = 0;
+ ps->partial = 0;
+}
+
+static void
+reset_incremental_usage (PS * ps)
+{
+ unsigned num_non_false;
+ Lit * lit, ** q;
+
+ check_sat_or_unsat_or_unknown_state (ps);
+
+ LOG ( fprintf (ps->out, "%sRESET incremental usage\n", ps->prefix));
+
+ if (ps->LEVEL)
+ undo (ps, 0);
+
+ reset_assumptions (ps);
+
+ if (ps->conflict)
+ {
+ num_non_false = 0;
+ for (q = ps->conflict->lits; q < end_of_lits (ps->conflict); q++)
+ {
+ lit = *q;
+ if (lit->val != FALSE)
+ num_non_false++;
+ }
+
+ // assert (num_non_false >= 2); // TODO: why this assertion?
+#ifdef NO_BINARY_CLAUSES
+ if (ps->conflict == &ps->cimpl)
+ resetcimpl (ps);
+#endif
+#ifndef NADC
+ if (ps->conflict == ps->adoconflict)
+ resetadoconflict (ps);
+#endif
+ ps->conflict = 0;
+ }
+
+#ifdef TRACE
+ reset_core (ps);
+#endif
+
+ reset_partial (ps);
+
+ ps->saved_flips = ps->flips;
+ ps->min_flipped = UINT_MAX;
+ ps->saved_max_var = ps->max_var;
+
+ ps->state = READY;
+}
+
+static void
+enter (PS * ps)
+{
+ if (ps->nentered++)
+ return;
+
+ check_ready (ps);
+ ps->entered = picosat_time_stamp ();
+}
+
+static void
+leave (PS * ps)
+{
+ assert (ps->nentered);
+ if (--ps->nentered)
+ return;
+
+ sflush (ps);
+}
+
+static void
+check_trace_support_and_execute (PS * ps,
+ FILE * file,
+ void (*f)(PS*,FILE*,int), int fmt)
+{
+ check_ready (ps);
+ check_unsat_state (ps);
+#ifdef TRACE
+ ABORTIF (!ps->trace, "API usage: tracing disabled");
+ enter (ps);
+ f (ps, file, fmt);
+ leave (ps);
+#else
+ (void) file;
+ (void) fmt;
+ (void) f;
+ ABORT ("compiled without trace support");
+#endif
+}
+
+static void
+extract_all_failed_assumptions (PS * ps)
+{
+ Lit ** p, ** eol;
+ Var * v, * u;
+ int pos;
+ Cls * c;
+
+ assert (!ps->extracted_all_failed_assumptions);
+
+ assert (ps->failed_assumption);
+ assert (ps->mhead == ps->marked);
+
+ if (ps->marked == ps->eom)
+ ENLARGE (ps->marked, ps->mhead, ps->eom);
+
+ v = LIT2VAR (ps->failed_assumption);
+ mark_var (ps, v);
+ pos = 0;
+
+ while (pos < ps->mhead - ps->marked)
+ {
+ v = ps->marked[pos++];
+ assert (v->mark);
+ c = var2reason (ps, v);
+ if (!c)
+ continue;
+ eol = end_of_lits (c);
+ for (p = c->lits; p < eol; p++)
+ {
+ u = LIT2VAR (*p);
+ if (!u->mark)
+ mark_var (ps, u);
+ }
+#ifdef NO_BINARY_CLAUSES
+ if (c == &ps->impl)
+ resetimpl (ps);
+#endif
+ }
+
+ for (p = ps->als; p < ps->alshead; p++)
+ {
+ u = LIT2VAR (*p);
+ if (!u->mark) continue;
+ u->failed = 1;
+ LOG ( fprintf (ps->out,
+ "%sfailed assumption %d\n",
+ ps->prefix, LIT2INT (*p)));
+ }
+
+ while (ps->mhead > ps->marked)
+ (*--ps->mhead)->mark = 0;
+
+ ps->extracted_all_failed_assumptions = 1;
+}
+
+const char *
+picosat_copyright (void)
+{
+ return "Copyright (c) 2006 - 2014 Armin Biere JKU Linz";
+}
+
+PicoSAT *
+picosat_init (void)
+{
+ return init (0, 0, 0, 0);
+}
+
+PicoSAT *
+picosat_minit (void * pmgr,
+ picosat_malloc pnew,
+ picosat_realloc presize,
+ picosat_free pfree)
+{
+ ABORTIF (!pnew, "API usage: zero 'picosat_malloc' argument");
+ ABORTIF (!presize, "API usage: zero 'picosat_realloc' argument");
+ ABORTIF (!pfree, "API usage: zero 'picosat_free' argument");
+ return init (pmgr, pnew, presize, pfree);
+}
+
+
+void
+picosat_adjust (PS * ps, int new_max_var)
+{
+ unsigned new_size_vars;
+
+ ABORTIF (abs (new_max_var) > (int) ps->max_var && ps->CLS != ps->clshead,
+ "API usage: adjusting variable index after 'picosat_push'");
+ enter (ps);
+
+ new_max_var = abs (new_max_var);
+ new_size_vars = new_max_var + 1;
+
+ if (ps->size_vars < new_size_vars)
+ enlarge (ps, new_size_vars);
+
+ while (ps->max_var < (unsigned) new_max_var)
+ inc_max_var (ps);
+
+ leave (ps);
+}
+
+int
+picosat_inc_max_var (PS * ps)
+{
+ if (ps->measurealltimeinlib)
+ enter (ps);
+ else
+ check_ready (ps);
+
+ inc_max_var (ps);
+
+ if (ps->measurealltimeinlib)
+ leave (ps);
+
+ return ps->max_var;
+}
+
+int
+picosat_context (PS * ps)
+{
+ return ps->clshead == ps->CLS ? 0 : LIT2INT (ps->clshead[-1]);
+}
+
+int
+picosat_push (PS * ps)
+{
+ int res;
+ Lit *lit;
+ Var * v;
+
+ if (ps->measurealltimeinlib)
+ enter (ps);
+ else
+ check_ready (ps);
+
+ if (ps->state != READY)
+ reset_incremental_usage (ps);
+
+ if (ps->rils != ps->rilshead)
+ {
+ res = *--ps->rilshead;
+ assert (ps->vars[res].internal);
+ }
+ else
+ {
+ inc_max_var (ps);
+ res = ps->max_var;
+ v = ps->vars + res;
+ assert (!v->internal);
+ v->internal = 1;
+ ps->internals++;
+ LOG ( fprintf (ps->out, "%snew internal variable index %d\n", ps->prefix, res));
+ }
+
+ lit = int2lit (ps, res);
+
+ if (ps->clshead == ps->eocls)
+ ENLARGE (ps->CLS, ps->clshead, ps->eocls);
+ *ps->clshead++ = lit;
+
+ ps->contexts++;
+
+ LOG ( fprintf (ps->out, "%snew context %d at depth %ld after push\n",
+ ps->prefix, res, (long)(ps->clshead - ps->CLS)));
+
+ if (ps->measurealltimeinlib)
+ leave (ps);
+
+ return res;
+}
+
+int
+picosat_pop (PS * ps)
+{
+ Lit * lit;
+ int res;
+ ABORTIF (ps->CLS == ps->clshead, "API usage: too many 'picosat_pop'");
+ ABORTIF (ps->added != ps->ahead, "API usage: incomplete clause");
+
+ if (ps->measurealltimeinlib)
+ enter (ps);
+ else
+ check_ready (ps);
+
+ if (ps->state != READY)
+ reset_incremental_usage (ps);
+
+ assert (ps->CLS < ps->clshead);
+ lit = *--ps->clshead;
+ LOG ( fprintf (ps->out, "%sclosing context %d at depth %ld after pop\n",
+ ps->prefix, LIT2INT (lit), (long)(ps->clshead - ps->CLS) + 1));
+
+ if (ps->cilshead == ps->eocils)
+ ENLARGE (ps->cils, ps->cilshead, ps->eocils);
+ *ps->cilshead++ = LIT2INT (lit);
+
+ if (ps->cilshead - ps->cils > MAXCILS) {
+ LOG ( fprintf (ps->out,
+ "%srecycling %ld interals with forced simplification\n",
+ ps->prefix, (long)(ps->cilshead - ps->cils)));
+ simplify (ps, 1);
+ }
+
+ res = picosat_context (ps);
+ if (res)
+ LOG ( fprintf (ps->out, "%snew context %d at depth %ld after pop\n",
+ ps->prefix, res, (long)(ps->clshead - ps->CLS)));
+ else
+ LOG ( fprintf (ps->out, "%souter most context reached after pop\n", ps->prefix));
+
+ if (ps->measurealltimeinlib)
+ leave (ps);
+
+ return res;
+}
+
+void
+picosat_set_verbosity (PS * ps, int new_verbosity_level)
+{
+ check_ready (ps);
+ ps->verbosity = new_verbosity_level;
+}
+
+void
+picosat_set_plain (PS * ps, int new_plain_value)
+{
+ check_ready (ps);
+ ps->plain = new_plain_value;
+}
+
+int
+picosat_enable_trace_generation (PS * ps)
+{
+ int res = 0;
+ check_ready (ps);
+#ifdef TRACE
+ ABORTIF (ps->addedclauses,
+ "API usage: trace generation enabled after adding clauses");
+ res = ps->trace = 1;
+#endif
+ return res;
+}
+
+void
+picosat_set_incremental_rup_file (PS * ps, FILE * rup_file, int m, int n)
+{
+ check_ready (ps);
+ assert (!ps->rupstarted);
+ ps->rup = rup_file;
+ ps->rupvariables = m;
+ ps->rupclauses = n;
+}
+
+void
+picosat_set_output (PS * ps, FILE * output_file)
+{
+ check_ready (ps);
+ ps->out = output_file;
+}
+
+void
+picosat_measure_all_calls (PS * ps)
+{
+ check_ready (ps);
+ ps->measurealltimeinlib = 1;
+}
+
+void
+picosat_set_prefix (PS * ps, const char * str)
+{
+ check_ready (ps);
+ new_prefix (ps, str);
+}
+
+void
+picosat_set_seed (PS * ps, unsigned s)
+{
+ check_ready (ps);
+ ps->srng = s;
+}
+
+void
+picosat_reset (PS * ps)
+{
+ check_ready (ps);
+ reset (ps);
+}
+
+int
+picosat_add (PS * ps, int int_lit)
+{
+ int res = ps->oadded;
+ Lit *lit;
+
+ if (ps->measurealltimeinlib)
+ enter (ps);
+ else
+ check_ready (ps);
+
+ ABORTIF (ps->rup && ps->rupstarted && ps->oadded >= (unsigned)ps->rupclauses,
+ "API usage: adding too many clauses after RUP header written");
+#ifndef NADC
+ ABORTIF (ps->addingtoado,
+ "API usage: 'picosat_add' and 'picosat_add_ado_lit' mixed");
+#endif
+ if (ps->state != READY)
+ reset_incremental_usage (ps);
+
+ if (ps->saveorig)
+ {
+ if (ps->sohead == ps->eoso)
+ ENLARGE (ps->soclauses, ps->sohead, ps->eoso);
+
+ *ps->sohead++ = int_lit;
+ }
+
+ if (int_lit)
+ {
+ lit = import_lit (ps, int_lit, 1);
+ add_lit (ps, lit);
+ }
+ else
+ simplify_and_add_original_clause (ps);
+
+ if (ps->measurealltimeinlib)
+ leave (ps);
+
+ return res;
+}
+
+int
+picosat_add_arg (PS * ps, ...)
+{
+ int lit;
+ va_list ap;
+ va_start (ap, ps);
+ while ((lit = va_arg (ap, int)))
+ (void) picosat_add (ps, lit);
+ va_end (ap);
+ return picosat_add (ps, 0);
+}
+
+int
+picosat_add_lits (PS * ps, int * lits)
+{
+ const int * p;
+ int lit;
+ for (p = lits; (lit = *p); p++)
+ (void) picosat_add (ps, lit);
+ return picosat_add (ps, 0);
+}
+
+void
+picosat_add_ado_lit (PS * ps, int external_lit)
+{
+#ifndef NADC
+ Lit * internal_lit;
+
+ if (ps->measurealltimeinlib)
+ enter (ps);
+ else
+ check_ready (ps);
+
+ if (ps->state != READY)
+ reset_incremental_usage (ps);
+
+ ABORTIF (!ps->addingtoado && ps->ahead > ps->added,
+ "API usage: 'picosat_add' and 'picosat_add_ado_lit' mixed");
+
+ if (external_lit)
+ {
+ ps->addingtoado = 1;
+ internal_lit = import_lit (ps, external_lit, 1);
+ add_lit (ps, internal_lit);
+ }
+ else
+ {
+ ps->addingtoado = 0;
+ add_ado (ps);
+ }
+ if (ps->measurealltimeinlib)
+ leave (ps);
+#else
+ (void) ps;
+ (void) external_lit;
+ ABORT ("compiled without all different constraint support");
+#endif
+}
+
+static void
+assume (PS * ps, Lit * lit)
+{
+ if (ps->alshead == ps->eoals)
+ {
+ assert (ps->alstail == ps->als);
+ ENLARGE (ps->als, ps->alshead, ps->eoals);
+ ps->alstail = ps->als;
+ }
+
+ *ps->alshead++ = lit;
+ LOG ( fprintf (ps->out, "%sassumption %d\n", ps->prefix, LIT2INT (lit)));
+}
+
+static void
+assume_contexts (PS * ps)
+{
+ Lit ** p;
+ if (ps->als != ps->alshead)
+ return;
+ for (p = ps->CLS; p != ps->clshead; p++)
+ assume (ps, *p);
+}
+
+#ifndef RCODE
+static const char * enumstr (int i) {
+ int last = i % 10;
+ if (last == 1) return "st";
+ if (last == 2) return "nd";
+ if (last == 3) return "rd";
+ return "th";
+}
+#endif
+
+static int
+tderef (PS * ps, int int_lit)
+{
+ Lit * lit;
+ Var * v;
+
+ assert (abs (int_lit) <= (int) ps->max_var);
+
+ lit = int2lit (ps, int_lit);
+
+ v = LIT2VAR (lit);
+ if (v->level > 0)
+ return 0;
+
+ if (lit->val == TRUE)
+ return 1;
+
+ if (lit->val == FALSE)
+ return -1;
+
+ return 0;
+}
+
+static int
+pderef (PS * ps, int int_lit)
+{
+ Lit * lit;
+ Var * v;
+
+ assert (abs (int_lit) <= (int) ps->max_var);
+
+ v = ps->vars + abs (int_lit);
+ if (!v->partial)
+ return 0;
+
+ lit = int2lit (ps, int_lit);
+
+ if (lit->val == TRUE)
+ return 1;
+
+ if (lit->val == FALSE)
+ return -1;
+
+ return 0;
+}
+
+static void
+minautarky (PS * ps)
+{
+ unsigned * occs, maxoccs, tmpoccs, npartial;
+ int * p, * c, lit, best, val;
+#ifdef LOGGING
+ int tl;
+#endif
+
+ assert (!ps->partial);
+
+ npartial = 0;
+
+ NEWN (occs, 2*ps->max_var + 1);
+ CLRN (occs, 2*ps->max_var + 1);
+ occs += ps->max_var;
+ for (p = ps->soclauses; p < ps->sohead; p++)
+ occs[*p]++;
+ assert (occs[0] == ps->oadded);
+
+ for (c = ps->soclauses; c < ps->sohead; c = p + 1)
+ {
+#ifdef LOGGING
+ tl = 0;
+#endif
+ best = 0;
+ maxoccs = 0;
+ for (p = c; (lit = *p); p++)
+ {
+ val = tderef (ps, lit);
+ if (val < 0)
+ continue;
+ if (val > 0)
+ {
+#ifdef LOGGING
+ tl = 1;
+#endif
+ best = lit;
+ maxoccs = occs[lit];
+ }
+
+ val = pderef (ps, lit);
+ if (val > 0)
+ break;
+ if (val < 0)
+ continue;
+ val = int2lit (ps, lit)->val;
+ assert (val);
+ if (val < 0)
+ continue;
+ tmpoccs = occs[lit];
+ if (best && tmpoccs <= maxoccs)
+ continue;
+ best = lit;
+ maxoccs = tmpoccs;
+ }
+ if (!lit)
+ {
+ assert (best);
+ LOG ( fprintf (ps->out, "%sautark %d with %d occs%s\n",
+ ps->prefix, best, maxoccs, tl ? " (top)" : ""));
+ ps->vars[abs (best)].partial = 1;
+ npartial++;
+ }
+ for (p = c; (lit = *p); p++)
+ {
+ assert (occs[lit] > 0);
+ occs[lit]--;
+ }
+ }
+ occs -= ps->max_var;
+ DELETEN (occs, 2*ps->max_var + 1);
+ ps->partial = 1;
+
+ if (ps->verbosity)
+ fprintf (ps->out,
+ "%sautarky of size %u out of %u satisfying all clauses (%.1f%%)\n",
+ ps->prefix, npartial, ps->max_var, PERCENT (npartial, ps->max_var));
+}
+
+void
+picosat_assume (PS * ps, int int_lit)
+{
+ Lit *lit;
+
+ if (ps->measurealltimeinlib)
+ enter (ps);
+ else
+ check_ready (ps);
+
+ if (ps->state != READY)
+ reset_incremental_usage (ps);
+
+ assume_contexts (ps);
+ lit = import_lit (ps, int_lit, 1);
+ assume (ps, lit);
+
+ if (ps->measurealltimeinlib)
+ leave (ps);
+}
+
+int
+picosat_sat (PS * ps, int l)
+{
+ int res;
+ char ch;
+
+ enter (ps);
+
+ ps->calls++;
+ LOG ( fprintf (ps->out, "%sSTART call %u\n", ps->prefix, ps->calls));
+
+ if (ps->added < ps->ahead)
+ {
+#ifndef NADC
+ if (ps->addingtoado)
+ ABORT ("API usage: incomplete all different constraint");
+ else
+#endif
+ ABORT ("API usage: incomplete clause");
+ }
+
+ if (ps->state != READY)
+ reset_incremental_usage (ps);
+
+ assume_contexts (ps);
+
+ res = sat (ps, l);
+
+ assert (ps->state == READY);
+
+ switch (res)
+ {
+ case PICOSAT_UNSATISFIABLE:
+ ch = '0';
+ ps->state = UNSAT;
+ break;
+ case PICOSAT_SATISFIABLE:
+ ch = '1';
+ ps->state = SAT;
+ break;
+ default:
+ ch = '?';
+ ps->state = UNKNOWN;
+ break;
+ }
+
+ if (ps->verbosity)
+ {
+ report (ps, 1, ch);
+ rheader (ps);
+ }
+
+ leave (ps);
+ LOG ( fprintf (ps->out, "%sEND call %u result %d\n", ps->prefix, ps->calls, res));
+
+ ps->last_sat_call_result = res;
+
+ return res;
+}
+
+int
+picosat_res (PS * ps)
+{
+ return ps->last_sat_call_result;
+}
+
+int
+picosat_deref (PS * ps, int int_lit)
+{
+ Lit *lit;
+
+ check_ready (ps);
+ check_sat_state (ps);
+ ABORTIF (!int_lit, "API usage: can not deref zero literal");
+ ABORTIF (ps->mtcls, "API usage: deref after empty clause generated");
+
+#ifdef STATS
+ ps->derefs++;
+#endif
+
+ if (abs (int_lit) > (int) ps->max_var)
+ return 0;
+
+ lit = int2lit (ps, int_lit);
+
+ if (lit->val == TRUE)
+ return 1;
+
+ if (lit->val == FALSE)
+ return -1;
+
+ return 0;
+}
+
+int
+picosat_deref_toplevel (PS * ps, int int_lit)
+{
+ check_ready (ps);
+ ABORTIF (!int_lit, "API usage: can not deref zero literal");
+
+#ifdef STATS
+ ps->derefs++;
+#endif
+ if (abs (int_lit) > (int) ps->max_var)
+ return 0;
+
+ return tderef (ps, int_lit);
+}
+
+int
+picosat_inconsistent (PS * ps)
+{
+ check_ready (ps);
+ return ps->mtcls != 0;
+}
+
+int
+picosat_corelit (PS * ps, int int_lit)
+{
+ check_ready (ps);
+ check_unsat_state (ps);
+ ABORTIF (!int_lit, "API usage: zero literal can not be in core");
+
+ assert (ps->mtcls || ps->failed_assumption);
+
+#ifdef TRACE
+ {
+ int res = 0;
+ ABORTIF (!ps->trace, "tracing disabled");
+ if (ps->measurealltimeinlib)
+ enter (ps);
+ core (ps);
+ if (abs (int_lit) <= (int) ps->max_var)
+ res = ps->vars[abs (int_lit)].core;
+ assert (!res || ps->failed_assumption || ps->vars[abs (int_lit)].used);
+ if (ps->measurealltimeinlib)
+ leave (ps);
+ return res;
+ }
+#else
+ ABORT ("compiled without trace support");
+ return 0;
+#endif
+}
+
+int
+picosat_coreclause (PS * ps, int ocls)
+{
+ check_ready (ps);
+ check_unsat_state (ps);
+
+ ABORTIF (ocls < 0, "API usage: negative original clause index");
+ ABORTIF (ocls >= (int)ps->oadded, "API usage: original clause index exceeded");
+
+ assert (ps->mtcls || ps->failed_assumption);
+
+#ifdef TRACE
+ {
+ Cls ** clsptr, * c;
+ int res = 0;
+
+ ABORTIF (!ps->trace, "tracing disabled");
+ if (ps->measurealltimeinlib)
+ enter (ps);
+ core (ps);
+ clsptr = ps->oclauses + ocls;
+ assert (clsptr < ps->ohead);
+ c = *clsptr;
+ if (c)
+ res = c->core;
+ if (ps->measurealltimeinlib)
+ leave (ps);
+
+ return res;
+ }
+#else
+ ABORT ("compiled without trace support");
+ return 0;
+#endif
+}
+
+int
+picosat_failed_assumption (PS * ps, int int_lit)
+{
+ Lit * lit;
+ Var * v;
+ ABORTIF (!int_lit, "API usage: zero literal as assumption");
+ check_ready (ps);
+ check_unsat_state (ps);
+ if (ps->mtcls)
+ return 0;
+ assert (ps->failed_assumption);
+ if (abs (int_lit) > (int) ps->max_var)
+ return 0;
+ if (!ps->extracted_all_failed_assumptions)
+ extract_all_failed_assumptions (ps);
+ lit = import_lit (ps, int_lit, 1);
+ v = LIT2VAR (lit);
+ return v->failed;
+}
+
+int
+picosat_failed_context (PS * ps, int int_lit)
+{
+ Lit * lit;
+ Var * v;
+ ABORTIF (!int_lit, "API usage: zero literal as context");
+ ABORTIF (abs (int_lit) > (int) ps->max_var, "API usage: invalid context");
+ check_ready (ps);
+ check_unsat_state (ps);
+ assert (ps->failed_assumption);
+ if (!ps->extracted_all_failed_assumptions)
+ extract_all_failed_assumptions (ps);
+ lit = import_lit (ps, int_lit, 0);
+ v = LIT2VAR (lit);
+ return v->failed;
+}
+
+const int *
+picosat_failed_assumptions (PS * ps)
+{
+ Lit ** p, * lit;
+ Var * v;
+ int ilit;
+
+ ps->falshead = ps->fals;
+ check_ready (ps);
+ check_unsat_state (ps);
+ if (!ps->mtcls)
+ {
+ assert (ps->failed_assumption);
+ if (!ps->extracted_all_failed_assumptions)
+ extract_all_failed_assumptions (ps);
+
+ for (p = ps->als; p < ps->alshead; p++)
+ {
+ lit = *p;
+ v = LIT2VAR (*p);
+ if (!v->failed)
+ continue;
+ ilit = LIT2INT (lit);
+ if (ps->falshead == ps->eofals)
+ ENLARGE (ps->fals, ps->falshead, ps->eofals);
+ *ps->falshead++ = ilit;
+ }
+ }
+ if (ps->falshead == ps->eofals)
+ ENLARGE (ps->fals, ps->falshead, ps->eofals);
+ *ps->falshead++ = 0;
+ return ps->fals;
+}
+
+const int *
+picosat_mus_assumptions (PS * ps, void * s, void (*cb)(void*,const int*), int fix)
+{
+ int i, j, ilit, len, nwork, * work, res;
+ signed char * redundant;
+ Lit ** p, * lit;
+ int failed;
+ Var * v;
+#ifndef NDEBUG
+ int oldlen;
+#endif
+#ifndef RCODE
+ int norig = ps->alshead - ps->als;
+#endif
+
+ check_ready (ps);
+ check_unsat_state (ps);
+ len = 0;
+ if (!ps->mtcls)
+ {
+ assert (ps->failed_assumption);
+ if (!ps->extracted_all_failed_assumptions)
+ extract_all_failed_assumptions (ps);
+
+ for (p = ps->als; p < ps->alshead; p++)
+ if (LIT2VAR (*p)->failed)
+ len++;
+ }
+
+ if (ps->mass)
+ DELETEN (ps->mass, ps->szmass);
+ ps->szmass = len + 1;
+ NEWN (ps->mass, ps->szmass);
+
+ i = 0;
+ for (p = ps->als; p < ps->alshead; p++)
+ {
+ lit = *p;
+ v = LIT2VAR (lit);
+ if (!v->failed)
+ continue;
+ ilit = LIT2INT (lit);
+ assert (i < len);
+ ps->mass[i++] = ilit;
+ }
+ assert (i == len);
+ ps->mass[i] = 0;
+ if (ps->verbosity)
+ fprintf (ps->out,
+ "%sinitial set of failed assumptions of size %d out of %d (%.0f%%)\n",
+ ps->prefix, len, norig, PERCENT (len, norig));
+ if (cb)
+ cb (s, ps->mass);
+
+ nwork = len;
+ NEWN (work, nwork);
+ for (i = 0; i < len; i++)
+ work[i] = ps->mass[i];
+
+ NEWN (redundant, nwork);
+ CLRN (redundant, nwork);
+
+ for (i = 0; i < nwork; i++)
+ {
+ if (redundant[i])
+ continue;
+
+ if (ps->verbosity > 1)
+ fprintf (ps->out,
+ "%strying to drop %d%s assumption %d\n",
+ ps->prefix, i, enumstr (i), work[i]);
+ for (j = 0; j < nwork; j++)
+ {
+ if (i == j) continue;
+ if (j < i && fix) continue;
+ if (redundant[j]) continue;
+ picosat_assume (ps, work[j]);
+ }
+
+ res = picosat_sat (ps, -1);
+ if (res == 10)
+ {
+ if (ps->verbosity > 1)
+ fprintf (ps->out,
+ "%sfailed to drop %d%s assumption %d\n",
+ ps->prefix, i, enumstr (i), work[i]);
+
+ if (fix)
+ {
+ picosat_add (ps, work[i]);
+ picosat_add (ps, 0);
+ }
+ }
+ else
+ {
+ assert (res == 20);
+ if (ps->verbosity > 1)
+ fprintf (ps->out,
+ "%ssuceeded to drop %d%s assumption %d\n",
+ ps->prefix, i, enumstr (i), work[i]);
+ redundant[i] = 1;
+ for (j = 0; j < nwork; j++)
+ {
+ failed = picosat_failed_assumption (ps, work[j]);
+ if (j <= i)
+ {
+ assert ((j < i && fix) || redundant[j] == !failed);
+ continue;
+ }
+
+ if (!failed)
+ {
+ redundant[j] = -1;
+ if (ps->verbosity > 1)
+ fprintf (ps->out,
+ "%salso suceeded to drop %d%s assumption %d\n",
+ ps->prefix, j, enumstr (j), work[j]);
+ }
+ }
+
+#ifndef NDEBUG
+ oldlen = len;
+#endif
+ len = 0;
+ for (j = 0; j < nwork; j++)
+ if (!redundant[j])
+ ps->mass[len++] = work[j];
+ ps->mass[len] = 0;
+ assert (len < oldlen);
+
+ if (fix)
+ {
+ picosat_add (ps, -work[i]);
+ picosat_add (ps, 0);
+ }
+
+#ifndef NDEBUG
+ for (j = 0; j <= i; j++)
+ assert (redundant[j] >= 0);
+#endif
+ for (j = i + 1; j < nwork; j++)
+ {
+ if (redundant[j] >= 0)
+ continue;
+
+ if (fix)
+ {
+ picosat_add (ps, -work[j]);
+ picosat_add (ps, 0);
+ }
+
+ redundant[j] = 1;
+ }
+
+ if (ps->verbosity)
+ fprintf (ps->out,
+ "%sreduced set of failed assumptions of size %d out of %d (%.0f%%)\n",
+ ps->prefix, len, norig, PERCENT (len, norig));
+ if (cb)
+ cb (s, ps->mass);
+ }
+ }
+
+ DELETEN (work, nwork);
+ DELETEN (redundant, nwork);
+
+ if (ps->verbosity)
+ {
+ fprintf (ps->out, "%sreinitializing unsat state\n", ps->prefix);
+ fflush (ps->out);
+ }
+
+ for (i = 0; i < len; i++)
+ picosat_assume (ps, ps->mass[i]);
+
+#ifndef NDEBUG
+ res =
+#endif
+ picosat_sat (ps, -1);
+ assert (res == 20);
+
+ if (!ps->mtcls)
+ {
+ assert (!ps->extracted_all_failed_assumptions);
+ extract_all_failed_assumptions (ps);
+ }
+
+ return ps->mass;
+}
+
+static const int *
+mss (PS * ps, int * a, int size)
+{
+ int i, j, k, res;
+
+ assert (!ps->mtcls);
+
+ if (ps->szmssass)
+ DELETEN (ps->mssass, ps->szmssass);
+
+ ps->szmssass = 0;
+ ps->mssass = 0;
+
+ ps->szmssass = size + 1;
+ NEWN (ps->mssass, ps->szmssass);
+
+ LOG ( fprintf (ps->out, "%ssearch MSS over %d assumptions\n", ps->prefix, size));
+
+ k = 0;
+ for (i = k; i < size; i++)
+ {
+ for (j = 0; j < k; j++)
+ picosat_assume (ps, ps->mssass[j]);
+
+ LOG ( fprintf (ps->out,
+ "%strying to add assumption %d to MSS : %d\n",
+ ps->prefix, i, a[i]));
+
+ picosat_assume (ps, a[i]);
+
+ res = picosat_sat (ps, -1);
+ if (res == 10)
+ {
+ LOG ( fprintf (ps->out,
+ "%sadding assumption %d to MSS : %d\n", ps->prefix, i, a[i]));
+
+ ps->mssass[k++] = a[i];
+
+ for (j = i + 1; j < size; j++)
+ {
+ if (picosat_deref (ps, a[j]) <= 0)
+ continue;
+
+ LOG ( fprintf (ps->out,
+ "%salso adding assumption %d to MSS : %d\n",
+ ps->prefix, j, a[j]));
+
+ ps->mssass[k++] = a[j];
+
+ if (++i != j)
+ {
+ int tmp = a[i];
+ a[i] = a[j];
+ a[j] = tmp;
+ }
+ }
+ }
+ else
+ {
+ assert (res == 20);
+
+ LOG ( fprintf (ps->out,
+ "%signoring assumption %d in MSS : %d\n", ps->prefix, i, a[i]));
+ }
+ }
+ ps->mssass[k] = 0;
+ LOG ( fprintf (ps->out, "%sfound MSS of size %d\n", ps->prefix, k));
+
+ return ps->mssass;
+}
+
+static void
+reassume (PS * ps, const int * a, int size)
+{
+ int i;
+ LOG ( fprintf (ps->out, "%sreassuming all assumptions\n", ps->prefix));
+ for (i = 0; i < size; i++)
+ picosat_assume (ps, a[i]);
+}
+
+const int *
+picosat_maximal_satisfiable_subset_of_assumptions (PS * ps)
+{
+ const int * res;
+ int i, *a, size;
+
+ ABORTIF (ps->mtcls,
+ "API usage: CNF inconsistent (use 'picosat_inconsistent')");
+
+ enter (ps);
+
+ size = ps->alshead - ps->als;
+ NEWN (a, size);
+
+ for (i = 0; i < size; i++)
+ a[i] = LIT2INT (ps->als[i]);
+
+ res = mss (ps, a, size);
+ reassume (ps, a, size);
+
+ DELETEN (a, size);
+
+ leave (ps);
+
+ return res;
+}
+
+static void
+check_mss_flags_clean (PS * ps)
+{
+#ifndef NDEBUG
+ unsigned i;
+ for (i = 1; i <= ps->max_var; i++)
+ {
+ assert (!ps->vars[i].msspos);
+ assert (!ps->vars[i].mssneg);
+ }
+#else
+ (void) ps;
+#endif
+}
+
+static void
+push_mcsass (PS * ps, int lit)
+{
+ if (ps->nmcsass == ps->szmcsass)
+ {
+ ps->szmcsass = ps->szmcsass ? 2*ps->szmcsass : 1;
+ RESIZEN (ps->mcsass, ps->nmcsass, ps->szmcsass);
+ }
+
+ ps->mcsass[ps->nmcsass++] = lit;
+}
+
+static const int *
+next_mss (PS * ps, int mcs)
+{
+ int i, *a, size, mssize, mcsize, lit, inmss;
+ const int * res, * p;
+ Var * v;
+
+ if (ps->mtcls) return 0;
+
+ check_mss_flags_clean (ps);
+
+ if (mcs && ps->mcsass)
+ {
+ DELETEN (ps->mcsass, ps->szmcsass);
+ ps->nmcsass = ps->szmcsass = 0;
+ ps->mcsass = 0;
+ }
+
+ size = ps->alshead - ps->als;
+ NEWN (a, size);
+
+ for (i = 0; i < size; i++)
+ a[i] = LIT2INT (ps->als[i]);
+
+ (void) picosat_sat (ps, -1);
+
+ //TODO short cut for 'picosat_res () == 10'?
+
+ if (ps->mtcls)
+ {
+ assert (picosat_res (ps) == 20);
+ res = 0;
+ goto DONE;
+ }
+
+ res = mss (ps, a, size);
+
+ if (ps->mtcls)
+ {
+ res = 0;
+ goto DONE;
+ }
+
+ for (p = res; (lit = *p); p++)
+ {
+ v = ps->vars + abs (lit);
+ if (lit < 0)
+ {
+ assert (!v->msspos);
+ v->mssneg = 1;
+ }
+ else
+ {
+ assert (!v->mssneg);
+ v->msspos = 1;
+ }
+ }
+
+ mssize = p - res;
+ mcsize = INT_MIN;
+
+ for (i = 0; i < size; i++)
+ {
+ lit = a[i];
+ v = ps->vars + abs (lit);
+ if (lit > 0 && v->msspos)
+ inmss = 1;
+ else if (lit < 0 && v->mssneg)
+ inmss = 1;
+ else
+ inmss = 0;
+
+ if (mssize < mcsize)
+ {
+ if (inmss)
+ picosat_add (ps, -lit);
+ }
+ else
+ {
+ if (!inmss)
+ picosat_add (ps, lit);
+ }
+
+ if (!inmss && mcs)
+ push_mcsass (ps, lit);
+ }
+ picosat_add (ps, 0);
+ if (mcs)
+ push_mcsass (ps, 0);
+
+ for (i = 0; i < size; i++)
+ {
+ lit = a[i];
+ v = ps->vars + abs (lit);
+ v->msspos = 0;
+ v->mssneg = 0;
+ }
+
+DONE:
+
+ reassume (ps, a, size);
+ DELETEN (a, size);
+
+ return res;
+}
+
+const int *
+picosat_next_maximal_satisfiable_subset_of_assumptions (PS * ps)
+{
+ const int * res;
+ enter (ps);
+ res = next_mss (ps, 0);
+ leave (ps);
+ return res;
+}
+
+const int *
+picosat_next_minimal_correcting_subset_of_assumptions (PS * ps)
+{
+ const int * res, * tmp;
+ enter (ps);
+ tmp = next_mss (ps, 1);
+ res = tmp ? ps->mcsass : 0;
+ leave (ps);
+ return res;
+}
+
+const int *
+picosat_humus (PS * ps,
+ void (*callback)(void*state,int nmcs,int nhumus),
+ void * state)
+{
+ int lit, nmcs, j, nhumus;
+ const int * mcs, * p;
+ unsigned i;
+ Var * v;
+ enter (ps);
+#ifndef NDEBUG
+ for (i = 1; i <= ps->max_var; i++)
+ {
+ v = ps->vars + i;
+ assert (!v->humuspos);
+ assert (!v->humusneg);
+ }
+#endif
+ nhumus = nmcs = 0;
+ while ((mcs = picosat_next_minimal_correcting_subset_of_assumptions (ps)))
+ {
+ for (p = mcs; (lit = *p); p++)
+ {
+ v = ps->vars + abs (lit);
+ if (lit < 0)
+ {
+ if (!v->humusneg)
+ {
+ v->humusneg = 1;
+ nhumus++;
+ }
+ }
+ else
+ {
+ if (!v->humuspos)
+ {
+ v->humuspos = 1;
+ nhumus++;
+ }
+ }
+ }
+ nmcs++;
+ LOG ( fprintf (ps->out,
+ "%smcs %d of size %d humus %d\n",
+ ps->prefix, nmcs, (int)(p - mcs), nhumus));
+ if (callback)
+ callback (state, nmcs, nhumus);
+ }
+ assert (!ps->szhumus);
+ ps->szhumus = 1;
+ for (i = 1; i <= ps->max_var; i++)
+ {
+ v = ps->vars + i;
+ if (v->humuspos)
+ ps->szhumus++;
+ if (v->humusneg)
+ ps->szhumus++;
+ }
+ assert (nhumus + 1 == ps->szhumus);
+ NEWN (ps->humus, ps->szhumus);
+ j = 0;
+ for (i = 1; i <= ps->max_var; i++)
+ {
+ v = ps->vars + i;
+ if (v->humuspos)
+ {
+ assert (j < nhumus);
+ ps->humus[j++] = (int) i;
+ }
+ if (v->humusneg)
+ {
+ assert (j < nhumus);
+ assert (i < INT_MAX);
+ ps->humus[j++] = - (int) i;
+ }
+ }
+ assert (j == nhumus);
+ assert (j < ps->szhumus);
+ ps->humus[j] = 0;
+ leave (ps);
+ return ps->humus;
+}
+
+int
+picosat_usedlit (PS * ps, int int_lit)
+{
+ int res;
+ check_ready (ps);
+ check_sat_or_unsat_or_unknown_state (ps);
+ ABORTIF (!int_lit, "API usage: zero literal can not be used");
+ int_lit = abs (int_lit);
+ res = (int_lit <= (int) ps->max_var) ? ps->vars[int_lit].used : 0;
+ return res;
+}
+
+void
+picosat_write_clausal_core (PS * ps, FILE * file)
+{
+ check_trace_support_and_execute (ps, file, write_core_wrapper, 0);
+}
+
+void
+picosat_write_compact_trace (PS * ps, FILE * file)
+{
+ check_trace_support_and_execute (ps, file, write_trace,
+ COMPACT_TRACECHECK_TRACE_FMT);
+}
+
+void
+picosat_write_extended_trace (PS * ps, FILE * file)
+{
+ check_trace_support_and_execute (ps, file, write_trace,
+ EXTENDED_TRACECHECK_TRACE_FMT);
+}
+
+void
+picosat_write_rup_trace (PS * ps, FILE * file)
+{
+ check_trace_support_and_execute (ps, file, write_trace, RUP_TRACE_FMT);
+}
+
+size_t
+picosat_max_bytes_allocated (PS * ps)
+{
+ check_ready (ps);
+ return ps->max_bytes;
+}
+
+void
+picosat_set_propagation_limit (PS * ps, unsigned long long l)
+{
+ ps->lpropagations = l;
+}
+
+unsigned long long
+picosat_propagations (PS * ps)
+{
+ return ps->propagations;
+}
+
+unsigned long long
+picosat_visits (PS * ps)
+{
+ return ps->visits;
+}
+
+unsigned long long
+picosat_decisions (PS * ps)
+{
+ return ps->decisions;
+}
+
+int
+picosat_variables (PS * ps)
+{
+ check_ready (ps);
+ return (int) ps->max_var;
+}
+
+int
+picosat_added_original_clauses (PS * ps)
+{
+ check_ready (ps);
+ return (int) ps->oadded;
+}
+
+void
+picosat_stats (PS * ps)
+{
+#ifndef RCODE
+ unsigned redlits;
+#endif
+#ifdef STATS
+ check_ready (ps);
+ assert (ps->sdecisions + ps->rdecisions + ps->assumptions == ps->decisions);
+#endif
+ if (ps->calls > 1)
+ fprintf (ps->out, "%s%u calls\n", ps->prefix, ps->calls);
+ if (ps->contexts)
+ {
+ fprintf (ps->out, "%s%u contexts", ps->prefix, ps->contexts);
+#ifdef STATS
+ fprintf (ps->out, " %u internal variables", ps->internals);
+#endif
+ fprintf (ps->out, "\n");
+ }
+ fprintf (ps->out, "%s%u iterations\n", ps->prefix, ps->iterations);
+ fprintf (ps->out, "%s%u restarts", ps->prefix, ps->restarts);
+#ifdef STATS
+ fprintf (ps->out, " (%u skipped)", ps->skippedrestarts);
+#endif
+ fputc ('\n', ps->out);
+#ifndef NFL
+ fprintf (ps->out, "%s%u failed literals", ps->prefix, ps->failedlits);
+#ifdef STATS
+ fprintf (ps->out,
+ ", %u calls, %u rounds, %llu propagations",
+ ps->flcalls, ps->flrounds, ps->flprops);
+#endif
+ fputc ('\n', ps->out);
+#ifdef STATS
+ fprintf (ps->out,
+ "%sfl: %u = %.1f%% implicit, %llu oopsed, %llu tried, %llu skipped\n",
+ ps->prefix,
+ ps->ifailedlits, PERCENT (ps->ifailedlits, ps->failedlits),
+ ps->floopsed, ps->fltried, ps->flskipped);
+#endif
+#endif
+ fprintf (ps->out, "%s%u conflicts", ps->prefix, ps->conflicts);
+#ifdef STATS
+ fprintf (ps->out, " (%u uips = %.1f%%)\n", ps->uips, PERCENT(ps->uips,ps->conflicts));
+#else
+ fputc ('\n', ps->out);
+#endif
+#ifndef NADC
+ fprintf (ps->out, "%s%u adc conflicts\n", ps->prefix, ps->adoconflicts);
+#endif
+#ifdef STATS
+ fprintf (ps->out, "%s%llu dereferenced literals\n", ps->prefix, ps->derefs);
+#endif
+ fprintf (ps->out, "%s%u decisions", ps->prefix, ps->decisions);
+#ifdef STATS
+ fprintf (ps->out, " (%u random = %.2f%%",
+ ps->rdecisions, PERCENT (ps->rdecisions, ps->decisions));
+ fprintf (ps->out, ", %u assumptions", ps->assumptions);
+ fputc (')', ps->out);
+#endif
+ fputc ('\n', ps->out);
+#ifdef STATS
+ fprintf (ps->out,
+ "%s%u static phase decisions (%.1f%% of all variables)\n",
+ ps->prefix,
+ ps->staticphasedecisions, PERCENT (ps->staticphasedecisions, ps->max_var));
+#endif
+ fprintf (ps->out, "%s%u fixed variables\n", ps->prefix, ps->fixed);
+ assert (ps->nonminimizedllits >= ps->minimizedllits);
+#ifndef RCODE
+ redlits = ps->nonminimizedllits - ps->minimizedllits;
+#endif
+ fprintf (ps->out, "%s%u learned literals\n", ps->prefix, ps->llitsadded);
+ fprintf (ps->out, "%s%.1f%% deleted literals\n",
+ ps->prefix, PERCENT (redlits, ps->nonminimizedllits));
+
+#ifdef STATS
+#ifdef TRACE
+ fprintf (ps->out,
+ "%s%llu antecedents (%.1f antecedents per clause",
+ ps->prefix, ps->antecedents, AVERAGE (ps->antecedents, ps->conflicts));
+ if (ps->trace)
+ fprintf (ps->out, ", %.1f bytes/antecedent)", AVERAGE (ps->znts, ps->antecedents));
+ fputs (")\n", ps->out);
+#endif
+
+ fprintf (ps->out, "%s%llu propagations (%.1f propagations per decision)\n",
+ ps->prefix, ps->propagations, AVERAGE (ps->propagations, ps->decisions));
+ fprintf (ps->out, "%s%llu visits (%.1f per propagation)\n",
+ ps->prefix, ps->visits, AVERAGE (ps->visits, ps->propagations));
+ fprintf (ps->out,
+ "%s%llu binary clauses visited (%.1f%% %.1f per propagation)\n",
+ ps->prefix, ps->bvisits,
+ PERCENT (ps->bvisits, ps->visits),
+ AVERAGE (ps->bvisits, ps->propagations));
+ fprintf (ps->out,
+ "%s%llu ternary clauses visited (%.1f%% %.1f per propagation)\n",
+ ps->prefix, ps->tvisits,
+ PERCENT (ps->tvisits, ps->visits),
+ AVERAGE (ps->tvisits, ps->propagations));
+ fprintf (ps->out,
+ "%s%llu large clauses visited (%.1f%% %.1f per propagation)\n",
+ ps->prefix, ps->lvisits,
+ PERCENT (ps->lvisits, ps->visits),
+ AVERAGE (ps->lvisits, ps->propagations));
+ fprintf (ps->out, "%s%llu other true (%.1f%% of visited clauses)\n",
+ ps->prefix, ps->othertrue, PERCENT (ps->othertrue, ps->visits));
+ fprintf (ps->out,
+ "%s%llu other true in binary clauses (%.1f%%)"
+ ", %llu upper (%.1f%%)\n",
+ ps->prefix, ps->othertrue2, PERCENT (ps->othertrue2, ps->othertrue),
+ ps->othertrue2u, PERCENT (ps->othertrue2u, ps->othertrue2));
+ fprintf (ps->out,
+ "%s%llu other true in large clauses (%.1f%%)"
+ ", %llu upper (%.1f%%)\n",
+ ps->prefix, ps->othertruel, PERCENT (ps->othertruel, ps->othertrue),
+ ps->othertruelu, PERCENT (ps->othertruelu, ps->othertruel));
+ fprintf (ps->out, "%s%llu ternary and large traversals (%.1f per visit)\n",
+ ps->prefix, ps->traversals, AVERAGE (ps->traversals, ps->visits));
+ fprintf (ps->out, "%s%llu large traversals (%.1f per large visit)\n",
+ ps->prefix, ps->ltraversals, AVERAGE (ps->ltraversals, ps->lvisits));
+ fprintf (ps->out, "%s%llu assignments\n", ps->prefix, ps->assignments);
+#else
+ fprintf (ps->out, "%s%llu propagations\n", ps->prefix, picosat_propagations (ps));
+ fprintf (ps->out, "%s%llu visits\n", ps->prefix, picosat_visits (ps));
+#endif
+ fprintf (ps->out, "%s%.1f%% variables used\n", ps->prefix, PERCENT (ps->vused, ps->max_var));
+
+ sflush (ps);
+ fprintf (ps->out, "%s%.1f seconds in library\n", ps->prefix, ps->seconds);
+ fprintf (ps->out, "%s%.1f megaprops/second\n",
+ ps->prefix, AVERAGE (ps->propagations / 1e6f, ps->seconds));
+ fprintf (ps->out, "%s%.1f megavisits/second\n",
+ ps->prefix, AVERAGE (ps->visits / 1e6f, ps->seconds));
+ fprintf (ps->out, "%sprobing %.1f seconds %.0f%%\n",
+ ps->prefix, ps->flseconds, PERCENT (ps->flseconds, ps->seconds));
+#ifdef STATS
+ fprintf (ps->out,
+ "%srecycled %.1f MB in %u reductions\n",
+ ps->prefix, ps->rrecycled / (double) (1 << 20), ps->reductions);
+ fprintf (ps->out,
+ "%srecycled %.1f MB in %u simplifications\n",
+ ps->prefix, ps->srecycled / (double) (1 << 20), ps->simps);
+#else
+ fprintf (ps->out, "%s%u simplifications\n", ps->prefix, ps->simps);
+ fprintf (ps->out, "%s%u reductions\n", ps->prefix, ps->reductions);
+ fprintf (ps->out, "%s%.1f MB recycled\n", ps->prefix, ps->recycled / (double) (1 << 20));
+#endif
+ fprintf (ps->out, "%s%.1f MB maximally allocated\n",
+ ps->prefix, picosat_max_bytes_allocated (ps) / (double) (1 << 20));
+}
+
+#ifndef NGETRUSAGE
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <sys/unistd.h>
+#endif
+
+double
+picosat_time_stamp (void)
+{
+ double res = -1;
+#ifndef NGETRUSAGE
+ struct rusage u;
+ res = 0;
+ if (!getrusage (RUSAGE_SELF, &u))
+ {
+ res += u.ru_utime.tv_sec + 1e-6 * u.ru_utime.tv_usec;
+ res += u.ru_stime.tv_sec + 1e-6 * u.ru_stime.tv_usec;
+ }
+#endif
+ return res;
+}
+
+double
+picosat_seconds (PS * ps)
+{
+ check_ready (ps);
+ return ps->seconds;
+}
+
+void
+picosat_print (PS * ps, FILE * file)
+{
+#ifdef NO_BINARY_CLAUSES
+ Lit * lit, *other, * last;
+ Ltk * stack;
+#endif
+ Lit **q, **eol;
+ Cls **p, *c;
+ unsigned n;
+
+ if (ps->measurealltimeinlib)
+ enter (ps);
+ else
+ check_ready (ps);
+
+ n = 0;
+ n += ps->alshead - ps->als;
+
+ for (p = SOC; p != EOC; p = NXC (p))
+ {
+ c = *p;
+
+ if (!c)
+ continue;
+
+#ifdef TRACE
+ if (c->collected)
+ continue;
+#endif
+ n++;
+ }
+
+#ifdef NO_BINARY_CLAUSES
+ last = int2lit (ps, -ps->max_var);
+ for (lit = int2lit (ps, 1); lit <= last; lit++)
+ {
+ stack = LIT2IMPLS (lit);
+ eol = stack->start + stack->count;
+ for (q = stack->start; q < eol; q++)
+ if (*q >= lit)
+ n++;
+ }
+#endif
+
+ fprintf (file, "p cnf %d %u\n", ps->max_var, n);
+
+ for (p = SOC; p != EOC; p = NXC (p))
+ {
+ c = *p;
+ if (!c)
+ continue;
+
+#ifdef TRACE
+ if (c->collected)
+ continue;
+#endif
+
+ eol = end_of_lits (c);
+ for (q = c->lits; q < eol; q++)
+ fprintf (file, "%d ", LIT2INT (*q));
+
+ fputs ("0\n", file);
+ }
+
+#ifdef NO_BINARY_CLAUSES
+ last = int2lit (ps, -ps->max_var);
+ for (lit = int2lit (ps, 1); lit <= last; lit++)
+ {
+ stack = LIT2IMPLS (lit);
+ eol = stack->start + stack->count;
+ for (q = stack->start; q < eol; q++)
+ if ((other = *q) >= lit)
+ fprintf (file, "%d %d 0\n", LIT2INT (lit), LIT2INT (other));
+ }
+#endif
+
+ {
+ Lit **r;
+ for (r = ps->als; r < ps->alshead; r++)
+ fprintf (file, "%d 0\n", LIT2INT (*r));
+ }
+
+ fflush (file);
+
+ if (ps->measurealltimeinlib)
+ leave (ps);
+}
+
+void
+picosat_enter (PS * ps)
+{
+ enter (ps);
+}
+
+void
+picosat_leave (PS * ps)
+{
+ leave (ps);
+}
+
+void
+picosat_message (PS * ps, int vlevel, const char * fmt, ...)
+{
+ va_list ap;
+
+ if (vlevel > ps->verbosity)
+ return;
+
+ fputs (ps->prefix, ps->out);
+ va_start (ap, fmt);
+ vfprintf (ps->out, fmt, ap);
+ va_end (ap);
+ fputc ('\n', ps->out);
+}
+
+int
+picosat_changed (PS * ps)
+{
+ int res;
+
+ check_ready (ps);
+ check_sat_state (ps);
+
+ res = (ps->min_flipped <= ps->saved_max_var);
+ assert (!res || ps->saved_flips != ps->flips);
+
+ return res;
+}
+
+void
+picosat_reset_phases (PS * ps)
+{
+ rebias (ps);
+}
+
+void
+picosat_reset_scores (PS * ps)
+{
+ Rnk * r;
+ ps->hhead = ps->heap + 1;
+ for (r = ps->rnks + 1; r <= ps->rnks + ps->max_var; r++)
+ {
+ CLR (r);
+ hpush (ps, r);
+ }
+}
+
+void
+picosat_remove_learned (PS * ps, unsigned percentage)
+{
+ enter (ps);
+ reset_incremental_usage (ps);
+ reduce (ps, percentage);
+ leave (ps);
+}
+
+void
+picosat_set_global_default_phase (PS * ps, int phase)
+{
+ check_ready (ps);
+ ABORTIF (phase < 0, "API usage: 'picosat_set_global_default_phase' "
+ "with negative argument");
+ ABORTIF (phase > 3, "API usage: 'picosat_set_global_default_phase' "
+ "with argument > 3");
+ ps->defaultphase = phase;
+}
+
+void
+picosat_set_default_phase_lit (PS * ps, int int_lit, int phase)
+{
+ unsigned newphase;
+ Lit * lit;
+ Var * v;
+
+ check_ready (ps);
+
+ lit = import_lit (ps, int_lit, 1);
+ v = LIT2VAR (lit);
+
+ if (phase)
+ {
+ newphase = (int_lit < 0) == (phase < 0);
+ v->defphase = v->phase = newphase;
+ v->usedefphase = v->assigned = 1;
+ }
+ else
+ {
+ v->usedefphase = v->assigned = 0;
+ }
+}
+
+void
+picosat_set_more_important_lit (PS * ps, int int_lit)
+{
+ Lit * lit;
+ Var * v;
+ Rnk * r;
+
+ check_ready (ps);
+
+ lit = import_lit (ps, int_lit, 1);
+ v = LIT2VAR (lit);
+ r = VAR2RNK (v);
+
+ ABORTIF (r->lessimportant, "can not mark variable more and less important");
+
+ if (r->moreimportant)
+ return;
+
+ r->moreimportant = 1;
+
+ if (r->pos)
+ hup (ps, r);
+}
+
+void
+picosat_set_less_important_lit (PS * ps, int int_lit)
+{
+ Lit * lit;
+ Var * v;
+ Rnk * r;
+
+ check_ready (ps);
+
+ lit = import_lit (ps, int_lit, 1);
+ v = LIT2VAR (lit);
+ r = VAR2RNK (v);
+
+ ABORTIF (r->moreimportant, "can not mark variable more and less important");
+
+ if (r->lessimportant)
+ return;
+
+ r->lessimportant = 1;
+
+ if (r->pos)
+ hdown (ps, r);
+}
+
+#ifndef NADC
+
+unsigned
+picosat_ado_conflicts (PS * ps)
+{
+ check_ready (ps);
+ return ps->adoconflicts;
+}
+
+void
+picosat_disable_ado (PS * ps)
+{
+ check_ready (ps);
+ assert (!ps->adodisabled);
+ ps->adodisabled = 1;
+}
+
+void
+picosat_enable_ado (PS * ps)
+{
+ check_ready (ps);
+ assert (ps->adodisabled);
+ ps->adodisabled = 0;
+}
+
+void
+picosat_set_ado_conflict_limit (PS * ps, unsigned newadoconflictlimit)
+{
+ check_ready (ps);
+ ps->adoconflictlimit = newadoconflictlimit;
+}
+
+#endif
+
+void
+picosat_simplify (PS * ps)
+{
+ enter (ps);
+ reset_incremental_usage (ps);
+ simplify (ps, 1);
+ leave (ps);
+}
+
+int
+picosat_haveados (void)
+{
+#ifndef NADC
+ return 1;
+#else
+ return 0;
+#endif
+}
+
+void
+picosat_save_original_clauses (PS * ps)
+{
+ if (ps->saveorig) return;
+ ABORTIF (ps->oadded, "API usage: 'picosat_save_original_clauses' too late");
+ ps->saveorig = 1;
+}
+
+void picosat_set_interrupt (PicoSAT * ps,
+ void * external_state,
+ int (*interrupted)(void * external_state))
+{
+ ps->interrupt.state = external_state;
+ ps->interrupt.function = interrupted;
+}
+
+int
+picosat_deref_partial (PS * ps, int int_lit)
+{
+ check_ready (ps);
+ check_sat_state (ps);
+ ABORTIF (!int_lit, "API usage: can not partial deref zero literal");
+ ABORTIF (ps->mtcls, "API usage: deref partial after empty clause generated");
+ ABORTIF (!ps->saveorig, "API usage: 'picosat_save_original_clauses' missing");
+
+#ifdef STATS
+ ps->derefs++;
+#endif
+
+ if (!ps->partial)
+ minautarky (ps);
+
+ return pderef (ps, int_lit);
+}
diff --git a/test/picosat-965/picosat.h b/test/picosat-965/picosat.h
new file mode 100644
index 00000000..668bc00b
--- /dev/null
+++ b/test/picosat-965/picosat.h
@@ -0,0 +1,658 @@
+/****************************************************************************
+Copyright (c) 2006 - 2015, Armin Biere, Johannes Kepler University.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
+****************************************************************************/
+
+#ifndef picosat_h_INCLUDED
+#define picosat_h_INCLUDED
+
+/*------------------------------------------------------------------------*/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stddef.h>
+
+/*------------------------------------------------------------------------*/
+/* The following macros allows for users to distiguish between different
+ * versions of the API. The first 'PICOSAT_REENTRANT_API' is defined for
+ * the new reentrant API which allows to generate multiple instances of
+ * PicoSAT in one process. The second 'PICOSAT_API_VERSION' defines the
+ * (smallest) version of PicoSAT to which this API conforms.
+ */
+#define PICOSAT_REENTRANT_API
+#define PICOSAT_API_VERSION 953 /* API version */
+
+/*------------------------------------------------------------------------*/
+/* These are the return values for 'picosat_sat' as for instance
+ * standardized by the output format of the SAT competition.
+ */
+#define PICOSAT_UNKNOWN 0
+#define PICOSAT_SATISFIABLE 10
+#define PICOSAT_UNSATISFIABLE 20
+
+/*------------------------------------------------------------------------*/
+
+typedef struct PicoSAT PicoSAT;
+
+/*------------------------------------------------------------------------*/
+
+const char *picosat_version (void);
+const char *picosat_config (void);
+const char *picosat_copyright (void);
+
+/*------------------------------------------------------------------------*/
+/* You can make PicoSAT use an external memory manager instead of the one
+ * provided by LIBC. But then you need to call these three function before
+ * 'picosat_init'. The memory manager functions here all have an additional
+ * first argument which is a pointer to the memory manager, but otherwise
+ * are supposed to work as their LIBC counter parts 'malloc', 'realloc' and
+ * 'free'. As exception the 'resize' and 'delete' function have as third
+ * argument the number of bytes of the block given as second argument.
+ */
+
+typedef void * (*picosat_malloc)(void *, size_t);
+typedef void * (*picosat_realloc)(void*, void *, size_t, size_t);
+typedef void (*picosat_free)(void*, void*, size_t);
+
+/*------------------------------------------------------------------------*/
+
+PicoSAT * picosat_init (void); /* constructor */
+
+PicoSAT * picosat_minit (void * state,
+ picosat_malloc,
+ picosat_realloc,
+ picosat_free);
+
+void picosat_reset (PicoSAT *); /* destructor */
+
+/*------------------------------------------------------------------------*/
+/* The following five functions are essentially parameters to 'init', and
+ * thus should be called right after 'picosat_init' before doing anything
+ * else. You should not call any of them after adding a literal.
+ */
+
+/* Set output file, default is 'stdout'.
+ */
+void picosat_set_output (PicoSAT *, FILE *);
+
+/* Measure all time spent in all calls in the solver. By default only the
+ * time spent in 'picosat_sat' is measured. Enabling this function might
+ * for instance triple the time needed to add large CNFs, since every call
+ * to 'picosat_add' will trigger a call to 'getrusage'.
+ */
+void picosat_measure_all_calls (PicoSAT *);
+
+/* Set the prefix used for printing verbose messages and statistics.
+ * Default is "c ".
+ */
+void picosat_set_prefix (PicoSAT *, const char *);
+
+/* Set verbosity level. A verbosity level of 1 and above prints more and
+ * more detailed progress reports on the output file, set by
+ * 'picosat_set_output'. Verbose messages are prefixed with the string set
+ * by 'picosat_set_prefix'.
+ */
+void picosat_set_verbosity (PicoSAT *, int new_verbosity_level);
+
+/* Disable/Enable all pre-processing, currently only failed literal probing.
+ *
+ * new_plain_value != 0 only 'plain' solving, so no preprocessing
+ * new_plain_value == 0 allow preprocessing
+ */
+void picosat_set_plain (PicoSAT *, int new_plain_value);
+
+/* Set default initial phase:
+ *
+ * 0 = false
+ * 1 = true
+ * 2 = Jeroslow-Wang (default)
+ * 3 = random initial phase
+ *
+ * After a variable has been assigned the first time, it will always
+ * be assigned the previous value if it is picked as decision variable.
+ * The initial assignment can be chosen with this function.
+ */
+void picosat_set_global_default_phase (PicoSAT *, int);
+
+/* Set next/initial phase of a particular variable if picked as decision
+ * variable. Second argument 'phase' has the following meaning:
+ *
+ * negative = next value if picked as decision variable is false
+ *
+ * positive = next value if picked as decision variable is true
+ *
+ * 0 = use global default phase as next value and
+ * assume 'lit' was never assigned
+ *
+ * Again if 'lit' is assigned afterwards through a forced assignment,
+ * then this forced assignment is the next phase if this variable is
+ * used as decision variable.
+ */
+void picosat_set_default_phase_lit (PicoSAT *, int lit, int phase);
+
+/* You can reset all phases by the following function.
+ */
+void picosat_reset_phases (PicoSAT *);
+
+/* Scores can be erased as well. Note, however, that even after erasing
+ * scores and phases, learned clauses are kept. In addition head tail
+ * pointers for literals are not moved either. So expect a difference
+ * between calling the solver in incremental mode or with a fresh copy of
+ * the CNF.
+ */
+void picosat_reset_scores (PicoSAT *);
+
+/* Reset assignment if in SAT state and then remove the given percentage of
+ * less active (large) learned clauses. If you specify 100% all large
+ * learned clauses are removed.
+ */
+void picosat_remove_learned (PicoSAT *, unsigned percentage);
+
+/* Set some variables to be more important than others. These variables are
+ * always used as decisions before other variables are used. Dually there
+ * is a set of variables that is used last. The default is
+ * to mark all variables as being indifferent only.
+ */
+void picosat_set_more_important_lit (PicoSAT *, int lit);
+void picosat_set_less_important_lit (PicoSAT *, int lit);
+
+/* Allows to print to internal 'out' file from client.
+ */
+void picosat_message (PicoSAT *, int verbosity_level, const char * fmt, ...);
+
+/* Set a seed for the random number generator. The random number generator
+ * is currently just used for generating random decisions. In our
+ * experiments having random decisions did not really help on industrial
+ * examples, but was rather helpful to randomize the solver in order to
+ * do proper benchmarking of different internal parameter sets.
+ */
+void picosat_set_seed (PicoSAT *, unsigned random_number_generator_seed);
+
+/* If you ever want to extract cores or proof traces with the current
+ * instance of PicoSAT initialized with 'picosat_init', then make sure to
+ * call 'picosat_enable_trace_generation' right after 'picosat_init'. This
+ * is not necessary if you only use 'picosat_set_incremental_rup_file'.
+ *
+ * NOTE, trace generation code is not necessarily included, e.g. if you
+ * configure PicoSAT with full optimzation as './configure.sh -O' or with
+
+ * you do not get any results by trying to generate traces.
+ *
+ * The return value is non-zero if code for generating traces is included
+ * and it is zero if traces can not be generated.
+ */
+int picosat_enable_trace_generation (PicoSAT *);
+
+/* You can dump proof traces in RUP format incrementally even without
+ * keeping the proof trace in memory. The advantage is a reduction of
+ * memory usage, but the dumped clauses do not necessarily belong to the
+ * clausal core. Beside the file the additional parameters denotes the
+ * maximal number of variables and the number of original clauses.
+ */
+void picosat_set_incremental_rup_file (PicoSAT *, FILE * file, int m, int n);
+
+/* Save original clauses for 'picosat_deref_partial'. See comments to that
+ * function further down.
+ */
+void picosat_save_original_clauses (PicoSAT *);
+
+/* Add a call back which is checked regularly to notify the SAT solver
+ * to terminate earlier. This is useful for setting external time limits
+ * or terminate early in say a portfolio style parallel SAT solver.
+ */
+void picosat_set_interrupt (PicoSAT *,
+ void * external_state,
+ int (*interrupted)(void * external_state));
+
+/*------------------------------------------------------------------------*/
+/* This function returns the next available unused variable index and
+ * allocates a variable for it even though this variable does not occur as
+ * assumption, nor in a clause or any other constraints. In future calls to
+ * 'picosat_sat', 'picosat_deref' and particularly for 'picosat_changed',
+ * this variable is treated as if it had been used.
+ */
+int picosat_inc_max_var (PicoSAT *);
+
+/*------------------------------------------------------------------------*/
+/* Push and pop semantics for PicoSAT. 'picosat_push' opens up a new
+ * context. All clauses added in this context are attached to it and
+ * discarded when the context is closed with 'picosat_pop'. It is also
+ * possible to nest contexts.
+ *
+ * The current implementation uses a new internal variable for each context.
+ * However, the indices for these internal variables are shared with
+ * ordinary external variables. This means that after any call to
+ * 'picosat_push', new variable indices should be obtained with
+ * 'picosat_inc_max_var' and not just by incrementing the largest variable
+ * index used so far.
+ *
+ * The return value is the index of the literal that assumes this context.
+ * This literal can only be used for 'picosat_failed_context' otherwise
+ * it will lead to an API usage error.
+ */
+int picosat_push (PicoSAT *);
+
+/* This is as 'picosat_failed_assumption', but only for internal variables
+ * generated by 'picosat_push'.
+ */
+int picosat_failed_context (PicoSAT *, int lit);
+
+/* Returns the literal that assumes the current context or zero if the
+ * outer context has been reached.
+ */
+int picosat_context (PicoSAT *);
+
+/* Closes the current context and recycles the literal generated for
+ * assuming this context. The return value is the literal for the new
+ * outer context or zero if the outer most context has been reached.
+ */
+int picosat_pop (PicoSAT *);
+
+/* Force immmediate removal of all satisfied clauses and clauses that are
+ * added or generated in closed contexts. This function is called
+ * internally if enough units are learned or after a certain number of
+ * contexts have been closed. This number is fixed at compile time
+ * and defined as MAXCILS in 'picosat.c'.
+ *
+ * Note that learned clauses which only involve outer contexts are kept.
+ */
+void picosat_simplify (PicoSAT *);
+
+/*------------------------------------------------------------------------*/
+/* If you know a good estimate on how many variables you are going to use
+ * then calling this function before adding literals will result in less
+ * resizing of the variable table. But this is just a minor optimization.
+ * Beside exactly allocating enough variables it has the same effect as
+ * calling 'picosat_inc_max_var'.
+ */
+void picosat_adjust (PicoSAT *, int max_idx);
+
+/*------------------------------------------------------------------------*/
+/* Statistics.
+ */
+int picosat_variables (PicoSAT *); /* p cnf <m> n */
+int picosat_added_original_clauses (PicoSAT *); /* p cnf m <n> */
+size_t picosat_max_bytes_allocated (PicoSAT *);
+double picosat_time_stamp (void); /* ... in process */
+void picosat_stats (PicoSAT *); /* > output file */
+unsigned long long picosat_propagations (PicoSAT *); /* #propagations */
+unsigned long long picosat_decisions (PicoSAT *); /* #decisions */
+unsigned long long picosat_visits (PicoSAT *); /* #visits */
+
+/* The time spent in calls to the library or in 'picosat_sat' respectively.
+ * The former is returned if, right after initialization
+ * 'picosat_measure_all_calls' is called.
+ */
+double picosat_seconds (PicoSAT *);
+
+/*------------------------------------------------------------------------*/
+/* Add a literal of the next clause. A zero terminates the clause. The
+ * solver is incremental. Adding a new literal will reset the previous
+ * assignment. The return value is the original clause index to which
+ * this literal respectively the trailing zero belong starting at 0.
+ */
+int picosat_add (PicoSAT *, int lit);
+
+/* As the previous function, but allows to add a full clause at once with an
+ * at compiled time known size. The list of argument literals has to be
+ * terminated with a zero literal. Literals beyond the first zero literal
+ * are discarded.
+ */
+int picosat_add_arg (PicoSAT *, ...);
+
+/* As the previous function but with an at compile time unknown size.
+ */
+int picosat_add_lits (PicoSAT *, int * lits);
+
+/* Print the CNF to the given file in DIMACS format.
+ */
+void picosat_print (PicoSAT *, FILE *);
+
+/* You can add arbitrary many assumptions before the next 'picosat_sat'
+ * call. This is similar to the using assumptions in MiniSAT, except that
+ * for PicoSAT you do not have to collect all your assumptions in a vector
+ * yourself. In PicoSAT you can add one after the other, to be used in the
+ * next call to 'picosat_sat'.
+ *
+ * These assumptions can be interpreted as adding unit clauses with those
+ * assumptions as literals. However these assumption clauses are only valid
+ * for exactly the next call to 'picosat_sat', and will be removed
+ * afterwards, e.g. in following future calls to 'picosat_sat' after the
+ * next 'picosat_sat' call, unless they are assumed again trough
+ * 'picosat_assume'.
+ *
+ * More precisely, assumptions actually remain valid even after the next
+ * call to 'picosat_sat' has returned. Valid means they remain 'assumed'
+ * internally until a call to 'picosat_add', 'picosat_assume', or a second
+ * 'picosat_sat', following the first 'picosat_sat'. The reason for keeping
+ * them valid is to allow 'picosat_failed_assumption' to return correct
+ * values.
+ *
+ * Example:
+ *
+ * picosat_assume (1); // assume unit clause '1 0'
+ * picosat_assume (-2); // additionally assume clause '-2 0'
+ * res = picosat_sat (1000); // assumes 1 and -2 to hold
+ * // 1000 decisions max.
+ *
+ * if (res == PICOSAT_UNSATISFIABLE)
+ * {
+ * if (picosat_failed_assumption (1))
+ * // unit clause '1 0' was necessary to derive UNSAT
+ *
+ * if (picosat_failed_assumption (-2))
+ * // unit clause '-2 0' was necessary to derive UNSAT
+ *
+ * // at least one but also both could be necessary
+ *
+ * picosat_assume (17); // previous assumptions are removed
+ * // now assume unit clause '17 0' for
+ * // the next call to 'picosat_sat'
+ *
+ * // adding a new clause, actually the first literal of
+ * // a clause would also make the assumptions used in the previous
+ * // call to 'picosat_sat' invalid.
+ *
+ * // The first two assumptions above are not assumed anymore. Only
+ * // the assumptions, since the last call to 'picosat_sat' returned
+ * // are assumed, e.g. the unit clause '17 0'.
+ *
+ * res = picosat_sat (-1);
+ * }
+ * else if (res == PICOSAT_SATISFIABLE)
+ * {
+ * // now the assignment is valid and we can call 'picosat_deref'
+ *
+ * assert (picosat_deref (1) == 1));
+ * assert (picosat_deref (-2) == 1));
+ *
+ * val = picosat_deref (15);
+ *
+ * // previous two assumptions are still valid
+ *
+ * // would become invalid if 'picosat_add' or 'picosat_assume' is
+ * // called here, but we immediately call 'picosat_sat'. Now when
+ * // entering 'picosat_sat' the solver knows that the previous call
+ * // returned SAT and it can safely reset the previous assumptions
+ *
+ * res = picosat_sat (-1);
+ * }
+ * else
+ * {
+ * assert (res == PICOSAT_UNKNOWN);
+ *
+ * // assumptions valid, but assignment invalid
+ * // except for top level assigned literals which
+ * // necessarily need to have this value if the formula is SAT
+ *
+ * // as above the solver nows that the previous call returned UNKWOWN
+ * // and will before doing anything else reset assumptions
+ *
+ * picosat_sat (-1);
+ * }
+ */
+void picosat_assume (PicoSAT *, int lit);
+
+/*------------------------------------------------------------------------*/
+/* This is an experimental feature for handling 'all different constraints'
+ * (ADC). Currently only one global ADC can be handled. The bit-width of
+ * all the bit-vectors entered in this ADC (stored in 'all different
+ * objects' or ADOs) has to be identical.
+ *
+ * TODO: also handle top level assigned literals here.
+ */
+void picosat_add_ado_lit (PicoSAT *, int);
+
+/*------------------------------------------------------------------------*/
+/* Call the main SAT routine. A negative decision limit sets no limit on
+ * the number of decisions. The return values are as above, e.g.
+ * 'PICOSAT_UNSATISFIABLE', 'PICOSAT_SATISFIABLE', or 'PICOSAT_UNKNOWN'.
+ */
+int picosat_sat (PicoSAT *, int decision_limit);
+
+/* As alternative to a decision limit you can use the number of propagations
+ * as limit. This is more linearly related to execution time. This has to
+ * be called after 'picosat_init' and before 'picosat_sat'.
+ */
+void picosat_set_propagation_limit (PicoSAT *, unsigned long long limit);
+
+/* Return last result of calling 'picosat_sat' or '0' if not called.
+ */
+int picosat_res (PicoSAT *);
+
+/* After 'picosat_sat' was called and returned 'PICOSAT_SATISFIABLE', then
+ * the satisfying assignment can be obtained by 'dereferencing' literals.
+ * The value of the literal is return as '1' for 'true', '-1' for 'false'
+ * and '0' for an unknown value.
+ */
+int picosat_deref (PicoSAT *, int lit);
+
+/* Same as before but just returns true resp. false if the literals is
+ * forced to this assignment at the top level. This function does not
+ * require that 'picosat_sat' was called and also does not internally reset
+ * incremental usage.
+ */
+int picosat_deref_toplevel (PicoSAT *, int lit);
+
+/* After 'picosat_sat' was called and returned 'PICOSAT_SATISFIABLE' a
+ * partial satisfying assignment can be obtained as well. It satisfies all
+ * original clauses. The value of the literal is return as '1' for 'true',
+ * '-1' for 'false' and '0' for an unknown value. In order to make this
+ * work all original clauses have to be saved internally, which has to be
+ * enabled by 'picosat_save_original_clauses' right after initialization.
+ */
+int picosat_deref_partial (PicoSAT *, int lit);
+
+/* Returns non zero if the CNF is unsatisfiable because an empty clause was
+ * added or derived.
+ */
+int picosat_inconsistent (PicoSAT *);
+
+/* Returns non zero if the literal is a failed assumption, which is defined
+ * as an assumption used to derive unsatisfiability. This is as accurate as
+ * generating core literals, but still of course is an overapproximation of
+ * the set of assumptions really necessary. The technique does not need
+ * clausal core generation nor tracing to be enabled and thus can be much
+ * more effective. The function can only be called as long the current
+ * assumptions are valid. See 'picosat_assume' for more details.
+ */
+int picosat_failed_assumption (PicoSAT *, int lit);
+
+/* Returns a zero terminated list of failed assumption in the last call to
+ * 'picosat_sat'. The pointer is valid until the next call to
+ * 'picosat_sat' or 'picosat_failed_assumptions'. It only makes sense if the
+ * last call to 'picosat_sat' returned 'PICOSAT_UNSATISFIABLE'.
+ */
+const int * picosat_failed_assumptions (PicoSAT *);
+
+/* Returns a zero terminated minimized list of failed assumption for the last
+ * call to 'picosat_sat'. The pointer is valid until the next call to this
+ * function or 'picosat_sat' or 'picosat_mus_assumptions'. It only makes sense
+ * if the last call to 'picosat_sat' returned 'PICOSAT_UNSATISFIABLE'.
+ *
+ * The call back function is called for all successful simplification
+ * attempts. The first argument of the call back function is the state
+ * given as first argument to 'picosat_mus_assumptions'. The second
+ * argument to the call back function is the new reduced list of failed
+ * assumptions.
+ *
+ * This function will call 'picosat_assume' and 'picosat_sat' internally but
+ * before returning reestablish a proper UNSAT state, e.g.
+ * 'picosat_failed_assumption' will work afterwards as expected.
+ *
+ * The last argument if non zero fixes assumptions. In particular, if an
+ * assumption can not be removed it is permanently assigned true, otherwise
+ * if it turns out to be redundant it is permanently assumed to be false.
+ */
+const int * picosat_mus_assumptions (PicoSAT *, void *,
+ void(*)(void*,const int*),int);
+
+/* Compute one maximal subset of satisfiable assumptions. You need to set
+ * the assumptions, call 'picosat_sat' and check for 'picosat_inconsistent',
+ * before calling this function. The result is a zero terminated array of
+ * assumptions that consistently can be asserted at the same time. Before
+ * returing the library 'reassumes' all assumptions.
+ *
+ * It could be beneficial to set the default phase of assumptions
+ * to true (positive). This can speed up the computation.
+ */
+const int * picosat_maximal_satisfiable_subset_of_assumptions (PicoSAT *);
+
+/* This function assumes that you have set up all assumptions with
+ * 'picosat_assume'. Then it calls 'picosat_sat' internally unless the
+ * formula is already inconsistent without assumptions, i.e. it contains
+ * the empty clause. After that it extracts a maximal satisfiable subset of
+ * assumptions.
+ *
+ * The result is a zero terminated maximal subset of consistent assumptions
+ * or a zero pointer if the formula contains the empty clause and thus no
+ * more maximal consistent subsets of assumptions can be extracted. In the
+ * first case, before returning, a blocking clause is added, that rules out
+ * the result for the next call.
+ *
+ * NOTE: adding the blocking clause changes the CNF.
+ *
+ * So the following idiom
+ *
+ * const int * mss;
+ * picosat_assume (a1);
+ * picosat_assume (a2);
+ * picosat_assume (a3);
+ * picosat_assume (a4);
+ * while ((mss = picosat_next_maximal_satisfiable_subset_of_assumptions ()))
+ * process_mss (mss);
+ *
+ * can be used to iterate over all maximal consistent subsets of
+ * the set of assumptions {a1,a2,a3,a4}.
+ *
+ * It could be beneficial to set the default phase of assumptions
+ * to true (positive). This might speed up the computation.
+ */
+const int *
+picosat_next_maximal_satisfiable_subset_of_assumptions (PicoSAT *);
+
+/* Similarly we can iterate over all minimal correcting assumption sets.
+ * See the CAMUS literature [M. Liffiton, K. Sakallah JAR 2008].
+ *
+ * The result contains each assumed literal only once, even if it
+ * was assumed multiple times (in contrast to the maximal consistent
+ * subset functions above).
+ *
+ * It could be beneficial to set the default phase of assumptions
+ * to true (positive). This might speed up the computation.
+ */
+const int *
+picosat_next_minimal_correcting_subset_of_assumptions (PicoSAT *);
+
+/* Compute the union of all minmal correcting sets, which is called
+ * the 'high level union of all minimal unsatisfiable subset sets'
+ * or 'HUMUS' in our papers.
+ *
+ * It uses 'picosat_next_minimal_correcting_subset_of_assumptions' and
+ * the same notes and advices apply. In particular, this implies that
+ * after calling the function once, the current CNF becomes inconsistent,
+ * and PicoSAT has to be reset. So even this function internally uses
+ * PicoSAT incrementally, it can not be used incrementally itself at this
+ * point.
+ *
+ * The 'callback' can be used for progress logging and is called after
+ * each extracted minimal correcting set if non zero. The 'nhumus'
+ * parameter of 'callback' denotes the number of assumptions found to be
+ * part of the HUMUS sofar.
+ */
+const int *
+picosat_humus (PicoSAT *,
+ void (*callback)(void * state, int nmcs, int nhumus),
+ void * state);
+
+/*------------------------------------------------------------------------*/
+/* Assume that a previous call to 'picosat_sat' in incremental usage,
+ * returned 'SATISFIABLE'. Then a couple of clauses and optionally new
+ * variables were added (a new variable is a variable that has an index
+ * larger then the maximum variable added so far). The next call to
+ * 'picosat_sat' also returns 'SATISFIABLE'. If this function
+ * 'picosat_changed' returns '0', then the assignment to the old variables
+ * is guaranteed to not have changed. Otherwise it might have changed.
+ *
+ * The return value to this function is only valid until new clauses are
+ * added through 'picosat_add', an assumption is made through
+ * 'picosat_assume', or again 'picosat_sat' is called. This is the same
+ * assumption as for 'picosat_deref'.
+ *
+ * TODO currently this function might also return a non zero value even if
+ * the old assignment did not change, because it only checks whether the
+ * assignment of at least one old variable was flipped at least once during
+ * the search. In principle it should be possible to be exact in the other
+ * direction as well by using a counter of variables that have an odd number
+ * of flips. But this is not implemented yet.
+ */
+int picosat_changed (PicoSAT *);
+
+/*------------------------------------------------------------------------*/
+/* The following six functions internally extract the variable and clausal
+ * core and thus require trace generation to be enabled with
+ * 'picosat_enable_trace_generation' right after calling 'picosat_init'.
+ *
+ * TODO: using these functions in incremental mode with failed assumptions
+ * has only been tested for 'picosat_corelit' thoroughly. The others
+ * probably only work in non-incremental mode or without using
+ * 'picosat_assume'.
+ */
+
+/* This function determines whether the i'th added original clause is in the
+ * core. The 'i' is the return value of 'picosat_add', which starts at zero
+ * and is incremented by one after a original clause is added (that is after
+ * 'picosat_add (0)'). For the index 'i' the following has to hold:
+ *
+ * 0 <= i < picosat_added_original_clauses ()
+ */
+int picosat_coreclause (PicoSAT *, int i);
+
+/* This function gives access to the variable core, which is made up of the
+ * variables that were resolved in deriving the empty clause.
+ */
+int picosat_corelit (PicoSAT *, int lit);
+
+/* Write the clauses that were used in deriving the empty clause to a file
+ * in DIMACS format.
+ */
+void picosat_write_clausal_core (PicoSAT *, FILE * core_file);
+
+/* Write a proof trace in TraceCheck format to a file.
+ */
+void picosat_write_compact_trace (PicoSAT *, FILE * trace_file);
+void picosat_write_extended_trace (PicoSAT *, FILE * trace_file);
+
+/* Write a RUP trace to a file. This trace file contains only the learned
+ * core clauses while this is not necessarily the case for the RUP file
+ * obtained with 'picosat_set_incremental_rup_file'.
+ */
+void picosat_write_rup_trace (PicoSAT *, FILE * trace_file);
+
+/*------------------------------------------------------------------------*/
+/* Keeping the proof trace around is not necessary if an over-approximation
+ * of the core is enough. A literal is 'used' if it was involved in a
+ * resolution to derive a learned clause. The core literals are necessarily
+ * a subset of the 'used' literals.
+ */
+
+int picosat_usedlit (PicoSAT *, int lit);
+/*------------------------------------------------------------------------*/
+#endif
diff --git a/test/picosat-965/version.c b/test/picosat-965/version.c
new file mode 100644
index 00000000..71c322bd
--- /dev/null
+++ b/test/picosat-965/version.c
@@ -0,0 +1,14 @@
+#include "config.h"
+
+const char *
+picosat_version (void)
+{
+ return PICOSAT_VERSION;
+}
+
+const char *
+picosat_config (void)
+{
+ return PICOSAT_CC " " PICOSAT_CFLAGS;
+}
+