Word size problem with MSC

Devin_E_Ben-Hur at cup.portal.com Devin_E_Ben-Hur at cup.portal.com
Fri Apr 14 03:03:15 AEST 1989


jdm at hodge.cts.com [uunet zardoz vdelta crash]!hodge!jdm writes:
> I am currently porting some code from a 32 bit Unix environmant
> to a 16 bit MS-DOS environment and my target compiler is
> Microsoft C.  The problem I have encountered is the switch() function
> in MSC (and in Turbo C for that matter) will only accept a 16 bit
> integer argument.  The code I am porting requires 32 bit values to
> be used in a case statement.
> 
> Normally I would convert the case statement to a series of
> if...else statements, but I have been informed that the part of the
> code I am working on cannot be changed for reasons of future
> compatability.  Is there a patch for the MS C compiler, or some other
> action, that will allow the switch() function to accept 32 bit
> integers?

I don't believe there's a patch for either TC or MSC to allow this.
You might try an approach like this:
--------------
/* original switch case constants */
#define CONST32_0	big#	/* these names are really your */
#define CONST32_1	big#	/* original names and values */
...
#define CONST32_N	big#

#if INTS_ARE_16_BITS

#define CONST32_0_16	0
#define CONST32_1_16	1
...
#define CONST32_N_16	N

long switch_tab[] = { CONST32_0, CONST32_1, ... CONST32_N };

int map32_16(long value)
{
	int i;
	for (i=0; i < (sizeof switch_tab/sizeof switch_tab[0]); ++i)
		if (switch_tab[i] == value)
			return i;
	return -1;
}

#define SWITCH_EXPR(value)	map32_16(value)
#define SWITCH_CONS(value)	(value ## _16)

#else /* integers are 32 bits */

#define SWITCH_EXPR(value)	(value)
#define SWITCH_CONS(value)	(value)

#endif

...

	switch (SWITCH_EXPR(Some32BitExpression)) {
	case SWITCH_CONS(CONST32_0):
		...
	case SWITCH_CONS(CONST32_1):
		...
	case SWITCH_CONS(CONST32_N):
		...
	}

---------------
This calls for only minor changes to the original switch statement and
case constants and will produce code/performance very similar to what
the compiler would do with a large scattered range of case values.

It does provide for the maintainance headache of all the value_16 constants
and switch_tab.  But this is the penalty you pay for not considering 16bit
hosts when the original code was written.

As an alternative to the table look-up, you might see if there's a simple
hash function whould map all your 32bit switch values into a 16bit range.
This would have to be expressible as a compile time reducible expression
(no loops or function calls) so you can get constant expressions in your
cases.

Devin_Ben-Hur at Cup.Portal.Com
...ucbvax!sun!portal!cup.portal.com!devin_ben-hur



More information about the Comp.lang.c mailing list