r/shittyprogramming • • Jun 28 '15

Embedded developer adventures

Post image
297 Upvotes

55 comments sorted by

35

u/say_wot_again Jun 28 '15

But.... That's not what they meant when they said double! And how would you tell 1, 10, and 100 apart? I call shenanigans.

20

u/amphetamachine Jun 28 '15

Cast it as a string and pass it to atof

9

u/immibis Jun 28 '15

You mean like this?

int getRightPartOfDecimal(double d) {
    // XXX 2015-06-28 H.K. - /u/amphetamachine suggested this method to extract the second part of a double
    // XXX 2015-06-28 H.K. - Not sure why this needs an int cast in between though
    return atof((char*)(int)d);
}

2

u/ThisIsADogHello Jun 29 '15
     // XXX 2015-06-28 H.K. - /u/amphetamachine suggested this meth
od to extract the second part of a double

Interesting place for a line break to appear...

2

u/immibis Jun 29 '15

What browser breaks lines in the middle of words?

1

u/ThisIsADogHello Jun 29 '15

Apparently Chrome will, if it's in a <code> tag. I'd rather have that than having to sidescroll, though.

1

u/amphetamachine Jun 29 '15

Chrome could at least let us set sbr.

1

u/amphetamachine Jun 29 '15

Oops, I guess all that higher-level programming has ruined me. By "cast as a string" I really meant sprintf(strval, "%f", floatval)

5

u/alienangel2 Jun 28 '15 edited Jun 28 '15

Presumably he meant they were using the high bytes of the double for one integer and the low bytes for another, not storing string representations (which wouldn't fit in a double anyway). So the top 4 bytes of a 64bit double would store 0 as a 32bit int, and the lower 4 bytes would store 10 as another 32bit int, and there'd be no issue differentiating between 1, 10 and 100.

Still a bit pointless unless the system they're using doesn't have a compiler with things like uint32 defined, but that might actually be the case for some architectures.

If he's reading it right and it really is storing the values on sides of the decimal that is extra bizarre, since doubles are floating point. I don't think anyone would jump through the hoops needed to try to make that work, so yeah he's either making it up or misreading.

Anyway, it's not like shitty devs don't exist all over, op would find other retardedness outside of embedded. The int inside doubles thing might be justified though (actually if the c compiler is homebrewed for some obscure chip, even the code in header files might be justified). Given that OP has two weeks of experience, he probably doesn't understand what is going on enough to judge what really is bad in an embedded system and what is due to compiler or hardware idiosyncrasies.

1

u/fb39ca4 Jun 29 '15 edited Jun 29 '15

No, it sounds shittier than that. By either side of the decimal point, I think it meant this:

Let A and B be the positive integers you want to store. Store them as A + 1/B in a floating point number. To extract them, A = integer part, and B = 1/fractional part. (If B = 0, don't put anything in the fractional part.)

For example, 10 and 7 would become 10.142857... A = 10, and B = 1/.142857.

If you wanted to store signs, you could use the sign bit from the floating point number for one, and then for the other, remap the integers so that positive integers become positive even integers and negative integers become positive odd integers.

1

u/alienangel2 Jun 29 '15

Yeah you can come up with mappings that can be interpreted as storing the two numbers in the two parts. But to do so you have to understand floating point representation to begin with, and and I don't think anyone who does would try to store two ints like this, rather than just writing them directly into the high and low bytes for the double.

The most likely thing IMO is that the guy posting about it didn't understand what he was reading.

1

u/fb39ca4 Jun 29 '15

This is /r/shittyprogramming so I am just going to assume it is shitty. But the method I described would work for any floating point format as long as it has sufficient precision.

1

u/oddark Jun 28 '15

Why has no one considered that the numbers are padded on the left with zeros?

If you want to store 0xF01 and 0x42, the result would be 0xF01.0042. Just take the decimal part and multiply it by 0x10000.

1

u/fb39ca4 Jun 29 '15

If you wanted to store two integers in a floating point number, you could store one as the integer part of the number, and the other in the fractional part as its reciprocal. Of course, you can only store one of the numbers signed that way.

0

u/[deleted] Jun 28 '15

[deleted]

6

u/thorium220 Jun 28 '15

All you'd have to do is get a pointer to it and then you could write two integers into it. As long as you remember what it is and you read it appropriately, it's a very efficient way of sending two integers around.

I've personally made excellent use of a library that masks integers as 16.16 fixed-point reals. Very useful when you've got, say, a 10-bit ADC and a bunch of maths to do, but not enough RAM or processing time between samples to work in floats.

7

u/ZugNachPankow Jun 28 '15

But why not use a struct? They are literally meant to be a data structure to store several pieces of data of several types.

5

u/thorium220 Jun 28 '15

Honestly, I have no idea why you'd perform this particular 'hack', I'm just saying that for this particular instance it probably gave some small speed or memory advantage.

I agree, it shouldn't make a difference.

1

u/stone_henge Jun 28 '15

That would complicate mathematical operations on the numbers quite a bit. With fixed point representation, add is still add, subtract is subtract and multiplication is multiplication and right shift. This also allows effective conversion between and operations on numbers with different radix points.

I am not sure why you would approach the integer and fractional parts as different datatypes. Just name a new type for the whole thing to be explicit about it.

0

u/[deleted] Jun 28 '15

[deleted]

1

u/thorium220 Jun 28 '15

In general, I'd agree, I'm just saying that I can understand how a situation such as the OP described could come about. Maybe the developer was running low on RAM and decided to use a float-only register to store these two ints in?

1

u/apocryphalmaster Jun 28 '15 edited Jun 28 '15

Or just write one of them backwards?

8

u/avinassh Jun 28 '15

OP in which chan I have to lurk for more programming gems?

2

u/amphetamachine Jun 28 '15

I don't know. I've never visited 4chan.

4

u/Kattzalos Jun 28 '15

/g/

2

u/avinassh Jun 29 '15 edited Jun 29 '15

thank you ^_^

107

u/thorium220 Jun 28 '15

I can understand why stuff like this happens. Programmers who have only ever worked in full systems don't always have a good understanding of how embedded is different. You've got very limited resources, and often you need to get creative to ensure that your work is done fast enough.

Say you've built a data acquisition system. Your main() function is going to have two parts: the configuration section (setting all the registers), and the main while(1) loop. yes, while(1). No, I don't want to leave the loop, this processor is going to sit in place doing its thing until the head death of the universe.

My ADC is sampling at 200 samples/second. I pull this into a 16 sample dedicated ADC buffer, then pull those 16 samples into my program for work. I need to have completely finished with processing those 16 samples by the time the next 16 samples are ready, so I probably don't have time to pissfart around with fancy floats, so a library that pretends an int is a 16.16 fixed-point number is going to have to do.

Also, there's no OS. You want to dynamically allocate memory? From who? The RAM is yours. You either declare everything at compile time, or you keep careful track of what memory you have addressed (and how much of the memory will be used by the program stack). remember, I've got to finish what I'm doing in (1/200,000)*16 seconds, or I'm going to lose data. If I have to hard-cap the size of a buffer in order to make my code run at a reliable speed, you'd best believe I'm going to do it.

And yes, I have used an oscilloscope to measure the duty cycle of my microprocessor in order to make sure I won't drop any samples.

Maaaan, I miss embedded work.

21

u/kosairox Jun 28 '15

I'm actually considering trying to become an embedded systems programmer, is it worth it? I do hobby projects using uC's in my spare time and I don't want to be a "normal" programmer. Aside from programming, I enjoy electronics etc. Why did you leave embedded? How's it like?

46

u/thorium220 Jun 28 '15

Ah, I can see I've vastly over-sold my experience.

I was an embedded programmer for the 14 weeks of my required work placement in order to complete my computer engineering degree.

I have changed industries because there just aren't enough jobs for embedded programmers here in Australia, and I was unable to land one. I now audit fixed cabling infrastructure for a data cabling company.

The thing I like about embedded programming is that you work so much closer to the bare silicon. You spend so much time looking over datasheets, timing diagrams, pinouts etc, hooking up the I/O pins to an oscilloscope to check that flags are as they should be etc. There's no black box between your code and what actually goes on inside the ALU.

The reason I don't do any hobbyist embedded work is because I'm lazy, and when I get home from work all I want to do is play games and watch anime.

16

u/barrows_arctic Jun 28 '15 edited Jun 28 '15

I've spent the last 7 years as an "embedded programmer". It's really not a programming job in the typical sense. You spend most of your time getting hardware to work properly, which means you need to have a good understanding of circuitry, schematics, RTL, digital logic, lab work (logic analyzers, protocol analyzers, oscilloscopes, etc.), serial timing protocols, CPU and pipeline architecture, and so on. Most embedded software guys that I've encountered have an EE-type education in their past, not CS. But if you enjoy electronics and are comfortable getting your hands dirty with it, and aren't afraid of bare-metal debugging, you've certainly got enough of a start to join the game. Getting a job writing drivers and low-level communications protocols for new components might be a good way to get oriented and get started.

You must know and be comfortable in C. You should probably know some assembly (preferably something RISC like PowerPC, MIPS, or ARM, but x86 does show up here and there, too). In terms of software architectures, it can and will range everywhere from raw, bare-metal programming (think free-standing single-threaded boot loaders), to RTOS land, to linux. Nowadays linux is very common in many of the larger embedded applications and devices.

Now, is it worth yet? I personally love it. You get exposure to the hardware development process, get to work with people in a whole range of development "functions" (e.g. silicon design, board design, thermal design, manufacturing, compliance, etc), and get to solve a lot of really complex and challenging problems. And there do seem to be A LOT of people who hate it and they try and go do something else, meaning the job market right now (at least in California) is excellent for embedded guys.

2

u/[deleted] Jun 30 '15

Nowadays linux is very common in many of the larger embedded applications and devices.

Do you see Linux becoming standard? What has been a reason that you've seen where Linux wasn't used?

What's least interesting part of your job? Are you fighting with other tools often?

IF YOU ANSWER I WILL LOVE YOU as a friend.

2

u/barrows_arctic Jul 01 '15

What has been a reason that you've seen where Linux wasn't used?

Lack of determinism in interrupt response times is one very big reason. Linux cannot be truly Real Time, or anything close to it, but it does have a number of mechanisms these days that make it more than "good enough" for a large number of embedded applications. Ten years ago, that wasn't really the case. But that factor alone makes Linux completely unusable in certain embedded applications (safety critical stuff comes to mind).

Size and boot time are other things holding Linux back a bit, but they are also getting better.

What's least interesting part of your job? Are you fighting with other tools often?

Hmm...that's a tough one. Design document review maybe (for components, new processors, and so on), 'cause it can get boring during those phases of a project. But that's probably not something that's specific to the embedded space. As for tools, like any job, you love some and you hate some. The more you spend, chances get better that you'll be happy with it (e.g. an ARM DSTREAM or a Lauterbach debugger == amazing). That what you mean?

1

u/asuspower Jul 26 '15

Can I pick your brain for a second?

I'm doing EE and I want to land a job that has a) analog design and b) digital design and c) programming. I really like analog design and programming, and I'm not sure what to go into. I like the idea of embedded engineering but I'm guessing there won't be much analog design.

1

u/barrows_arctic Jul 26 '15

I was actually in a similar boat about 8 years ago. My experience in college and even in grad school was split between analog work and DSP embedded work, with some CPU architecture and digital design work, and I liked all three quite a bit. Around graduation time, I ended up just picking one over the others, based on comparing job offers...but the embedded space gets you exposure to at least two of the three. I just left all the analog stuff behind, which probably isn't what you want to hear :)

There's plenty of work out there in the signal integrity space that certainly has some analog work to it, and there might be some way to do some some embedded systems programming that interfaces directly with the SI stuff, but a job that does design for all three (analog/digital/embedded software) is not something I've had the pleasure of doing. A jack-of-all-trades job of that nature seems to me like something you might find at a smaller company like a startup. I've only ever worked at the big companies, so I don't really have any personal pointers for you there.

Understanding of analog and digital design, in additional to basic programming skills, is certainly something that would make you an attractive candidate for an embedded systems job, but yeah...at least in the jobs I've had, there's no direct analog work.

Perhaps working with someone like TI or Linear Tech or Maxim doing component work, or Marvell or Broadcom on the wireless and RF side? These are wild guesses. Sorry I can't help more. Two out of the three is certainly easy to find, but I don't have any personal experience that has shown me any jobs that get you all three...

1

u/asuspower Jul 26 '15 edited Jul 26 '15

Yeah, there seems to be a lot more jobs around the digital/software domain (no pun intended) so I might go for embedded. I'll be sad to leave analog behind, but I guess you have to leave something behind, so maybe I'll tinker around with analog stuff in my spare time.

There's plenty of work out there in the signal integrity space that certainly has some analog work to it

I read a book about signal integrity, and most of it just went over my head. I'll have to read it again when I have more experience in the respective areas.

Perhaps working with someone like TI or Linear Tech or Maxim doing component work, or Marvell or Broadcom

I live in Australia so sadly most of that is out of the question. :/

Control systems is something else I could look into I guess.

Thanks for the input :)

10

u/i_was_a_lurker_AMA Jun 28 '15

i'm working in embedded right now and all of this sang to my soul. especially trying to measure the duty cycle with an oscillloscope to diagnose erratic behavior in the adc module.

also, we totally have to use assembly from time to time. i used it last week to write an intelligent i2c slave on a uC that monitors environmental status.

1

u/[deleted] Jun 30 '15

How is the pay?

I'm doing this sort of stuff now, board controllers for r&d stuff, usb devices, uC, bit banging protocols, RPC, kernel modules, etc, but I didn't realize this was considered embedded. I don't think I'm being paid for it though.

1

u/i_was_a_lurker_AMA Jul 01 '15 edited Jul 01 '15

embedded really doesn't mean anything more than "single-purpose computer in an enclosure", so a lot of embedded stuff isn't any different (often written in Java, &c.) than general purpose computer programming (from the developer's point of view), but the stuff you're doing sounds like pretty hard-core limited-resources embedded work (isn't it hella fun? :D).

i'm still a student, so i'm paid hourly (it's an internship type of situation) and the company is a small one. i don't think i'm being paid better than i would be if i were doing non-embedded stuff at other comparably-sized companies, though.

1

u/[deleted] Jul 01 '15

isn't it hella fun? :D

Supa! I don't care for programming that just lights up pixels on a display, but making something do something in the real world is awesome.

0

u/[deleted] Jun 28 '15

[deleted]

13

u/NathanAlexMcCarty Jun 28 '15

The trick is, a lot of embedded systems don't have hardware support for floating point operations, so you have to emulate it in software, which causes a massive slowdown. Which is why embedded processors usually aren't measured in flops, but instead just regular ops. FLOPS is just one of many metrics used for personal computers and things that act like them, it is far from a universal measure of performance.

7

u/barrows_arctic Jun 28 '15

Floats are a flat-out "no-no" in many embedded systems contexts, either because the response to something must be guaranteed real-time deterministic and/or because the architecture simply does not have floating point support at all.

4

u/arabidkoala Jun 28 '15

When your platform doesn't have the capability of doing floating-point operations in hardware, fixed point is usually much faster.

1

u/Cyph0n Jun 28 '15

I recall a single FP mulitplication takes around 30 clock cycles to execute on the ATmega328P. In the context of embedded systems, that's a LONG time.

I had to minimize FP operations as much as possible, and use integers whenever it makes sense.

2

u/hardolaf Jun 28 '15

That's why most of my systems if they really need floating point send that data to something else for processing. It's beautiful... until you have a communication issue between 7 processors...

1

u/[deleted] Jul 17 '15

Late reply, but yes. On of my biggest takeaways from embedded and digital design is that 1 microsecond is an eternity.

8

u/ericanderton Jun 28 '15

Using a float as a fixed-point ratio?! Bro, do you even bit shift?

5

u/Bubumeister Jun 28 '15

You need to add code in the header if you work with templates (if I recall correctly)

5

u/SnowdensOfYesteryear Jun 28 '15 edited Jun 28 '15

> embedded

> c++

1

u/fb39ca4 Jun 29 '15

Eh, ARM is found on a lot of embedded platforms, and you can definitely write C++ for that.

2

u/SnowdensOfYesteryear Jun 29 '15

You can with pretty much any compiled languages without a runtime, but no one really bothers to.

1

u/[deleted] Jul 17 '15

The fear is that people don't know enough about C++ and its implications in embedded. You'd be (hopefully) surprised how often you find print fin an embedded system, and sometimes even in a critical section.

1

u/fb39ca4 Jul 17 '15

Yep. The drawback of C++ is it provides a lot of tools which are not suitable for embedded programs. But if you use the right parts of the language, you can generate code that is much more readable than pure C and will perform just as well.

3

u/printers_suck Jun 28 '15

This is how Excel calculates dates. Is Excel shitty?

3

u/AranHase Jun 28 '15

Some time ago I had to implement a supervisor (a system that manages many others smaller systems) for a manufacturing plant on a PIC microcontroller. So, I did shitty programming. Lots and lots of if/else, and global vars, and lots of macros and everything in a single file. Here is some of it (github link].

So, why so shitty? Well, the supervisor logic was mapped into a graph with thousands of nodes. And the PIC I was using had very limited memory (can't recall exactly, but like 16KB), and it was divided into Data Memory and Instruction Code Memory. I could not fit the graph into Data Memory. So I had a plan, lets put the data into instruction code memory :) And there we go, lots of if/else.

It was so shitty that I had to write a Python program to code it for me (Link].

Well, it worked (Video In Action), but then again, I'm not very proud, and never touched embedded system again.

1

u/hardolaf Jun 28 '15

Bah casual. Real embedded programmers do everything by hand! (I'm kidding, they hire interns to do it for them).

1

u/fb39ca4 Jun 29 '15

I remember trying to do something similar by encoding a video entirely inside of an OpenGL fragment shader using a massive tree of if statements. There is no limitation on the size and complexity of shader programs in the specification, but in the real world, I ran into compiler limits, so I couldn't get it to work.

2

u/ekolis Jun 30 '15

And they probably have another class that stores a double as a two-element array of ints...