sum_xa.c

来自「在ARM7和UC/OSII的平台上实现了GPS自动报站的功能,涉及GPS模块LE」· C语言 代码 · 共 53 行

C
53
字号
/* sum_xa.c

   Copyright 2000 by InterNIche Technologies Inc. All rights reserved.
   Copyright 1990-1996 by NetPort Software.

*/

#include "ipport.h"

/* cksum(buf, len) performs an Internet type checksum. buf points to where
 	the checksumming should begin, len is the number of 16 bit words
	to be summed.

   This version for philips XA CPUs in 16 bit mode
*/

//#pragma optimize 1
#pragma O0
/*typedef union {
   unsigned long m_long;
   struct {
      unsigned int lint;
      unsigned int hint;
   } m_int;
} LONG_UNION;
*/
typedef union{
   unsigned int m_long;
   struct{
      unsigned short lint;
      unsigned short hint;
   }m_int;
}LONG_UNION;

unsigned short cksum(void* buf, unsigned short word_count)
{
   LONG_UNION sum;
   unsigned short *ptr;
   
   ptr = (unsigned short *)buf;
   sum.m_long = 0;
   for (; word_count > 0; word_count--) {
      sum.m_long += *ptr++;
      if (sum.m_int.hint > 0) {
          sum.m_int.lint += sum.m_int.hint;
          sum.m_int.hint  = 0;
      }
   }
   
   return sum.m_int.lint;
}
//#pragma endoptimize

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?