Check bits in the CCR

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Check bits in the CCR

3,346 次查看
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,182 次查看
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,182 次查看
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,182 次查看
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 项奖励
回复