Page MenuHomeFreeBSD

sound: Turn PCM_CLAMP_* macros into a function
Needs ReviewPublic

Authored by christos on Fri, Jan 10, 6:36 PM.
Tags
None
Referenced Files
F109367486: D48421.diff
Tue, Feb 4, 3:41 AM
Unknown Object (File)
Sat, Feb 1, 4:02 PM
Unknown Object (File)
Fri, Jan 31, 5:48 AM
Unknown Object (File)
Fri, Jan 31, 4:34 AM
Unknown Object (File)
Fri, Jan 31, 2:34 AM
Unknown Object (File)
Thu, Jan 30, 7:55 AM
Unknown Object (File)
Sun, Jan 19, 6:20 AM
Unknown Object (File)
Sat, Jan 18, 8:21 AM
Subscribers

Details

Summary

This makes some subsequent feeder refactors easier to implement.

Sponsored by: The FreeBSD Foundation
MFC after: 1 week

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Skipped
Unit
Tests Skipped
Build Status
Buildable 61653
Build 58537: arc lint + arc unit

Event Timeline

sys/dev/sound/pcm/pcm.h
339

Not sure if we should return 0 or just sample unmodified.

This currently shifts 32bit samples out of the 32bit range in case SND_PCM_64 is not set. sample << PCM_FXSHIFT happens twice. The root cause is in D47932 though.

sys/dev/sound/pcm/pcm.h
316

To match how pcm_clamp() is used in the macros, and make it 64bit in case SND_PCM_64 is set (yes, bad type name).

333–335

This doesn't make sense to check for a 32bit signed int (typedef int32_t intpcm_t;). Also it's not what the original clamp checks if SND_PCM_64 is not set.

339

I think this code should never be executed anyway. Means we can return 0, but please put an assertion here.

christos added inline comments.
sys/dev/sound/pcm/pcm.h
333–335

I did fix the clamp checks, but I'm not sure what you mean by "to check for a 32bit signed int".

sys/dev/sound/pcm/pcm.h
333–335

That was a tautology, you can forget about it if you fix the clamp checks and the type of the sample argument. It was just the first thing to cross my mind, that a 32bit signed integer cannot exceed 32bit signed max and min values, and thus the check doesn't make sense.

I have a proposal to simplify this code, may be worth considering. The general idea would be to

  • Use normalized, full 32bit magnitude values for calculation.
  • In feeder_mixer.c, for 32bit calculation, check potential overflow before doing the addition, substitute PCM_S32_MIN or PCM_S32_MAX if it would overflow.
  • In feeder_volume.c, always use 64bit multiplication but make it clear to the compiler that the volume value is 16bit.

For the mixer this just takes a bit more logic on 32bit architectures, and for the volume multiplying a 16bit sample with a 16bit volume would now be two multiplication ops. On the positive side, we can always maintain the full 32bit resolution, and get rid of the whole *_calc() sample handling.

Does that make sense?

sys/dev/sound/pcm/pcm.h
333–335

This is still incorrect in case of 32bit calculations. The macro checks for 24bit min and max values in that case, and we're missing the shift back to 32bit magnitude before writing the sample value. But there may be a better solution, see my general comment.