Xref: utzoo comp.lang.c:17650 comp.os.misc:864 comp.sys.ibm.pc:27429 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!shadooby!accuvax.nwu.edu!tank!shamash!nic.MR.NET!srcsip!csd4.milw.wisc.edu!lll-winken!uunet!portal!cup.portal.com!Devin_E_Ben-Hur From: Devin_E_Ben-Hur@cup.portal.com Newsgroups: comp.lang.c,comp.os.misc,comp.sys.ibm.pc Subject: Re: Word size problem with MSC Message-ID: <17110@cup.portal.com> Date: 13 Apr 89 17:03:15 GMT References: <9462@hodge.UUCP> Organization: The Portal System (TM) Lines: 80 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