Article View: comp.lang.misc
Article #1632downward compatibility
From: Doug Moen
Date: Sat, 29 Sep 1990 00:40
Date: Sat, 29 Sep 1990 00:40
55 lines
2913 bytes
2913 bytes
brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes:
>Rather than this abstract discussion, let's examine a concrete example.
>Someone just asked in comp.lang.c how to count the number of bits in a
>32-bit integer. Most people would use a table of 256 or 65536 values and
>shift appropriately. But one person pointed out that count-the-bits is a
>single instruction on at least one machine.
>
>How, without making any preparations in the language specification
>beforehand, could we let the programmer extend the language downwards to
>take advantage of this instruction?
>
>... I envision a solution like this:
>
> quickpick
> fast(define builtin_bitcount 0)
> count = bitcounts[j & 65535] + bitcounts[(j >> 16) % 65535];
> fast(define builtin_bitcount 1)
> unsigned long i;
> for(i = j;i;i >>= 1) count += (i & 1);
> endquickpick
I think I see a general solution to this problem.
1. First, let's invent a new language feature which tests an
identifier to see if it is defined. The compile-time expression
Defined(id)
is TRUE if 'id' is defined, otherwise FALSE. Let's also assume that
the language allows arbitrary blocks of code to be conditionally
compiled, based on the result of the Defined operator.
2. Second, let's assume that the compiler writer, knowing that his
machine has a single instruction for counting the bits in a word,
extends the language with a 'bitcount' operation.
3. A programmer wants to use 'bitcount', but wants to maintain
backwards portability with older compilers that do not support bitcount.
He therefore writes:
if (Defined(bitcount))
count = bitcount(j);
else
count = bitcounts[j & 65535] + bitcounts[(j >> 16) % 65535];
This mechanism solves not just the problem of getting closer to the
machine in a portable way, but the more general problem of writing
code that ports to multiple environments, where there is divergence
in the features provided by the standard libraries.
As a Unix programmer, I have often wanted to write code that is conditionally
compiled depending on whether vfork, or time_t, or strchr, is defined.
It may be too late for C, but I think this would be a great feature to
include in new language designs.
--
Doug Moen
{mnetor,alias,geac,torsqnt,lsuc}!nixtdc!doug
77 Carlton #1504, Toronto, Ontario, Canada M5B 2J7
Technical Details
Message-ID:
<1990Sep29.004009.11959@nixtdc.uucp>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!torsqnt!nixtdc!dougReferences:
<PCG.90Sep23181145@teachc.cs.aber.ac.uk> <28750:Sep2402:58:2290@kramden.acf.nyu.edu> <1990Sep24.160705.21113@newcastle.ac.uk> <9363:Sep2521:41:1290@kramden.acf.nyu.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!torsqnt!nixtdc!doug From: doug@nixtdc.uucp (Doug Moen) Newsgroups: comp.lang.misc Subject: downward compatibility Message-ID: <1990Sep29.004009.11959@nixtdc.uucp> Date: 29 Sep 90 00:40:09 GMT References: <PCG.90Sep23181145@teachc.cs.aber.ac.uk> <28750:Sep2402:58:2290@kramden.acf.nyu.edu> <1990Sep24.160705.21113@newcastle.ac.uk> <9363:Sep2521:41:1290@kramden.acf.nyu.edu> Reply-To: doug@nixtdc.UUCP (Doug Moen) Organization: Nixdorf Computer Engineering Company Lines: 55