Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Nigglypuff
Nov 9, 2006


BUY ME BONESTORM
OR
GO TO HELL

BattleMaster posted:

Durr hurr

I was thinking more along the lines of memory poo poo that other compilers wouldn't understand.

Can you post a compact example of code like this? I'm confused and curious to see what it looks like.

Adbot
ADBOT LOVES YOU

BattleMaster
Aug 14, 2000

Nigglypuff posted:

Can you post a compact example of code like this? I'm confused and curious to see what it looks like.

It's not too exciting, just loads of non-standard type qualifiers and preprocessor directives. I also think that some other features it has are not widely supported, such as anonymous structures, but those are non-essential and wouldn't come into play in that situation.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
There's also the option of going all-out and writing a new LLVM target. The LLVM stuff is really easy to work with.

necrobobsledder
Mar 21, 2005
Lay down your soul to the gods rock 'n roll
Nap Ghost

Avenging Dentist posted:

A real man would use the GCC frontend with LLVM to compile to (optimized) C and then compile with the old-n-busted compiler you've got.
I used to do something similar when working on some really obscure archs (they're not made anymore I believe) like BattleMaster here. The old-n-busted compiler's bugs must be dealt with somehow, and trying to be a real man never got my projects finished before either. There's also some cutesy bugs that you may have to deal with that writing a different frontend would only obscure.

For writing embedded poo poo that only fits into maybe <128KB of total memory, the complexity / legibility of the code isn't as big of an issue as it would be for much larger systems - there's only so much code you can write in that space (unless you get into awesome mutative poo poo like Synthesis OS, but 128KB hardly has room for a process scheduler). With that little bit of space you're not exactly about to write a TCP Vegas implementation or anything for your tiny web server. On the other hand, I've usually resorted to just going with straight assembly in those situations, which negates the point of LLVM / compiler usage anymore. If you're working with microcontrollers and really small CPLDs, you're probably better off staying with assembly rather than C if you're doing anything serious, old, and proprietary as gently caress.

I dunno, writing poo poo just to keep it aesthetically modern when it's targeting completely outdated platforms that nobody cares about seems like a waste of time to me. And I'm someone that loves to tackle poo poo just for the sake of flexing my kung foo.

blorpy
Jan 5, 2005

Otto Skorzeny posted:

C'mon guys, plenty of people make lovely first posts, our job is to educate them so they don't end up like Victor or Chain Chomp

gently caress you

Mill Town
Apr 17, 2006

necrobobsledder posted:

I dunno, writing poo poo just to keep it aesthetically modern when it's targeting completely outdated platforms that nobody cares about seems like a waste of time to me. And I'm someone that loves to tackle poo poo just for the sake of flexing my kung foo.

PIC18 isn't outdated, it's one of the newer additions to the PIC families of microcontrollers which are using in billions of devices all over the world.

For people who think this is a silly or irrelevant architecture, it's not. There are literally three times as many PICs in the world than there are PCs.

Vanadium
Jan 8, 2005

How many programmers are there targetting it, though, compared to all of us plebs writing PC or web apps in perl?

Mill Town
Apr 17, 2006

Vanadium posted:

How many programmers are there targetting it, though, compared to all of us plebs writing PC or web apps in perl?

Not nearly as many, but that wasn't the point. The point is that there are times when code size still matters and not everyone is a PC babby with an infinite number of garbage-collected languages at their disposal.

duck monster
Dec 15, 2004

Trabisnikof posted:

Political Science major programmer checking in

hi5 buddy. our code is ideologically correct.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Mill Town posted:

For people who think this is a silly or irrelevant architecture, it's not. There are literally three times as many PICs in the world than there are PCs.

For people who think that ants are a silly or irrelevant species, they're not. There are literally 142857 times as many ants in the world than there are humans.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Avenging Dentist posted:

For people who think that ants are a silly or irrelevant species, they're not. There are literally 142857 times as many ants in the world than there are humans.
I've seen about 20 sci-fi movies from the 1950s that lend significant weight to this statement :tinfoil:

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
You can call ants irrelevant all you want but if your house gets an infestation its loving infinity dollars to fix it :mad:

BattleMaster
Aug 14, 2000

Avenging Dentist posted:

For people who think that ants are a silly or irrelevant species, they're not. There are literally 142857 times as many ants in the world than there are humans.

That's the dumbest analogy I've ever seen

Edit: I mean it was really bad even compared to your usual schtick.

Edit 2: Sorry dude bad jokes are still bad

BattleMaster fucked around with this message at 07:53 on Jul 26, 2009

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

BattleMaster posted:

That's the dumbest analogy I've ever seen

Congrats, I guess you get jokes?

whateverfor
Jul 23, 2007
fuck you sped

Otto Skorzeny posted:

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil." - Tony Hoare

The quote was popularized by Knuth. People (not Knuth, who spoke pretty clearly in the article where he first quoted Hoare that nobody bothers to read and everyone ignores) fail to realize that Hoare was referring to counting cycles. Accounting for known limits of your compiler by translating one simple structure into another that is also semantically valid for what you're doing is nothing like 'premature optimization'.

other important words: "small", "97%", and "premature". the point isn't that programmer time is vastly more important than writing efficient code, but that you should work on "small" efficiencies at the end when you're sure you need them AND have a much better sense what parts of your program need to be optimized.

tef
May 30, 2004

-> some l-system crap ->
you have to measure to find the slowdowns before removing them.

the one possible exception is where you write a purposefully and knowingly use piece of slow code, or use a slow library in place of a faster one for ease of development.

but even so, it's too easy to be mistaken about possible improvements, and sometimes for a supposedly better algorithm a big-o notation hides really big constant values, or piss poor cache behaviour.

BattleMaster
Aug 14, 2000

There's no point trying to optimize code in a high level language for a PC until there's actually a problem because compilers are usually better than humans at that and there's too much stuff going on behind the scenes. Trying to be clever usually is a waste of time and sometimes results in worse results.

Now this discussion about pre-optimization was started when I mentioned that I not only do not program for x86 PCs, but rather 8-bit microcontrollers and I know the limits of the compiler, instruction set, and hardware I'm working with and know what language features to use in certain situations. This is quite a bit different than the bad kind of "pre-optimization" that we're talking about.

The system I'm writing for has an 8-bit data bus, no cache, and prefetches only one instruction. It is a RISC machine with instructions that are all 16 bit and take exactly 4 clock cycles to complete. I could probably recite most of the instructions and what they do from memory, because there are only well under 200 of them. I have lots of experience writing assembly and picking apart the code the compiler generates which is often very easy to follow because of the simplicity of the hardware. I'm not some insane code-ninja, it's just that this platform is extremely simple and straightforward.

So essentially, it's not pre-optimization to use an unsigned char instead of a signed int because you know the code it generates will be three times smaller in every case. It's not pre-optimization to use switch/case instead of an if chain if you know your compiler will only generate an efficient jump table with switch/case. It's not pre-optimization to avoid large local variables because you know the hardware and compiler sucks at stack operations and requires three times as much code to do them than absolute addressing.

Those are more like best practices than optimizations, anyways. If I truly thought I knew better than the compiler I would be using assembly instead.

BattleMaster fucked around with this message at 17:07 on Jul 26, 2009

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
Nobody caaaaaaaaares

BattleMaster
Aug 14, 2000

It's not my fault that most people can't imagine programming anything except their pet scripting language.

Edit: Oh you know regular expressions? Cool well I'm building alarm clocks and USB-connected instrumentation :c00l:

BattleMaster fucked around with this message at 18:31 on Jul 26, 2009

Zombywuf
Mar 29, 2008

Avenging Dentist posted:

For people who think that ants are a silly or irrelevant species, they're not. There are literally 142857 times as many ants in the world than there are humans.
MY
MONKEY
SPHERE

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

BattleMaster posted:

Edit: Oh you know regular expressions? Cool well I'm building alarm clocks and USB-connected instrumentation :c00l:

If you aren't coding for systems with at least 32 GB of RAM you have made some unfortunate life decisions.

Lonely Wolf
Jan 20, 2003

Will hawk false idols for heaps and heaps of dough.
I prefer premature pessimization because alliteration will never lead you astray.

antpocas
Jun 30, 2004

IF THIS POST ISN'T ABOUT GLORIOUS SALAZAR YOU MUST BE READING IT WRONG

Avenging Dentist posted:

If you aren't coding for systems with at least 32 GB of RAM you have made some unfortunate life decisions.
Do you ever have nightmares where you find yourself programming MUMPS again?

Kelson
Jan 23, 2005

BattleMaster posted:

It's not my fault that most people can't imagine programming anything except their pet scripting language.

Edit: Oh you know regular expressions? Cool well I'm building alarm clocks and USB-connected instrumentation :c00l:

Lets get back to the poo poo code, not posts.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

antpocas posted:

Do you ever have nightmares where you find yourself programming MUMPS again?

Um, what's a "MUMPS"?

RussianManiac
Dec 27, 2005

by Ozmaugh
code:
do {
    if (b == null) { // abort
      break;
    }
     ... // do something useful
} while (false);

... // cleanup
I have seen examples of this in a source code in a large Silicon Valley company which I won't name.

I don't think it is an example of coding horror, but it does look weird the first time you see it.

RussianManiac fucked around with this message at 02:03 on Jul 27, 2009

mr_jim
Oct 30, 2006

OUT OF THE DARK

RussianManiac posted:

code:
do {
    if (b == null) { // abort
      break;
    }
     ... // do something useful
} while (false);

... // cleanup
I have seen examples of this in a source code in a large Silicon Valley company which I won't name.

I don't think it is an example of coding horror, but it does look weird the first time you see it.

Someone posted it a couple of pages ago.

http://forums.somethingawful.com/showthread.php?threadid=2803713&perpage=40&pagenumber=66#post363641531

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Wow, and I was thinking about replying "that's not really that bad, it's useful when you have to implement multi-statement macros in C". :barf:

mr_jim
Oct 30, 2006

OUT OF THE DARK

Avenging Dentist posted:

Wow, and I was thinking about replying "that's not really that bad, it's useful when you have to implement multi-statement macros in C". :barf:

I thought you were joking. Then I read this: http://c-faq.com/cpp/multistmt.html

:barf: indeed.


But using it to avoid an early return, or a goto to cleanup code? That's just silly.

mr_jim fucked around with this message at 02:20 on Jul 27, 2009

RussianManiac
Dec 27, 2005

by Ozmaugh

mr_jim posted:

I thought you were joking. Then I read this: http://c-faq.com/cpp/multistmt.html

:barf: indeed.


But using it to avoid an early return, or a goto to cleanup code? That's just silly.

Better use that technique than goto. Avoiding early return is stupid tho, i agree.

mr_jim
Oct 30, 2006

OUT OF THE DARK

RussianManiac posted:

Better use that technique than goto. Avoiding early return is stupid tho, i agree.

Why?

code:
if (b == null) goto CLEANUP;

// Do something useful

CLEANUP:

// Cleanup

How is that wrong?

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge

mr_jim posted:

Why?

code:
if (b == null) goto CLEANUP;

// Do something useful

CLEANUP:

// Cleanup

How is that wrong?

It's not. Some people have "OMG goto is evil" beaten into them by idiots who should know better.

RussianManiac
Dec 27, 2005

by Ozmaugh
I stopped using goto after I picked up a language that was more advanced than QBASIC or assembly.

Ok sure, its not evil or stupid in all cases, but a nice do while block is nicer looking to me than same with goto statements, maybe just because I didn't see goto in such a long while.

In any case the do/while(false) is not any less efficient than similar alternatives, and is about as easy to read/comprehend as anything else.

mr_jim
Oct 30, 2006

OUT OF THE DARK

Zakalwe posted:

It's not. Some people have "OMG goto is evil" beaten into them by idiots who should know better.

They are half right, in that if there is a control statement that clearly expresses what you want to accomplish, you should use it.

I figured out what bugs me about this code.

code:
do {
    if (b == null) { // abort
      break;
    }
     ... // do something useful
} while (false);

... // cleanup
That break statement is a goto. But to make it look less like a goto, it is wrapped inside a loop statement that has been abused into unconditionally executing a list of statements exactly once. That is the opposite of clarity.

If your language supports try/finally, then using that is a better choice because it makes your intent clear. In C, a goto is the next best thing.

mr_jim fucked around with this message at 04:06 on Jul 27, 2009

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

RussianManiac posted:

In any case the do/while(false) is not any less efficient than similar alternatives, and is about as easy to read/comprehend as anything else.

I disagree. Consider the relatively common problem of progressively constructing a resource, which can fail and different stages, requiring different amounts of cleanup. Furthermore, sometimes we can't detect failure without going a conditional or two deeper:

code:
do {
  // ...
  if ( ... ) {
    // ...
    if ( ... )
      break;
    // ...
  };
  do {
    // ...
    if ( ... ) {
      // ...
    };
    // ...
    if ( ... )
      break;
    do {
      // ...
      if ( ... )
        break;
      // ...
    } while (0);
    // cleanup 3
  } while (0);
  // cleanup 2
} while (0);
// cleanup 1
Notice that for any given break, we have to scan up to work out what do we're breaking out of, then scan down to find the matching while that we jump to. Because our first failure case is itself in some conditional code, the first and second break are at the same indentation level, even though indentation is the only direct indication of where the break is going to go; at a first glance, it is very easy to assume they are going to the same place. Finally, notice how the "normal" codepath gradually moves farther and farther to the right.

In contrast, the version with gotos:

code:
  // ...
  if ( ... ) {
    // ...
    if ( ... ) {
      goto CLEANUP_1;
    // ...
  };
  // ...
  if ( ... ) {
    // ...
  };
  // ...
  if ( ... )
    goto CLEANUP_2;
  // ...
  if ( ... )
    goto CLEANUP_3;
  // ...
CLEANUP_3:
  // ...
CLEANUP_2:
  // ...
CLEANUP_1:
  // ...
It's very clear at each failure condition exactly where the control flow moves. The normal codepath is left-flushed and isn't interrupted by do/while structures.

And this isn't even considering if there are legitimate uses for do/while in your algorithm, which would make the dual use of do/while for exceptions even worse for someone reading the code.

TSDK
Nov 24, 2003

I got a wooden uploading this one
Hand-coding exception handling or destructor emulation in C is nature's way of telling you that you should be using C++ ;)

dancavallaro
Sep 10, 2006
My title sucks

mr_jim posted:

Why?

code:
if (b == null) goto CLEANUP;

// Do something useful

CLEANUP:

// Cleanup

How is that wrong?

Ummmmm duh didn't you know that a famous computer scientist said gotos are bad? So obviously we should never use them. And also Knuth said we should never do optimizations because they're evil.

tef
May 30, 2004

-> some l-system crap ->

dancavallaro posted:

Ummmmm duh didn't you know that a famous computer scientist said gotos are bad? So obviously we should never use them. And also Knuth said we should never do optimizations because they're evil.

http://pplab.snu.ac.kr/courses/adv_pl05/papers/p261-knuth.pdf

mr_jim
Oct 30, 2006

OUT OF THE DARK

dancavallaro posted:

Ummmmm duh didn't you know that a famous computer scientist said gotos are bad? So obviously we should never use them. And also Knuth said we should never do optimizations because they're evil.

You're right. I fixed the code to avoid gotos, ugly do/while hacks, and still have a single point of exit!

code:
int is_it_safe;
if (b == null) {
    is_it_safe = 0;
} else {
    is_it_safe = 1;
}

if (is_it_safe) {
    // do stuff
}

// cleanup
:downs:

Adbot
ADBOT LOVES YOU

dancavallaro
Sep 10, 2006
My title sucks

mr_jim posted:

You're right. I fixed the code to avoid gotos, ugly do/while hacks, and still have a single point of exit!

code:
int is_it_safe;
if (b == null) {
    is_it_safe = 0;
} else {
    is_it_safe = 1;
}

if (is_it_safe) {
    // do stuff
}

// cleanup
:downs:

Thanks, that's much better! Doesn't that feel good?

:science:

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply