大端序 (SPARC) 处理器上的位域排序



请考虑以下代码:

#include <stdio.h>
#include <stdlib.h>
#define FORCE_CAST(var, type) *(type*)&var
struct processor_status_register
{
unsigned int cwp:5;
unsigned int et:1;
unsigned int ps:1;
unsigned int s:1;
unsigned int pil:4;
unsigned int ef:1;
unsigned int ec:1;
unsigned int reserved:6;
unsigned int c:1;
unsigned int v:1;
unsigned int z:1;
unsigned int n:1;
unsigned int ver:4;
unsigned int impl:4;
}__attribute__ ((__packed__));

struct registers
{
       unsigned long* registerSet;
       unsigned long* globalRegisters;
       unsigned long* cwptr;
       unsigned long wim, tbr, y, pc, npc;
       unsigned short registerWindows;
       /* Though Intel x86 architecture allows un-aligned memory access, SPARC mandates memory accesses to be 8 byte aligned. Without __attribute__ ((aligned (8))) or a preceding dummy byte e.g. unsigned short dummyByte, the code below crashes with a dreaded Bus error and Core dump. For more details, follow the links below:
        http://blog.jgc.org/2007/04/debugging-solaris-bus-error-caused-by.html
        https://groups.google.com/forum/?fromgroups=#!topic/comp.unix.solaris/8SgFiMudGL4
*/
       struct processor_status_register __attribute__ ((aligned (8))) psr;
}__attribute__ ((__packed__));

int getBit(unsigned long bitStream, int position)
{
int bit;
bit = (bitStream & (1 << position)) >> position;
return bit;
}

char* showBits(unsigned long bitStream, int startPosition, int endPosition)
{
// Allocate one extra byte for NULL character
char* bits = (char*)malloc(endPosition - startPosition + 2);
int bitIndex;
for(bitIndex = 0; bitIndex <= endPosition; bitIndex++)
bits[bitIndex] = (getBit(bitStream, endPosition - bitIndex)) ? '1' : '0';
bits[bitIndex] = '';
return bits;
}

int main()
{
struct registers sparcRegisters; short isLittleEndian;
// Check for Endianness
        unsigned long checkEndian = 0x00000001;
        if(*((char*)(&checkEndian)))
            {printf("Little Endiann"); isLittleEndian = 1;} // Little
Endian architecture detected
        else
            {printf("Big Endiann"); isLittleEndian = 0;} // Big
Endian architecture detected
unsigned long registerValue = 0xF30010A7;
unsigned long swappedRegisterValue = isLittleEndian ? registerValue :
__builtin_bswap32(registerValue);
sparcRegisters.psr = FORCE_CAST(swappedRegisterValue, struct
processor_status_register);
registerValue = isLittleEndian ? FORCE_CAST (sparcRegisters.psr,
unsigned long) : __builtin_bswap32(FORCE_CAST (sparcRegisters.psr,
unsigned long));
printf("nPSR=0x%0X, IMPL=%u, VER=%u, CWP=%un", registerValue,
sparcRegisters.psr.impl, sparcRegisters.psr.ver,
sparcRegisters.psr.cwp);
printf("PSR=%sn",showBits(registerValue, 0, 31));
sparcRegisters.psr.cwp = 7;
sparcRegisters.psr.et = 1;
sparcRegisters.psr.ps = 0;
sparcRegisters.psr.s = 1;
sparcRegisters.psr.pil = 0;
sparcRegisters.psr.ef = 0;
sparcRegisters.psr.ec = 0;
sparcRegisters.psr.reserved = 0;
sparcRegisters.psr.c = 0;
sparcRegisters.psr.v = 0;
sparcRegisters.psr.z = 0;
sparcRegisters.psr.n = 0;
sparcRegisters.psr.ver = 3;
sparcRegisters.psr.impl = 0xF;
registerValue = isLittleEndian ? FORCE_CAST (sparcRegisters.psr,
unsigned long) : __builtin_bswap32(FORCE_CAST (sparcRegisters.psr,
unsigned long));
printf("nPSR=0x%0X, IMPL=%u, VER=%u, CWP=%un", registerValue,
sparcRegisters.psr.impl, sparcRegisters.psr.ver,
sparcRegisters.psr.cwp);
printf("PSR=%snn",showBits(registerValue, 0, 31));
return 0;
}  

我在 SPARC 上的 Solaris 10 上使用 gcc-4.7.2 编译以下内容代码以生成大端输出:

Big Endian
PSR=0xF30010A7, IMPL=3, VER=15, CWP=20
PSR=11110011000000000001000010100111
PSR=0x3F00003D, IMPL=15, VER=3, CWP=7
PSR=00111111000000000000000000111101

我已经在英特尔-x86上的Ubuntu-10.04上使用gcc-4.4来编译以下代码生成小端输出:

Little Endian
PSR=0xF30010A7, IMPL=15, VER=3, CWP=7
PSR=11110011000000000001000010100111
PSR=0xF30000A7, IMPL=15, VER=3, CWP=7
PSR=11110011000000000000000010100111

虽然后者如预期的那样,但任何人都可以解释一下大端对应物?考虑 showBits() 方法是正确,PSR=0x3F00003D怎么能产生 IMPL=15、VER=3、CWP=7值?位域是如何排列和解释的大端系统上的内存?

。PSR=0x3F00003D产生 IMPL=15, VER=3, CWP=7 值?

它不能。 我不知道你为什么要调用__builtin_bswap32 0x3F00003D但它并不代表你初始化时sparcRegisters结构的内存。

让我们检查一下此代码:

sparcRegisters.psr.cwp = 7;
sparcRegisters.psr.et = 1;
sparcRegisters.psr.ps = 0;
sparcRegisters.psr.s = 1;
sparcRegisters.psr.pil = 0;
sparcRegisters.psr.ef = 0;
sparcRegisters.psr.ec = 0;
sparcRegisters.psr.reserved = 0;
sparcRegisters.psr.c = 0;
sparcRegisters.psr.v = 0;
sparcRegisters.psr.z = 0;
sparcRegisters.psr.n = 0;
sparcRegisters.psr.ver = 3;
sparcRegisters.psr.impl = 0xF;

各个翻译如下:

7 => 00111
1 => 1
0 => 0
1 => 1
0 => 0000
0 => 0
0 => 0
0 => 000000
0 => 0
0 => 0
0 => 0
0 => 0
3 => 0011
F => 1111

因此,内存中的结构变得00111101000000000000000000111111,这在大端序中0x3D00003F。

您可以使用以下代码进行确认(在 solaris 中使用 CC 进行测试):

#include <stdio.h>
#include <string.h>
struct processor_status_register
{
   unsigned int cwp:5;
   unsigned int et:1;
   unsigned int ps:1;
   unsigned int s:1;
   unsigned int pil:4;
   unsigned int ef:1;
   unsigned int ec:1;
   unsigned int reserved:6;
   unsigned int c:1;
   unsigned int v:1;
   unsigned int z:1;
   unsigned int n:1;
   unsigned int ver:4;
   unsigned int impl:4;
}__attribute__ ((__packed__));
int getBit(unsigned long bitStream, int position)
{
   int bit;
   bit = (bitStream & (1 << position)) >> position;
   return bit;
}
char* showBits(unsigned long bitStream, int startPosition, int endPosition)
{
   // Allocate one extra byte for NULL character
   static char bits[33];
   memset(bits, 0, 33);
   int bitIndex;
   for(bitIndex = 0; bitIndex <= endPosition; bitIndex++)
   {
      bits[bitIndex] = (getBit(bitStream, endPosition - bitIndex)) ? '1' : '0';
   }
   return bits;
}
int main()
{
   processor_status_register psr;
   psr.cwp = 7;
   psr.et = 1;
   psr.ps = 0;
   psr.s = 1;
   psr.pil = 0;
   psr.ef = 0;
   psr.ec = 0;
   psr.reserved = 0;
   psr.c = 0;
   psr.v = 0;
   psr.z = 0;
   psr.n = 0;
   psr.ver = 3;
   psr.impl = 0xF;
   unsigned long registerValue = 0;
   memcpy(&registerValue, &psr, sizeof(registerValue));
   printf("nPSR=0x%0X, IMPL=%u, VER=%u, CWP=%un", registerValue,
      psr.impl, psr.ver,
      psr.cwp);
   printf("PSR=%snn",showBits(registerValue, 0, 31)); 
   return 0;
}

其输出为:

PSR=0x3D00003F, IMPL=15, VER=3, CWP=7
PSR=00111101000000000000000000111111

最新更新