home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / std / c / 3326 < prev    next >
Encoding:
Text File  |  1993-01-06  |  2.8 KB  |  67 lines

  1. Path: sparky!uunet!gatech!rpi!uwm.edu!ogicse!psgrain!ee.und.ac.za!tplinfm
  2. From: barrett@daisy.ee.und.ac.za (Alan P Barrett)
  3. Newsgroups: comp.std.c
  4. Subject: Sequence space arithmetic
  5. Message-ID: <1ifb0pINN7qq@daisy.ee.und.ac.za>
  6. Date: 6 Jan 93 23:13:29 GMT
  7. Article-I.D.: daisy.1ifb0pINN7qq
  8. Organization: Dept. Elec. Eng., Univ. Natal, Durban, S. Africa
  9. Lines: 55
  10. NNTP-Posting-Host: daisy.ee.und.ac.za
  11.  
  12. I would appreciate comments on the following macro, which is intended
  13. to determine whether (a > b) in 32-bit sequence space arithmetic.
  14. This might be used for comparing SOA serial numbers in a DNS nameserver,
  15. as described in RFC 1034 and RFC 1035.
  16.  
  17. #include <limits.h>
  18. /* how to check if (a >= b) in 32-bit sequence space arithmetic. */
  19. #if (UINT_MAX == 0xffffffff)            /* int is exactly 32 bits */
  20. #        define SEQ_GT(a,b) ((int)((unsigned int)(a)-(b)) > 0)
  21. #else
  22. #  if (ULONG_MAX == 0xffffffff)         /* long is exactly 32 bits */
  23. #        define SEQ_GT(a,b) ((long)((unsigned long)(a)-(b)) > 0)
  24. #  else
  25. #    if (ULONG_MAX > 0xffffffff)        /* long is wider than 32 bits */
  26. #      if 1 /* if you insist on evaluating a and b exactly once each */
  27. #        define SEQ_GT(a,b) \
  28.                 ((((unsigned long)(a)-(b)) & 0xffffffff)-1 < 0x7fffffff)
  29. #      else /* this is more intuitively obvious */
  30. #        define SEQ_GT(a,b) \
  31.                 (((a)!=(b)) && ((((unsigned long)(a)-(b)) & 0x80000000) == 0))
  32. #      endif
  33. #    else                               /* long is narrower than 32 bits */
  34.        #error "ULONG_MAX is less than 32 bits.  Get a real compiler."
  35. #    endif
  36. #  endif
  37. #endif
  38.  
  39. Are both methods for handling longs wider than 32 bits guaranteed to work?
  40. Will they also work with longs of exactly 32 bits? 
  41. Which is likely to be handled better by existing compilers?
  42.  
  43. I believe that casting unsigned values into signed types is implementation
  44. defined, and that this affects the parts that handle int or long of
  45. exactly 32 bits.  Is that the case?
  46.  
  47. If we assume that the implementation does the obvious thing with
  48. casts (just interprets the bits differently), will that code work?
  49. How reasonable is it to make such an assumption, given that the code is
  50. intended to run on a Unix system?
  51.  
  52. Would it be reasonable to omit all the #if tests, leaving just the part
  53. that deals with longs wider than 32 bits, and trust the compiler to make
  54. the relevant optimisations if a 32-bit type is available?
  55.  
  56. Would it be reasonable to omit all the #if tests, leaving just the part
  57. that deals with longs of exactly 32 bits, and hope that the code never
  58. runs on a system that has wider longs?
  59.  
  60. I realise that several of these questions are not directly related to
  61. standards, but I would appreciate hearing opinions on them anyway.
  62.  
  63. Thanks,
  64. --apb
  65. Alan Barrett, Dept. of Electronic Eng., Univ. of Natal, Durban, South Africa
  66. RFC822: barrett@ee.und.ac.za                    Bang: m2xenix!undeed!barrett
  67.