January 1996 routines for then employer Motorola containing bitcount(num) documented in the background section of United States patent number 8,560,586 B2, Linear bit counting implementations.
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Change Bits in Number
1. bi Kon Jan 22 17i18z09 1996 1
/ •
* Filei Utnan.c
* AuthorI Itoltln BelX
* Copyright (c) 1993, 199< by Hotorola, Inc.
«
« PleaB« r«£or to ch^ter 2«9 (pages 48-49} of
* "The C ProgruMBing L«ngu*go" Second Edition by
* Brian W. X^rnlghon mnA Dmmim M. Rlt^l«
* tor r«l«v«nt bicJcground oo »ost of theaa routlnaa.
*
* Itta aucbor walconas any coBaantm/i^provMants
* you'd Ilka to a>^rasa partainlog to thla coda,
* particularly aa thay raXata to Incraaalng tha
* apaad of any of tha xoutlnaa.
•/
•Includa <atdlo.h>
/•
* gatblt8{x»p*n*dapth)
* X • tha muabar froai ii^lch blta will be extracted
* p » tha leftaoat poaltlon fron %rhlch bits will be extracted
* n• tha nuiftbar of blta that will be extracted
* depth m neatlng level of currant function call
*
'arror ciMcklng/noznallzing ia done on p and n
*!
unclgnad 9«tbita(unslgned x« unalgnad p, unalgnad n« unalgnad depth)
(
If (p < (alxeo((x|• 81)
• If tn <.(p• 1))
It (n > 0)
/* The tollovlng line differs from the boo)c because t)te shift operator
ght
, but
rc2
ed)
te
Id
« and » use ((right operand) % (sizeof(unsigned) * ()) for ri
operands greater than 31 or less than zero (if signed, which
doesn't apply to thia routine aince all paraitatars are unsigned
does apply in the general case of the shift operators) on a spa
instead of returning a number with all zeroa or ahitted in the
opposite direction. The nodiflcation of the line, however, is
particularly relevant for n » 32 on a sparc2i
-0 « n should e(]ual zero in ny opinion instead of ffffffff.
nie case tor shitting with a right operand less than zero (sign
is probably best handled by a se[arate routine that changes the
direction of the shifting then uses the absolute value of
the right e^perand if this value la less than 32. It the absolu
value ot the right operand la greater than 31, the routine ahou
return zero (again, ny opinion...). */
return (n < (sizeot(x) * S>) 7 ((X » (p 4- 1 - n}) t -(~Q « n)) i (x
» (p • 1 - n))I
else
fprintf(Btderr, *n •• 0 ERROR f%d la geCbltsI^ngetbita computed
with n -• ln*, depth)>
return getblts(x, p, 1, depth -t- l)i
) .- -
else
<
fprintf(stderr. *n > (p » 1) ERROR ilcl'hln g^bitslngetblta computed w
ieh n «• p » 1 - %dn*, depth, p -> l)i
return getbitslx, p. p + 1. depthD)
' >felse
(
fprintf(stderr, 'p >• (sizeof(x) « 1) ERROR ltd in getbitslngetbits
puted with p •> Bizeof(x) * S - 1• *dn*, depth, aizeof(x) * t - i)>
return getblts(x. sizeot(x) *(- 1, n, depth t i)i
)
)
(define getbitsl(a,b.c) getbits((a),(b),(c),1)
/•
• changabit(x.p,n,depth)•
• X • tbe nuiibsr in which a bit will be changed
• p - the location of the bit within x that will be changed
• n • tha value that the bit will be changed to --> 0 orl
• depth > nesting level ot current function call
•
< error chedcing/nonnallzing is dona on p and n
•/
unsigned changeblt(unalgned x. unsigned p, unsigned n, unsigned depth)
(
it (p < (sizeof(x) * 8))
if ((n ..0) II (n .. D)
return (n »- 0) 7 (x k -(01 « p)) i (x t (01 « p))i
else
(
fprintf(stderr, *(n l« 0) (n l> 1) ERROR ltd in changebitinchangei
computed with OxOl'getbitsl|%02x,%d,1) - *dn*, depth, x, p, 0x01 * getbitsli
P, l))i
return changeblt(x, p, OxOl " getbital(x, p, 1), depth t l)i
1
else
(
fprintt(stderr, "p >• (slzeof(x) • «) ERROR l%d in changabitInchaogebit
oi«>uted with p Bizeot{x) * t - 1 • tdn*. depth, slzeof(x)*•-!)>
return c^angabit(x< aizeotlx) *• - 1, n, depth * i)i
)
)
•define changebiti(a,b,c) changabit((a),(b),(c),1)
/»
* satblts(x,p,n,y,depth]
* X • the nuaber in which bits will be changed
* p• the leftmost position within x of bits to be changed
* n• the lamber ot rightmost bits in y that bits in x beginning at p
* will be changed to
* y• the number that will be the source tor changed blta tn x
* depth • neating level ot current function call
• error chec)cing/noraalizlng ia done on p and n
unsigned unsigned x, unsigned p, unsigned n. untJ^jStK^.V^ W»#i|8>e<l'^ajjltShJj
int it
«
ynmm
•..m
y? s.-
it (P < (SiZeOf(X) • t|) iS- , i.<ii ' * #
if (n <- (Blzeof(y) *8)) .
it (n <- (p + 11) rfA*
it (n > 0)
tor (1• Oi i < ni i++) • >
X.changebltKx, p• 1 - n• i,
retum x» ^ 'Mi-igSfU
) " "'
else
(
fprintf(Btderr, 'n -- 0 ERROR l%d in setbitsinsatbits comput<
with n •> in*, depth)I
return setbits(x, p, 1, y, depth « 1)i
)