CnUnix

[알림판목록 I] [알림판목록 II] [글목록][이 전][다 음]
[ CnUnix ] in KIDS
글 쓴 이(By): gureumi (구르미)
날 짜 (Date): 2002년 11월 17일 일요일 오후 02시 17분 30초
제 목(Title): [질문] 다음 어셈블리 설명 부탁..


#define gethrtime(x)      {asm volatile (".byte 0x0f, 0x31" : "=A" (x));}

----
솔라리스의 64bit nanosecond(그만큼 precision이 되진 않습니다만..)급 wall clock을
효과적으로 구하는 데 쓰는 gethrtime()을 인텔 플랫폼에서 구현해보고자 하는 차원에서 
만든 매크로 같습니다만..
인텐 x86계열의 cpu들은 각자가 clock tick을 count 하는 레지스터가 있는데
거기서 읽어오는게 아닌가 싶습니다만...


사용은 그냥


#include <stdio.h>

typedef unsigned long long hrtime_t; /* 64bit으로 컴파일하지 않는다면... :)  */

/*  LINUX platform이라고 해둡시당....  */
double getCPUMHz()
{
  FILE *cpuinfo;
  int i=0;
  char line[120];

  cpuinfo = fopen("/proc/cpuinfo", "r");
  for(i=0;i<7;i++) fgets(line, 120, cpuinfo);
  return atof(line+11);
}

main()
{
  unsigned int i;
  hrtime_t  t1,t2;
  double cpuMHz=getCPUMHz();
  
  gethrtime(t1);
  for(i=0;i<10000;i++);
  /* do something */
  gethrtime(t2);

  printf("%lld\n", (hrtime_t)((double)(t2-t1)/cpuMHz)); 
}


위와 같이 하면 되긴 할 텐데..
제가 궁금한 것은 이 글 첫 라인에 쓰인 어셈블리어의 뜻이 멀까 하는 것이져...
혹시 아시는 분?

참고로 전 이 마크로를 발견하기 전에는 다음과 같은 펑션을 사용했었죠..

void inline gethrtime(volatile hrtime_t *t)
{
  volatile unsigned long *parts = (volatile unsigned long *) t;

  __asm__ __volatile__("rdtsc" : "=a" ( parts[0] ), "=d" (  parts[1] )) ;
  return;
}

또는  크게 다르지 않습니다만..

#define RDTSC2(h,l) __asm__ volatile("rdtsc" : "=a"(l), "=d" (h))
hrtime_t gethrtime()
{
  union {
    unsigned long long l1;
    struct {unsigned long l,h; } st;
  } t1;
  RDTSC2(t1.st.h, t1.st.l);
  return t1.l1;
}

rdtsc는 바로 그 cpu가 갖고 있는 time stamp clock register를 읽어 들이는 명령어임다.
근데 이 instruction 이름도 Pentium II부터인가 바뀐걸루 알고 있슴다.

---
                      雲心如水心
[알림판목록 I] [알림판목록 II] [글 목록][이 전][다 음]
키 즈 는 열 린 사 람 들 의 모 임 입 니 다.