473,473 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

new literal && optimization


i wrote a display driver for a lcd segment display which doesnt recognize
ascii character so that each character of an output string needs to be
converted byte by byte by looking in a const character table. in short,
when calling my output function display.output("hallo") i dont want to call
the output function at runtime with the ascii character string but with
display.output(CONVERTED("hallo")). ok therefor i could maybe write a macro
with a loop (must be difficult), but while reading bjarne stroustrup c++ i
just thought i could use implicit type conversion and maybe g++ optimizes
the conversion away.
i wrote a test class for this, but g++ doesnt optimize the conversion loop
away:

static const char table[] = {'j','o','s','e','f'};
class DisplayData{
public:
DisplayData(const char* str) : ptr(str) {
for(int i=0;i<5;i++){
const_cast<char*>(str)[i] = table[const_cast<char*>(str)[i]]; // each
byte is converted in another one
}
}
const char* ptr;
};
void output(const DisplayData& d){
cout << d.ptr << endl;
}
int main(){
const char str[] = "\00\01\02\03\04"; // the original character string
which should be optimized away
output(str);
}

any idea to get it optimized or another way to solve this problem ?

josef
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 19 '05 #1
4 2660


josef angermeier wrote:

i wrote a display driver for a lcd segment display which doesnt recognize
ascii character so that each character of an output string needs to be
converted byte by byte by looking in a const character table. in short,
when calling my output function display.output("hallo") i dont want to call
the output function at runtime with the ascii character string but with
display.output(CONVERTED("hallo")). ok therefor i could maybe write a macro
with a loop (must be difficult), but while reading bjarne stroustrup c++ i
just thought i could use implicit type conversion and maybe g++ optimizes
the conversion away.
i wrote a test class for this, but g++ doesnt optimize the conversion loop
away:

static const char table[] = {'j','o','s','e','f'};
class DisplayData{
public:
DisplayData(const char* str) : ptr(str) {
for(int i=0;i<5;i++){
const_cast<char*>(str)[i] = table[const_cast<char*>(str)[i]]; // each
byte is converted in another one
}
}
const char* ptr;
};
void output(const DisplayData& d){
cout << d.ptr << endl;
}
int main(){
const char str[] = "\00\01\02\03\04"; // the original character string
which should be optimized away
output(str);
}

any idea to get it optimized
I don't think that you will find a compiler, which does this optimization
you intend. After all, you could do the translation by hand when you
write the code. But then: How many constant texts do you have in your
program, and is it really necessary to optimize them? Usually the
actual output to the device takes much longer then the table lookup.
or another way to solve this problem ?


Yup. Don't translate at the point of the call, but translate in
the output routine. The output routine will need to grab each
character as it is sent to the device. This is the place where
I would insert the translation step.

void output( const char* Text )
{
int Len = strlen( Text );
for( int i = 0; i < Len; ++i ) {
int Translated = table[ Text[i] ];
Send_Translated_Character_to_Device( Translated );
}
}

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #2

"josef angermeier" <jo**************@web.de> wrote in message news:op**************@news.uni-erlangen.de...
int main(){

const char str[] = "\00\01\02\03\04"; // the original character string
which should be optimized away
output(str);
}

Not only is it unlikely to get optimized away, what you have written is undefined
behavior. You're lucky you got useful code at all, let alone what you wanted.
Jul 19 '05 #3


josef angermeier wrote:

hi! first thanks for your reply!
I don't think that you will find a compiler, which does this optimization
you intend. After all, you could do the translation by hand when you
write the code. But then: How many constant texts do you have in your
program, and is it really necessary to optimize them? Usually the
ok your right, its not a must-have feature but doing the table-lookup at
compile would make the ascii-to-bitmap-table unnecessary, which would save
me around 128 bytes. after all its just a simple loop, shouldnt this work
somehow ?


Are you that low on memory, that 128 bytes really hurt you? Even on my
small One-Card computer, I have a total of 2K RAM available (+8K EPROM)
and I wouldn't worry about 128 bytes for a translation table. If something
is needed, then it is needed.

Besides: How do you think should the translation process proceed
in case of non-constant texts? So it seems, no matter what you do,
you *will* need this table.
Yup. Don't translate at the point of the call, but translate in

i extracted this conversion loop from the "write-to-lcd"-code because i
thought this would also help g++ to optimize it away

well, maybe i should after all use macros, but iv never done a loop with
macro...


You seem to have a wrong understanding of what Macros can do and
what they can not do.
The proprocessor is nothing more then a glorified text editor (roughly
speaking). The macro capability in this context is nothing more then a
'search and replace' facility embedded in the text you write (again
roughly speaking). The preprocessor replaces some text with some other
text and thats it.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #4
On Wed, 16 Jul 2003 16:50:30 +0200, Karl Heinz Buchegger
<kb******@gascad.at> wrote:
Are you that low on memory, that 128 bytes really hurt you? Even on my
small One-Card computer, I have a total of 2K RAM available (+8K EPROM)
and I wouldn't worry about 128 bytes for a translation table. If
something is needed, then it is needed.
Besides: How do you think should the translation process proceed
in case of non-constant texts? So it seems, no matter what you do,
you *will* need this table.
ok right, but unnecessary code wasting space remains for me evitable. your
right there could be also non const char arrays, if so this of course
should be converted at runtime but the others at compile time.
You seem to have a wrong understanding of what Macros can do and
what they can not do.

ok your right, but are you sure its absolutely not possible to do this with
c macros ? i think when using the nasm assembly macros can have compile
time loops and so i thought maybe the C preprocessor can so too....

using an external program for this is possible but not very convinient!

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Andrew | last post by:
Section 17.4.3 of the ECMA-334 C# Language Specification says 1 When a field-declaration includes a volatile modifier, the fields introduced by that declaration are volatile fields. 2 For...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
4
by: Mark Rae | last post by:
Hi, VS.NET 2003 on WinXPPro + all latest service packs etc. I have a totally standard WebForm wherein I need to include one of several HTML files according to various parameters etc. I'm...
5
by: Tom Gurath | last post by:
http://osnews.com/story.php?news_id=5602&page=2 This benchmark tests the Math & File I/O of 9 languages/run-times. Visual C++ (Version 7 - not managed) Visual C# gcc C Visual Basic.NET Visual...
1
by: JeffP | last post by:
Currently I have a user control which contain a <a href> link tag. I have a requirement to change the name property of the anchor tag to something like this: <a href="somepage.aspx" id"thislink"...
9
by: will | last post by:
I have an XML input that includes things like: <foo>line of text another line of text yet another</foo> And I want the entities PRESERVED (not translated) on the result, so: <bar>line of...
30
by: Alf P. Steinbach | last post by:
I once suggested in that SomeOne Else(TM) should propose a string value class that accepted literals and char pointers and so on, with possible custom deleter, and in case of literal strings just...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.