SAMD21 signed INT8 comparison issue for Min and Max Value on array

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
		if (Select->ConfigOSMath.isMaxEnable==true)				
		{									// The data is INT8 (signed INT 8 or int8).
			INT8 actual = iData8.arr[i];					// This code does not work, actual =0, MaxValue = 129 or -127 in INT8. 
			INT8 MaxValue = Select->iMax.arr[i];				// it seem the complier or Cortex M3 did not handle 8 bit 2nd complement number.
			if (actual > MaxValue)								// Incorrect response.
				Select->iMax.arr[i] = iData8.arr[i];			// 
		}
		if (Select->ConfigOSMath.isMaxEnable==true)				// Method B is to convert INT8 to INT16 and do math correctly. 		
		{
			INT16 actual = (INT16) iData8.arr[i]<<8;			// 0 goes to 0
			INT16 MaxValue = (INT16) Select->iMax.arr[i]<<8;	        // -127 goes to -32512 (which is indicated in Atmel Studio Watch correctly, but not INT8)
			if (actual > MaxValue)						// 0 > -32512 and it work correctly with max = 0. 
				Select->iMax.arr[i] = iData8.arr[i];
		}

 

The above code with INT8 and INT8 comparison did not work correctly, I get impression either complier or Cortex M3 do not support 2nd complement math for INT8

 

I transpose the INT8 to INT16 by right shift 8 and do a comparison with correct response, which shown it supported by the MCU. 

 

Does this mean I cannot do math on INT8 variable or have I missing something?, it there guideline or limitation of using INT8 in math?

 

Thanks.

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's not possible to comment without knowing how INT8, iData8.arr and Select->iMax.arr are declared. A general answer is there is no 8-bit math because of integer promotion.

/Lars

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

typedef char                INT8;       // 8 bit signed
typedef unsigned char       uINT8;      // 8 bit unsigned

 

typedef struct tagi8XYZ
{
    union
    {
        INT8 arr[3];
        struct  {
            INT8 X;
            INT8 Y;
            INT8 Z;
        };
    };
} _i8XYZ;

 

What about ' --signed-char' in compiler flag, do they work?

 

Do you know any link/pdf that details more about using INT8/uINT8 for Cortex M0 or Atmel D21 device?, this is new to me.

 

 

Last Edited: Mon. Apr 8, 2019 - 02:12 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

char need not be the same as a signed char (that is what the compiler option is about). You should be using signed char if that is what you need (even better use the stdint types from stdint.h, in this case int8_t).

/Lars

 

Last Edited: Mon. Apr 8, 2019 - 02:42 PM