Article View: comp.std.c
Article #3332Sequence space arithmetic
From: Alan P Barrett
Date: Wed, 06 Jan 1993 23:13
Date: Wed, 06 Jan 1993 23:13
55 lines
2980 bytes
2980 bytes
I would appreciate comments on the following macro, which is intended
to determine whether (a > b) in 32-bit sequence space arithmetic.
This might be used for comparing SOA serial numbers in a DNS nameserver,
as described in RFC 1034 and RFC 1035.
#include <limits.h>
/* how to check if (a >= b) in 32-bit sequence space arithmetic. */
#if (UINT_MAX == 0xffffffff) /* int is exactly 32 bits */
# define SEQ_GT(a,b) ((int)((unsigned int)(a)-(b)) > 0)
#else
# if (ULONG_MAX == 0xffffffff) /* long is exactly 32 bits */
# define SEQ_GT(a,b) ((long)((unsigned long)(a)-(b)) > 0)
# else
# if (ULONG_MAX > 0xffffffff) /* long is wider than 32 bits */
# if 1 /* if you insist on evaluating a and b exactly once each */
# define SEQ_GT(a,b) \
((((unsigned long)(a)-(b)) & 0xffffffff)-1 < 0x7fffffff)
# else /* this is more intuitively obvious */
# define SEQ_GT(a,b) \
(((a)!=(b)) && ((((unsigned long)(a)-(b)) & 0x80000000) == 0))
# endif
# else /* long is narrower than 32 bits */
#error "ULONG_MAX is less than 32 bits. Get a real compiler."
# endif
# endif
#endif
Are both methods for handling longs wider than 32 bits guaranteed to work?
Will they also work with longs of exactly 32 bits?
Which is likely to be handled better by existing compilers?
I believe that casting unsigned values into signed types is implementation
defined, and that this affects the parts that handle int or long of
exactly 32 bits. Is that the case?
If we assume that the implementation does the obvious thing with
casts (just interprets the bits differently), will that code work?
How reasonable is it to make such an assumption, given that the code is
intended to run on a Unix system?
Would it be reasonable to omit all the #if tests, leaving just the part
that deals with longs wider than 32 bits, and trust the compiler to make
the relevant optimisations if a 32-bit type is available?
Would it be reasonable to omit all the #if tests, leaving just the part
that deals with longs of exactly 32 bits, and hope that the code never
runs on a system that has wider longs?
I realise that several of these questions are not directly related to
standards, but I would appreciate hearing opinions on them anyway.
Thanks,
--apb
Alan Barrett, Dept. of Electronic Eng., Univ. of Natal, Durban, South Africa
RFC822: barrett@ee.und.ac.za Bang: m2xenix!undeed!barrett
Technical Details
Message-ID:
<1ifb0pINN7qq@daisy.ee.und.ac.za>Path:
arc1-ca.pugleaf.net!sparky!uunet!gatech!rpi!uwm.edu!ogicse!psgrain!ee.und.ac.za!tplinfmShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!gatech!rpi!uwm.edu!ogicse!psgrain!ee.und.ac.za!tplinfm From: barrett@daisy.ee.und.ac.za (Alan P Barrett) Newsgroups: comp.std.c Subject: Sequence space arithmetic Message-ID: <1ifb0pINN7qq@daisy.ee.und.ac.za> Date: 6 Jan 93 23:13:29 GMT Article-I.D.: daisy.1ifb0pINN7qq Organization: Dept. Elec. Eng., Univ. Natal, Durban, S. Africa Lines: 55 NNTP-Posting-Host: daisy.ee.und.ac.za