4

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?

Ivan Kovacevic
  • 1,322
  • 12
  • 30
  • Maybe some four char codes? – Macmade Mar 11 '14 at 20:13
  • 1
    See FourCC: http://en.m.wikipedia.org/wiki/FourCC – Macmade Mar 11 '14 at 20:14
  • I always thought of FourCC codes as video codec identifiers. So this is definitely new to me... As it says on Wikipedia "The concept originated in the OSType scheme used in the Macintosh system software". So that probably explains why these data types are identified by four characters. However I still don't know anything about mentioned types: fpe2, sp78 and others... – Ivan Kovacevic Mar 11 '14 at 20:27
  • 1
    I am the maintainer of the repo you referenced and would like to know the answer as well. I am sorry, but I have no idea, I just extracted code from others (I just updated the README.md) However, you could check these for more info: https://github.com/search?q=%22%23define+DATATYPE_SP78%22&type=Code – sebastien Mar 16 '14 at 18:11

1 Answers1

11

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.

Alexander
  • 59,041
  • 12
  • 98
  • 151
Chris J. Kiick
  • 1,487
  • 11
  • 14
  • How can I make the reverse of it ? I mean to convert int to sp78 I tired to shift left by 2 and print as %x (Didn't work ) :( – Coldsteel48 Mar 15 '15 at 02:11
  • This might help you https://github.com/beltex/SMCKit/blob/master/SMCKit/SMC.swift – johndpope Nov 30 '15 at 23:16
  • 1
    I'd point out that these are "fixed" point representations of real numbers. Floating point numbers normally have some sort of exponent, which moves the integer/fraction point up or down (hence "floating"). See for example https://stackoverflow.com/a/7524916/446767 – Ted Jul 30 '17 at 17:35