It turns out that the int_sqrt() function in oom_kill.c gets it wrong.
But fb_sqrt() in fbmon.c gets its math right. Move that function into
lib/int_sqrt.c, and consolidate.
(oom_kill.c fix from Thomas Schlichter <schlicht@uni-mannheim.de>)
u32 vtotal;
};
-/*
- * a simple function to get the square root of integers
- */
-static u32 fb_sqrt(int x)
-{
- register int op, res, one;
-
- op = x;
- res = 0;
-
- one = 1 << 30;
- while (one > op) one >>= 2;
-
- while (one != 0) {
- if (op >= res + one) {
- op = op - (res + one);
- res = res + 2 * one;
- }
- res /= 2;
- one /= 4;
- }
- return((u32) res);
-}
-
/**
* fb_get_vblank - get vertical blank time
* @hfreq: horizontal freq
h_period += (M_VAL * xres * 2 * 1000)/(5 * dclk);
h_period *=10000;
- h_period = fb_sqrt((int) h_period);
+ h_period = int_sqrt(h_period);
h_period -= (100 - C_VAL) * 100;
h_period *= 1000;
h_period /= 2 * M_VAL;
asmlinkage int printk(const char * fmt, ...)
__attribute__ ((format (printf, 1, 2)));
+unsigned long int_sqrt(unsigned long);
+
static inline void console_silent(void)
{
console_loglevel = 0;
lib-y := errno.o ctype.o string.o vsprintf.o cmdline.o \
bust_spinlocks.o rbtree.o radix-tree.o dump_stack.o \
- kobject.o idr.o div64.o parser.o
+ kobject.o idr.o div64.o parser.o int_sqrt.o
lib-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o
lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o
--- /dev/null
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+
+/**
+ * int_sqrt - rough approximation to sqrt
+ * @x: integer of which to calculate the sqrt
+ *
+ * A very rough approximation to the sqrt() function.
+ */
+unsigned long int_sqrt(unsigned long x)
+{
+ unsigned long op, res, one;
+
+ op = x;
+ res = 0;
+
+ one = 1 << 30;
+ while (one > op)
+ one >>= 2;
+
+ while (one != 0) {
+ if (op >= res + one) {
+ op = op - (res + one);
+ res = res + 2 * one;
+ }
+ res /= 2;
+ one /= 4;
+ }
+ return res;
+}
+EXPORT_SYMBOL(int_sqrt);
/* #define DEBUG */
-/**
- * int_sqrt - oom_kill.c internal function, rough approximation to sqrt
- * @x: integer of which to calculate the sqrt
- *
- * A very rough approximation to the sqrt() function.
- */
-static unsigned int int_sqrt(unsigned int x)
-{
- unsigned int out = x;
- while (x & ~(unsigned int)1) x >>=2, out >>=1;
- if (x) out -= out >> 2;
- return (out ? out : 1);
-}
-
/**
* oom_badness - calculate a numeric value for how bad this task has been
* @p: task struct of which task we should calculate
static int badness(struct task_struct *p)
{
- int points, cpu_time, run_time;
+ int points, cpu_time, run_time, s;
if (!p->mm)
return 0;
cpu_time = (p->utime + p->stime) >> (SHIFT_HZ + 3);
run_time = (get_jiffies_64() - p->start_time) >> (SHIFT_HZ + 10);
- points /= int_sqrt(cpu_time);
- points /= int_sqrt(int_sqrt(run_time));
+ s = int_sqrt(cpu_time);
+ if (s)
+ points /= s;
+ s = int_sqrt(int_sqrt(run_time));
+ if (s)
+ points /= s;
/*
* Niced processes are most likely less important, so double