Subject: Re: [PATCH v3] cpuidle: Fix last_residency division

From: Shreyas B Prabhu <[email-masked]>
Date: Wed, 29 Jun 2016 14:29:36 +0530

>>
>> +/*
>> + * Used for calculating last_residency in usec. Optimized for case
>> + * where last_residency in nsecs is < INT_MAX/2 by using faster
>> + * approximation. Approximated value has less than 1% error.
>> + */
>> +static inline int convert_nsec_to_usec(u64 nsec)
>> +{
>> +    if (likely(nsec < INT_MAX / 2)) {
> 
> UINT_MAX ?

I don't think I can use UINT_MAX here since usec += usec >> 5 can
overflow. Also using INT_MAX / 2 instead of INT_MAX since potentially
usec += usec >> 5 can be negative and usec >> 10 will retain the sign bit.

> 
>> +        int usec = (int)nsec;
>> +
>> +        usec += usec >> 5;
>> +        usec = usec >> 10;
>> +        return usec;
>> +    } else {
>> +        u64 usec = div_u64(nsec, 1000);
>> +
>> +        if (usec > INT_MAX)
>> +            usec = INT_MAX;
>> +        return (int)usec;
>> +    }
>> +}
> 

Thanks,
Shreyas