[util] Add big-endian short msg read/write

And improve the generated code for MSG_ReadShort

I suspect gcc didn't like all the excess pointer dereferences and so
couldn't assume that the bytes were being read sequentially.
This commit is contained in:
Bill Currie 2021-04-04 15:19:38 +09:00
parent afd7b38551
commit 9a2f82bbc6
2 changed files with 41 additions and 2 deletions

View File

@ -36,6 +36,7 @@
void MSG_WriteByte (sizebuf_t *sb, int c);
void MSG_WriteShort (sizebuf_t *sb, int c);
void MSG_WriteShortBE (sizebuf_t *sb, int c);
void MSG_WriteLong (sizebuf_t *sb, int c);
void MSG_WriteLongBE (sizebuf_t *sb, int c);
void MSG_WriteFloat (sizebuf_t *sb, float f);
@ -96,6 +97,16 @@ int MSG_ReadByte (qmsg_t *msg);
*/
int MSG_ReadShort (qmsg_t *msg);
/** Read a single big-endian unsigned short from the message.
Advances the read index.
\param msg The message from which the short will be read.
\return The short value (0 - 65535), or -1 if already at
the end of the message.
*/
int MSG_ReadShortBE (qmsg_t *msg);
/** Read a single little-endian long from the message.
Advances the read index.

View File

@ -68,6 +68,16 @@ MSG_WriteShort (sizebuf_t *sb, int c)
*buf = ((unsigned int) c) >> 8;
}
VISIBLE void
MSG_WriteShortBE (sizebuf_t *sb, int c)
{
byte *buf;
buf = SZ_GetSpace (sb, 2);
*buf++ = ((unsigned int) c) >> 8;
*buf = ((unsigned int) c) & 0xff;
}
VISIBLE void
MSG_WriteLong (sizebuf_t *sb, int c)
{
@ -265,8 +275,26 @@ MSG_ReadShort (qmsg_t *msg)
int c;
if (msg->readcount + 2 <= msg->message->cursize) {
c = (msg->message->data[msg->readcount]
+ (msg->message->data[msg->readcount + 1] << 8));
byte *buf = msg->message->data + msg->readcount;
c = *buf++;
c |= (*buf) << 8;
msg->readcount += 2;
return c;
}
msg->readcount = msg->message->cursize;
msg->badread = true;
return -1;
}
VISIBLE int
MSG_ReadShortBE (qmsg_t *msg)
{
int c;
if (msg->readcount + 2 <= msg->message->cursize) {
byte *buf = msg->message->data + msg->readcount;
c = (*buf++) << 8;
c |= *buf;
msg->readcount += 2;
return c;
}