AVR-LibC  2.2.0
Standard C library for AVR-GCC
 

AVR-LibC Documentation

Logo

AVR-LibC Development Pages

Main Page

User Manual

Library Reference

FAQ

Example Projects

File List

Loading...
Searching...
No Matches
parity.h
Go to the documentation of this file.
1/* Copyright (c) 2002, Marek Michalkiewicz
2 Copyright (c) 2004,2005,2007 Joerg Wunsch
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 * Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the
14 distribution.
15
16 * Neither the name of the copyright holders nor the names of
17 contributors may be used to endorse or promote products derived
18 from this software without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE. */
31
32/* $Id$ */
33
34#ifndef _UTIL_PARITY_H_
35#define _UTIL_PARITY_H_
36
37#include <stdint.h>
38
39#ifndef __DOXYGEN__
40#ifndef __ATTR_ALWAYS_INLINE__
41#define __ATTR_ALWAYS_INLINE__ __inline__ __attribute__((__always_inline__))
42#endif
43#endif /* !DOXYGEN */
44
45/** \file */
46/** \defgroup util_parity <util/parity.h>: Parity bit generation
47 \code #include <util/parity.h> \endcode
48
49 This header file contains optimized assembler code to calculate
50 the parity bit for a byte.
51*/
52/** \fn uint8_t parity_even_bit (uint8_t val);
53 \ingroup util_parity
54 \returns 1 if \c val has an odd number of bits set, and 0 otherwise. */
55
56static __ATTR_ALWAYS_INLINE__
58{
59 if (__builtin_constant_p (__builtin_parity (__val)))
60 return (uint8_t) __builtin_parity (__val);
61
62 __asm (/* parity is in [0..7] */
63 "mov __tmp_reg__, %0" "\n\t"
64 "swap __tmp_reg__" "\n\t"
65 "eor %0, __tmp_reg__" "\n\t"
66 /* parity is in [0..3] */
67 "subi %0, -4" "\n\t"
68 "andi %0, -5" "\n\t"
69 "subi %0, -6" "\n\t"
70 /* parity is in [0,3] */
71 "sbrc %0, 3" "\n\t"
72 "inc %0"
73 /* parity is in [0] */
74 : "+d" (__val) :: "r0");
75
76 return __val & 1;
77}
78
79#endif /* _UTIL_PARITY_H_ */
unsigned char uint8_t
Definition: stdint.h:83
static uint8_t parity_even_bit(uint8_t __val)
Definition: parity.h:57