News for hackers. Stuff that matters.
Re:A critique of "How to C in 2016" by ue_ on Friday January 15, 2016 @05:42AM (#10908416)
>Bytes and octets are the same today. I never came across a system where this wasn't true.

The fact that all modern systems use eight bit bytes is no reason to assume that a byte is eight bits. The fact is that a byte is not eight bits large.

People may wish to run code on "historical" systems, and someone may wish to create a 9-bit byte system for fun.

To write C code with an assumption that things are one way, when the standard does not define this assumption, screams of unportable code. You might as well use GCC extensions - after all, for a long time, what would it matter? Everyone used GCC, right?

Re:A critique of "How to C in 2016" by lultimouomo on Friday January 15, 2016 @06:00AM (#10908474)
By the same argument we shouldn't assume that bits have only two values - what, you mean I can't write C for my ternary logic computer?

You need to make a decision about which simplifying assumptions you make - C has decided not to assume there are 8 bits to the byte. I think it's arguable that in 2016 this is not the best decision, and they could get rid of some cognitive overhead by making the simplifying assumption.

Re:A critique of "How to C in 2016" by dietrichepp on Friday January 15, 2016 @06:22AM (#10908547)
Unfortunately, there are real world systems with C compilers that do not have 8 bit bytes. It's easy to forget the embedded world.

C is one of the very, very few languages that you can reasonably expect to run on your weird embedded microprocessor, microcontroller, DSP, or whatever.

Bit, however, means "binary digit". That's the literal definition of the word. Historical usage of the word "byte" has varied.

Re:A critique of "How to C in 2016" by lultimouomo on Friday January 15, 2016 @06:35AM (#10908601)
> C is one of the very, very few languages that you can reasonably expect to run on your weird embedded microprocessor, microcontroller, DSP, or whatever.

I'm not an expert on DSP processing in any way, but I'd argue that if the environment is so peculiar that it doesn't have 8-bit bytes, you won't be able to use so many of the (formally optional) parts of C that you might as well call your code "weirdC" and do without char. It's not like you're going to use normal C libraries, and you are most probably relying on a specific compiler implementation and a lot of platform specific weirdnesses.

Re:A critique of "How to C in 2016" by hamiltonkibbe on Friday January 15, 2016 @07:44AM (#10908930)
You can use char, and for the most part you won't notice any difference, until you start looking at the memory display in your IDE and have a short-lived WTF moment. A c-string looks strange in the memory view because there's a NULL octet between each character but that's an implementation detail that you don't really notice 99% of the time, because everything on the processor is designed to use 16-bit words. If you write a buffer of chars to the UART tx register, it will only put the half of the word with the character data onto the wire, just like with any other microcontroller, just like with PySerial, and just like with JS and websockets. That's why the C standard doesn't say a char is 8-bits, it just says that sizeof(char) is 1. I would argue that any code doing anything under the assumption that a char variable occupies 8 bits in memory and that the next char in an array is physically adjacent to it with no wasted space in between is just poorly-written and inherently not portable.

As for why this is the case, Generally DSPs are designed to do a few tasks very well (multiply-accumulate being the most obvious example, simultaneous reads from X and y memory, etc.) and are generally optimized for a specific word size. If you really need to access an octet on E.g. A 16-bit TI DSP you can, you just need to shift and mask. That's obviously not very efficient, because that's not really what it's optimized for. If that's what you really need, you picked the wrong part. You're gonna be filter-/FFT-/DCT-/whatever-ing way more 12 or 16-bit ADC samples per second, per dollar, and per milliWatt on that 16-bit DSP than you would be on your octet-addressable MSP430.

Regarding "Normal" c libraries, If they're valid C they should work just fine for the most part, With some obvious exceptions: you're not going to have a fun time with your "floats" on a processor without FP HW. That's why there's a standard, if you're not relying on undefined or implementation-defined behavior, you'll be fine(ish)

Re:A critique of "How to C in 2016" by lultimouomo on Friday January 15, 2016 @08:06AM (#10909035)
> I would argue that any code doing anything under the assumption that a char variable occupies 8 bits in memory and that the next char in an array is physically adjacent to it with no wasted space in between is just poorly-written and inherently not portable

I don't think that's fair at all. It's well written and perfectly portable, under that assumption. If that assumption happens to hold for all the systems that could is meant to run on, why shouldn't you make it? Why assuming that unsigned integers overflow to 0 is fair, and assuming 8 bit bytes is not? They are both pragmatic choices. I think arguing that the C standard didn't make the best choice is at the least a reasonable position. Saying that people who disagree with you on this are writing bad code is not the right way to approach the subject, IMO.

> If they're valid C they should work just fine for the most part

I've yet to find a library that is valid C, in the strictest sense of the term - i.e., no floats or doubles, no 8-bit char, no uint8_t, etc. I'm sure there are, among libraries meant to run on DSPs. Does it make sense for them and libraries meant to run on bigger processors to adhere to the same exact standard? I'm not sure.

Re:A critique of "How to C in 2016" by GFK_of_xmaspast on Friday January 15, 2016 @08:27AM (#10909130)
> If that assumption happens to hold for all the systems that could is meant to run on, why shouldn't you make it?

Because what's going to happen two years down the road when you need to get that code working on a different platform.

(In the old days, they called that thinking "all the world's a VAX", which became "all the world's linux" in the not-quite-as-old days)

Re:A critique of "How to C in 2016" by lultimouomo on Friday January 15, 2016 @09:23AM (#10909441)
> Because what's going to happen two years down the road when you need to get that code working on a different platform.

And what happens when the different platform handles overflow differently? Or has non-IEEE floats? Or breaks whatever assumption the C standard already makes?

Here's the thing: C is already built on assumptions, which might or might not hold for a specific platform.

Choosing which assumptions we hold true is a pragmatic choice that must be done. C does it, and does it consciously - but it doesn't mean that it nails every choice.

Re:A critique of "How to C in 2016" by GFK_of_xmaspast on Friday January 15, 2016 @01:42PM (#10911187)
If you're in a situation where you need specific implementation details of floating point arithmetic, then may the devil have mercy on your soul, because god certainly won't.
Re:A critique of "How to C in 2016" by Solarsail on Friday January 15, 2016 @03:46PM (#10912015)
Wouldn't debugging / testability imply this is a fairly large proportion of programs? Having a calculation give different results on different hardware would make testing difficult: http://yosefk.com/blog/consistency-how-to-defeat-the-purpose...
Re:A critique of "How to C in 2016" by GFK_of_xmaspast on Friday January 15, 2016 @04:01PM (#10912117)
Your own link explains how easy it is to get different results even on the same hardware depending on compiler flags.
Re:A critique of "How to C in 2016" by hamiltonkibbe on Friday January 15, 2016 @02:29PM (#10911501)
Maybe "poorly-written" was a bit strong, but if you're making that assumption, you are by definition relying on un- or implementation-defined behavior.

The fact that a char is stored on silicon in the lowest 8 bits of the smallest addressable memory space really makes no difference. char arrays act just like they do on any other platform.

There aren't a lot of libraries floating around that you'd be using on a DSP, aside from vendor-supplied ones. Generally you're using a DSP because you have specific signal processing needs and power and cost requirements, which are pretty orthogonal to the requirements of most desktop development. As such, there probably isn't a huge amount of overlap between the libraries you'd be using.

Another reason for that being that quite a bit of the time you're not running an OS. There is no transparent abstraction layer that magically maps an ADC IC connected via SPI to an arbitrary group of remappable peripheral pins to /dev/adc0. I think that's kind of the big disconnect between desktop and embedded, so much of the code is specific to the processor/DSP and the arbitrary external hardware connected to it. You can't take code written for a DsPIC that uses the internal ADC and run it on an MSP430 with an internal ADC for the same reason. There isn't necessarily a POSIX layer, so it's a very different beast than writing code that works on UNIX, Linux, and OSX. Look at the Linux source and see how many architecture-specific files there are in each architecture's folder. That's what you need to get a consistant interface across different processors. Without all that non-portable abstraction code running below yours, posix- and standard-compliant code still wouldn't be portable.

As far as using the same standard, I think it's a good thing that there is agreement on what the effect of a given construct is. I think the issue comes when you expect that code written for one platform to just work(TM) on another.

Re:A critique of "How to C in 2016" by snakeanus on Friday January 15, 2016 @09:33AM (#10909492)
ehmmmm standard C has floats and doubles. so they are valid C

>Why assuming that unsigned integers overflow to 0 is fair

because the standard says so

In your other posts I noticed some more misunderstanding of the C standard, so I suggest not to talk about things you are not aware about.

Re:A critique of "How to C in 2016" by lmm on Friday January 15, 2016 @10:33AM (#10909844)
> ehmmmm standard C has floats and doubles. so they are valid C

Sure, but the standard says hardly anything about their behaviour, making it practically impossible to write code that actually does anything with floats and doubles that will behave correctly on any standards-compliant C implementation.

I suggest you refrain from personal attacks, especially based on your own misreadings of the comment you're replying to.

Re:A critique of "How to C in 2016" by snakeanus on Friday January 15, 2016 @04:58PM (#10912495)
>but the standard says hardly anything about their behaviour, making it practically impossible to write code that actually does anything with floats and doubles that will behave correctly on any standards-compliant C implementation.

No, not really. This reminds me the common "you can't write anything serious in C without undefined behaviour" bs.

However, if you DO have a real need of IEEE-compatible floats for whatever reason, then there is nothing wrong with checking __STDC_IEC_559__ and reporting an error if it is not defined. In fact, if you are using a system that doesn't have an IEEE float implementation, using another language won't save you.

>I suggest you refrain from personal attacks, especially based on your own misreadings of the comment you're replying to.

Feel free to show me what I misread. I would also suggest that you try to read the nonsensical bullshit he has been spreading in this thread.

Re:A critique of "How to C in 2016" by lmm on Friday January 15, 2016 @07:15PM (#10913368)
> However, if you DO have a real need of IEEE-compatible floats for whatever reason, then there is nothing wrong with checking __STDC_IEC_559__ and reporting an error if it is not defined.

Sure - but in that case why not do the same thing for insisting that char be 8 bits?

Re:A critique of "How to C in 2016" by snakeanus on Friday January 15, 2016 @07:42PM (#10913501)
Except than the fact that it does not limit you except in some very specific cases, unlike the IEEE incompatible floats thing.

If it does limit you somehow and you really need char be 8 bits, then sure.

Re:A critique of "How to C in 2016" by dietrichepp on Friday January 15, 2016 @06:41AM (#10908626)
I think you're underestimating the amount of legacy code that people have. We're not talking about "I have a new processor, let's write code for it." That's unrealistic. It's more like, "I have a new processor, let's get our existing code base working on it, because if we don't, we're going to lose millions of dollars."
Re:A critique of "How to C in 2016" by lultimouomo on Friday January 15, 2016 @08:09AM (#10909048)
That doesn't touch my argument. If you're code is meant to run on DSPs it will already enforce all the relevant rules; if not, you'll have to port it anyway. It's not like your compiler backend that outputs code for DSPs will just compile standard C.
Re:A critique of "How to C in 2016" by hamiltonkibbe on Friday January 15, 2016 @02:43PM (#10911591)
All the code using non-optional c constructs will compile. If you're doing something that uses floating point math and expecting to run it on a 16-bit DSP, you're probably doing something wrong. The biggest hurdle isn't compiler support, rather converting all the floating point operations to fixed-point and ensuring that you have enough resolution such that rounding errors don't make your IIR filter unstable for example. That's really outside the scope of the discussion though, as that is just an aspect of practical DSP using fixed point math.
Re:A critique of "How to C in 2016" by clort on Friday January 15, 2016 @09:11AM (#10909361)
As a male human of questionable maturity, I'd just like to say that I am really looking forward to working with ternary digits!
Re:A critique of "How to C in 2016" by pcwalton on Friday January 15, 2016 @06:00AM (#10908473)
As a matter of fact, GCC extensions are de facto standards at this point. Clang had to copy most of them.

Same with bytes not being 8 bits. Nobody will ever make a system where that's not the case anymore. You'd break de facto compatibility with almost all C code for no reason. Market realities trump making systems "for fun".

Re:A critique of "How to C in 2016" by ue_ on Friday January 15, 2016 @06:09AM (#10908506)
>You'd break de facto compatibility with almost all C code for no reason.

The authors of that code should have written portable code if they didn't want it to break.

Re:A critique of "How to C in 2016" by lmm on Friday January 15, 2016 @10:37AM (#10909874)
If a few people write unportable code in your language, they're bad programmers. But if everyone writes unportable code in your language, it's a bad language.
Re:A critique of "How to C in 2016" by mondoshawan on Friday January 15, 2016 @07:55AM (#10908973)
Texhnically, they did. The go to compiler for years was GCC, with all the GNU extensions turned on. The problem is that those extensions were classically on by default, so it was very easy to just use them without thinking too much about it.
Re:A critique of "How to C in 2016" by pjmlp on Friday January 15, 2016 @09:27AM (#10909466)
On GNU/Linux, yes.

Outside world, there are tons of C compilers to chose from.

Re:A critique of "How to C in 2016" by pcwalton on Friday January 15, 2016 @12:59PM (#10910894)
If you're designing a new ISA, it's extremely difficult to succeed in the market no matter what you do. But one excellent way to guarantee you won't succeed is to have that attitude toward your developers.
Re:A critique of "How to C in 2016" by methyl on Friday January 15, 2016 @05:49AM (#10908437)
> People may wish to run code on "historical" systems, and someone may wish to create a 9-bit byte system for fun.

I bet there will be then bigger problems with porting the code than assuming byte is 8 bit long if someone is creating a 9-bit system "for fun".

Re:A critique of "How to C in 2016" by whyever on Friday January 15, 2016 @08:55AM (#10909272)
I would be very surprised if some non-trivial C code written for x86 compiles and runs unmodified on exotic platforms. Besides, I don't want to be hold back by people wishing to run code on historical or experimental systems.