source: CIVL/include/impls/unsigned_arith.cvl@ 4e993a9

main test-branch
Last change on this file since 4e993a9 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: 2.6 KB
RevLine 
[82ff369]1#ifndef _UNSIGNED_ARITH
2#define _UNSIGNED_ARITH
3
[ad6ea7c]4$system int $remainder(int x, int y);
5
[82ff369]6/* This file defines certain arithmetic functions on unsigned integers
7 * in terms of ordinary integer arithmetic operations.
8 * Each function consumes the integer arguments for the operation, and
9 * an additional parameter named bound. The bound should be one
10 * greater than the maximum unsigned value of the particular type.
11 * Hence these functions can be used for all unsigned integer types
12 * by invoking with the appropriate bound: unsigned char, unsigned
13 * short, unsigned int, unsigned long, unsigned long long.
14 *
15 * There is no need for division or modulus operations, since the
16 * usual integer operations can be used for these. (As they
17 * cannot overflow or underflow.)
[281deb5]18 *
19 * For an expression x, the expression x++ can be translated as
20 * (x<bound-1 ? x++ : ((x=0), bound-1)).
21 *
22 * The expression ++x can be translated as
23 * (x<bound-1 ? ++x : (x=0))
24 * or
25 * (x=(x<bound-1 ? x+1 : 0)).
[09db71b]26 *
27 * x--:
[9ff4d3a]28 * (x<1-bound ? ((x=0), -bound) : x--)
[09db71b]29 *
30 * --x:
31 * (x<1-bound ? (x=0) : --x)
[82ff369]32 */
33
[1360f5b]34/* Computes sum of two unsigned values.
35 * Preconditions: 0<bound and 0<=x,y<bound.
36 * Postconditions: 0<=result<bound and result=x+y mod bound.
37 */
38int $unsigned_add(int x, int y, int bound) {
[82ff369]39 if (x+y < bound)
40 return x+y;
41 else
42 return x+y-bound;
43}
44
[1360f5b]45/* Computes difference of two unsigned values.
46 * Preconditions: 0<bound and 0<=x,y<bound.
47 * Postconditions: 0<=result<bound and result=x-y mod bound.
48 */
49int $unsigned_subtract(int x, int y, int bound) {
[82ff369]50 if (x>=y)
51 return x-y;
52 else
53 return x-y+bound;
54}
55
[1360f5b]56/* Computes product of two unsigned values.
57 * Preconditions: 0<bound and 0<=x,y<bound.
58 * Postconditions: 0<=result<bound and result=x*y mod bound.
59 */
60int $unsigned_multiply(int x, int y, int bound) {
[82ff369]61 if (x*y < bound)
62 return x*y;
63 else
[ad6ea7c]64 return $remainder((x*y),bound);
[82ff369]65}
66
[281deb5]67/* Converts a signed integer to an unsigned integer.
68 * According to the C Standard, the result is obtained by repeatedly
69 * adding bound to x until the result is nonnegative.
[1360f5b]70 *
71 * Preconditions: 0<bound and 0<=x<bound.
72 * Postconditions: 0<=result<bound and result=x mod bound.
[281deb5]73 */
[1360f5b]74int $signed_to_unsigned(int x, int bound) {
[281deb5]75 if (x >= 0) {
76 if (x < bound)
[1360f5b]77 return x;
[281deb5]78 else
[ad6ea7c]79 return $remainder(x,bound);
[281deb5]80 } else {
81 if (-x <= bound)
82 return bound + x;
83 else
[ad6ea7c]84 return bound - $remainder(-x,bound);
[281deb5]85 }
86}
87
[1360f5b]88
89/* Computes -x for an unsigned integer x.
90 * Preconditions: 0<bound and 0<=x<bound
91 * Postconditions: 0<=result<bound and result=-x mod bound.
92 */
93int $unsigned_neg(int x, int bound) {
94 if (x==0)
95 return 0;
96 else
97 return bound - x;
98}
99
[82ff369]100#endif
Note: See TracBrowser for help on using the repository browser.