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