/* Funtion    : common library SW version                                   */
/* dummy routine that does nothing  */
extern void mbdr_dsp_utils_nop_single_pointer_param(void *pi_dummy_p);
/*                                                                          */
/* Function    : define utilities to cyclic buffers                         */
/*                                                                          */
/*  Available routines:                                                     */
/*    1) void* mbdr_dsp_utils_memcpy_dw_by_dw(void       *po_dst_buf_p,     */
/*                                     const void *pi_src_buf_p,            */
/*                                     size_t      pi_buf_byte_size)        */
/*                                                                          */

#include  "windef.h"
#include  "mbdr_dsp_utils.h"
#include  "string.h"

/* never read dummy/debug */
LONG  mbdr_dsp_utils_dummy;
SHORT mbdr_dsp_utils_dbg_main_loop_index;


/* never read dummy pointer */
void *mbdr_dsp_utils_dummy_p;


void* mbdr_dsp_utils_memcpy_dw_by_dw(void       *po_dst_buf_p,
                                     const void *pi_src_buf_p,
                                     size_t      pi_buf_byte_size)
{/* mbdr_dsp_utils_memcpy_dw_by_dw */
  register int   j, loop_iteration_number;
  register LONG  *src_dw_p, *dst_dw_p;

  src_dw_p = (LONG *)pi_src_buf_p;
  dst_dw_p = (LONG *)po_dst_buf_p;

  /* loop copy dw by dw */
  loop_iteration_number = pi_buf_byte_size/4;
  for (j=0; j<loop_iteration_number; j++)
        *dst_dw_p++ = *src_dw_p++;

  return(po_dst_buf_p);

}/* mbdr_dsp_utils_memcpy_dw_by_dw */




void mbdr_dsp_utils_nop_single_param(SHORT PI_PARAM)
{
 /* avoid error */
 mbdr_dsp_utils_dummy = PI_PARAM;
}

/* routine that do nothing */
void mbdr_dsp_utils_nop(void)
{
}

/* returns s32 sum of input vector  */
LONG mbdr_dsp_vec_s32_sum_elements_aligned(
                                   LONG  *pi_dw_vec_p,
                                   LONG   pi_vec_byte_size_align_32)
{/* mbdr_dsp_vec_s32_sum_elements_aligned */

  LONG i, odd_sum, even_sum;

  odd_sum  = 0;
  even_sum = 0;
  i        = 0;

  do
  {

    even_sum += pi_dw_vec_p[i*8+0]
               +pi_dw_vec_p[i*8+2]
               +pi_dw_vec_p[i*8+4]
               +pi_dw_vec_p[i*8+6];
    odd_sum +=  pi_dw_vec_p[i*8+1]
               +pi_dw_vec_p[i*8+3]
               +pi_dw_vec_p[i*8+5]
               +pi_dw_vec_p[i*8+7];
     i ++;
  }while(i
         <
         (pi_vec_byte_size_align_32/(8*sizeof(LONG))) );

  return(odd_sum+even_sum);


}/* mbdr_dsp_vec_s32_sum_elements_aligned */


void mbdr_dsp_read_sync_2_dw(volatile LONG *pi_src_2dw_buf_p,
                             LONG           pi_sync_bit_number,
							 mbdr_dsp_read_sync_2_dw_debug_counters_t 
                                           *pio_debug_counters_p,
                             LONG          *po_dst_read_sync_2dw_buf_p)
{/* mbdr_dsp_read_sync_2_dw */

 register LONG mask, bits_difference_f;
 register LONG first_dw, second_dw;

 /* increments call debug counter */
 pio_debug_counters_p->start_count++;

 /* pipe line init */
 first_dw  = pi_src_2dw_buf_p[0];
 second_dw = pi_src_2dw_buf_p[1];
 mask      = (1<<pi_sync_bit_number);
 do
 {
  /* assume success, stores the last read data in putput buffer anyway */
  po_dst_read_sync_2dw_buf_p[0] = first_dw;
  po_dst_read_sync_2dw_buf_p[1] = second_dw;

  /* set if and only if interesting bit differs between first and second DW */
  bits_difference_f =
            ( ((first_dw^second_dw) & mask)
              !=
              0);

  /* reread for a case of fault synchronization in previous read */
  first_dw  = pi_src_2dw_buf_p[0];
  second_dw = pi_src_2dw_buf_p[1];

 }while(bits_difference_f);

 /* increments exit debug counter */
 pio_debug_counters_p->exit_count++;


}/* mbdr_dsp_read_sync_2_dw */

/* dummy routine that does nothing  */
void mbdr_dsp_utils_nop_single_pointer_param(void *pi_dummy_p)
{/* mbdr_dsp_utils_nop_single_pointer_param */

  /* avoid warning */
  mbdr_dsp_utils_dummy_p = pi_dummy_p;
        
}/* mbdr_dsp_utils_nop_single_pointer_param */
