Check bits in the CCR

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Check bits in the CCR

2,856 Views
Chris_UTS
Contributor I
Hi,

I'm sorry if this has been answered before but i could not find a solution.

Background, using CodeWarrior 5.7, Chip is type HC12S and programming in C.

I'm performing a 16bit x 16bit multiplication and need to check if there is an overflow. I assume i should just check the relevant bits in the CCR but i'm not sure how to do this programmically.

Any help would be much appericiated.

Thank you.
Chris


Labels (1)
Tags (1)
0 Kudos
3 Replies

692 Views
CompilerGuru
NXP Employee
NXP Employee
The HW of the S12 implements a 16x16=32 multiplication, therefore there is no overflow, and no
bit in the CCS to test.

Instead try a 16*16=32 bit multiplication in C and check if the resulting long fits into 16 bit.
If performance is critical, then note that not the multiplication is the expensive operation here but everything else Smiley Happy.

Daniel



Code:
Code:#include <limits.h>unsigned int a,b,c;unsigned char overflow;void mul() {  unsigned long ab = (unsigned long)a * b;  if (ab > UINT_MAX) {    overflow = TRUE;  } else {    c = (unsigned int)ab;      }}

 

0 Kudos

692 Views
Lundin
Senior Contributor IV
Right... there is no bit in the CCR, but you could clear index register Y before executing EMUL, then check if Y is different from zero afterwards. It would still have to be done in assembler.
0 Kudos

692 Views
Lundin
Senior Contributor IV
You will indeed have to check the CCR through inline assembler, there is no obvious way to check for overflow in C.

I suppose you could do like this:

#include <limits.h>

uint16 a;
uint16 b;
uint32 a32 = a;
uint32 b32 = b;
uint32 result = a32 * b32;

if(result > UINT_MAX)
{
  /*  a*b will give overflow  */
}

Though the assembler version will be way more efficient.
0 Kudos