[질문] 이상한 코드...
첨부된 test.cpp를 돌리는데, (첨부가 안되어 글의 맨 아래에 소스를 적어두었습니다.)
win64, SUN32, SUN64에서의 동작이 다릅니다.
좀 더 정확히는
win64, SUN32는 동일하고 (win32도 동일합니다.)
SUN64만 이상하게 동작하네요.
결과는 다음과 같습니다.
IEEE의 값이 SUN-64일 때만 이상하게 찍히는데, 그 원인을 모르겠습니다.
그리고 솔직히 test.cpp 내에서
sudword IEEE = (sudword&)(x); 나
float tempIEEEf = (float&)(IEEE); 와 같은 구문이 있는데,
(subword&), (float&)가 정확히 어떤 일을 하는지도 모르겠습니다.
도움 부탁드립니다.
/*****************************************/
(SUN-64)컴파일 옵션 : CC -xarch=generic64 test.cpp
------------------------
IEEE = 0000000000000000
tempIEEEf = 1.0000000000000000
------------------------
IEEE = 000000003e4d2659
tempIEEEf = 1.0000000000000000
/*****************************************/
(SUN-32)컴파일 옵션 : CC test.cpp
------------------------
IEEE = 000000003f800000
tempIEEEf = 1.0000000000000000
------------------------
IEEE = 000000003f800000
tempIEEEf = 1.0000000000000000
/*****************************************/
(win-64)
VS2005사용------------------------
IEEE = 000000003f800000
tempIEEEf = 1.0000000000000000
------------------------
IEEE = 000000003f800000
tempIEEEf = 1.0000000000000000
/***********************************************/
소스코드
/***********************************************/
// melong64.cpp : Defines the entry point for the console application.
//
#include //required for std::swap
#define ByteSwap5(x) ByteSwap((unsigned char *) &x,sizeof(x))
void ByteSwap(unsigned char * b, int n)
{
{
register int i = 0;
register int j = n-1;
while (i
{
//HP는그냥swap(b[i], b[j]); (std:: 안쓴다)
std::swap(b[i], b[j]);
++i, --j;
}
}
}
typedef unsigned long long sudword;
#include
int main(void)
{
int ud = sizeof(sudword);
int flag = 0;
float mU = 0.2003416f;
if( ((sudword&)mU) > 0x3f800000) {
flag = -333;
}
//float x = 1.0f;
int x= 2;
sudword IEEE = (sudword&)(x);
ByteSwap5(x);
sudword IEEE2 = (sudword&)(x);
int pointersize = sizeof(void*);
printf("size & pointer_size = %d, %d\n", ud, pointersize);
printf("flag = %d\n\n",flag);
printf("IEEE = %16.16x\n",IEEE);
printf("IEEE pointer= %16.16x\n",&IEEE);
printf("IEEE2 = %16.16x\n",IEEE2);
printf("IEEE2 pointer= %16.16x\n",&IEEE2);
printf("\n\npointer of x = %x\n\n",&x);
// 1.0 => 0x000000003f800000
// 1.0 => 0x3f80000000000000
sudword temp32 = 0x3f800000;
sudword temp64 = 0x3e4d2659;
float tempIEEEf = (float&)(IEEE);
float temp32f = (float&) temp32;
float temp64f = (float&) temp64;
printf("tempIEEEf = %16.16f\n", temp32f);
printf("temp32f = %16.16f\n", temp32f);
printf("temp64f = %16.16f\n", temp64f);
double dTemp = 110.23;
sudword sudTemp = (sudword&)dTemp;
printf("dTemp = %16.16x\n", (sudword&)dTemp);
printf("dTemp = %16.16f\n", (double&)sudTemp);
return 0;
}
현재까지의 상황
SUN64에서
000000003e4d2659라고 나오는 것은
float mU = 0.2003416f; 이라는 코드의 0.2003416f 라는 숫자의 비트값이네요
이 mU값을 바꾸면 그에 대응되는 비트값으로 바뀝니다.
결국 SUN64에서는 sudword IEEE = (sudword&)(x);가 정상작동하지 않는다는 이야기인데,
대체 왜 그럴까요?
정말 깨끗하고 멋진 프로그램을 짜고 싶습니다.
댓글 달기