Page 1 of 1

GPL Question

Posted: Fri Sep 02, 2011 10:49 pm
by kingliveson
A program is not derived or based on GPLed Stockfish, is it possible without violating its license to lift the following function?

Code: Select all

inline void __cpuid(int CPUInfo[4], int InfoType)
{
  int* eax = CPUInfo + 0;
  int* ebx = CPUInfo + 1;
  int* ecx = CPUInfo + 2;
  int* edx = CPUInfo + 3;

  *eax = InfoType;
  *ecx = 0;
  __asm__("cpuid" : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
                  : "0" (*eax), "2" (*ecx));
}

Re: GPL Question

Posted: Fri Sep 02, 2011 11:25 pm
by hyatt
I would think that is perfectly OK. You are copying exactly one x86 assembly language instruction, "cpuid". I don't think anyone would make an issue of that. In fact, it is likely that that code came from some other place anyway, using that to discover everything from # of cores to cache size and such is quite common...

Re: GPL Question

Posted: Sat Sep 03, 2011 11:02 pm
by kingliveson
Yes, I would have to agree. In the case of the above function, ecx register is right shifted 23 bits to see if it is set and ANDed with 1, for SSE4a (population count) instruction support. The snippet is pretty much generic as there aren't really many other ways of calling cpuid opcode. A slight modification for vendor checking:

Code: Select all

#include <stdio.h>
#include <string.h>

int main ()
{

  char VendorID[13];

  int CPUInfo[4] = {-1};
  int InfoType = 0x00000000;

  int* eax = CPUInfo + 0;
  int* ebx = CPUInfo + 1;
  int* ecx = CPUInfo + 2;
  int* edx = CPUInfo + 3;

  *eax = InfoType;
  *ecx = 0;

  {__asm__("cpuid" : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
                  : "0" (*eax) , "1" (*ebx) , "2" (*ecx) , "3" (*edx));
  }

  strncpy(VendorID, (char*) ebx, 4);
  strncat(VendorID, (char*) edx, 4);
  strncat(VendorID, (char*) ecx, 4);

  printf("Vendor ID: %s\n", VendorID);

  return 0;
}