units.h 2.7 KB

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