2010-10-23

Grand Rapids Day of DotNET Slides Available

Just got back from giving a talk at Day of .NET in Grand Rapids entitled "Designing Application Protocols for TCP/IP". I gave the same talk last year at BarCampGR, but this time I removed some of the introductory information and added an introduction to the Socket API at the end.

I did forget to mention during my talk that juggling multithreading concerns as well as TCP/IP concerns can be very challenging. I wrote some simple socket wrapper classes as part of the Nito.Async library (soon to be moved to the Nito.Communication library). These wrappers take care of all the multithreading concerns, so you can just focus on the TCP/IP concerns.

Slides are available here (thanks to Landmark Baptist Church for hosting).

2010-10-18

Implementing GCC's Builtin Functions

GCC has a number of useful builtin functions, which translate directly to the appropriate assembly instruction if the processor supports it. A certain algorithm I was coding made use of a few of these: __builtin_ffs (find first set bit), __builtin_clz (count leading zero bits), and __builtin_ctz (count trailing zero bits).

In theory, if the target processor does not support these instructions (like mine), then the gcc library for that target should implement them in software. Unfortunately, mine did not.

The solution was surprisingly simple: I just had to implement the expected functions myself. The mapping is fairly obvious (e.g., __builtin_clz is implemented by __clzsi2).

By adding the following code to the project, I was able to build the algorithm using __builtin_clz, __builtin_ctz, and __builtin_ffs:

// Returns the number of leading 0-bits in x, starting at the most significant bit position.
// If x is zero, the result is undefined.
int __clzsi2(unsigned x);
int __clzsi2(unsigned x)
{
  // This uses a binary search (counting down) algorithm from Hacker's Delight.
   unsigned y;
   int n = 32;
   y = x >>16;  if (y != 0) {n = n -16;  x = y;}
   y = x >> 8;  if (y != 0) {n = n - 8;  x = y;}
   y = x >> 4;  if (y != 0) {n = n - 4;  x = y;}
   y = x >> 2;  if (y != 0) {n = n - 2;  x = y;}
   y = x >> 1;  if (y != 0) return n - 2;
   return n - x;
}

// Returns the number of trailing 0-bits in x, starting at the least significant bit position.
// If x is zero, the result is undefined.
int __ctzsi2(unsigned x);
int __ctzsi2(unsigned x)
{
  // This uses a binary search algorithm from Hacker's Delight.
  int n = 1;
  if ((x & 0x0000FFFF) == 0) {n = n +16; x = x >>16;}
  if ((x & 0x000000FF) == 0) {n = n + 8; x = x >> 8;}
  if ((x & 0x0000000F) == 0) {n = n + 4; x = x >> 4;}
  if ((x & 0x00000003) == 0) {n = n + 2; x = x >> 2;}
  return n - (x & 1);
}

// Returns the index of the least significant 1-bit in x, or the value zero if x is zero.
// The least significant bit is index one.
int __ffsdi2 (unsigned x);
int __ffsdi2 (unsigned x)
{
  return (x == 0) ? 0 : __builtin_ctz(x) + 1;
}

Presumably, this same approach would work for the many other GCC builtins.

2010-10-15

Firmware???

This is a meta-blog entry.

I've been rather silent the last few weeks. There's a reason for that: a couple of months ago I became a "firmware guy."

Firmware???

Flexibility. It's a wonderful thing.

The last few weeks have been an intense learning period, and I didn't want to bother my blog readers with the details of deriving clock speeds from oscillating crystals, or flash wait states, or the proper design of a bootloader that can flash its own upgrade without the danger of leaving the system in an unusable state. Yes, I've been doing all this (starting from an existing code base, thank goodness!).

I am still doing .NET development as well. However, all of the .NET work I'm doing is not groundbreaking or really interesting, so I haven't been posting about it. It's just boring stuff like extending Nito.Async.Sockets to support Silverlight and the Compact Framework. There's only two interesting points in that project (which I'll blog about when the time comes); it's mostly just grunt work.

Now that my firmware learning curve is pretty much complete, I'll be inserting firmware content into the blog as well. Naturally, there will still be some .NET content interspersed with the new firmware content. Almost every day now, I write both C and .NET code (and even assembly if I can't avoid it).

Upcoming Stuff In My .NET World

I'll be speaking at Day of .NET in Grand Rapids next week on TCP/IP socket protocols.

It looks like Nito.Async will get a release around the turn of the year. The last release (in September of 2009) has been very stable; I'll just be spinning off Nito.Async.Sockets into a separate library, putting up some online documentation, and possibly adding Code Contracts.

I also have a special .NET-related surprise coming in February of 2011. More on that when the time comes. :)