staging: comedi: introduce some sample size manipulation functions
PATCH 1/1 staging: comedi: introduce some sample size manipulation functions
Introduce a few static inline helper functions:
comedi_bytes_per_sample(s) is the same as the existing bytes_per_sample(s) and determines the size of a comedi sample in bytes. (bytes_per_sample(s) will be removed.)
comedi_sample_shift(s) determines the log2 of the comedi sample size, so it can be used in bit-shift operations to multiply or divide by the sample size.
comedi_bytes_to_samples(s, nbytes) converts a number of bytes to a number of samples (rounding down).
comedi_samples_to_bytes(s, nsamples) converts a number of samples to a number of bytes.
--- drivers/staging/comedi/comedidev.h | 63 +++++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 4 deletions(-)
drivers/staging/comedi/comedidev.h +59 -4
@@ -391,12 +391,67 @@ static inline unsigned int comedi_offset_munge(struct comedi_subdevice *s,return val ^ s->maxdata ^ (s->maxdata >>1 ); } -static inline unsigned int bytes_per_sample (const struct comedi_subdevice *subd) +/** +* comedi_bytes_per_sample - determine subdevice sample size +* @s: comedi_subdevice struct +* +* The sample size will be 4 (sizeof int) or 2 (sizeof short) depending on +* whether the SDF_LSAMPL subdevice flag is set or not. +* +* Returns the subdevice sample size. +*/ +static inline unsigned int comedi_bytes_per_sample (struct comedi_subdevice *s) { -if (subd->subdev_flags &SDF_LSAMPL ) -return sizeof (unsigned int ); +return s->subdev_flags &SDF_LSAMPL ?sizeof (int ) :sizeof (short ); +} -return sizeof (short ); +/* to be removed */ +static inline unsigned int bytes_per_sample (struct comedi_subdevice *s) +{ +return comedi_bytes_per_sample (s); +} + +/** +* comedi_sample_shift - determine log2 of subdevice sample size +* @s: comedi_subdevice struct +* +* The sample size will be 4 (sizeof int) or 2 (sizeof short) depending on +* whether the SDF_LSAMPL subdevice flag is set or not. The log2 of the +* sample size will be 2 or 1 and can be used as the right operand of a +* bit-shift operator to multiply or divide something by the sample size. +* +* Returns log2 of the subdevice sample size. +*/ +static inline unsigned int comedi_sample_shift (struct comedi_subdevice *s) +{ +return s->subdev_flags &SDF_LSAMPL ?2 :1 ; +} + +/** +* comedi_bytes_to_samples - converts a number of bytes to a number of samples +* @s: comedi_subdevice struct +* @nbytes: number of bytes +* +* Returns the number of bytes divided by the subdevice sample size. +*/ +static inline unsigned int comedi_bytes_to_samples (struct comedi_subdevice *s, +unsigned int nbytes) +{ +return nbytes >>comedi_sample_shift (s); +} + +/** +* comedi_samples_to_bytes - converts a number of samples to a number of bytes +* @s: comedi_subdevice struct +* @nsamples: number of samples +* +* Returns the number of samples multiplied by the subdevice sample size. +* Does not check for arithmetic overflow. +*/ +static inline unsigned int comedi_samples_to_bytes (struct comedi_subdevice *s, +unsigned int nsamples) +{ +return nsamples <<comedi_sample_shift (s); }/*
Signature · 1 line
2.1.1
Quoted 34 lines · Ian Abbott
Introduce a few static inline helper functions:
comedi_bytes_per_sample(s)is the same as the existingbytes_per_sample(s)and determines the size of a comedi sample in bytes. (bytes_per_sample(s)will be removed.)
comedi_sample_shift(s)determines the log2 of the comedi sample size, so it can be used in bit-shift operations to multiply or divide by the sample size.
comedi_bytes_to_samples(s, nbytes)converts a number of bytes to a number of samples (rounding down).
comedi_samples_to_bytes(s, nsamples)converts a number of samples to a number of bytes.Signed-off-by Ian Abbott <[email protected]>--- drivers/staging/comedi/comedidev.h | 63 +++++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 4 deletions(-)drivers/staging/comedi/comedidev.h +4
-1@@ -391,12 +391,67 @@ static inline unsigned int comedi_offset_munge(struct comedi_subdevice *s,return val ^ s->maxdata ^ (s->maxdata >>1 ); } -static inline unsigned int bytes_per_sample (const struct comedi_subdevice *subd) +/** +* comedi_bytes_per_sample - determine subdevice sample size +* @s: comedi_subdevice struct +*
drivers/staging/comedi/comedidev.h static inline unsigned int comedi_offset_munge(struct comedi_subdevice *s, · line 405
Quoted 10 lines
drivers/staging/comedi/comedidev.h +7
-2+* The sample size will be 4 (sizeof int) or 2 (sizeof short) depending on +* whether the SDF_LSAMPL subdevice flag is set or not. +* +* Returns the subdevice sample size. +*/ +static inline unsigned int comedi_bytes_per_sample (struct comedi_subdevice *s) { -if (subd->subdev_flags &SDF_LSAMPL ) -return sizeof (unsigned int ); +return s->subdev_flags &SDF_LSAMPL ?sizeof (int ) :sizeof (short );
The samples are really 'unsigned' types but I guess it doesn't matter here.
drivers/staging/comedi/comedidev.h static inline unsigned int comedi_offset_munge(struct comedi_subdevice *s, · line 406
Quoted 1 line
drivers/staging/comedi/comedidev.h +1
-0+}