<commit_before>/*
 * Zorbage: an algebraic data hierarchy for use in numeric processing.
 *
 * Copyright (C) 2016-2017 <NAME>
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package zorbage.type.data.int128;

import zorbage.algorithm.Gcd;
import zorbage.algorithm.Lcm;
import zorbage.type.algebra.BitOperations;
import zorbage.type.algebra.Bounded;
import zorbage.type.algebra.Integer;
import zorbage.type.algebra.Random;

/**
 * 
 * @author <NAME>
 *
 */
public class UnsignedInt128Group
	implements
		Integer<UnsignedInt128Group, UnsignedInt128Member>,
		Bounded<UnsignedInt128Member>,
		BitOperations<UnsignedInt128Member>,
		Random<UnsignedInt128Member>
{

	// TODO
	// 1) convert byte references and constants to long references and constants
	// 2) speed test versus imglib
	
	private static final java.util.Random rng = new java.util.Random(System.currentTimeMillis());
	private static final UnsignedInt128Member ZERO = new UnsignedInt128Member();
	private static final UnsignedInt128Member ONE = new UnsignedInt128Member((byte)0,(byte)1);

	@Override
	public UnsignedInt128Member construct() {
		return new UnsignedInt128Member();
	}

	@Override
	public UnsignedInt128Member construct(UnsignedInt128Member other) {
		return new UnsignedInt128Member(other);
	}

	@Override
	public UnsignedInt128Member construct(String str) {
		return new UnsignedInt128Member(str);
	}

	@Override
	public boolean isEqual(UnsignedInt128Member a, UnsignedInt128Member b) {
		return a.lo == b.lo && a.hi == b.hi;
	}

	@Override
	public boolean isNotEqual(UnsignedInt128Member a, UnsignedInt128Member b) {
		return !isEqual(a, b);
	}

	@Override
	public void assign(UnsignedInt128Member from, UnsignedInt128Member to) {
		to.set(from);
	}

	@Override
	public void zero(UnsignedInt128Member a) {
		a.hi = a.lo = 0;
	}

	// TODO: this shows that unsigned numbers aren't quite ints. They should derive slightly differently
	// in the algebra hierarchy.
	
	@Override
	public void negate(UnsignedInt128Member a, UnsignedInt128Member b) {
		assign(a,b); // ignore
	}

	@Override
	public void add(UnsignedInt128Member a, UnsignedInt128Member b, UnsignedInt128Member c) {
		byte cLo = (byte) (a.lo + b.lo);
		byte cHi = (byte) (a.hi + b.hi);
		int correction = 0;
		byte alh = (byte) (a.lo & 0x80);
		byte blh = (byte) (b.lo & 0x80);
		if (alh != 0 && blh != 0) {
			correction = 1;
		}
		else if ((alh != 0 && blh == 0) || (alh == 0 && blh != 0)) {
			byte all = (byte) (a.lo & 0x7f);
			byte bll = (byte) (b.lo & 0x7f);
			if ((byte)(all + bll) < 0)
				correction = 1;
		}
		cHi += correction;
		c.lo = cLo;
		c.hi = cHi;
	}

	@Override
	public void subtract(UnsignedInt128Member a, UnsignedInt128Member b, UnsignedInt128Member c) {
		byte cHi = (byte) (a.hi - b.hi);
		byte cLo = (byte) (a.lo - b.lo);
		final int correction;
		int alh = a.lo & 0x80;
		int blh = b.lo & 0x80;
		if (alh < blh)
			correction = 1;
		else if (alh > blh) {
			correction = 0;
		}
		else { // alh == blh
			int all = a.lo & 0x7f;
			int bll = b.lo & 0x7f;
			if (all < bll)
				correction = 1;
			else // (all >= bll)
				correction = 0;
		}
		cHi -= correction;
		c.lo = cLo;
		c.hi = cHi;<commit_msg>UInt128: make subtract code more resilient in its use of sign bits<commit_after>/*
 * Zorbage: an algebraic data hierarchy for use in numeric processing.
 *
 * Copyright (C) 2016-2017 <NAME>
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package zorbage.type.data.int128;

import zorbage.algorithm.Gcd;
import zorbage.algorithm.Lcm;
import zorbage.type.algebra.BitOperations;
import zorbage.type.algebra.Bounded;
import zorbage.type.algebra.Integer;
import zorbage.type.algebra.Random;

/**
 * 
 * @author <NAME>
 *
 */
public class UnsignedInt128Group
	implements
		Integer<UnsignedInt128Group, UnsignedInt128Member>,
		Bounded<UnsignedInt128Member>,
		BitOperations<UnsignedInt128Member>,
		Random<UnsignedInt128Member>
{

	// TODO
	// 1) convert byte references and constants to long references and constants
	// 2) speed test versus imglib
	
	private static final java.util.Random rng = new java.util.Random(System.currentTimeMillis());
	private static final UnsignedInt128Member ZERO = new UnsignedInt128Member();
	private static final UnsignedInt128Member ONE = new UnsignedInt128Member((byte)0,(byte)1);

	@Override
	public UnsignedInt128Member construct() {
		return new UnsignedInt128Member();
	}

	@Override
	public UnsignedInt128Member construct(UnsignedInt128Member other) {
		return new UnsignedInt128Member(other);
	}

	@Override
	public UnsignedInt128Member construct(String str) {
		return new UnsignedInt128Member(str);
	}

	@Override
	public boolean isEqual(UnsignedInt128Member a, UnsignedInt128Member b) {
		return a.lo == b.lo && a.hi == b.hi;
	}

	@Override
	public boolean isNotEqual(UnsignedInt128Member a, UnsignedInt128Member b) {
		return !isEqual(a, b);
	}

	@Override
	public void assign(UnsignedInt128Member from, UnsignedInt128Member to) {
		to.set(from);
	}

	@Override
	public void zero(UnsignedInt128Member a) {
		a.hi = a.lo = 0;
	}

	// TODO: this shows that unsigned numbers aren't quite ints. They should derive slightly differently
	// in the algebra hierarchy.
	
	@Override
	public void negate(UnsignedInt128Member a, UnsignedInt128Member b) {
		assign(a,b); // ignore
	}

	@Override
	public void add(UnsignedInt128Member a, UnsignedInt128Member b, UnsignedInt128Member c) {
		byte cLo = (byte) (a.lo + b.lo);
		byte cHi = (byte) (a.hi + b.hi);
		int correction = 0;
		byte alh = (byte) (a.lo & 0x80);
		byte blh = (byte) (b.lo & 0x80);
		if (alh != 0 && blh != 0) {
			correction = 1;
		}
		else if ((alh != 0 && blh == 0) || (alh == 0 && blh != 0)) {
			byte all = (byte) (a.lo & 0x7f);
			byte bll = (byte) (b.lo & 0x7f);
			if ((byte)(all + bll) < 0)
				correction = 1;
		}
		cHi += correction;
		c.lo = cLo;
		c.hi = cHi;
	}

	@Override
	public void subtract(UnsignedInt128Member a, UnsignedInt128Member b, UnsignedInt128Member c) {
		byte cHi = (byte) (a.hi - b.hi);
		byte cLo = (byte) (a.lo - b.lo);
		final int correction;
		int alh = a.lo & 0x80;
		int blh = b.lo & 0x80;
		if (alh == 0 && blh != 0)
			correction = 1;
		else if (alh != 0 && blh == 0) {
			correction = 0;
		}
		else { // alh == blh
			int all = a.lo & 0x7f;
			int bll = b.lo & 0x7f;
			if (all < bll)
				correction = 1;
			else // (all >= bll)
				correction = 0;
		}
		cHi -= correction;
		c.lo = cLo;
		c.hi = cHi;
{'fraction_non_alphanumeric': 0.0999888030455716, 'fraction_numerical': 0.03392677191803829, 'mean_word_length': 3.828108108108108, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5040127', 'n_tokens_mistral': 3337, 'n_tokens_neox': 2838, 'n_words': 994}