473,698 Members | 2,181 Online
Bytes | Software Development & Data Engineering Community
+ 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("hall o")). 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(con st 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\0 4"; // 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 2672


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("hall o")). 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(con st 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\0 4"; // 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_D evice( 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\0 4"; // 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******@gasca d.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
1912
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 non-volatile fields, optimization techniques that reorder instructions can lead to unexpected and unpredictable results in multi-threaded programs that access fields without synchronization such as that provided by the lock-statement (15.12). 3 These...
7
4358
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 type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
5
3973
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 variable **var make it an array of pointers? I realize that 'char **var' is a pointer to a pointer of type char (I hope). And I realize that with var, var is actually a memory address (or at
4
1952
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 trying to use an <asp:Literal /> control for this, and intend to modify its Text property in the Page_Load event, but it's not working. The code I'm using is as follows:
5
1604
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 J# Java 1.3.1 Java 1.4.2
1
1972
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" name="&lid=somename">Link</a> The problem is that when the aspx page renders this user control, the & character is converted to <literal>&amp;</literal>. So what I end with is: <a href="somepage.aspx" id"thislink"
9
1860
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 text another line of text yet another</bar> I've tried <xsl:copy-of select="foo/text()" />, I've tried
30
2456
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 carrying the original pointer. In other words, for the simplest usage code: * no overhead (just carrying a pointer or two), and * no possibility of exceptions (for that case).
0
8668
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9014
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8855
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7708
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6515
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5857
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2320
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1995
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.