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