[comp.os.misc] Word size problem with MSC

jdm@hodge.UUCP (jdm) (04/13/89)

	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?

-- 

jdm@hodge.cts.com [uunet zardoz vdelta crash]!hodge!jdm

	James D. Murray, Ethnounixologist
	Hodge Computer Research Corp.
	1588 N. Batavia Street 
	Orange, CA 92667  USA
	TEL: (714) 998-7750
	FAX: (714) 921-8038

Devin_E_Ben-Hur@cup.portal.com (04/14/89)

jdm@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@Cup.Portal.Com
...ucbvax!sun!portal!cup.portal.com!devin_ben-hur

bumby@math.rutgers.edu (Richard Bumby) (04/14/89)

In article <9462@hodge.UUCP> jdm@hodge.UUCP (jdm) writes:

          . . . <some stuff omitted> . . .

> 	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?
> 
> -- 
	This may not be the patch you are looking for, but as long as
you really don't need more than 65536 cases, you should probably find
a simple function that maps your domain into something that can be
represented by a 16 bit integer.  As long as it is declared to be an
int, the internal representation will be irrelevant.  The resulting
code will then be compatable with both 16 and 32 bit processors.
-- 

--R. T. Bumby ** Math ** Rutgers ** New Brunswick **
(in one form or another for all kinds of mail)
[bumby@math.rutgers.edu]