Skip to content

Commit

Permalink
math.h: Introduce data types for fractional numbers
Browse files Browse the repository at this point in the history
Introduce a macro to produce data types like

	struct TYPE_fract {
		__TYPE numerator;
		__TYPE denominator;
	};

to be used in the code wherever it's needed.

In the following changes convert some users to it.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Link: https://lore.kernel.org/r/20220126135353.24007-1-andriy.shevchenko@linux.intel.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
  • Loading branch information
Andy Shevchenko authored and Jonathan Cameron committed Jan 30, 2022
1 parent de645b2 commit e820a33
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/linux/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#ifndef _LINUX_MATH_H
#define _LINUX_MATH_H

#include <linux/types.h>
#include <asm/div64.h>
#include <uapi/linux/kernel.h>

Expand Down Expand Up @@ -106,6 +107,17 @@
} \
)

#define __STRUCT_FRACT(type) \
struct type##_fract { \
__##type numerator; \
__##type denominator; \
};
__STRUCT_FRACT(s16)
__STRUCT_FRACT(u16)
__STRUCT_FRACT(s32)
__STRUCT_FRACT(u32)
#undef __STRUCT_FRACT

/*
* Multiplies an integer by a fraction, while avoiding unnecessary
* overflow or loss of precision.
Expand Down

0 comments on commit e820a33

Please sign in to comment.