Question

I'm trying to monitor the CPU temperature and calculate the delta T in a Linux kernel module. I know not much about the kernel module but I'm using do_div() to divide with an integer. I don't understand why I always get a base 1. Here is my code:

deltaT = sfan_temp - sfan_temp_old;
remainder = do_div ( deltaT, sfan_temp );

My output is always deltaT = 1 and remainder = x. My desired output is deltaT = x and remainder = y. My formula for delta T is:

(sfan_temp-sfan_temp_old)/sfan_temp * 100; 

For example if sfan_temp = 75°C and sfan_temp_old = 65°C then

(75-65)/75*100 = 13.3333333
Was it helpful?

Solution

I don't know whether you should use do_div(). But if you use it then:

From div64.h:

// the semantics of do_div() macros are:
uint32_t do_div(uint64_t *n, uint32_t base) {
  uint32_t remainder = *n % base;
  *n = *n / base;
  return remainder;
} 

In your example:

n = 75 - 65 = 10
base = 75

// =>
remainder = 10 % 75 = 10
deltaT = n = 10 / 75 = 0

It is unclear how you can get 1 instead of 0 for deltaT in this case.

Apply *100 before do_div():

n = (sfan_temp - sfan_temp_old)*100;
remainder = do_div(n, sfan_temp)

// =>
remainder = 1000 % 75 = 25 // remainder/sfan_temp = 0.3333
n = 1000/75 = 13
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top