//! returns: 0

/// Combine shifts, bitwise expressions, and unsigned word addresses.
@default fn main() -> i32 {
    for x in [0 as u64, 1, 0x8000000000000000, 0xffffffffffffffff, 0x123456789abcdef0] {
        assert multiplyThree(x) == x + x + x;
        assert multiplyFive(x) == multiplyThree(x) + x + x;
        assert multiplyNine(x) == multiplyFive(x) + multiplyThree(x) + x;
        assert maskHalf(x) == x as u16 as u64;
        assert maskWord(x) == x as u32 as u64;
        assert shiftAdd(x, 9) == (x << 3) + 9;
        assert invertedAnd(x, 0x123456789abcdef0) == (x & ~0x123456789abcdef0);
        assert invertedOr(x, 0x123456789abcdef0) == (x | ~0x123456789abcdef0);
        assert invertedXor(x, 0x123456789abcdef0) == ~(x ^ 0x123456789abcdef0);
        assert rotate(x) == ((x >> 61) | (x << 3));
        assert rotateWord(x as u32) == ((x as u32 >> 29) | (x as u32 << 3));
        for bit in [0 as u64, 31, 32, 63, 64, 127] {
            assert rotateVariable(x, bit) == rotateReference(x, bit);
            assert rotateWordVariable(x as u32, bit as u32)
                == rotateWordReference(x as u32, bit as u32);
            let mask = 1 as u64 << (bit & 63);
            assert setBit(x, bit) == (x | mask);
            assert clearBit(x, bit) == (x & ~mask);
            assert invertBit(x, bit) == (x ^ mask);
            assert extractBit(x, bit) == ((x >> (bit & 63)) & 1);
        }
        assert extendAdd(x as u32, 9) == (x & 0xffffffff) + 9;
        assert extendShift(x as u32) == ((x & 0xffffffff) << 3);
        assert extendShiftAdd(x as u32, 9) == ((x & 0xffffffff) << 3) + 9;
        assert extendByte(x as i8) == x as i8 as i64;
        assert extendHalf(x as i16) == x as i16 as i64;
        assert unsignedHalf(x as u16) == (x & 0xffff);
    }
    let words = [11 as u64, 22, 33, 44];
    for i in 0 .. 4 {
        assert words[i] == (i as u64 + 1) * 11;
    }
    return 0;
}

/// Add a scaled full-width value.
fn shiftAdd(x: u64, y: u64) -> u64 {
    return (x << 3) + y;
}

/// Clear the bits set in the second operand.
fn invertedAnd(x: u64, y: u64) -> u64 {
    return x & ~y;
}

/// Set the bits clear in the second operand.
fn invertedOr(x: u64, y: u64) -> u64 {
    return x | ~y;
}

/// Return the complement of the exclusive-or.
fn invertedXor(x: u64, y: u64) -> u64 {
    return ~(x ^ y);
}

/// Rotate the full register left by three bits.
fn rotate(x: u64) -> u64 {
    return (x << 3) | (x >> 61);
}

/// Rotate the low word left by three bits.
fn rotateWord(x: u32) -> u32 {
    return (x << 3) | (x >> 29);
}

/// Set the selected bit.
fn setBit(x: u64, n: u64) -> u64 {
    return x | (1 as u64 << n);
}

/// Clear the selected bit.
fn clearBit(x: u64, n: u64) -> u64 {
    return x & ~(1 as u64 << n);
}

/// Invert the selected bit.
fn invertBit(x: u64, n: u64) -> u64 {
    return x ^ (1 as u64 << n);
}

/// Extract the selected bit.
fn extractBit(x: u64, n: u64) -> u64 {
    return (x >> n) & 1;
}

/// Zero-extend a word and add a full-width value.
fn extendAdd(x: u32, y: u64) -> u64 {
    return x as u64 + y;
}

/// Shift a zero-extended word.
fn extendShift(x: u32) -> u64 {
    return x as u64 << 3;
}

/// Add a scaled zero-extended word.
fn extendShiftAdd(x: u32, y: u64) -> u64 {
    return (x as u64 << 3) + y;
}

/// Sign-extend a byte.
fn extendByte(x: i8) -> i64 {
    return x as i64;
}

/// Sign-extend a halfword.
fn extendHalf(x: i16) -> i64 {
    return x as i64;
}

/// Zero-extend a halfword.
fn unsignedHalf(x: u16) -> u64 {
    return x as u64;
}

/// Rotate a register by a masked runtime count.
fn rotateVariable(x: u64, n: u64) -> u64 {
    return (x << n) | (x >> (64 - n));
}

/// Compute a rotation with explicitly masked counts.
fn rotateReference(x: u64, n: u64) -> u64 {
    let k = n & 63;
    return (x << k) | (x >> ((64 - k) & 63));
}

/// Rotate a word by a masked runtime count.
fn rotateWordVariable(x: u32, n: u32) -> u32 {
    return (x << n) | (x >> (32 - n));
}

/// Compute a word rotation with explicitly masked counts.
fn rotateWordReference(x: u32, n: u32) -> u32 {
    let k = n & 31;
    return (x << k) | (x >> ((32 - k) & 31));
}

/// Multiply a register by three.
fn multiplyThree(x: u64) -> u64 {
    return x * 3;
}

/// Multiply a register by five.
fn multiplyFive(x: u64) -> u64 {
    return x * 5;
}

/// Multiply a register by nine.
fn multiplyNine(x: u64) -> u64 {
    return x * 9;
}

/// Keep the low halfword.
fn maskHalf(x: u64) -> u64 {
    return x & 0xffff;
}

/// Keep the low word.
fn maskWord(x: u64) -> u64 {
    return x & 0xffffffff;
}
