3

I searched a lot of other questions inside StackOverflow, but none of them really solved my problem.

I am writing a linux kernel module and I need to compute a percentage value by diving an integer number by another integer number in order to get a float value ranging between 0 and 100:

int v1 = 5;
int v2 = 25;
float perc = v1 / v2;

For all the reasons we already know, when I try to compile it I get the "SSE register return with SSE disabled" error.

Is there a workaround to compute such division inside a Linux Kernel Module?

Thank you so much. Antonio

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74

1 Answers1

8

You can just use integer arithmetic, e.g.

int perc = 100 * v1 / v2;

This will give an integer percentage. If you need higher resolution than 1% then use a scale factor larger than 100 and insert a decimal point for display purposes as required.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 2
    +1 The right way to do floating point in the kernel is to not do it. OP: Is there a reason why you must do this in floating point? – tangrs Jun 14 '17 at 08:10
  • FOP is not a must, the workaround provided @Paul solved the problem. – Antonio Petricca Jun 14 '17 at 08:21
  • 1
    Just in case someone has not yet googled why floating point operations are not allowed in kernel space, here is a mailing list thread which tries to explain it. http://www.spinics.net/lists/newbies/msg30262.html – vtha Jun 15 '17 at 05:54