[v2,1/3] units: Add SI metric prefix definitions

Message ID 20210607152344.57458-1-andriy.shevchenko@linux.intel.com
State Superseded
Headers show
Series
  • [v2,1/3] units: Add SI metric prefix definitions
Related show

Commit Message

Andy Shevchenko June 7, 2021, 3:23 p.m.
Sometimes it's useful to have well-defined SI metric prefix to be used
to self-describe the formulas or equations.

List most popular ones in the units.h.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
v2: no changes
 include/linux/units.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

Comments

Andy Shevchenko June 7, 2021, 3:57 p.m. | #1
On Mon, Jun 07, 2021 at 08:43:02AM -0700, Joe Perches wrote:
> On Mon, 2021-06-07 at 18:23 +0300, Andy Shevchenko wrote:
> > Sometimes it's useful to have well-defined SI metric prefix to be used
> > to self-describe the formulas or equations.

...

> > +/* Metric prefixes in accordance with Système international (d'unités) */
> > +#define PETA	1000000000000000LL
> > +#define TERA	1000000000000LL
> > +#define GIGA	1000000000L
> > +#define MEGA	1000000L
> > +#define KILO	1000L
> > +#define HECTO	100L
> > +#define DECA	10L
> > +#define DECI	10L
> > +#define CENTI	100L
> > +#define MILLI	1000L
> > +#define MICRO	1000000L
> > +#define NANO	1000000000L
> > +#define PICO	1000000000000LL
> > +#define FEMTO	1000000000000000LL

> Somewhat surprisingly to me, this seems safe.
> 
> (though I suggest using UL and ULL rather than L and LL)

Okay.

> The only use of any of these seems to be:
> 
> sound/pcmcia/vx/vxp_ops.c:      [VX_MICRO]      = 0x0c,         // MICRO
> sound/pcmcia/vx/vxp_ops.c:              vx_outb(chip, MICRO, level);
> sound/pcmcia/vx/vxp_ops.c:                      vx_outb(chip, MICRO, vx_compute_mic_level(chip->mic_level));
> 
> and these vx_outb uses are themselves macros that prepend VX_ to the 2nd arg.

Is it a real issue there?

(Yes, I saw it, but I didn't check compilation in the assumption that units.h
 is not anyhow included in that file).

Okay, I have compiled it, no problems registered.
Jarkko Nikula June 8, 2021, 8:42 a.m. | #2
On 6/7/21 10:09 PM, Joe Perches wrote:
> On Mon, 2021-06-07 at 18:23 +0300, Andy Shevchenko wrote:

>> +#define HECTO 100L

>> +#define DECA 10L

> 

> Should it be DECA or DEKA or both?

> 

> https://www.nist.gov/pml/weights-and-measures/metric-si-prefixes

> deka Example: dekameter da 	10**1 	Ten

> 

>> +#define DECI	10L

>> +#define CENTI	100L

>> +#define MILLI	1000L

>> +#define MICRO	1000000L

>> +#define NANO	1000000000L

>> +#define PICO	1000000000000LL

>> +#define FEMTO	1000000000000000LL

> 

> IMO: Might as well include all the prefixes up to 10**24

> 

> EXA ZETTA YOTTA, ATTA ZEPTO YOCTO

> 

> And how do people avoid using MILLI for KILO, MEGA for MICRO, etc...

> The compiler won't care but usage could look odd.

> I find this patch creates more confusion than solves a real problem 

(typo where one zero is missing or one extra) and adds potentially 
needless churn. For instance I don't assume much use for the HECTO now 
or in the future.

How about if absolutely necessary just adding only KILO, MEGA and GIGA 
and questionable defines only after when the real needs arises and can 
see does "1/1000L" etc define cause any troubles to real calculations?

Jarkko
Andy Shevchenko July 12, 2021, 2:16 p.m. | #3
On Tue, Jun 08, 2021 at 11:42:46AM +0300, Jarkko Nikula wrote:
> On 6/7/21 10:09 PM, Joe Perches wrote:

> > On Mon, 2021-06-07 at 18:23 +0300, Andy Shevchenko wrote:

> > > +#define HECTO 100L

> > > +#define DECA 10L

> > 

> > Should it be DECA or DEKA or both?

> > 

> > https://www.nist.gov/pml/weights-and-measures/metric-si-prefixes

> > deka Example: dekameter da 	10**1 	Ten

> > 

> > > +#define DECI	10L

> > > +#define CENTI	100L

> > > +#define MILLI	1000L

> > > +#define MICRO	1000000L

> > > +#define NANO	1000000000L

> > > +#define PICO	1000000000000LL

> > > +#define FEMTO	1000000000000000LL

> > 

> > IMO: Might as well include all the prefixes up to 10**24

> > 

> > EXA ZETTA YOTTA, ATTA ZEPTO YOCTO

> > 

> > And how do people avoid using MILLI for KILO, MEGA for MICRO, etc...

> > The compiler won't care but usage could look odd.

> > I find this patch creates more confusion than solves a real problem

> (typo where one zero is missing or one extra) and adds potentially needless

> churn. For instance I don't assume much use for the HECTO now or in the

> future.

> 

> How about if absolutely necessary just adding only KILO, MEGA and GIGA and

> questionable defines only after when the real needs arises and can see does

> "1/1000L" etc define cause any troubles to real calculations?


You are probably looking to the I2C codebase, while I based my choice on other
sightings. I agree about the usage count for DECI/CENTI et al., but having them
defined targets at least the following:

 1) occupies namespace so if we need them in the future there will be no
    ambiguity;
 2) provides a name for multipliers for the code to read and understand the
    semantics better.

-- 
With Best Regards,
Andy Shevchenko

Patch

diff --git a/include/linux/units.h b/include/linux/units.h
index dcc30a53fa93..7366fcd45ec2 100644
--- a/include/linux/units.h
+++ b/include/linux/units.h
@@ -4,6 +4,22 @@ 
 
 #include <linux/math.h>
 
+/* Metric prefixes in accordance with Système international (d'unités) */
+#define PETA	1000000000000000LL
+#define TERA	1000000000000LL
+#define GIGA	1000000000L
+#define MEGA	1000000L
+#define KILO	1000L
+#define HECTO	100L
+#define DECA	10L
+#define DECI	10L
+#define CENTI	100L
+#define MILLI	1000L
+#define MICRO	1000000L
+#define NANO	1000000000L
+#define PICO	1000000000000LL
+#define FEMTO	1000000000000000LL
+
 #define MILLIWATT_PER_WATT	1000L
 #define MICROWATT_PER_MILLIWATT	1000L
 #define MICROWATT_PER_WATT	1000000L