Check bits in the CCR

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Check bits in the CCR

3,344件の閲覧回数
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


ラベル(1)
タグ(1)
0 件の賞賛
返信
3 返答(返信)

1,180件の閲覧回数
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 件の賞賛
返信

1,180件の閲覧回数
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 件の賞賛
返信

1,180件の閲覧回数
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 件の賞賛
返信