Question

I've been analysing the code needed to get CPU temperature and CPU fan speed on Mac OS X. There are many examples out there. Here is one of them:

https://github.com/lavoiesl/osx-cpu-temp

Now, in the smc.h file there are some strange(to me) data types defined:

#define DATATYPE_FPE2         "fpe2"

#define DATATYPE_SP78         "sp78"

These are data types that later Apple's IOKit writes in memory as a return value, and that then need to be converted to something usable. The author of the code does it like so (Note that he made a typo writing fp78 instead sp78 in comments...):

// convert fp78 value to temperature
int intValue = (val.bytes[0] * 256 + val.bytes[1]) >> 2;
return intValue / 64.0;

What I find mind boggling is that I'm unable to find any note about these two codes fpe2 and sp78, beside in unofficial code examples for accessing temp and fan readings on a Mac.

Does anyone here know how would one ever figure this out on his own, about these codes?! And basically can someone point me out to some documentation about this and/or explain here what those data types are?

Was it helpful?

Solution

While there doesn't seem to be any "official" documentation of these type names, they are generic enough to figure out.

FP = Floating point, unsigned.

SP = floating point, signed.

The last two (hex) digits indicate the integer/fraction bits. The total tells us that the value fits into 16 bits.

So: FPE2 = floating point, unsigned, 14 (0xE) bits integer, 2 (0x2) bits fraction.

15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
I  I  I  I  I  I  I  I  I  I  I  I  I  I  F  F

The SP values have the added complication of a sign bit.

15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
S  I  I  I  I  I  I  I  F  F  F  F  F  F  F  F

To convert these values to integers, discard the F bits (by shifting) and cast to an integer type. Be careful with the sign bit on the SP values, whether or not the sign is preserved depends on the type you are shifting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top