1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/math.h>
7 /* Metric prefixes in accordance with Système international (d'unités) */
8 #define PETA 1000000000000000ULL
9 #define TERA 1000000000000ULL
10 #define GIGA 1000000000UL
11 #define MEGA 1000000UL
18 #define MICRO 1000000UL
19 #define NANO 1000000000UL
20 #define PICO 1000000000000ULL
21 #define FEMTO 1000000000000000ULL
23 #define HZ_PER_KHZ 1000UL
24 #define KHZ_PER_MHZ 1000UL
25 #define HZ_PER_MHZ 1000000UL
27 #define MILLIWATT_PER_WATT 1000UL
28 #define MICROWATT_PER_MILLIWATT 1000UL
29 #define MICROWATT_PER_WATT 1000000UL
31 #define ABSOLUTE_ZERO_MILLICELSIUS -273150
33 static inline long milli_kelvin_to_millicelsius(long t)
35 return t + ABSOLUTE_ZERO_MILLICELSIUS;
38 static inline long millicelsius_to_milli_kelvin(long t)
40 return t - ABSOLUTE_ZERO_MILLICELSIUS;
43 #define MILLIDEGREE_PER_DEGREE 1000
44 #define MILLIDEGREE_PER_DECIDEGREE 100
46 static inline long kelvin_to_millicelsius(long t)
48 return milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DEGREE);
51 static inline long millicelsius_to_kelvin(long t)
53 t = millicelsius_to_milli_kelvin(t);
55 return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DEGREE);
58 static inline long deci_kelvin_to_celsius(long t)
60 t = milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DECIDEGREE);
62 return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DEGREE);
65 static inline long celsius_to_deci_kelvin(long t)
67 t = millicelsius_to_milli_kelvin(t * MILLIDEGREE_PER_DEGREE);
69 return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DECIDEGREE);
73 * deci_kelvin_to_millicelsius_with_offset - convert Kelvin to Celsius
74 * @t: temperature value in decidegrees Kelvin
75 * @offset: difference between Kelvin and Celsius in millidegrees
77 * Return: temperature value in millidegrees Celsius
79 static inline long deci_kelvin_to_millicelsius_with_offset(long t, long offset)
81 return t * MILLIDEGREE_PER_DECIDEGREE - offset;
84 static inline long deci_kelvin_to_millicelsius(long t)
86 return milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DECIDEGREE);
89 static inline long millicelsius_to_deci_kelvin(long t)
91 t = millicelsius_to_milli_kelvin(t);
93 return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DECIDEGREE);
96 static inline long kelvin_to_celsius(long t)
98 return t + DIV_ROUND_CLOSEST(ABSOLUTE_ZERO_MILLICELSIUS,
99 MILLIDEGREE_PER_DEGREE);
102 static inline long celsius_to_kelvin(long t)
104 return t - DIV_ROUND_CLOSEST(ABSOLUTE_ZERO_MILLICELSIUS,
105 MILLIDEGREE_PER_DEGREE);
108 #endif /* _LINUX_UNITS_H */