Antonio Petricca
Antonio Petricca

Reputation: 11040

Floating point operations in linux kernel module (again)

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

Upvotes: 3

Views: 2752

Answers (1)

Paul R
Paul R

Reputation: 213120

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.

Upvotes: 8

Related Questions