lkml.org 
[lkml]   [2008]   [Mar]   [14]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [PATCH 01/10] Add macros similar to min/max/min_t/max_t.
Harvey Harrison wrote:
> On Fri, 2008-03-14 at 09:49 -0700, Randy Dunlap wrote:
>> Harvey Harrison wrote:
>>> On Fri, 2008-03-14 at 09:34 -0700, Randy Dunlap wrote:
>>>> On Thu, 13 Mar 2008 22:18:45 -0700 Harvey Harrison wrote:
>>>> Where is some blurb/comment about what "clamp" means/does?
>>>> min/max are well understood, but clamp? Is that a shop tool?
>>>> I think I have a few out in my garage.
>>> Sure, I'll do that..does kernel-doc actually work for macros?
>> Yes, it does.
>>
>
> OK, here's what I've come up with:
>
> /**
> * clamp - return a value clamped to a given range with strict typechecking
> * @val: current value
> * @min: minimum allowable value
> * @max: maximum allowable value
> *
> * This macro does strict typechecking of min/max to make sure they of the

they are of

> * same type as val. See the unnecessary pointer comparisons.
> */
> #define clamp(val, min, max) ({ \
> typeof(val) __val = (val); \
> typeof(min) __min = (min); \
> typeof(max) __max = (max); \
> (void) (&__val == &__min); \
> (void) (&__val == &__max); \
> __val = __val < __min ? __min: __val; \
> __val > __max ? __max: __val; })
>
> /**
> * clamp_t - return a value clamped to a given range using a given type
> * @type: the type of variable to use
> * @val: current value
> * @min: minimum allowable value
> * @max: maximum allowable value
> *
> * This macro does no typechecking and uses temporary variables of type
> * 'type' to make all the comparisons.
> */
> #define clamp_t(type, val, min, max) ({ \
> type __val = (val); \
> type __min = (min); \
> type __max = (max); \
> __val = __val < __min ? __min: __val; \
> __val > __max ? __max: __val; })
>
> /**
> * clamp_val - return a value clamped to a given range using val's type
> * @val: current value
> * @min: minimum allowable value
> * @max: maximum allowable value
> *
> * This macro does no typechecking and uses temporary variables of whatever
> * type the input argument 'val' is. This is useful when val is an unisgned

unsigned

> * type and min and max are literals that will otherwise be assigned a signed
> * integer type.
> */
> #define clamp_val(val, min, max) ({ \
> typeof(val) __val = (val); \
> typeof(val) __min = (min); \
> typeof(val) __max = (max); \
> __val = __val < __min ? __min: __val; \
> __val > __max ? __max: __val; })
>
> Comments? Please let me know if I got the kerneldoc format right.

Yes, it looks fine. Thanks.

--
~Randy


\
 
 \ /
  Last update: 2008-03-14 18:15    [W:0.034 / U:12.920 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site