/*
 * sum_e_3f.c
 *
 *  Created on: 30-07-2019
 *      Author: Krzysztof Jakubczyk
 */

#include <math.h>

#include "tdefs.h"
#include "misc.h"

#include "helper.h"
#include "sum_e_3f.h"

int sum_e_3f_initlog(void *arguments, void *logic)
{
	struct sum_e_3f_args *args = (struct sum_e_3f_args *)arguments;
	struct sum_e_3f_logic *log = (struct sum_e_3f_logic *)logic;

	if(set_float_ptr(args->io.in_l1_orta_a_float_in,&log->in_l1_orta_a))
		return -1;
	if(set_float_ptr(args->io.in_l1_ortb_a_float_in,&log->in_l1_ortb_a))
		return -1;
	if(set_float_ptr(args->io.in_l1_orta_b_float_in,&log->in_l1_orta_b))
		return -1;
	if(set_float_ptr(args->io.in_l1_ortb_b_float_in,&log->in_l1_ortb_b))
		return -1;

	if(set_float_ptr(args->io.in_l2_orta_a_float_in,&log->in_l2_orta_a))
		return -1;
	if(set_float_ptr(args->io.in_l2_ortb_a_float_in,&log->in_l2_ortb_a))
		return -1;
	if(set_float_ptr(args->io.in_l2_orta_b_float_in,&log->in_l2_orta_b))
		return -1;
	if(set_float_ptr(args->io.in_l2_ortb_b_float_in,&log->in_l2_ortb_b))
		return -1;

	if(set_float_ptr(args->io.in_l3_orta_a_float_in,&log->in_l3_orta_a))
		return -1;
	if(set_float_ptr(args->io.in_l3_ortb_a_float_in,&log->in_l3_ortb_a))
		return -1;
	if(set_float_ptr(args->io.in_l3_orta_b_float_in,&log->in_l3_orta_b))
		return -1;
	if(set_float_ptr(args->io.in_l3_ortb_b_float_in,&log->in_l3_ortb_b))
		return -1;

	if(set_float_ptr(args->io.in_3i0_orta_a_float_in,&log->in_3i0_orta_a))
		return -1;
	if(set_float_ptr(args->io.in_3i0_ortb_a_float_in,&log->in_3i0_ortb_a))
		return -1;
	if(set_float_ptr(args->io.in_3i0_orta_b_float_in,&log->in_3i0_orta_b))
		return -1;
	if(set_float_ptr(args->io.in_3i0_ortb_b_float_in,&log->in_3i0_ortb_b))
		return -1;

	if(set_float_ptr(args->io.out_l1_orta_float_out,&log->out_l1_orta))
		return -1;
	if(set_float_ptr(args->io.out_l1_ortb_float_out,&log->out_l1_ortb))
		return -1;
	if(set_float_ptr(args->io.out_l1_esk_float_out,&log->out_l1_esk))
		return -1;

	if(set_float_ptr(args->io.out_l2_orta_float_out,&log->out_l2_orta))
		return -1;
	if(set_float_ptr(args->io.out_l2_ortb_float_out,&log->out_l2_ortb))
		return -1;
	if(set_float_ptr(args->io.out_l2_esk_float_out,&log->out_l2_esk))
		return -1;

	if(set_float_ptr(args->io.out_l3_orta_float_out,&log->out_l3_orta))
		return -1;
	if(set_float_ptr(args->io.out_l3_ortb_float_out,&log->out_l3_ortb))
		return -1;
	if(set_float_ptr(args->io.out_l3_esk_float_out,&log->out_l3_esk))
		return -1;

	if(set_float_ptr(args->io.out_3i0_orta_float_out,&log->out_3i0_orta))
		return -1;
	if(set_float_ptr(args->io.out_3i0_ortb_float_out,&log->out_3i0_ortb))
		return -1;
	if(set_float_ptr(args->io.out_3i0_esk_float_out,&log->out_3i0_esk))
		return -1;

	return 0;
}

void sum_e_3f(void *arguments, void *logic)
{
	struct sum_e_3f_logic *log = (struct sum_e_3f_logic *)logic;
	struct sum_e_3f_args *args = (struct sum_e_3f_args *)arguments;
	float m1 = args->params.m1;
	float m2 = args->params.m2;

	if(m1!=1.0f || m2!=1.0f)
	{
		*log->out_l1_orta=(*log->in_l1_orta_a * m1) + (*log->in_l1_orta_b * m2);
		*log->out_l1_ortb=(*log->in_l1_ortb_a * m1) + (*log->in_l1_ortb_b * m2);
		*log->out_l1_esk=((*log->out_l1_orta * *log->out_l1_orta) + (*log->out_l1_ortb * *log->out_l1_ortb))/2;

		*log->out_l2_orta=(*log->in_l2_orta_a * m1) + (*log->in_l2_orta_b * m2);
		*log->out_l2_ortb=(*log->in_l2_ortb_a * m1) + (*log->in_l2_ortb_b * m2);
		*log->out_l2_esk=((*log->out_l2_orta * *log->out_l2_orta) + (*log->out_l2_ortb * *log->out_l2_ortb))/2;

		*log->out_l3_orta=(*log->in_l3_orta_a * m1) + (*log->in_l3_orta_b * m2);
		*log->out_l3_ortb=(*log->in_l3_ortb_a * m1) + (*log->in_l3_ortb_b * m2);
		*log->out_l3_esk=((*log->out_l3_orta * *log->out_l3_orta) + (*log->out_l3_ortb * *log->out_l3_ortb))/2;

		*log->out_3i0_orta=(*log->in_3i0_orta_a * m1) + (*log->in_3i0_orta_b * m2);
		*log->out_3i0_ortb=(*log->in_3i0_ortb_a * m1) + (*log->in_3i0_ortb_b * m2);
		*log->out_3i0_esk=((*log->out_3i0_orta * *log->out_3i0_orta) + (*log->out_3i0_ortb * *log->out_3i0_ortb))/2;
	}
	else
	{
		*log->out_l1_orta=(*log->in_l1_orta_a) + (*log->in_l1_orta_b);
		*log->out_l1_ortb=(*log->in_l1_ortb_a) + (*log->in_l1_ortb_b);
		*log->out_l1_esk=((*log->out_l1_orta * *log->out_l1_orta) + (*log->out_l1_ortb * *log->out_l1_ortb))/2;

		*log->out_l2_orta=(*log->in_l2_orta_a) + (*log->in_l2_orta_b);
		*log->out_l2_ortb=(*log->in_l2_ortb_a) + (*log->in_l2_ortb_b);
		*log->out_l2_esk=((*log->out_l2_orta * *log->out_l2_orta) + (*log->out_l2_ortb * *log->out_l2_ortb))/2;

		*log->out_l3_orta=(*log->in_l3_orta_a) + (*log->in_l3_orta_b);
		*log->out_l3_ortb=(*log->in_l3_ortb_a) + (*log->in_l3_ortb_b);
		*log->out_l3_esk=((*log->out_l3_orta * *log->out_l3_orta) + (*log->out_l3_ortb * *log->out_l3_ortb))/2;

		*log->out_3i0_orta=(*log->in_3i0_orta_a) + (*log->in_3i0_orta_b);
		*log->out_3i0_ortb=(*log->in_3i0_ortb_a) + (*log->in_3i0_ortb_b);
		*log->out_3i0_esk=((*log->out_3i0_orta * *log->out_3i0_orta) + (*log->out_3i0_ortb * *log->out_3i0_ortb))/2;
	}
}
