1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_UNITS_H 3 #define _LINUX_UNITS_H 4 5 #include <linux/bits.h> 6 #include <linux/math.h> 7 8 /* Metric prefixes in accordance with Système international (d'unités) */ 9 #define PETA 1000000000000000ULL 10 #define TERA 1000000000000ULL 11 #define GIGA 1000000000UL 12 #define MEGA 1000000UL 13 #define KILO 1000UL 14 #define HECTO 100UL 15 #define DECA 10UL 16 #define DECI 10UL 17 #define CENTI 100UL 18 #define MILLI 1000UL 19 #define MICRO 1000000UL 20 #define NANO 1000000000UL 21 #define PICO 1000000000000ULL 22 #define FEMTO 1000000000000000ULL 23 24 #define NANOHZ_PER_HZ 1000000000UL 25 #define MICROHZ_PER_HZ 1000000UL 26 #define MILLIHZ_PER_HZ 1000UL 27 #define HZ_PER_KHZ 1000UL 28 #define KHZ_PER_MHZ 1000UL 29 #define HZ_PER_MHZ 1000000UL 30 31 #define MILLIWATT_PER_WATT 1000UL 32 #define MICROWATT_PER_MILLIWATT 1000UL 33 #define MICROWATT_PER_WATT 1000000UL 34 35 #define BYTES_PER_KBIT (KILO / BITS_PER_BYTE) 36 #define BYTES_PER_MBIT (MEGA / BITS_PER_BYTE) 37 #define BYTES_PER_GBIT (GIGA / BITS_PER_BYTE) 38 39 #define ABSOLUTE_ZERO_MILLICELSIUS -273150 40 milli_kelvin_to_millicelsius(long t)41static inline long milli_kelvin_to_millicelsius(long t) 42 { 43 return t + ABSOLUTE_ZERO_MILLICELSIUS; 44 } 45 millicelsius_to_milli_kelvin(long t)46static inline long millicelsius_to_milli_kelvin(long t) 47 { 48 return t - ABSOLUTE_ZERO_MILLICELSIUS; 49 } 50 51 #define MILLIDEGREE_PER_DEGREE 1000 52 #define MILLIDEGREE_PER_DECIDEGREE 100 53 kelvin_to_millicelsius(long t)54static inline long kelvin_to_millicelsius(long t) 55 { 56 return milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DEGREE); 57 } 58 millicelsius_to_kelvin(long t)59static inline long millicelsius_to_kelvin(long t) 60 { 61 t = millicelsius_to_milli_kelvin(t); 62 63 return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DEGREE); 64 } 65 deci_kelvin_to_celsius(long t)66static inline long deci_kelvin_to_celsius(long t) 67 { 68 t = milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DECIDEGREE); 69 70 return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DEGREE); 71 } 72 celsius_to_deci_kelvin(long t)73static inline long celsius_to_deci_kelvin(long t) 74 { 75 t = millicelsius_to_milli_kelvin(t * MILLIDEGREE_PER_DEGREE); 76 77 return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DECIDEGREE); 78 } 79 80 /** 81 * deci_kelvin_to_millicelsius_with_offset - convert Kelvin to Celsius 82 * @t: temperature value in decidegrees Kelvin 83 * @offset: difference between Kelvin and Celsius in millidegrees 84 * 85 * Return: temperature value in millidegrees Celsius 86 */ deci_kelvin_to_millicelsius_with_offset(long t,long offset)87static inline long deci_kelvin_to_millicelsius_with_offset(long t, long offset) 88 { 89 return t * MILLIDEGREE_PER_DECIDEGREE - offset; 90 } 91 deci_kelvin_to_millicelsius(long t)92static inline long deci_kelvin_to_millicelsius(long t) 93 { 94 return milli_kelvin_to_millicelsius(t * MILLIDEGREE_PER_DECIDEGREE); 95 } 96 millicelsius_to_deci_kelvin(long t)97static inline long millicelsius_to_deci_kelvin(long t) 98 { 99 t = millicelsius_to_milli_kelvin(t); 100 101 return DIV_ROUND_CLOSEST(t, MILLIDEGREE_PER_DECIDEGREE); 102 } 103 kelvin_to_celsius(long t)104static inline long kelvin_to_celsius(long t) 105 { 106 return t + DIV_ROUND_CLOSEST(ABSOLUTE_ZERO_MILLICELSIUS, 107 MILLIDEGREE_PER_DEGREE); 108 } 109 celsius_to_kelvin(long t)110static inline long celsius_to_kelvin(long t) 111 { 112 return t - DIV_ROUND_CLOSEST(ABSOLUTE_ZERO_MILLICELSIUS, 113 MILLIDEGREE_PER_DEGREE); 114 } 115 116 #endif /* _LINUX_UNITS_H */ 117