lib_dsp vector Topic is solved

If you have a simple question and just want an answer.
Post Reply
User avatar
Sebastian
Active Member
Posts: 39
Joined: Wed Jul 20, 2016 9:15 am

lib_dsp vector

Post by Sebastian »

Hi guys,

actually I play a little bit around with the vector functions from the lib_dsp.

But I get stuck with with: dsp_vector_mulv_addv

It seems, that this function only make the addition, but not the multiplication.
Maybe someone had an idea what I do wrong in this example.

dsp_vector_mulv_subv
produces the result as expected.

Here is my simple example + make file + xrun output

I'm using: 14.3.0 and lib_dsp: 3.1.0 (last commit from 27.Apr)

Bests,
Sebastian

Code: Select all

#include <stdio.h>
#include <xs1.h>
#include <dsp.h>
#include <platform.h>


#define Q_N                   24
#define SHORT_SAMPLE_LENGTH   5


int32_t  Src[]  = { Q24(.11), Q24(.12), Q24(.13), Q24(.14), Q24(.15)};
int32_t  Src2[] = { Q24(.51), Q24(.52), Q24(.53), Q24(.54), Q24(.55)};
int32_t  Src3[] = { Q24(8.0), Q24(-8.62), Q24(.63), Q24(.64), Q24(.65)};
int32_t  Dst[SHORT_SAMPLE_LENGTH];


void calc (void){

  int32_t i;

  dsp_vector_mulv_addv (Src,                    // Input vector
                        Src2,                   // Input vector 2
                        Src3,                   // Input vector 2
                        Dst,                    // Output vector
                        SHORT_SAMPLE_LENGTH,    // Vector length
                        Q_N);                   // Q Format N

  printf ("Vector / Vector multiplication and vector addition Result\n");
  for (i = 0; i < SHORT_SAMPLE_LENGTH; i++)
  {
    printf ("Dst[%d] = %lf\n", i, F24 (Dst[i]));
  }

  dsp_vector_mulv_subv (Src,                    // Input vector
                        Src2,                   // Input vector 2
                        Src3,                   // Input vector 2
                        Dst,                    // Output vector
                        SHORT_SAMPLE_LENGTH,    // Vector length
                        Q_N);                   // Q Format N

  printf ("Vector / Vector multiplication and vector subtraction Result\n");
  for (i = 0; i < SHORT_SAMPLE_LENGTH; i++)
  {
    printf ("Dst[%d] = %lf\n", i, F24 (Dst[i]));
  }

}

int main(void)
{

par
{
  on tile[0]: calc();
}
  return (0);
}

Code: Select all

# The TARGET variable determines what target system the application is
# compiled for. It either refers to an XN file in the source directories
# or a valid argument for the --target option when compiling.

TARGET = XEF232-1024-FB374-C40.xn

# The APP_NAME variable determines the name of the final .xe file. It should
# not include the .xe postfix. If left blank the name will default to
# the project name
APP_NAME = 
# This variable controls where the include files for the app are found.
# In this application the extra include files may be found in a target
# specific directory for XMOS development boards

INCLUDE_DIRS = src

# This header file is marked as optional since it is only included for
# a known XMOS development board
OPTIONAL_HEADERS =

# The flags passed to xcc when building the application
# You can also set the following to override flags for a particular language:
#
#    XCC_XC_FLAGS, XCC_C_FLAGS, XCC_ASM_FLAGS, XCC_CPP_FLAGS
#
# If the variable XCC_MAP_FLAGS is set it overrides the flags passed to
# xcc for the final link (mapping) stage.
XCC_FLAGS = -O0 -g -report 
USED_MODULES = lib_dsp

#=============================================================================
# The following part of the Makefile includes the common build infrastructure
# for compiling XMOS applications. You should not need to edit below here.

XMOS_MAKE_PATH ?= ../..
include $(XMOS_MAKE_PATH)/xcommon/module_xcommon/build/Makefile.common

Code: Select all

Vector / Vector multiplication and vector addition Result
Dst[0] = 8.000000
Dst[1] = -8.620000
Dst[2] = 0.630000
Dst[3] = 0.640000
Dst[4] = 0.650000
Vector / Vector multiplication and vector subtraction Result
Dst[0] = -7.943900
Dst[1] = 8.682400
Dst[2] = -0.561100
Dst[3] = -0.564400
Dst[4] = -0.567500


View Solution
User avatar
CousinItt
Respected Member
Posts: 360
Joined: Wed May 31, 2017 6:55 pm

Post by CousinItt »

Hi,

it looks like a bug in dsp_vector_mulv_addv code to me. I just tried your example with simpler input data and got similar nonsense.

I compared the code for dsp_vector_mulv_addv and dsp_vector_mulv_subv in dsp_vector.c. The code for the partially unrolled loop looks very similar, with additions replaced with subtractions, but at the end there's a difference:

dsp_vector_mulv_subv()

Code: Select all

    ...
    while( vl-- )
    {
        x0 = *input_vector_X++; y0 = *input_vector_Y++; z0 = *input_vector_Z++;
        asm("maccs %0,%1,%2,%3":"=r"(ah),"=r"(al):"r"(x0),"r"(y0),"0"(0),"1"(1<<(q_format-1)));
        asm("lsats %0,%1,%2":"=r"(ah),"=r"(al):"r"(q_format),"0"(ah),"1"(al));
        asm("lextract %0,%1,%2,%3,32":"=r"(x0):"r"(ah),"r"(al),"r"(q_format));
        *result_vector_R++ = x0 - z0;
    }
dsp_vector_mulv_addv()

Code: Select all

    ...
    while( vl-- )
    {
        x0 = *input_vector_X++; y0 = *input_vector_Y++; z0 = *input_vector_Z++;
        //{ah,al} = macs( x0, y0, 0, (1 << (q_format-1)) );
        asm("lsats %0,%1,%2":"=r"(ah),"=r"(al):"r"(q_format),"0"(ah),"1"(al));
        asm("lextract %0,%1,%2,%3,32":"=r"(x0):"r"(ah),"r"(al),"r"(q_format));
        *result_vector_R++ = x0 + z0;
    }
That commented line definitely looks suspicious.

Try this. I just pasted the maccs line from dsp_vector_mulv_subv. I did a quick try and it looked OK, but I've not done a full test.

dsp_vector_mulv_addv() revised

Code: Select all

    ...
    while( vl-- )
    {
        x0 = *input_vector_X++; y0 = *input_vector_Y++; z0 = *input_vector_Z++;
        asm("maccs %0,%1,%2,%3":"=r"(ah),"=r"(al):"r"(x0),"r"(y0),"0"(0),"1"(1<<(q_format-1)));
        asm("lsats %0,%1,%2":"=r"(ah),"=r"(al):"r"(q_format),"0"(ah),"1"(al));
        asm("lextract %0,%1,%2,%3,32":"=r"(x0):"r"(ah),"r"(al),"r"(q_format));
        *result_vector_R++ = x0 + z0;
    }
I'm going to need to rely on the dsp library myself soon. This really makes me concerned about its quality. Thanks for asking about it.
User avatar
johned
XCore Addict
Posts: 185
Joined: Tue Mar 26, 2013 12:10 pm
Contact:

Post by johned »

Morning, folks,

I've taken a look at the dsp_vector_mulv_addv function and CousinItt is absolutely correct that the final loop is wrong.
CousinItt's modified final loop is correct.
Sorry for the error. In general, lib_dsp has received a lot of testing so I'm not sure how this got through.
I will get this updated ASAP.

All the best,
John
User avatar
Sebastian
Active Member
Posts: 39
Joined: Wed Jul 20, 2016 9:15 am

Post by Sebastian »

Thank you for your answers,

@CousinItt
Thank you for your quick reply. I have tested your version an hour ago, and it worked for me too.

But Jhon was faster than me :)
Great to have a solution.

Bests,
Sebastian
User avatar
CousinItt
Respected Member
Posts: 360
Joined: Wed May 31, 2017 6:55 pm

Post by CousinItt »

Good to know it's sorted.

Thanks all.
Post Reply