<commit_before>/* Aravis - Digital camera library
 *
 * Copyright © 2009-2019 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Author: <NAME> <<EMAIL>>
 */

#include <arvgcmaskedintregnode.h>
#include <arvgcregisternodeprivate.h>
#include <arvgcinteger.h>
#include <arvgcselector.h>

typedef struct {
	ArvGcPropertyNode *lsb;
	ArvGcPropertyNode *msb;
	ArvGcPropertyNode *sign;
	ArvGcPropertyNode *endianess;
	ArvGcPropertyNode *unit;

	GSList *selecteds;
	GSList *selected_features;
} ArvGcMaskedIntRegNodePrivate;

static void arv_gc_masked_int_reg_node_init (ArvGcMaskedIntRegNode *self);
static void arv_gc_masked_int_reg_node_integer_interface_init (ArvGcIntegerInterface *interface);
static void arv_gc_masked_int_reg_node_selector_interface_init (ArvGcSelectorInterface *interface);

G_DEFINE_TYPE_WITH_CODE (ArvGcMaskedIntRegNode, arv_gc_masked_int_reg_node, ARV_TYPE_GC_REGISTER_NODE,
			 G_ADD_PRIVATE (ArvGcMaskedIntRegNode)
			 G_IMPLEMENT_INTERFACE (ARV_TYPE_GC_INTEGER, arv_gc_masked_int_reg_node_integer_interface_init)
			 G_IMPLEMENT_INTERFACE (ARV_TYPE_GC_SELECTOR, arv_gc_masked_int_reg_node_selector_interface_init))

static const char *
arv_gc_masked_int_reg_node_get_node_name (ArvDomNode *node)
{
	return "MaskedIntReg";
}

static void
arv_gc_masked_int_reg_node_post_new_child (ArvDomNode *self, ArvDomNode *child)
{
	ArvGcMaskedIntRegNodePrivate *priv = arv_gc_masked_int_reg_node_get_instance_private (ARV_GC_MASKED_INT_REG_NODE (self));

	if (ARV_IS_GC_PROPERTY_NODE (child)) {
		ArvGcPropertyNode *property_node = ARV_GC_PROPERTY_NODE (child);

		switch (arv_gc_property_node_get_node_type (property_node)) {
			case ARV_GC_PROPERTY_NODE_TYPE_LSB:
				priv->lsb = property_node;
				break;
			case ARV_GC_PROPERTY_NODE_TYPE_MSB:
				priv->msb = property_node;
				break;
			case ARV_GC_PROPERTY_NODE_TYPE_BIT:
				priv->msb = property_node;
				priv->lsb = property_node;
				break;
			case ARV_GC_PROPERTY_NODE_TYPE_SIGN:
				priv->sign = property_node;
				break;
			case ARV_GC_PROPERTY_NODE_TYPE_ENDIANESS:
				priv->endianess = property_node;
				break;
			case ARV_GC_PROPERTY_NODE_TYPE_UNIT:
				priv->unit = property_node;
				break;
			case ARV_GC_PROPERTY_NODE_TYPE_P_SELECTED:
				priv->selecteds = g_slist_prepend (priv->selecteds, property_node);
				break;

				/* TODO Representation */

			default:
				ARV_DOM_NODE_CLASS (arv_gc_masked_int_reg_node_parent_class)->post_new_child (self, child);
				break;
		}
	} else {
		ARV_DOM_NODE_CLASS (arv_gc_masked_int_reg_node_parent_class)->post_new_child (self, child);
	}
}

static gint64
arv_gc_masked_int_reg_node_get_integer_value (ArvGcInteger *self, GError **error)
{
	ArvGcMaskedIntRegNodePrivate *priv = arv_gc_masked_int_reg_node_get_instance_private (ARV_GC_MASKED_INT_REG_NODE (self));

	return arv_gc_register_node_get_masked_integer_value (ARV_GC_REGISTER_NODE (self),
							      arv_gc_property_node_get_lsb (priv->lsb, 0),
							      arv_gc_property_node_get_msb (priv->msb, 31),
							      arv_gc_property_node_get_sign (priv->sign, ARV_GC_SIGNEDNESS_UNSIGNED),
							      arv_gc_property_node_get_endianess (priv->endianess, G_LITTLE_ENDIAN),
							      ARV_GC_CACHABLE_UNDEFINED,
							      TRUE, error);
}

static void
arv_gc_masked_int_reg_node_set_integer_value (ArvGcInteger *self, gint64 value, GError **error)
{
	ArvGcMaskedIntRegNodePrivate *priv = arv_gc_masked_int_reg_node_get_instance_private (ARV_GC_MASKED_INT_REG_NODE (self));

	arv_gc_register_node_set_masked_integer_value (ARV_GC_REGISTER_NODE (self),
						       arv_gc_property_node_get_lsb (priv->lsb, 0),
						       arv_gc_property_node_get_msb (priv->msb, 31),
						       arv_gc_property_node_get_sign (priv->sign, ARV_GC_SIGNEDNESS_UNSIGNED),
						       arv_gc_property_node_get_endianess (priv->endianess, G_LITTLE_ENDIAN),
						       ARV_GC_CACHABLE_UNDEFINED,
						       TRUE, value, error);
}

static gint64
arv_gc_masked_int_reg_node_get_min (ArvGcInteger *self, GError **error)
{
	ArvGcMaskedIntRegNodePrivate *priv = arv_gc_masked_int_reg_node_get_instance_private (ARV_GC_MASKED_INT_REG_NODE (self));
	gint64 lsb, msb;
	ArvGcSignedness signedness;

	lsb = arv_gc_property_node_get_lsb (priv->lsb, 0);
	msb = arv_gc_property_node_get_msb (priv->msb, 31);
	signedness = arv_gc_property_node_get_sign (priv->sign, ARV_GC_SIGNEDNESS_UNSIGNED);

	/* TODO endianess */

	if (signedness == ARV_GC_SIGNEDNESS_SIGNED) {
		return -((1 << (msb - lsb  - 1)));
	} else {
		return 0;
	}
}

static gint64
arv_gc_masked_int_reg_node_get_max (ArvGcInteger *self, GError **error)
{
	ArvGcMaskedIntRegNodePrivate *priv = arv_gc_masked_int_reg_node_get_instance_private (ARV_GC_MASKED_INT_REG_NODE (self));
	gint64 lsb, msb;
	ArvGcSignedness signedness;

	lsb = arv_gc_property_node_get_lsb (priv->lsb, 0);
	msb = arv_gc_property_node_get_msb (priv->msb, 31);
	signedness = arv_gc_property_node_get_sign (priv->sign, ARV_GC_SIGNEDNESS_UNSIGNED);

	/* TODO endianess */

	if (signedness == ARV_GC_SIGNEDNESS_SIGNED) {
		return ((1 << (msb - lsb  - 1)) - 1);
	} else {
		return (1 << (msb - lsb)) - 1;
	}
}

static const char *
arv_gc_masked_int_reg_node_get_unit (ArvGcInteger *self, GError **error)
{
	ArvGcMaskedIntRegNodePrivate *priv = arv_gc_masked_int_reg_node_get_instance_private (ARV_GC_MASKED_INT_REG_NODE (self));

	if (priv->unit == NULL)
		return NULL;

	return arv_gc_property_node_get_string (priv->unit, error);
}

/**
 * arv_gc_masked_int_reg_node_new:
 *
 * Returns: a new MaskedIntReg node
 *
 * Since:0.8.0
 */

ArvGcNode *
arv_gc_masked_int_reg_node_new	(void)<commit_msg>maskedintreg: fix min/max implementation<commit_after>/* Aravis - Digital camera library
 *
 * Copyright © 2009-2019 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Author: <NAME> <<EMAIL>>
 */

#include <arvgcmaskedintregnode.h>
#include <arvgcregisternodeprivate.h>
#include <arvgcinteger.h>
#include <arvgcselector.h>

#define ARV_GC_MASKED_INT_REG_NODE_DEFAULT_SIGNEDNESS ARV_GC_SIGNEDNESS_UNSIGNED
#define ARV_GC_MASKED_INT_REG_NODE_DEFAULT_ENDIANESS G_LITTLE_ENDIAN

typedef struct {
	ArvGcPropertyNode *lsb;
	ArvGcPropertyNode *msb;
	ArvGcPropertyNode *sign;
	ArvGcPropertyNode *endianess;
	ArvGcPropertyNode *unit;

	GSList *selecteds;
	GSList *selected_features;
} ArvGcMaskedIntRegNodePrivate;

static void arv_gc_masked_int_reg_node_init (ArvGcMaskedIntRegNode *self);
static void arv_gc_masked_int_reg_node_integer_interface_init (ArvGcIntegerInterface *interface);
static void arv_gc_masked_int_reg_node_selector_interface_init (ArvGcSelectorInterface *interface);

G_DEFINE_TYPE_WITH_CODE (ArvGcMaskedIntRegNode, arv_gc_masked_int_reg_node, ARV_TYPE_GC_REGISTER_NODE,
			 G_ADD_PRIVATE (ArvGcMaskedIntRegNode)
			 G_IMPLEMENT_INTERFACE (ARV_TYPE_GC_INTEGER, arv_gc_masked_int_reg_node_integer_interface_init)
			 G_IMPLEMENT_INTERFACE (ARV_TYPE_GC_SELECTOR, arv_gc_masked_int_reg_node_selector_interface_init))

static const char *
arv_gc_masked_int_reg_node_get_node_name (ArvDomNode *node)
{
	return "MaskedIntReg";
}

static void
arv_gc_masked_int_reg_node_post_new_child (ArvDomNode *self, ArvDomNode *child)
{
	ArvGcMaskedIntRegNodePrivate *priv = arv_gc_masked_int_reg_node_get_instance_private (ARV_GC_MASKED_INT_REG_NODE (self));

	if (ARV_IS_GC_PROPERTY_NODE (child)) {
		ArvGcPropertyNode *property_node = ARV_GC_PROPERTY_NODE (child);

		switch (arv_gc_property_node_get_node_type (property_node)) {
			case ARV_GC_PROPERTY_NODE_TYPE_LSB:
				priv->lsb = property_node;
				break;
			case ARV_GC_PROPERTY_NODE_TYPE_MSB:
				priv->msb = property_node;
				break;
			case ARV_GC_PROPERTY_NODE_TYPE_BIT:
				priv->msb = property_node;
				priv->lsb = property_node;
				break;
			case ARV_GC_PROPERTY_NODE_TYPE_SIGN:
				priv->sign = property_node;
				break;
			case ARV_GC_PROPERTY_NODE_TYPE_ENDIANESS:
				priv->endianess = property_node;
				break;
			case ARV_GC_PROPERTY_NODE_TYPE_UNIT:
				priv->unit = property_node;
				break;
			case ARV_GC_PROPERTY_NODE_TYPE_P_SELECTED:
				priv->selecteds = g_slist_prepend (priv->selecteds, property_node);
				break;

				/* TODO Representation */

			default:
				ARV_DOM_NODE_CLASS (arv_gc_masked_int_reg_node_parent_class)->post_new_child (self, child);
				break;
		}
	} else {
		ARV_DOM_NODE_CLASS (arv_gc_masked_int_reg_node_parent_class)->post_new_child (self, child);
	}
}

static gint64
arv_gc_masked_int_reg_node_get_integer_value (ArvGcInteger *self, GError **error)
{
	ArvGcMaskedIntRegNodePrivate *priv = arv_gc_masked_int_reg_node_get_instance_private (ARV_GC_MASKED_INT_REG_NODE (self));

	return arv_gc_register_node_get_masked_integer_value
		(ARV_GC_REGISTER_NODE (self),
		 arv_gc_property_node_get_lsb (priv->lsb, 0),
		 arv_gc_property_node_get_msb (priv->msb, 31),
		 arv_gc_property_node_get_sign (priv->sign, ARV_GC_MASKED_INT_REG_NODE_DEFAULT_SIGNEDNESS),
		 arv_gc_property_node_get_endianess (priv->endianess, ARV_GC_MASKED_INT_REG_NODE_DEFAULT_ENDIANESS),
		 ARV_GC_CACHABLE_UNDEFINED,
		 TRUE, error);
}

static void
arv_gc_masked_int_reg_node_set_integer_value (ArvGcInteger *self, gint64 value, GError **error)
{
	ArvGcMaskedIntRegNodePrivate *priv = arv_gc_masked_int_reg_node_get_instance_private (ARV_GC_MASKED_INT_REG_NODE (self));

	arv_gc_register_node_set_masked_integer_value
		(ARV_GC_REGISTER_NODE (self),
		 arv_gc_property_node_get_lsb (priv->lsb, 0),
		 arv_gc_property_node_get_msb (priv->msb, 31),
		 arv_gc_property_node_get_sign (priv->sign, ARV_GC_MASKED_INT_REG_NODE_DEFAULT_SIGNEDNESS),
		 arv_gc_property_node_get_endianess (priv->endianess, ARV_GC_MASKED_INT_REG_NODE_DEFAULT_ENDIANESS),
		 ARV_GC_CACHABLE_UNDEFINED,
		 TRUE, value, error);
}

static gint64
arv_gc_masked_int_reg_node_get_min (ArvGcInteger *self, GError **error)
{
	ArvGcMaskedIntRegNodePrivate *priv = arv_gc_masked_int_reg_node_get_instance_private (ARV_GC_MASKED_INT_REG_NODE (self));
	gint64 lsb, msb;
	ArvGcSignedness signedness;
	guint endianess;

	lsb = arv_gc_property_node_get_lsb (priv->lsb, 0);
	msb = arv_gc_property_node_get_msb (priv->msb, 31);
	signedness = arv_gc_property_node_get_sign (priv->sign, ARV_GC_MASKED_INT_REG_NODE_DEFAULT_SIGNEDNESS);
	endianess =arv_gc_property_node_get_endianess (priv->endianess, ARV_GC_MASKED_INT_REG_NODE_DEFAULT_ENDIANESS);

	if (signedness == ARV_GC_SIGNEDNESS_SIGNED) {
		return endianess == G_BIG_ENDIAN ?
			-((1 << (lsb - msb))) :
			-((1 << (msb - lsb)));
	} else {
		return 0;
	}
}

static gint64
arv_gc_masked_int_reg_node_get_max (ArvGcInteger *self, GError **error)
{
	ArvGcMaskedIntRegNodePrivate *priv = arv_gc_masked_int_reg_node_get_instance_private (ARV_GC_MASKED_INT_REG_NODE (self));
	gint64 lsb, msb;
	ArvGcSignedness signedness;
	guint endianess;

	lsb = arv_gc_property_node_get_lsb (priv->lsb, 0);
	msb = arv_gc_property_node_get_msb (priv->msb, 31);
	signedness = arv_gc_property_node_get_sign (priv->sign, ARV_GC_MASKED_INT_REG_NODE_DEFAULT_SIGNEDNESS);
	endianess =arv_gc_property_node_get_endianess (priv->endianess, ARV_GC_MASKED_INT_REG_NODE_DEFAULT_ENDIANESS);

	if (signedness == ARV_GC_SIGNEDNESS_SIGNED) {
		return endianess == G_BIG_ENDIAN ?
			((1 << (lsb - msb)) - 1) :
			((1 << (msb - lsb)) - 1);
	} else {
		return endianess == G_BIG_ENDIAN ?
			(1 << (lsb - msb + 1)) - 1 :
			(1 << (msb - lsb + 1)) - 1;
	}
}

static const char *
arv_gc_masked_int_reg_node_get_unit (ArvGcInteger *self, GError **error)
{
	ArvGcMaskedIntRegNodePrivate *priv = arv_gc_masked_int_reg_node_get_instance_private (ARV_GC_MASKED_INT_REG_NODE (self));

	if (priv->unit == NULL)
		return NULL;

	return arv_gc_property_node_get_string (priv->unit, error);
}

/**
 * arv_gc_masked_int_reg_node_new:
 *
 * Returns: a new MaskedIntReg node
 *
 * Since:0.8.0
 */

ArvGcNode *
arv_gc_masked_int_reg_node_new	(void)
{'fraction_non_alphanumeric': 0.0817256569952909, 'fraction_numerical': 0.008734619474403767, 'mean_word_length': 5.8578125, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 75, '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': '3598454', 'n_tokens_mistral': 5575, 'n_tokens_neox': 5164, 'n_words': 925}