home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / os / linux / 9536 < prev    next >
Encoding:
Text File  |  1992-08-31  |  3.8 KB  |  130 lines

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!uwm.edu!ogicse!news.u.washington.edu!serval!poly2!hlu
  2. From: hlu@poly2.eecs.wsu.edu (H.J. Lu)
  3. Newsgroups: comp.os.linux
  4. Subject: Re: gcc 2.2.2d: htonl, etc. problem with -O and netinet/in.h
  5. Message-ID: <1992Aug31.232558.6471@serval.net.wsu.edu>
  6. Date: 31 Aug 92 23:25:58 GMT
  7. Article-I.D.: serval.1992Aug31.232558.6471
  8. References: <1992Aug31.151452.26313@ERA.COM>
  9. Sender: hlu@poly2 (H.J. Lu)
  10. Organization: Washington State University
  11. Lines: 117
  12.  
  13. In article <1992Aug31.151452.26313@ERA.COM>, ejb@ERA.COM (Jay Berkenbilt) writes:
  14. |> 
  15. |> Hello.  With gcc 2.2.2d, htonl, htons, ntohl, or ntohs,
  16. |> everything works fine except when you #include <netinet/in.h>
  17. |> and compile with -O.  In this case, bytes/words are not swapped.
  18. |> This can be demonstrated with the following program:
  19. |> 
  20. |> #include <netinet/in.h>
  21. |> int main(void)
  22. |> {
  23. |>     printf("%08x, %08x, %04x, %04x\n",
  24. |>        htonl(0x01020304), ntohl(0x01020304),
  25. |>        htons(0x0102), ntohs(0x0102));
  26. |>     return 0;
  27. |> }
  28. |> 
  29. |> The output of this program should be
  30. |> 
  31. |> 04030201, 04030201, 0201, 0201
  32. |> 
  33. |> If you compile this with gcc -O, you will get
  34. |> 
  35. |> 01020304, 01020304, 0102, 0102
  36. |> 
  37. |> Although it seems that this should be an error with the inline
  38. |> assembly routines defined in netinet/in.h, I'm not sure whether
  39. |> that is really the problem or not.  Either not including
  40. |> <netinet/in.h> or not using -O produces correct results.
  41. |
  42.  
  43. My fault. Here is the fix.
  44.  
  45. H.J.
  46. ------
  47. *** ../disk2/usr/include/netinet/in.h    Fri Jul 17 10:21:10 1992
  48. --- include/netinet//in.h    Mon Aug 31 14:31:52 1992
  49. ***************
  50. *** 171,207 ****
  51.   static __inline__ unsigned long int
  52.   __ntohl(unsigned long int x)
  53.   {
  54.     __asm__ __volatile__ ("xchgb %%al,%%ah\n\t"   /* swap lower bytes */
  55.           "rorl $16,%%eax\n\t"    /* swap words */
  56.           "xchgb %%al,%%ah\n\t"   /* swap higher bytes */
  57. !         : : "a" (x));
  58. !   return x;
  59.   }
  60.   
  61.   static __inline__ unsigned short int
  62.   __ntohs(unsigned short int x)
  63.   {
  64.     __asm__ __volatile__ ("xchgb %%al,%%ah\n\t"   /* swap bytes */
  65. !         : : "a" (x));
  66. !   return x;
  67.   }
  68.   
  69.   static __inline__ unsigned long int
  70.   __htonl(unsigned long int x)
  71.   {
  72.     __asm__ __volatile__ ("xchgb %%al,%%ah\n\t"   /* swap lower bytes */
  73.           "rorl $16,%%eax\n\t"    /* swap words */
  74.           "xchgb %%al,%%ah\n\t"   /* swap higher bytes */
  75. !         : : "a" (x));
  76. !   return x;
  77.   }
  78.   
  79.   static __inline__ unsigned short int
  80.   __htons(unsigned short int x)
  81.   {
  82.     __asm__ __volatile__ ("xchgb %%al,%%ah\n\t"   /* swap bytes */
  83. !         : : "a" (x));
  84. !   return x;
  85.   }
  86.   
  87.   #ifdef  __OPTIMIZE__
  88. --- 171,211 ----
  89.   static __inline__ unsigned long int
  90.   __ntohl(unsigned long int x)
  91.   {
  92. +   register unsigned long int tmp __asm__ ("ax") = x;
  93.     __asm__ __volatile__ ("xchgb %%al,%%ah\n\t"   /* swap lower bytes */
  94.           "rorl $16,%%eax\n\t"    /* swap words */
  95.           "xchgb %%al,%%ah\n\t"   /* swap higher bytes */
  96. !         : "=a" (tmp) : "a" (tmp) );
  97. !   return tmp;
  98.   }
  99.   
  100.   static __inline__ unsigned short int
  101.   __ntohs(unsigned short int x)
  102.   {
  103. +   register unsigned short int tmp __asm__ ("ax") = x;
  104.     __asm__ __volatile__ ("xchgb %%al,%%ah\n\t"   /* swap bytes */
  105. !         : "=a" (tmp) : "a" (tmp));
  106. !   return tmp;
  107.   }
  108.   
  109.   static __inline__ unsigned long int
  110.   __htonl(unsigned long int x)
  111.   {
  112. +   register unsigned long int tmp __asm__ ("ax") = x;
  113.     __asm__ __volatile__ ("xchgb %%al,%%ah\n\t"   /* swap lower bytes */
  114.           "rorl $16,%%eax\n\t"    /* swap words */
  115.           "xchgb %%al,%%ah\n\t"   /* swap higher bytes */
  116. !         : "=a" (tmp) : "a" (tmp));
  117. !   return tmp;
  118.   }
  119.   
  120.   static __inline__ unsigned short int
  121.   __htons(unsigned short int x)
  122.   {
  123. +   register unsigned short int tmp __asm__ ("ax") = x;
  124.     __asm__ __volatile__ ("xchgb %%al,%%ah\n\t"   /* swap bytes */
  125. !         : "=a" (tmp) : "a" (tmp));
  126. !   return tmp;
  127.   }
  128.   
  129.   #ifdef  __OPTIMIZE__
  130.