source: CIVL/examples/verifyThis/parallelGCD.c@ bb03188

main test-branch
Last change on this file since bb03188 was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 1.8 KB
RevLine 
[260762f]1/*
[76f18de]2Author: Yihao Yan
[f3368e99]3
[41eb9b6]4See challenge 2 of: http://etaps2015.verifythis.org/challenges
5
6-----------------
7Problem description:
[260762f]8
9Various parallel GCD algorithms exist. In this challenge, we consider a
10simple Euclid-like algorithm with two parallel threads. One thread
11subtracts in one direction, the other thread subtracts in the other
12direction, and eventually this procedure converges on GCD.
13
[41eb9b6]14Verification tasks
15------------------
16
17Specify and verify the following behaviour of this parallel GCD algorithm:
18
19Input: two positive integers a and b
20Output: a positive number that is the greatest common divisor of a and b
21
[0cdc417]22-----------------
23Result:
24
25For any numbers A less than 5 and B less than 7, myGCD returns a common divisor d
26of them and the verification shows that there exists no common divisor of A and
27B that is greater than d. Therefore, myGCD behaves correctly.
[f7e5282]28
[0cdc417]29-----------------
30command: civl verify parallelGCD.c
[260762f]31*/
32
[f3368e99]33#include <civlc.cvh>
34#include <stdio.h>
35
36$input int A_BOUND=4;
37$input int B_BOUND=6;
38$input int A;
39$input int B;
40$assume (A>0 && B>0 && A<A_BOUND && B<B_BOUND);
41
[0ae124a]42int myGCD(int a, int b) {
[260762f]43 $proc proc_a;
44 $proc proc_b;
45
46 void worker1() {
47 while(a != b){
48 if(a>b){
49 int t1 = b;
50 int t2 = a-t1;
51 a = t2;
52 }
53 }
54 }
[0ae124a]55 void worker2() {
56 while(a != b) {
57 if(b>a) {
[260762f]58 int t1 = a;
59 int t2 = b-t1;
60 b = t2;
61 }
62 }
63 }
64 proc_a= $spawn worker1();
65 proc_b= $spawn worker2();
66 $wait(proc_a);
67 $wait(proc_b);
68 return a;
69}
70
[0ae124a]71int seqGCD(int a, int b) {
72 while(a != b) {
[260762f]73 if(a > b)
74 a = a-b;
75 if(b > a)
76 b = b-a;
77 }
78 return a;
79}
80
[0ae124a]81void main() {
[260762f]82 int result1 = myGCD(A, B);
83 int minAB = A < B ? A : B;
[f3368e99]84
[5cd8c92]85 $assert($forall (int i: (result1+1) .. (minAB)) (A%i != 0 || B%i != 0));
[0ae124a]86 $assert(A%result1 == 0 && B%result1 == 0);
[260762f]87}
Note: See TracBrowser for help on using the repository browser.