Java Code Examples for kodkod.ast.IntConstant#constant()

The following examples show how to use kodkod.ast.IntConstant#constant() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FloatExpressionTests.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
private void testReciprocalInitial(float v) {
	IntExpression ve = IntConstant.constant(Float.floatToIntBits(v));

	ve = floatAbs(ve);
	ve = ve.and(IntConstant.constant(exponentMask).not()).or(minusOne.plus(IntConstant.constant(exponentBias)).shl(IntConstant.constant(mantissaBits)));
	v = Float.intBitsToFloat(eval.evaluate(ve));
	
	IntExpression pe = floatMultiply(
		IntConstant.constant(Float.floatToIntBits(((float)32)/((float)17))), 
		ve);
	IntExpression xi = 
			floatMinus(
				IntConstant.constant(Float.floatToIntBits(((float)48)/((float)17))), 
				pe);

	float p = (((float)32)/((float)17))*v;
	float x0 = (float)48/(float)17 - p;		
	IntExpression x0e = IntConstant.constant(Float.floatToIntBits(x0));

	Assert.assertEquals(eval.evaluate(IntConstant.constant(Float.floatToIntBits(p))), eval.evaluate(pe));		
	Assert.assertEquals(eval.evaluate(x0e), eval.evaluate(xi));
}
 
Example 2
Source File: FloatExpressionTests.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
private void testDivide(float l, float r) {
	IntExpression lv = IntConstant.constant(Float.floatToIntBits(l));
	IntExpression rv = IntConstant.constant(Float.floatToIntBits(r));
	
	IntExpression computedSum = floatDivide(lv, rv);
	float computedQuotient = Float.intBitsToFloat(eval.evaluate(computedSum));
	float expectedQuotient = ((float)l)/((float)r);

	System.err.println(eval.evaluate(computedSum));
	dumpFloat(computedSum, "computed");
	dumpFloat(IntConstant.constant(Float.floatToIntBits(expectedQuotient)), "expected");
	
	dumpFloat(lv, "left");
	dumpFloat(rv, "right");

	Assert.assertTrue("expected " + expectedQuotient + " but got " + computedQuotient, Float.compare(computedQuotient*r, expectedQuotient*r) == 0);
	Assert.assertTrue("expected " + expectedQuotient + " but got " + computedQuotient, Float.compare(computedQuotient*r, l) == 0);
}
 
Example 3
Source File: NQueens.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * Prints the given solution
 */
void print(Instance instance, Options options) { 
	final Evaluator eval = new Evaluator(instance, options);
	for(int i = 0; i < n; i++) { 
		IntExpression ci = IntConstant.constant(i);
		for(int j = 0; j < n; j++) { 
			IntExpression cj = IntConstant.constant(j);
			Variable q = Variable.unary("q");
			if (eval.evaluate(q.join(x).sum().eq(ci).and(q.join(y).sum().eq(cj)).forSome(q.oneOf(queen)))) { 
				System.out.print(" Q");
			} else {
				System.out.print(" .");
			}
		}
		System.out.println();
	}
}
 
Example 4
Source File: FloatExpressionTests.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
private void testMultiply(float l, float r) {
	IntExpression lv = IntConstant.constant(Float.floatToIntBits(l));
	IntExpression rv = IntConstant.constant(Float.floatToIntBits(r));
	
	IntExpression computedSum = floatMultiply(lv, rv);
	IntExpression expectedSum = IntConstant.constant(Float.floatToIntBits(((float)l)*((float)r)));

	System.err.println(eval.evaluate(computedSum));
	dumpFloat(computedSum, "computed");
	dumpFloat(expectedSum, "expected");
	
	dumpFloat(lv, "left");
	dumpFloat(rv, "right");

	Assert.assertEquals("expected " + eval.evaluate(expectedSum) + " but got " + eval.evaluate(computedSum), eval.evaluate(expectedSum), eval.evaluate(computedSum));
}
 
Example 5
Source File: IntTest.java    From kodkod with MIT License 6 votes vote down vote up
@Test
public final void testExtremeShifts() {
	final int bw = 32;
	solver.options().setBitwidth(bw);
	final IntRange range = solver.options().integers();
	final int mask = ~(-1 << bw);
	final IntConstant min = IntConstant.constant(range.min());
	final IntConstant max = IntConstant.constant(range.max());
	testBinOp(SHL, min, min, min.value(), min.value(), 0, mask);
	testBinOp(SHL, min, max, min.value(), max.value(), 0, mask);
	testBinOp(SHL, max, min, max.value(), min.value(), 0, mask);
	testBinOp(SHL, max, max, max.value(), max.value(), 0, mask);
	testBinOp(SHA, min, min, min.value(), min.value(), -1, mask);
	testBinOp(SHA, min, max, min.value(), max.value(), -1, mask);
	testBinOp(SHA, max, min, max.value(), min.value(), 0, mask);
	testBinOp(SHA, max, max, max.value(), max.value(), 0, mask);
	testBinOp(SHR, min, min, min.value(), min.value(), 0, mask);
	testBinOp(SHR, min, max, min.value(), max.value(), 0, mask);
	testBinOp(SHR, max, min, max.value(), min.value(), 0, mask);
	testBinOp(SHR, max, max, max.value(), max.value(), 0, mask);
}
 
Example 6
Source File: FloatDivideTests.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
private static Formula makeTest(float l, float r) {
	IntExpression qe = quotient.sum();
	IntConstant re = IntConstant.constant(Float.floatToIntBits(r));
	IntExpression mult = floatMultiply(qe, re);
	IntConstant le = IntConstant.constant(Float.floatToIntBits(l));
	return le.eq(mult);
}
 
Example 7
Source File: FloatingPoint.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public static IntExpression floatDivide(IntExpression l, IntExpression r) {
	IntExpression sign = sign(l).xor(sign(r));
	
	l = floatAbs(l);
	IntExpression le = exponent(l).minus(exponent(r).plus(one)).plus(IntConstant.constant(exponentBias));
	l = l.and(IntConstant.constant(exponentMask).not()).or(le.shl(IntConstant.constant(mantissaBits)));
	
	r = floatAbs(r);
	r = r.and(IntConstant.constant(exponentMask).not()).or(minusOne.plus(IntConstant.constant(exponentBias)).shl(IntConstant.constant(mantissaBits)));
	
	IntExpression xi = 
		floatMinus(
			IntConstant.constant(Float.floatToIntBits((float)48/(float)17)), 
			floatMultiply(
				IntConstant.constant(Float.floatToIntBits((float)32/(float)17)), 
				r));
	
	IntExpression two = IntConstant.constant(Float.floatToIntBits(2f));
	
	// find out why this does not work...
	//int steps = newtonSteps(bitWidth);
	for(int i = 0; i < 7; i++) {
		xi = floatMultiply(xi, floatMinus(two, floatMultiply(r, xi)));
	}
	
	IntExpression answer = floatMultiply(xi, l);
	return answer.and(IntConstant.constant(signMask).not()).or(sign.shl(IntConstant.constant(exponentBits+mantissaBits)));
}
 
Example 8
Source File: ExpressionUtil.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public static IntExpression maxSetBit(IntExpression intValue) {
	IntExpression bit = IntConstant.constant(-1);
	for(int i = 0; i < bitWidth-1; i++) {
		bit = IntConstant.constant(0).eq(IntConstant.constant(1<<i).and(intValue)).not()
			.thenElse(IntConstant.constant(i), bit);
	}
	return intValue.lt(IntConstant.constant(0)).thenElse(IntConstant.constant(bitWidth-1), bit);
}
 
Example 9
Source File: FloatExpressionTests.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
private void testFullIntegerMultiply(int l, int r) {
	IntExpression le = IntConstant.constant(l); 
	IntExpression re = IntConstant.constant(r);
	
	long product = (long)l * (long)r;	
	IntConstant lower = IntConstant.constant((int) (product & 0x00000000FFFFFFFFL));
	IntConstant upper = IntConstant.constant((int) ((product & 0xFFFFFFFF00000000L)>>32));
	
	Pair<IntExpression,IntExpression> computed = fullUnsignedIntegerMultiply(le, re);
	
	System.err.println(Integer.toBinaryString(l));
	System.err.println(Integer.toBinaryString(r));
	System.err.println(product);
	System.err.println(eval.evaluate(upper));
	System.err.println(eval.evaluate(lower));
	System.err.println(eval.evaluate(computed.snd));
	System.err.println(eval.evaluate(computed.fst));
	System.err.println(Integer.toBinaryString(eval.evaluate(upper)));
	System.err.println(Integer.toBinaryString(eval.evaluate(lower)));
	System.err.println(Integer.toBinaryString(eval.evaluate(computed.snd)));
	System.err.println(Integer.toBinaryString(eval.evaluate(computed.fst)));
	for(int i = 0; i < 32; i++) {
		System.err.println(Integer.toBinaryString(eval.evaluate(re.shr(IntConstant.constant(32-i-1)))));			
	}
	
	Assert.assertEquals(eval.evaluate(lower), eval.evaluate(computed.fst));
	Assert.assertEquals(eval.evaluate(upper), eval.evaluate(computed.snd));
}
 
Example 10
Source File: FloatExpressionTests.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
private void testCompare(float l, float r) {
	IntExpression expected = IntConstant.constant(l > r? 1: l < r? -1: 0);
	IntExpression actual = floatCompare(
		IntConstant.constant(Float.floatToIntBits(l)),
		IntConstant.constant(Float.floatToIntBits(r)));

	Assert.assertEquals("expected " + eval.evaluate(actual) + " but got " + eval.evaluate(actual), eval.evaluate(expected), eval.evaluate(actual));
}
 
Example 11
Source File: Simplifier.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
/** @return canonical integer constant with the given value. */
final IntConstant constant(int val) { 
	IntConstant ret = constants.get(val);
	if (ret==null) {
		ret = IntConstant.constant(val);
		constants.put(val, ret);
	}
	return ret;
}
 
Example 12
Source File: FloatExpressionTests.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
private void testCastToFloat(int i) {		
	IntExpression cast = intToFloat(IntConstant.constant(i));
	int floatBits = Float.floatToIntBits((float)i);
	IntExpression raw = IntConstant.constant(floatBits);
	
	Assert.assertTrue("expected " + eval.evaluate(raw) + " but got " + eval.evaluate(cast), eval.evaluate(cast.eq(raw)));
}
 
Example 13
Source File: Viktor.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the sum of the elements in x (conditional on the non-emptiness of a
 * given a[i]) located at indices [lo..hi]
 *
 * @return the sum of cardinalities of the elements in x (conditional on the
 *         non-emptiness of a given a[i]) located at indices [lo..hi]
 */
private static IntExpression conditionalSum(Expression[] a, Expression[] x, int lo, int hi) {
    if (lo > hi)
        return IntConstant.constant(0);
    else if (lo == hi)
        return a[lo].some().thenElse(x[lo].sum(), IntConstant.constant(0));
    else {
        final int mid = (lo + hi) / 2;
        final IntExpression lsum = conditionalSum(a, x, lo, mid);
        final IntExpression hsum = conditionalSum(a, x, mid + 1, hi);
        return lsum.plus(hsum);
    }
}
 
Example 14
Source File: Viktor.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns the equations to be satisfied.
 * @return equations to be satisfied.
 */
public final Formula equations() {
	
	// each b <= cols-1
	Formula f0 = Formula.TRUE;
	final IntConstant colConst = IntConstant.constant(cols-1);
	for(IntExpression bi: b) {
		f0 = f0.and(bi.lte(colConst));
	}
	
	final Variable[] y = new Variable[rows];
	for(int i = 0; i < rows; i++) {
		y[i] = Variable.unary("y"+i);
	}
	
	Decls decls = y[0].oneOf(INTS);
	for(int i = 1; i < rows; i++)
		decls = decls.and(y[i].oneOf(INTS));
	
	Formula f1 = Formula.TRUE;
	final Expression[] combo = new Expression[rows];
	for(int i = 0; i < cols; i++) {
		for(int j = i+1; j < cols; j++) {
			for(int k = j+1; k < cols; k++) {
				Formula f2 = Formula.TRUE;
				for(int m = 0; m < rows; m++) {
					combo[0] = a[m][i];
					combo[1] = a[m][j];
					combo[2] = a[m][k];
					f2 = f2.and(conditionalSum(combo, y, 0, rows-1).eq(b[m]));
				}
				f1 = f1.and(f2.not().forAll(decls));
			}
		}
	}
	return f0.and(f1); 
}
 
Example 15
Source File: Viktor.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns the sum of the elements in x (conditional on the non-emptiness of a 
 * given a[i]) located at indices [lo..hi]
 * @return the sum of cardinalities of the elements in x (conditional on the non-emptiness of a 
 * given a[i]) located at indices [lo..hi]
 */
private static IntExpression conditionalSum(Expression[] a, Expression[] x, int lo, int hi) {
	if (lo>hi)
		return IntConstant.constant(0);
	else if (lo==hi) 
		return a[lo].some().thenElse(x[lo].sum(), IntConstant.constant(0));
	else {
		final int mid = (lo + hi) / 2;
		final IntExpression lsum = conditionalSum(a, x, lo, mid);
		final IntExpression hsum = conditionalSum(a, x, mid+1, hi);
		return lsum.plus(hsum);
	}
}
 
Example 16
Source File: TranslateAlloyToKodkod.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public Object visit(ExprConstant x) throws Err {
    switch (x.op) {
        case MIN :
            return IntConstant.constant(min); // TODO
        case MAX :
            return IntConstant.constant(max); // TODO
        case NEXT :
            return A4Solution.KK_NEXT;
        case TRUE :
            return Formula.TRUE;
        case FALSE :
            return Formula.FALSE;
        case EMPTYNESS :
            return Expression.NONE;
        case IDEN :
            return Expression.IDEN.intersection(a2k(UNIV).product(Expression.UNIV));
        case STRING :
            Expression ans = s2k(x.string);
            if (ans == null)
                throw new ErrorFatal(x.pos, "String literal " + x + " does not exist in this instance.\n");
            return ans;
        case NUMBER :
            int n = x.num();
            // [am] const
            // if (n<min) throw new ErrorType(x.pos, "Current bitwidth is
            // set to "+bitwidth+", thus this integer constant "+n+" is
            // smaller than the minimum integer "+min);
            // if (n>max) throw new ErrorType(x.pos, "Current bitwidth is
            // set to "+bitwidth+", thus this integer constant "+n+" is
            // bigger than the maximum integer "+max);
            return IntConstant.constant(n).toExpression();
    }
    throw new ErrorFatal(x.pos, "Unsupported operator (" + x.op + ") encountered during ExprConstant.accept()");
}
 
Example 17
Source File: OverflowTheoremTest.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
    super.setUp();
    setupOptions();
    setupBounds();

    ZERO = IntConstant.constant(0);
    MININT = IntConstant.constant(min(bw));
    MAXINT = IntConstant.constant(max(bw));
    a = Variable.unary("a");
    b = Variable.unary("b");
    as = a.sum();
    bs = b.sum();
    decls = a.oneOf(Expression.INTS).and(b.oneOf(Expression.INTS));
}
 
Example 18
Source File: Viktor.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the equations to be satisfied.
 *
 * @return equations to be satisfied.
 */
public final Formula equations() {

    // each b <= cols-1
    Formula f0 = Formula.TRUE;
    final IntConstant colConst = IntConstant.constant(cols - 1);
    for (IntExpression bi : b) {
        f0 = f0.and(bi.lte(colConst));
    }

    final Variable[] y = new Variable[rows];
    for (int i = 0; i < rows; i++) {
        y[i] = Variable.unary("y" + i);
    }

    Decls decls = y[0].oneOf(INTS);
    for (int i = 1; i < rows; i++)
        decls = decls.and(y[i].oneOf(INTS));

    Formula f1 = Formula.TRUE;
    final Expression[] combo = new Expression[rows];
    for (int i = 0; i < cols; i++) {
        for (int j = i + 1; j < cols; j++) {
            for (int k = j + 1; k < cols; k++) {
                Formula f2 = Formula.TRUE;
                for (int m = 0; m < rows; m++) {
                    combo[0] = a[m][i];
                    combo[1] = a[m][j];
                    combo[2] = a[m][k];
                    f2 = f2.and(conditionalSum(combo, y, 0, rows - 1).eq(b[m]));
                }
                f1 = f1.and(f2.not().forAll(decls));
            }
        }
    }
    return f0.and(f1);
}
 
Example 19
Source File: IntTest.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
private static IntExpression constant(int i) {
    return IntConstant.constant(i);
}
 
Example 20
Source File: IntTest.java    From kodkod with MIT License votes vote down vote up
private static IntExpression constant(int i) { return IntConstant.constant(i); }