home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!spool.mu.edu!sdd.hp.com!think.com!enterpoop.mit.edu!ira.uka.de!math.fu-berlin.de!news.th-darmstadt.de!rbg.informatik.th-darmstadt.de!misar
- From: misar@rbg.informatik.th-darmstadt.de (walter misar)
- Subject: Re: quick addition using char and shorts....
- Sender: news@news.th-darmstadt.de (The News System)
- Message-ID: <1992Dec11.183920@rbg.informatik.th-darmstadt.de>
- Date: Fri, 11 Dec 1992 17:39:20 GMT
- References: <1992Dec10.154834.8349@news2.cis.umn.edu>
- Nntp-Posting-Host: rbhp58.rbg.informatik.th-darmstadt.de
- Organization: TU Darmstadt
- Lines: 46
-
- In article <1992Dec10.154834.8349@news2.cis.umn.edu>, peschko@barracuda.micro.umn.edu (Edward Peschko) writes:
- > hello --
- >
- > I am writing an application in which both time AND memory are important....
- > (well, memory is more important... I am working on a massively parallel
- > environment in which each processor has only 64K). Hence, as much as possible
- > I will be using shorts and characters for calculations..
- >
- > Is there any way to take advantage of the shorts and characters having
- > only 8 and 16 bits respectively to do addition? (C, unfortunately, converts
- > them to int types before doing the actual addition.) I was thinking of doing
- > a function call, but would not the function call overhead be too much to make
- > this worthwhile?
-
- But if you store the result in chars or shorts, the expanding takes place in
- registers only, and don't waste memory. Furthermore most compilers would
- detect that to chars are added to form a new char, and omit the expansion when
- optimizing is done.
-
- > Also, I was having trouble doing the bit assignments (to substitute for
- > Boolean) I noticed that the only way one can do this is to put it inside a
- > structure.. However, when I do a sizeof (Boolean) with my newly defined type,
- > I get 4 (as in 4 bytes)...
-
- Since the machine doesn't handle single bits, it will store the one bit actually
- in a word or longword ( or whatever the 'natural' datasize is on your machine).
- But you could pack several booleans.
- Either:
- typedef struct { unsigned b0 :1;
- ....
- unsigned b31 :1;
- } many_bools;
- or:
- typedef int manybools;
- #define b0 1<<0
- ....
- #define b31 1<<31
-
- and then selecting the bits with &(~bn) and storing them with |bn .
-
- If you want an actual boolean type, a typedef char boolean is perhaps
- sufficiently optimized.
-
- --
- Walter Misar
- misar@rbhp56.rbg.informatik.th-darmstadt.de
-