% Copyright 2008 by Mark Wibrow
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.


\newif\ifpgfgateanchorsuseboundingrectangle%


% Keys for US/CDH logic gates.
%
\pgfkeys{/pgf/.cd,%
	logic gate input sep/.initial=0.125cm,
	logic gate inputs/.initial={normal,normal},%
	logic gate inverted radius/.initial=2pt,%
	logic gate anchors use bounding box/.is if=pgfgateanchorsuseboundingrectangle,%
}

% An internal macro for calculating internal dimensions of 
% the US + CDH gates (excluding and gate US and nand gate US).
%
\def\pgf@lib@sh@logicgates@dimensions@orUS{%
	\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/inner xsep}}%
	\advance\pgf@x.5\wd\pgfnodeparttextbox%
	\pgfmathsetlength\pgf@y{\pgfkeysvalueof{/pgf/inner ysep}}%
	\advance\pgf@y.5\ht\pgfnodeparttextbox%
	\advance\pgf@y.5\dp\pgfnodeparttextbox%
	%
	% Adjust the height for the number of inputs.
	%
	\c@pgf@counta\pgf@lib@sh@logicgate@numinputs%
	\advance\c@pgf@counta1\relax%
	\pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
	\advance\pgf@ya\pgflinewidth%
	\pgfmathsetlength\pgf@yb{\pgfkeysvalueof{/pgf/logic gate input sep}}%
	\pgf@yb.5\pgf@yb%
	\ifdim\pgf@ya<\pgf@yb%
		\pgf@ya\pgf@yb%
	\fi%	
	\multiply\pgf@ya\c@pgf@counta%
	\ifdim\pgf@y<\pgf@ya%
		\pgf@y\pgf@ya%
	\fi%
	%
	\ifdim\pgf@x>\pgf@y%
		\pgf@y\pgf@x%
	\else%
		\pgf@x\pgf@y%
	\fi%
	%
	% Adjust for minimum height and width.
	%
	\pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/minimum width}}%
	\pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/pgf/minimum height}}%
	\ifdim\pgf@y<.5\pgf@ya%
		\pgf@y.5\pgf@ya%
	\fi%
	\pgf@xb\pgf@x%
	\advance\pgf@xb1.732051\pgf@y% 2y*cos(30)
	\ifdim\pgf@xb<\pgf@xa%
		\pgf@xb\pgf@xa%
		\pgf@x0.366025\pgf@xb% xb / (1 + 2*cos(30))
	\fi%
	%
	\ifdim\pgf@x>\pgf@y%
		\pgf@y\pgf@x%
	\else%
		\pgf@x\pgf@y%
	\fi%
	\edef\halfside{\the\pgf@x}%
	\addtosavedmacro\halfside%
	%
	% Take into account the outer sep.
	%
	\pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/outer xsep}}%
	\pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/pgf/outer ysep}}%
	\edef\outerxsep{\the\pgf@xa}%
	\edef\outerysep{\the\pgf@ya}%
	\addtosavedmacro\outerxsep%
	\addtosavedmacro\outerysep%
	\advance\pgf@x\pgf@xa%
	\advance\pgf@y\pgf@ya%
	%
	\edef\halfwidth{\the\pgf@x}%
	\edef\halfheight{\the\pgf@y}%
	\addtosavedmacro\halfwidth%
	\addtosavedmacro\halfheight%
	%
	\pgfextract@process\tipanchor{%
		\advance\pgf@x-\pgf@xa%
		\advance\pgf@y-\pgf@ya%	
		\pgf@x-.16666\pgf@x%
		\pgf@yb2.0\pgf@y%
		\advance\pgf@x.866025\pgf@yb%	
		\advance\pgf@x1.154701\pgf@xa%
		\pgf@y0pt%
	}%
	\addtosavedmacro\tipanchor%		
}%


% An internal macro for calculating internal dimensions of 
% the and gate US and nand gate US.
%
\def\pgf@lib@sh@logicgates@dimensions@andUS{%
	\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/inner xsep}}%
	\advance\pgf@x.5\wd\pgfnodeparttextbox%
	\pgfmathsetlength\pgf@y{\pgfkeysvalueof{/pgf/inner ysep}}%
	\advance\pgf@y.5\ht\pgfnodeparttextbox%
	\advance\pgf@y.5\dp\pgfnodeparttextbox%
	%
	% Adjust the height for the number of inputs.
	%
	\c@pgf@counta\pgf@lib@sh@logicgate@numinputs%
	\advance\c@pgf@counta1\relax%
	\pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
	\advance\pgf@ya\pgflinewidth%
	\pgfmathsetlength\pgf@yb{\pgfkeysvalueof{/pgf/logic gate input sep}}%
	\pgf@yb.5\pgf@yb%
	\ifdim\pgf@ya<\pgf@yb%
		\pgf@ya\pgf@yb%
	\fi%	
	\multiply\pgf@ya\c@pgf@counta%
	\ifdim\pgf@y<\pgf@ya%
		\pgf@y\pgf@ya%
	\fi%
	%
	\ifdim\pgf@x>\pgf@y%
		\pgf@y\pgf@x%
	\else%
		\pgf@x\pgf@y%
	\fi%
	%
	% Adjust for minimum height and width.
	%
	\pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/minimum width}}%
	\pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/pgf/minimum height}}%
	\ifdim\pgf@y<.5\pgf@ya%
		\pgf@y.5\pgf@ya%
	\fi%
	\pgf@xb2.5\pgf@x%
	\ifdim\pgf@xb<\pgf@xa%
		\pgf@xb\pgf@xa%
		\pgf@x0.4\pgf@xb% 
	\fi% 
	%
	\ifdim\pgf@x>\pgf@y%
		\pgf@y\pgf@x%
	\else%
		\pgf@x\pgf@y%
	\fi%
	\edef\halfside{\the\pgf@x}%
	\addtosavedmacro\halfside%
	%
	% Take into account the outer sep.
	%
	\pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/outer xsep}}%
	\pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/pgf/outer ysep}}%
	\edef\outerxsep{\the\pgf@xa}%
	\edef\outerysep{\the\pgf@ya}%
	\addtosavedmacro\outerxsep%
	\addtosavedmacro\outerysep%
	\advance\pgf@x\pgf@xa%
	\advance\pgf@y\pgf@ya%
	%
	\edef\halfwidth{\the\pgf@x}%
	\edef\halfheight{\the\pgf@y}%
	\addtosavedmacro\halfwidth%
	\addtosavedmacro\halfheight%
	%
}%


% Internal macro for parsing inputs.
%
\expandafter\ifx\csname pgf@lib@sh@logicgate@parseinputs\endcsname\relax%
\def\pgf@lib@sh@logicgate@parseinputs#1{%
	\edef\pgf@lib@sh@temp{\pgfkeysvalueof{/pgf/logic gate inputs}}%
	\c@pgf@counta#1\relax%
	\c@pgf@countb0\relax%
	\expandafter\pgfutil@in@\expandafter,\expandafter{\pgf@lib@sh@temp}%
	\ifpgfutil@in@%
		\let\pgf@lib@sh@next\pgf@lib@sh@logicgate@parseinputs@long%
	\else%
		\let\pgf@lib@sh@next\pgf@lib@sh@logicgate@parseinputs@short%
	\fi%
	\pgf@lib@sh@next%
}	
\def\pgf@lib@sh@itext{i}
\def\pgf@lib@sh@invertedtext{inverted}
%
%	The `short' version for input specifcation is an extension of
%	ideas due to Juergen Werber and Christoph Bartoschek.
%
\def\pgf@lib@sh@logicgate@parseinputs@short{%
	\expandafter\pgf@lib@sh@logicgate@parseinputs@@short\pgf@lib@sh@temp\pgf@stop%
}
\def\pgf@lib@sh@logicgate@parseinputs@@short#1{%
	\ifx#1\pgf@stop%
		\edef\pgf@lib@sh@logicgate@numinputs{\the\c@pgf@countb}%
		\let\pgf@lib@sh@next\relax%
	\else%
		\ifnum\c@pgf@countb=\c@pgf@counta%
			\edef\pgf@lib@sh@logicgate@numinputs{\the\c@pgf@countb}%
			\let\pgf@lib@sh@next\relax%
		\else%
			\advance\c@pgf@countb1\relax%
			\expandafter\ifx\pgf@lib@sh@itext#1%
				\expandafter\pgf@sh@resavedmacro\expandafter{\csname input-\the\c@pgf@countb\endcsname}{%
					\expandafter\def\csname input-\the\c@pgf@countb\endcsname{i}}%
			\else%
				\expandafter\pgf@sh@resavedmacro\expandafter{\csname input-\the\c@pgf@countb\endcsname}{%
					\expandafter\def\csname input-\the\c@pgf@countb\endcsname{n}}%
			\fi%		
			\let\pgf@lib@sh@next\pgf@lib@sh@logicgate@parseinputs@@short%
		\fi%
	\fi%
	\pgf@lib@sh@next%
}
\def\pgf@lib@sh@logicgate@parseinputs@long{%
	\expandafter\pgf@lib@sh@logicgate@parseinputs@@long\pgf@lib@sh@temp,\pgf@stop,%
}
\def\pgf@lib@sh@logicgate@parseinputs@@long#1,{%
	\ifx#1\pgf@stop%
		\edef\pgf@lib@sh@logicgate@numinputs{\the\c@pgf@countb}%
		\let\pgf@lib@sh@next\relax%
	\else%
		\ifnum\c@pgf@countb=\c@pgf@counta%
			\edef\pgf@lib@sh@logicgate@numinputs{\the\c@pgf@countb}%
			\let\pgf@lib@sh@next\relax%
		\else%
			\advance\c@pgf@countb1\relax%
			\def\pgf@lib@sh@temp{#1}%
			\ifx\pgf@lib@sh@invertedtext\pgf@lib@sh@temp%
				\expandafter\pgf@sh@resavedmacro\expandafter{\csname input-\the\c@pgf@countb\endcsname}{%
					\expandafter\def\csname input-\the\c@pgf@countb\endcsname{i}}%
			\else%
				\expandafter\pgf@sh@resavedmacro\expandafter{\csname input-\the\c@pgf@countb\endcsname}{%
					\expandafter\def\csname input-\the\c@pgf@countb\endcsname{n}}%
			\fi%		
			\let\pgf@lib@sh@next\pgf@lib@sh@logicgate@parseinputs@@@long%
		\fi%
	\fi%
	\pgf@lib@sh@next%
}
\def\pgf@lib@sh@logicgate@parseinputs@@@long{%
	\pgfutil@ifnextchar x{\pgf@lib@sh@logicgate@parseinputs@@long}%
		{\pgf@lib@sh@logicgate@parseinputs@@long}%
}
\fi%

% Internal macro for calculation input anchors. Used by
% both and gates and nand gates, and also the not gate and buffer.
%
\def\pgf@lib@sh@logicgate@AND@inputanchor#1{%
	\dimensions%
	\centerpoint%
	\pgf@xa\halfside%
	\advance\pgf@x-.166666\pgf@xa%
	\advance\pgf@x-\halfwidth%
	\expandafter\ifx\expandafter\pgf@lib@sh@itext\csname input-#1\endcsname%
		\advance\pgf@x-\invertedradius%
		\advance\pgf@x-\outerinvertedradius%
	\fi%
	%
	\pgfutil@tempdima\halfside%
	\multiply\pgfutil@tempdima2\relax%
	\c@pgf@counta\numinputs%
	\advance\c@pgf@counta1\relax%
	\divide\pgfutil@tempdima\c@pgf@counta%
	\multiply\pgfutil@tempdima#1\relax%
	\advance\pgf@y\halfside%
	\advance\pgf@y-\pgfutil@tempdima%
}




% Shape and gate US
%
\pgfdeclareshape{and gate US}{%
	\expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@and gate US\endcsname{%
		\pgf@lib@sh@logicgate@parseinputs{1024}% Maximum 1024 (!) inputs.
		%
		\pgfmathloop%
		\ifnum\pgfmathcounter>\pgf@lib@sh@logicgate@numinputs%
		\else%
			\pgfutil@ifundefined{pgf@anchor@and gate US@input \pgfmathcounter}{%
				\expandafter\xdef\csname pgf@anchor@and gate US@input \pgfmathcounter\endcsname{%
					\noexpand\pgf@lib@sh@logicgate@AND@inputanchor{\pgfmathcounter}%
				}%
			}{}%
		\repeatpgfmathloop%
		\ifnum\pgf@lib@sh@logicgate@numinputs<2\relax%
			\PackageError{PGF}{An and gate must have at least two inputs}{}%
		\fi%
	}%
	\savedmacro\numinputs{\let\numinputs\pgf@lib@sh@logicgate@numinputs}%
	\saveddimen\invertedradius{%
		\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
	}%
	\saveddimen\outerinvertedradius{%
		\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
		\advance\pgf@x.5\pgflinewidth%
	}
	\savedmacro\dimensions{\pgf@lib@sh@logicgates@dimensions@andUS}
	\savedanchor\centerpoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgf@y.5\ht\pgfnodeparttextbox%
		\advance\pgf@y-.5\dp\pgfnodeparttextbox%
	}
	\savedanchor\midpoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgfmathsetlength\pgf@y{+0.5ex}%
	}
	\savedanchor\basepoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgf@y0pt%
	}
	\anchor{center}{\centerpoint}%
	\anchor{mid}{\midpoint}%
	\anchor{mid east}{%
		\csname pgf@anchor@and gate US@east\endcsname%
		\pgf@xa\pgf@x%
		\midpoint%
		\pgf@x\pgf@xa}
	\anchor{mid west}{%
		\csname pgf@anchor@and gate US@west\endcsname%
		\pgf@xa\pgf@x%
		\midpoint%
		\pgf@x\pgf@xa}
	\anchor{base}{\basepoint}%
	\anchor{base east}{%
		\csname pgf@anchor@and gate US@east\endcsname%
		\pgf@xa\pgf@x%
		\basepoint%
		\pgf@x\pgf@xa}
	\anchor{base west}{%
		\csname pgf@anchor@and gate US@west\endcsname%
		\pgf@xa\pgf@x%
		\basepoint%
		\pgf@x\pgf@xa}
	\anchor{east}{%
		\dimensions%
		\pgf@xa\halfwidth%
		\pgf@xb\pgf@xa%
		\advance\pgf@xb-\outerxsep%
		\centerpoint%
		\advance\pgf@x.333333\pgf@xb%
		\advance\pgf@x\pgf@xa%
	}
	\anchor{output}{\csname pgf@anchor@and gate US@east\endcsname}
	\anchor{north east}{%
		\dimensions%
		\ifpgfgateanchorsuseboundingrectangle%
			\csname pgf@anchor@nand gate US@east\endcsname%
			\advance\pgf@y\halfheight%
		\else
			\pgf@xa\halfwidth%
			\pgf@ya\halfheight%
			\pgf@xb\pgf@xa%
			\advance\pgf@xb-\outerxsep%
			\centerpoint%
			\advance\pgf@x.333333\pgf@xb%
			\advance\pgf@x.707106\pgf@xa%
			\advance\pgf@y.707106\pgf@ya%
		\fi%
	}
	\anchor{south east}{%
		\dimensions%
		\ifpgfgateanchorsuseboundingrectangle%
			\csname pgf@anchor@nand gate US@east\endcsname%
			\advance\pgf@y-\halfheight%
		\else
			\pgf@xa\halfwidth%
			\pgf@ya\halfheight%
			\pgf@xb\pgf@xa%
			\advance\pgf@xb-\outerxsep%
			\centerpoint%
			\advance\pgf@x.333333\pgf@xb%
			\advance\pgf@x.707106\pgf@xa%
			\advance\pgf@y-.707106\pgf@ya%
		\fi%
	}
	\anchor{north}{%
		\dimensions%
		\centerpoint%
		\advance\pgf@y\halfheight%		
	}
	\anchor{south}{%
		\dimensions%
		\centerpoint%
		\advance\pgf@y-\halfheight%
	}
	\anchor{south west}{%
		\dimensions%
		\centerpoint%
		\pgf@xa\halfside%
		\advance\pgf@x-1.16666\pgf@xa%
		\advance\pgf@x-\outerxsep%
		\advance\pgf@y-\halfheight%
	}
	\anchor{north west}{%
		\dimensions%
		\centerpoint%
		\pgf@xa\halfside%
		\advance\pgf@x-1.16666\pgf@xa%
		\advance\pgf@x-\outerxsep%
		\advance\pgf@y\halfheight%
	}
	\anchor{west}{%
		\dimensions%
		\centerpoint%
		\pgf@xa\halfside%
		\advance\pgf@x-1.16666\pgf@xa%
		\advance\pgf@x-\outerxsep%
	}
	\backgroundpath{%
		\dimensions%
		\pgf@xc\halfwidth%
		\pgf@yc\halfheight%
		\advance\pgf@xc-\outerxsep%
		\advance\pgf@yc-\outerysep%
		\pgf@xb.333333\pgf@xc%
		{%
			\pgftransformshift{\centerpoint}%
			\pgfpathmoveto{\pgfqpoint{\pgf@xb}{\pgf@yc}}%
			{%
				\edef\pgf@marshal{%
					\noexpand\pgfpatharc{90}{-90}{\the\pgf@yc}%
				}%
				\pgf@marshal%
			}
			\pgfpathlineto{\pgfqpoint{-1.16666\pgf@xc}{-\pgf@yc}}%
			\pgfpathlineto{\pgfqpoint{-1.16666\pgf@xc}{\pgf@yc}}%
			\pgfpathclose%
			%
			% Draw the inputs.
			%
			\pgfutil@tempdima2.0\pgf@yc%
			\c@pgf@counta\numinputs%
			\advance\c@pgf@counta1\relax%
			\divide\pgfutil@tempdima\c@pgf@counta%
			\pgfmathloop%
			\ifnum\pgfmathcounter>\numinputs%
			\else%
				\advance\pgf@yc-\pgfutil@tempdima%
				\expandafter\ifx\expandafter\pgf@lib@sh@itext\csname input-\pgfmathcounter\endcsname%
					{%
						\pgfpathcircle{%
							\pgf@xa\halfside%
							\pgf@x-1.16666\pgf@xa%
							\advance\pgf@x-.5\pgflinewidth%
							\advance\pgf@x-\invertedradius%
							\pgf@y\pgf@yc%
						}{+\invertedradius}%				
					}%
				\fi%				
			\repeatpgfmathloop%
		}%
	}%
	\anchorborder{%
		\pgfextract@process\externalpoint{}%
		\pgfextract@process\externalpoint{\pgfpointadd{\centerpoint}{\externalpoint}}%
		\pgfmathanglebetweenpoints{\centerpoint}{\externalpoint}%
		\let\externalangle\pgfmathresult%
		\dimensions%
		\pgf@xb\halfside%
		\pgf@xc1.66666\pgf@xb%
		\advance\pgf@xc\outerxsep%
		\pgf@yc\halfheight%
		\pgfmathanglebetweenpoints{\centerpoint}{\csname pgf@anchor@and gate US@north west\endcsname}%
		\ifdim\externalangle pt<\pgfmathresult pt\relax%
			\pgfmathanglebetweenpoints{\centerpoint}{\centerpoint\advance\pgf@x.333333\pgf@xb\advance\pgf@y\pgf@yc}%
			\ifdim\externalangle pt<\pgfmathresult pt\relax%
				\pgfmathpointintersectionoflineandarc{\externalpoint}{\centerpoint}%
					{%
						\centerpoint%
						\advance\pgf@x.333333\pgf@xb%	
					}%
					{0}{90}{+\halfwidth and +\halfheight}%
			\else%
				\pgfpointintersectionoflines{\externalpoint}{\centerpoint}%
					{\centerpoint\advance\pgf@x.333333\pgf@xb\advance\pgf@y\pgf@yc}%
					{\centerpoint\advance\pgf@x-\pgf@xc\advance\pgf@y\pgf@yc}%			
			\fi%
		\else%
			\pgfmathanglebetweenpoints{\centerpoint}{\centerpoint\advance\pgf@x.333333\pgf@xb\advance\pgf@y-\pgf@yc}%
			\ifdim\externalangle pt>\pgfmathresult pt\relax%
					\pgfmathpointintersectionoflineandarc{\externalpoint}{\centerpoint}%
					{%
						\centerpoint%
						\advance\pgf@x.333333\pgf@xb%		
					}%
					{270}{360}{+\halfwidth and +\halfheight}%
			\else%
				\pgfmathanglebetweenpoints{\centerpoint}{\csname pgf@anchor@and gate US@south west\endcsname}%
				\ifdim\externalangle pt>\pgfmathresult pt\relax%
					\pgfpointintersectionoflines{\externalpoint}{\centerpoint}%
						{\centerpoint\advance\pgf@x.333333\pgf@xb\advance\pgf@y-\pgf@yc}%
						{\csname pgf@anchor@and gate US@south west\endcsname}%	
				\else%
					\pgfpointintersectionoflines{\externalpoint}{\centerpoint}%
						{\csname pgf@anchor@and gate US@north west\endcsname}%
						{\csname pgf@anchor@and gate US@south west\endcsname}%
				\fi%
			\fi%
		\fi%
	}
}





% Shape nand gate US
%
\pgfdeclareshape{nand gate US}{%
	\expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@nand gate US\endcsname{%
		\pgf@lib@sh@logicgate@parseinputs{1024}% Maximum 1024 (!) inputs.
		%
		\pgfmathloop%
		\ifnum\pgfmathcounter>\pgf@lib@sh@logicgate@numinputs%
		\else%
			\pgfutil@ifundefined{pgf@anchor@nand gate US@input \pgfmathcounter}{%
				\expandafter\xdef\csname pgf@anchor@nand gate US@input \pgfmathcounter\endcsname{%
					\noexpand\pgf@lib@sh@logicgate@AND@inputanchor{\pgfmathcounter}%
				}%
			}{}%
		\repeatpgfmathloop%
		\ifnum\pgf@lib@sh@logicgate@numinputs<2\relax%
			\PackageError{PGF}{A nand gate must have at least two inputs}{}%
		\fi%
	}%
	\savedmacro\numinputs{\let\numinputs\pgf@lib@sh@logicgate@numinputs}%
	\saveddimen\invertedradius{%
		\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
	}%
	\saveddimen\outerinvertedradius{%
		\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
		\advance\pgf@x.5\pgflinewidth%
	}
	\savedmacro\dimensions{\pgf@lib@sh@logicgates@dimensions@andUS}
	\savedanchor\centerpoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgf@y.5\ht\pgfnodeparttextbox%
		\advance\pgf@y-.5\dp\pgfnodeparttextbox%
	}
	\savedanchor\midpoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgfmathsetlength\pgf@y{+0.5ex}%
	}
	\savedanchor\basepoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgf@y0pt%
	}
	\anchor{center}{\centerpoint}%
	\inheritanchor[from=and gate US]{mid}
	\inheritanchor[from=and gate US]{mid east}
	\inheritanchor[from=and gate US]{mid west}
	\inheritanchor[from=and gate US]{base}
	\inheritanchor[from=and gate US]{base east}
	\inheritanchor[from=and gate US]{base west}
	\anchor{output}{%
		\dimensions
		\pgfpointadd{\centerpoint}{%
			\pgf@xa\halfside%
			\pgf@x1.33333\pgf@xa%
			\advance\pgf@x\invertedradius%
			\advance\pgf@x\outerinvertedradius%
			\advance\pgf@x\outerxsep%
			\pgf@y0pt%
		}%
	}
	\inheritanchor[from=and gate US]{east}
	\inheritanchor[from=and gate US]{north east}
	\inheritanchor[from=and gate US]{south east}
	\inheritanchor[from=and gate US]{north}
	\inheritanchor[from=and gate US]{south}
	\inheritanchor[from=and gate US]{south west}
	\inheritanchor[from=and gate US]{north west}
	\inheritanchor[from=and gate US]{west}
	\backgroundpath{%
		\dimensions%
		\pgf@xc\halfwidth%
		\pgf@yc\halfheight%
		\advance\pgf@xc-\outerxsep%
		\advance\pgf@yc-\outerysep%
		\pgf@xb.333333\pgf@xc%
		{%
			\pgftransformshift{\centerpoint}%
			\pgfpathmoveto{\pgfqpoint{\pgf@xb}{\pgf@yc}}%
			{%
				\edef\pgf@marshal{%
					\noexpand\pgfpatharc{90}{-90}{\the\pgf@yc}%
				}%
				\pgf@marshal%
			}
			\pgfpathlineto{\pgfqpoint{-1.16666\pgf@xc}{-\pgf@yc}}%
			\pgfpathlineto{\pgfqpoint{-1.16666\pgf@xc}{\pgf@yc}}%
			\pgfpathclose%
			%
			% Draw the inputs.
			%
			\pgfutil@tempdima2.0\pgf@yc%
			\c@pgf@counta\numinputs%
			\advance\c@pgf@counta1\relax%
			\divide\pgfutil@tempdima\c@pgf@counta%
			\pgfmathloop%
			\ifnum\pgfmathcounter>\numinputs%
			\else%
				\advance\pgf@yc-\pgfutil@tempdima%
				\expandafter\ifx\expandafter\pgf@lib@sh@itext\csname input-\pgfmathcounter\endcsname%
					{%
						\pgfpathcircle{%
							\pgf@xa\halfside%
							\pgf@x-1.166666\pgf@xa%
							\advance\pgf@x-.5\pgflinewidth%
							\advance\pgf@x-\invertedradius%
							\pgf@y\pgf@yc%
						}{+\invertedradius}%				
					}%
				\fi%				
			\repeatpgfmathloop%
			%
			% Draw the output inverter.
			%
			{%
				\pgfpathcircle{%
					\pgf@x1.33333\pgf@xc%
					\advance\pgf@x\outerinvertedradius%
					\pgf@y0pt%
				}{+\invertedradius}%			
			}%
		}%
	}		
	\inheritanchorborder[from=and gate US]
}


% Internal macro for calculation input anchors. Used by
% or, nor xnor gates (US).
%
\def\pgf@lib@sh@logicgate@OR@inputanchor#1{%
	\dimensions%
	\pgf@ya\halfside%
	\pgf@yb2.0\pgf@ya%
	%
	\pgfutil@tempdima\halfside%
	\multiply\pgfutil@tempdima2\relax%
	\c@pgf@counta\numinputs%
	\advance\c@pgf@counta1\relax%
	\divide\pgfutil@tempdima\c@pgf@counta%
	\multiply\pgfutil@tempdima#1\relax%
	\pgf@yc\pgf@ya%
	\advance\pgf@yc-\pgfutil@tempdima%
	%
	\pgf@xb\pgf@yb%
	\advance\pgf@yb-\halflinewidth%
	\pgfmathdivide@{\pgfmath@tonumber{\pgf@yc}}{\pgfmath@tonumber{\pgf@yb}}%
	\pgfmathasin@{\pgfmathresult}%
	\pgfmathcos@{\pgfmathresult}%
	%
	\pgf@xc-1.166666\pgf@ya%
	\advance\pgf@xc-.866025\pgf@xb%
	\advance\pgf@xc\pgfmathresult\pgf@yb%
	\advance\pgf@xc\halflinewidth%
	\advance\pgf@xc-\outerxsep%
	%
	\expandafter\ifx\expandafter\pgf@lib@sh@itext\csname input-#1\endcsname%
		\advance\pgf@xc-\invertedradius%
		\advance\pgf@xc-\outerinvertedradius%
	\fi%
	%
	\centerpoint%
	\advance\pgf@x\pgf@xc%
	\advance\pgf@y\pgf@yc%
}


% Shape or gate US
%
\pgfdeclareshape{or gate US}{%
	\expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@or gate US\endcsname{%
		\pgf@lib@sh@logicgate@parseinputs{1024}% Maximum 1024 (!) inputs.
		%
		\pgfmathloop%
		\ifnum\pgfmathcounter>\pgf@lib@sh@logicgate@numinputs%
		\else%
			\pgfutil@ifundefined{pgf@anchor@or gate US@input \pgfmathcounter}{%
				\expandafter\xdef\csname pgf@anchor@or gate US@input \pgfmathcounter\endcsname{%
					\noexpand\pgf@lib@sh@logicgate@OR@inputanchor{\pgfmathcounter}%
				}%
			}{}%
		\repeatpgfmathloop%
		\ifnum\pgf@lib@sh@logicgate@numinputs<2\relax%
			\PackageError{PGF}{An or gate must have at least two inputs}{}%
		\fi%
	}%
	\savedmacro\numinputs{\let\numinputs\pgf@lib@sh@logicgate@numinputs}%
	\saveddimen\invertedradius{%
		\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
	}%
	\saveddimen\halflinewidth{%
		\pgf@x.5\pgflinewidth%
	}%
	\saveddimen\outerinvertedradius{%
		\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
		\advance\pgf@x.5\pgflinewidth%
	}
	\savedmacro\dimensions{\pgf@lib@sh@logicgates@dimensions@orUS}
	\savedanchor\centerpoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgf@y.5\ht\pgfnodeparttextbox%
		\advance\pgf@y-.5\dp\pgfnodeparttextbox%
	}
	\savedanchor\midpoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgfmathsetlength\pgf@y{+0.5ex}%
	}
	\savedanchor\basepoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgf@y0pt%
	}
	\savedanchor\centerpoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgf@y.5\ht\pgfnodeparttextbox%
		\advance\pgf@y-.5\dp\pgfnodeparttextbox%
	}
	\savedanchor\midpoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgfmathsetlength\pgf@y{+0.5ex}%
	}
	\savedanchor\basepoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgf@y0pt%
	}
	\anchor{center}{\centerpoint}%
	\anchor{mid}{\midpoint}%
	\anchor{mid east}{%
		\csname pgf@anchor@or gate US@east\endcsname%
		\pgf@xa\pgf@x%
		\midpoint%
		\pgf@x\pgf@xa}
	\anchor{mid west}{%
		\csname pgf@anchor@or gate US@north west\endcsname%
		\pgf@xa\pgf@x%
		\midpoint%
		\pgf@x\pgf@xa}
	\anchor{base}{\basepoint}%
	\anchor{base east}{%
		\csname pgf@anchor@or gate US@east\endcsname%
		\pgf@xa\pgf@x%
		\basepoint%
		\pgf@x\pgf@xa}
	\anchor{base west}{%
		\csname pgf@anchor@or gate US@north west\endcsname%
		\pgf@xa\pgf@x%
		\basepoint%
		\pgf@x\pgf@xa}
	\anchor{east}{%
		\dimensions%
		\pgfpointadd{\centerpoint}{\tipanchor}%
	}
	\anchor{output}{\csname pgf@anchor@or gate US@east\endcsname}
	\anchor{north east}{%
		\dimensions%
		\ifpgfgateanchorsuseboundingrectangle%
			\tipanchor%
			\pgf@xa\pgf@x%
			\centerpoint%
			\advance\pgf@x\pgf@xa%
			\advance\pgf@y\halfheight%
		\else%
			\centerpoint%
			\pgf@xa\halfwidth%
			\pgf@ya\halfheight%
			\pgf@xb\halfside%
			\advance\pgf@x-.166666\pgf@xb%
			\advance\pgf@xa\pgf@xb%
			\advance\pgf@x.5\pgf@xa%
			\advance\pgf@y-\pgf@xb%
			\advance\pgf@ya\pgf@xb%
			\advance\pgf@y.866025\pgf@ya%
		\fi%
	}
	\anchor{south east}{%
		\dimensions%
		\ifpgfgateanchorsuseboundingrectangle%
			\tipanchor%
			\pgf@xa\pgf@x%
			\centerpoint%
			\advance\pgf@x\pgf@xa%
			\advance\pgf@y-\halfheight%
		\else
			\centerpoint%
			\pgf@xa\halfwidth%
			\pgf@ya\halfheight%
			\pgf@xb\halfside%
			\advance\pgf@x-.166666\pgf@xb%
			\advance\pgf@xa\pgf@xb%
			\advance\pgf@x.5\pgf@xa%
			\advance\pgf@y\pgf@xb%
			\advance\pgf@ya\pgf@xb%
			\advance\pgf@y-.866025\pgf@ya%
		\fi%
	}
	\anchor{north}{%
		\dimensions%
		\centerpoint%
		\ifpgfgateanchorsuseboundingrectangle%
			\advance\pgf@y\halfheight%
		\else%
			\pgf@ya\halfheight%
			\advance\pgf@y.993043\pgf@ya%
		\fi%	
	}
	\anchor{south}{%
		\dimensions%
		\centerpoint%
		\ifpgfgateanchorsuseboundingrectangle%
			\advance\pgf@y-\halfheight%
		\else%
			\pgf@ya\halfheight%
			\advance\pgf@y-.993043\pgf@ya%
		\fi%	
	}
	\anchor{south west}{%
		\dimensions%
		\centerpoint%
		\pgf@xa\halfside%
		\advance\pgf@x-1.16666\pgf@xa%
		\pgf@xa\outerxsep%
		\advance\pgf@x-1.732050\pgf@xa%
		\advance\pgf@y-\halfheight%
	}
	\anchor{north west}{%
		\dimensions%
		\centerpoint%
		\pgf@xa\halfside%
		\advance\pgf@x-1.16666\pgf@xa%
		\pgf@xa\outerxsep%
		\advance\pgf@x-1.732050\pgf@xa%
		\advance\pgf@y\halfheight%
	}
	\anchor{west}{%
		\dimensions%
		\centerpoint%
		\pgf@xa\halfside%
		\ifpgfgateanchorsuseboundingrectangle%			
			\advance\pgf@x-1.16666\pgf@xa%
			\advance\pgf@x-\outerxsep%
		\else%
			\advance\pgf@x-1.16666\pgf@xa%
			\advance\pgf@x-\outerxsep%
			\advance\pgf@x0.267949\pgf@xa%
		\fi%
	}
	\backgroundpath{%
		\dimensions%
		\pgf@xc\halfwidth%
		\pgf@yc\halfheight%
		\advance\pgf@xc-\outerxsep%
		\advance\pgf@yc-\outerysep%
		{%
			\pgftransformshift{\centerpoint}%
			\pgfpathmoveto{\pgfqpoint{-.16666\pgf@xc}{\pgf@yc}}%
			{%
				\pgf@yc2.0\pgf@yc%
				\edef\pgf@marshal{%
					\noexpand\pgfpatharc{90}{30}{\the\pgf@yc}%
				}%
				\pgf@marshal%
			}
			{%
				\pgf@yc2.0\pgf@yc%
				\edef\pgf@marshal{%
					\noexpand\pgfpatharc{-30}{-90}{\the\pgf@yc}%
				}%
				\pgf@marshal%
			}
			\pgfpathlineto{\pgfqpoint{-1.16666\pgf@xc}{-\pgf@yc}}%
			{%
				\pgf@yc2.0\pgf@yc%
				\pgfpatharc{-30}{0}{+\pgf@yc}%			
			}
			{%
				\pgf@yc2.0\pgf@yc%
				\pgfpatharc{0}{30}{+\pgf@yc}%			
			}%
			\pgfpathclose%
			%
			% Draw the inputs.
			%
			\pgfutil@tempdima2.0\pgf@yc%
			\c@pgf@counta\numinputs%
			\advance\c@pgf@counta1\relax%
			\divide\pgfutil@tempdima\c@pgf@counta%
			\pgfmathloop%
			\ifnum\pgfmathcounter>\numinputs%
			\else%
				\advance\pgf@yc-\pgfutil@tempdima%
				\expandafter\ifx\expandafter\pgf@lib@sh@itext\csname input-\pgfmathcounter\endcsname%
					{%
						\pgfpathcircle{%
							\pgf@ya\halfside%
							\pgf@yb2.0\pgf@ya%
							\pgf@xa\pgf@yb%
							\advance\pgf@yb-\halflinewidth%
							\pgfmathdivide@{\pgfmath@tonumber{\pgf@yc}}{\pgfmath@tonumber{\pgf@yb}}%
							\let\sineangle\pgfmathresult%
							\pgfmathasin@{\pgfmathresult}%
							\pgfmathcos@{\pgfmathresult}%
							%
							\pgf@x-1.166666\pgf@ya%
							\advance\pgf@x-.866025\pgf@xa%
							\advance\pgf@x\pgfmathresult\pgf@yb%
							\advance\pgf@x-\invertedradius%
							\pgf@y\pgf@yc%
						}{+\invertedradius}%				
					}%
				\fi%				
			\repeatpgfmathloop%
		}%
	}%
	\anchorborder{%
		\pgfextract@process\externalpoint{}%
		\pgfextract@process\externalpoint{\pgfpointadd{\centerpoint}{\externalpoint}}%
		\pgf@xa\pgf@x%
		\pgf@ya\pgf@y%
		\centerpoint%
		\pgf@xb\pgf@x%
		\pgf@yb\pgf@y%
		\pgfmathanglebetweenpoints{\centerpoint}{\externalpoint}%
		\let\externalangle\pgfmathresult%
		\dimensions%
		\pgf@xc\halfside%
		%
		\pgf@xc-.166666\pgf@xc%
		\ifdim\pgf@xa<\pgf@xc%
			\pgfmathanglebetweenpoints{\centerpoint}{\csname pgf@anchor@or gate US@north west\endcsname}%
			\ifdim\externalangle pt<\pgfmathresult pt\relax%
				\pgfpointintersectionoflines{\externalpoint}{\centerpoint}%
					{%
						\pgfgateanchorsuseboundingrectangletrue%
						\csname pgf@anchor@or gate US@north\endcsname%
					}%
					{\csname pgf@anchor@or gate US@north west\endcsname}%
			\else%
				\pgfmathsubtract@{360}{\pgfmathresult}%
				\ifdim\externalangle pt>\pgfmathresult pt\relax%
					\pgfpointintersectionoflines{\externalpoint}{\centerpoint}%
					{%
						\pgfgateanchorsuseboundingrectangletrue%
						\csname pgf@anchor@or gate US@south\endcsname%
					}%
					{\csname pgf@anchor@or gate US@south west\endcsname}%
				\else%
					\ifdim\pgf@ya>\pgf@yb%
						\pgf@yc\halfheight%
						\advance\pgf@yc\halfside%
						\advance\pgf@yc-\outerxsep%
						\pgfextract@process\point{%
							\pgfmathpointintersectionoflineandarc{\centerpoint}{\externalpoint}%
							{%
								\centerpoint%
								\pgf@xa\halfside%
								\advance\pgf@x-1.166666\pgf@xa%
								\pgf@xa2.0\pgf@xa%
								\advance\pgf@x-.866025\pgf@xa%
								\advance\pgf@x-\outerxsep%
							}%
							{0}{90}{+\pgf@yc}%
						}%
					\else%
						\pgf@yc\halfheight%
						\advance\pgf@yc\halfside%
						\advance\pgf@yc-\outerxsep%
						\pgfextract@process\point{%
							\pgfmathpointintersectionoflineandarc{\centerpoint}{\externalpoint}%
							{%
								\centerpoint%
								\pgf@xa\halfside%
								\advance\pgf@x-1.166666\pgf@xa%
								\pgf@xa2.0\pgf@xa%
								\advance\pgf@x-.866025\pgf@xa%
								\advance\pgf@x-\outerxsep%
							}%
							{270}{360}{+\pgf@yc}%
						}%
					\fi%
				\fi%
			\fi%
		\else%
			\ifdim\pgf@y=0pt\relax%
				\csname pgf@anchor@and gate US@east\endcsname%
			\else%
				\pgf@xc\halfwidth%
				\advance\pgf@xc\halfside%
				\pgf@yc\halfheight%
				\advance\pgf@yc\halfside%
				\pgf@xb\halfside%
				\pgf@xb-.166666\pgf@xb%
				\ifdim\pgf@ya<0pt%
					\pgfmathpointintersectionoflineandarc{\externalpoint}{\centerpoint}%
					{%
						\centerpoint%
						\advance\pgf@y\halfside%	
						\advance\pgf@x\pgf@xb%		
					}%
					{270}{330}{+\pgf@yc}%
				\else%
					\pgfmathpointintersectionoflineandarc{\externalpoint}{\centerpoint}%
					{%
						\centerpoint%
						\advance\pgf@y-\halfside%	
						\advance\pgf@x\pgf@xb%
					}%
					{30}{90}{+\pgf@xc and +\pgf@yc}%
				\fi%
			\fi%
		\fi%
	}%
}




% Shape nor gate US
%
\pgfdeclareshape{nor gate US}{%
	\expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@nor gate US\endcsname{%
		\pgf@lib@sh@logicgate@parseinputs{1024}% Maximum 1024 (!) inputs.
		%
		\pgfmathloop%
		\ifnum\pgfmathcounter>\pgf@lib@sh@logicgate@numinputs%
		\else%
			\pgfutil@ifundefined{pgf@anchor@nor gate US@input \pgfmathcounter}{%
				\expandafter\xdef\csname pgf@anchor@nor gate US@input \pgfmathcounter\endcsname{%
					\noexpand\pgf@lib@sh@logicgate@OR@inputanchor{\pgfmathcounter}%
				}%
			}{}%
		\repeatpgfmathloop%
		\ifnum\pgf@lib@sh@logicgate@numinputs<2\relax%
			\PackageError{PGF}{A nor gate must have at least two inputs}{}%
		\fi%
	}%
	\savedmacro\numinputs{\let\numinputs\pgf@lib@sh@logicgate@numinputs}%
	\saveddimen\invertedradius{%
		\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
	}%
	\saveddimen\halflinewidth{%
		\pgf@x.5\pgflinewidth%
	}%
	\saveddimen\outerinvertedradius{%
		\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
		\advance\pgf@x.5\pgflinewidth%
	}
	\savedmacro\dimensions{\pgf@lib@sh@logicgates@dimensions@orUS}
	\savedanchor\centerpoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgf@y.5\ht\pgfnodeparttextbox%
		\advance\pgf@y-.5\dp\pgfnodeparttextbox%
	}
	\savedanchor\midpoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgfmathsetlength\pgf@y{+0.5ex}%
	}
	\savedanchor\basepoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgf@y0pt%
	}
	\inheritanchor[from=or gate US]{center}
	\inheritanchor[from=or gate US]{mid}
	\inheritanchor[from=or gate US]{mid east}
	\inheritanchor[from=or gate US]{mid west}
	\inheritanchor[from=or gate US]{base}
	\inheritanchor[from=or gate US]{base east}
	\inheritanchor[from=or gate US]{base west}
	\inheritanchor[from=or gate US]{east}
	\inheritanchor[from=or gate US]{north east}
	\inheritanchor[from=or gate US]{south east}
	\inheritanchor[from=or gate US]{north}
	\inheritanchor[from=or gate US]{south}
	\inheritanchor[from=or gate US]{south west}
	\inheritanchor[from=or gate US]{north west}
	\inheritanchor[from=or gate US]{west}
	\anchor{output}{%
		\dimensions%
		\centerpoint%
		\pgf@xc\halfside%
		\pgf@yc\pgf@xc%
		\advance\pgf@x-.166666\pgf@xc%
		\pgf@yc2.0\pgf@yc%
		\advance\pgf@x.866025\pgf@yc%
		\advance\pgf@x\outerinvertedradius%
		\advance\pgf@x\invertedradius%
		\advance\pgf@x\outerxsep%
	}%
	\backgroundpath{%
		\dimensions%
		\pgf@xc\halfwidth%
		\pgf@yc\halfheight%
		\advance\pgf@xc-\outerxsep%
		\advance\pgf@yc-\outerysep%
		{%
			\pgftransformshift{\centerpoint}%
			\pgfpathmoveto{\pgfqpoint{-.16666\pgf@xc}{\pgf@yc}}%
			{%
				\pgf@yc2.0\pgf@yc%
				\edef\pgf@marshal{%
					\noexpand\pgfpatharc{90}{30}{\the\pgf@yc}%
				}%
				\pgf@marshal%
			}
			{%
				\pgf@yc2.0\pgf@yc%
				\edef\pgf@marshal{%
					\noexpand\pgfpatharc{-30}{-90}{\the\pgf@yc}%
				}%
				\pgf@marshal%
			}
			\pgfpathlineto{\pgfqpoint{-1.16666\pgf@xc}{-\pgf@yc}}%
			{%
				\pgf@yc2.0\pgf@yc%
				\pgfpatharc{-30}{0}{+\pgf@yc}%			
			}
			{%
				\pgf@yc2.0\pgf@yc%
				\pgfpatharc{0}{30}{+\pgf@yc}%			
			}%
			\pgfpathclose%
			% Draw the output inverter.
			%
			{%
				\pgfpathcircle{%
					\pgf@x-.166666\pgf@xc%
					\pgf@yc2.0\pgf@yc%
					\advance\pgf@x.866025\pgf@yc%
					\advance\pgf@x\outerinvertedradius%
					\pgf@y0pt%
				}{+\invertedradius}%			
			}%
			%
			% Draw the inputs.
			%
			\pgfutil@tempdima2.0\pgf@yc%
			\c@pgf@counta\numinputs%
			\advance\c@pgf@counta1\relax%
			\divide\pgfutil@tempdima\c@pgf@counta%
			\pgfmathloop%
			\ifnum\pgfmathcounter>\numinputs%
			\else%
				\advance\pgf@yc-\pgfutil@tempdima%
				\expandafter\ifx\expandafter\pgf@lib@sh@itext\csname input-\pgfmathcounter\endcsname%
					{%
						\pgfpathcircle{%
							\pgf@ya\halfside%
							\pgf@yb2.0\pgf@ya%
							\pgf@xa\pgf@yb%
							\advance\pgf@yb-\halflinewidth%
							\pgfmathdivide@{\pgfmath@tonumber{\pgf@yc}}{\pgfmath@tonumber{\pgf@yb}}%
							\let\sineangle\pgfmathresult%
							\pgfmathasin@{\pgfmathresult}%
							\pgfmathcos@{\pgfmathresult}%
							%
							\pgf@x-1.166666\pgf@ya%
							\advance\pgf@x-.866025\pgf@xa%
							\advance\pgf@x\pgfmathresult\pgf@yb%
							\advance\pgf@x-\invertedradius%
							\pgf@y\pgf@yc%
						}{+\invertedradius}%				
					}%
				\fi%				
			\repeatpgfmathloop%
		}%
	}%
	\inheritanchorborder[from=or gate US]
}



% Internal macro for calculation input anchors. Used by
% xor and xnor gates (US).
%
\def\pgf@lib@sh@logicgate@XOR@inputanchor#1{%
	\dimensions%
	\pgf@ya\halfside%
	\pgf@yb2.0\pgf@ya%
	%
	\pgfutil@tempdima\halfside%
	\multiply\pgfutil@tempdima2\relax%
	\c@pgf@counta\numinputs%
	\advance\c@pgf@counta1\relax%
	\divide\pgfutil@tempdima\c@pgf@counta%
	\multiply\pgfutil@tempdima#1\relax%
	\pgf@yc\pgf@ya%
	\advance\pgf@yc-\pgfutil@tempdima%
	%
	\pgf@xb\pgf@yb%
	\advance\pgf@yb-\halflinewidth%
	\pgfmathdivide@{\pgfmath@tonumber{\pgf@yc}}{\pgfmath@tonumber{\pgf@yb}}%
	\pgfmathasin@{\pgfmathresult}%
	\pgfmathcos@{\pgfmathresult}%
	%
	\pgf@xc-1.166666\pgf@ya%
	\advance\pgf@xc-.866025\pgf@xb%
	\advance\pgf@xc\pgfmathresult\pgf@yb%
	\advance\pgf@xc\halflinewidth%
	\advance\pgf@xc-\outerxsep%
	%
	\expandafter\ifx\expandafter\pgf@lib@sh@itext\csname input-#1\endcsname%
		\advance\pgf@xc-\invertedradius%
		\advance\pgf@xc-\outerinvertedradius%
	\fi%
	%
	\centerpoint%
	\advance\pgf@x\pgf@xc%
	\advance\pgf@y\pgf@yc%
	\pgf@xa\halfside%
	\advance\pgf@x-.333333\pgf@xa%
}





% Shape xor gate US
%
\pgfdeclareshape{xor gate US}{%
	\expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@xor gate US\endcsname{%
		\pgf@lib@sh@logicgate@parseinputs{2}% 
		%
		\pgfmathloop%
		\ifnum\pgfmathcounter>\pgf@lib@sh@logicgate@numinputs%
		\else%
			\pgfutil@ifundefined{pgf@anchor@xor gate US@input \pgfmathcounter}{%
				\expandafter\xdef\csname pgf@anchor@xor gate US@input \pgfmathcounter\endcsname{%
					\noexpand\pgf@lib@sh@logicgate@XOR@inputanchor{\pgfmathcounter}%
				}%
			}{}%
		\repeatpgfmathloop%
		\ifnum\pgf@lib@sh@logicgate@numinputs<2\relax%
			\PackageError{PGF}{An xor gate must have at two inputs}{}%
		\fi%
	}%
	\savedmacro\numinputs{\let\numinputs\pgf@lib@sh@logicgate@numinputs}%
	\saveddimen\invertedradius{%
		\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
	}%
	\saveddimen\halflinewidth{%
		\pgf@x.5\pgflinewidth%
	}%
	\saveddimen\outerinvertedradius{%
		\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
		\advance\pgf@x.5\pgflinewidth%
	}
	\savedmacro\dimensions{\pgf@lib@sh@logicgates@dimensions@orUS}
	\savedanchor\centerpoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgf@y.5\ht\pgfnodeparttextbox%
		\advance\pgf@y-.5\dp\pgfnodeparttextbox%
	}
	\savedanchor\midpoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgfmathsetlength\pgf@y{+0.5ex}%
	}
	\savedanchor\basepoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgf@y0pt%
	}
	\anchor{center}{\centerpoint}%
	\anchor{mid}{\midpoint}%
	\inheritanchor[from=or gate US]{mid east}
	\anchor{mid west}{%
		\csname pgf@anchor@xor gate US@north west\endcsname%
		\pgf@xa\pgf@x%
		\midpoint%
		\pgf@x\pgf@xa}
	\anchor{base}{\basepoint}%
	\inheritanchor[from=or gate US]{base east}
	\anchor{base west}{%
		\csname pgf@anchor@xor gate US@north west\endcsname%
		\pgf@xa\pgf@x%
		\basepoint%
		\pgf@x\pgf@xa}
	\inheritanchor[from=or gate US]{base}
	\inheritanchor[from=or gate US]{output}
	\inheritanchor[from=or gate US]{east}
	\inheritanchor[from=or gate US]{north east}
	\inheritanchor[from=or gate US]{south east}
	\inheritanchor[from=or gate US]{north}
	\inheritanchor[from=or gate US]{south}
	\anchor{south west}{%
		\dimensions%
		\pgf@xa\halfside%
		\pgf@xa-3.232051\pgf@xa% (7/6 + 2*cos(30) + 1/3) * x
		\pgf@xb\halfside%
		\pgf@xb2.0\pgf@xb%
		\advance\pgf@xb-\halflinewidth%
		\advance\pgf@xa.866025\pgf@xb%
		\pgf@ya.5\pgf@xb%
		\centerpoint%
		\advance\pgf@x\pgf@xa%
		\ifpgfgateanchorsuseboundingrectangle%
			\advance\pgf@y-\halfheight%
		\else%
			\advance\pgf@y-\pgf@ya%
		\fi%
	}
	\anchor{north west}{%
		\dimensions%
		\pgf@xa\halfside%
		\pgf@xa-3.232051\pgf@xa% (7/6 + 2*cos(30) + 1/3) * x
		\pgf@xb\halfside%
		\pgf@xb2.0\pgf@xb%
		\advance\pgf@xb-\halflinewidth%
		\advance\pgf@xa.866025\pgf@xb%
		\pgf@ya.5\pgf@xb%
		\centerpoint%
		\advance\pgf@x\pgf@xa%
		\ifpgfgateanchorsuseboundingrectangle%
			\advance\pgf@y\halfheight%
		\else%
			\advance\pgf@y\pgf@ya%
		\fi%
	}
	\anchor{west}{%
		\dimensions%
		\pgf@ya\halfside%
		\pgf@yb2.0\pgf@ya%
		%
		\pgf@xb\pgf@yb%
		\advance\pgf@yb-\halflinewidth%
		\pgfmathdivide@{0}{\pgfmath@tonumber{\pgf@yb}}%
		\pgfmathasin@{\pgfmathresult}%
		\pgfmathcos@{\pgfmathresult}%
		%
		\pgf@xc-1.166666\pgf@ya%
		\advance\pgf@xc-.866025\pgf@xb%
		\advance\pgf@xc\pgfmathresult\pgf@yb%
		\advance\pgf@xc\halflinewidth%
		\advance\pgf@xc-\outerxsep%
		%
		\centerpoint%
		\advance\pgf@x\pgf@xc%
		\pgf@xa\halfside%
		\advance\pgf@x-.333333\pgf@xa%
		\ifpgfgateanchorsuseboundingrectangle%
			\pgf@xa2.0\pgf@xa%
			\advance\pgf@x-0.133974\pgf@xa%
		\fi%
	}
	\backgroundpath{%
		\dimensions%
		\pgf@xc\halfwidth%
		\pgf@yc\halfheight%
		\advance\pgf@xc-\outerxsep%
		\advance\pgf@yc-\outerysep%
		{%
			\pgftransformshift{\centerpoint}%
			\pgfpathmoveto{\pgfqpoint{-.16666\pgf@xc}{\pgf@yc}}%
			{%
				\pgf@yc2.0\pgf@yc%
				\edef\pgf@marshal{%
					\noexpand\pgfpatharc{90}{30}{\the\pgf@yc}%
				}%
				\pgf@marshal%
			}
			{%
				\pgf@yc2.0\pgf@yc%
				\edef\pgf@marshal{%
					\noexpand\pgfpatharc{-30}{-90}{\the\pgf@yc}%
				}%
				\pgf@marshal%
			}
			\pgfpathlineto{\pgfqpoint{-1.16666\pgf@xc}{-\pgf@yc}}%
			{%
				\pgf@yc2.66666\pgf@yc%
				\pgfpatharc{-22}{0}{+1.166666\pgf@yc and +\pgf@yc}%			
			}
			{%
				\pgf@yc2.66666\pgf@yc%
				\pgfpatharc{0}{22}{+1.166666\pgf@yc and +\pgf@yc}%			
			}%
			\pgfpathclose%
			%
			% Draw the inputs.
			%
			\pgfutil@tempdima2.0\pgf@yc%
			\c@pgf@counta\numinputs%
			\advance\c@pgf@counta1\relax%
			\divide\pgfutil@tempdima\c@pgf@counta%
			\pgfmathloop%
			\ifnum\pgfmathcounter>\numinputs%
			\else%
				\advance\pgf@yc-\pgfutil@tempdima%
				\expandafter\ifx\expandafter\pgf@lib@sh@itext\csname input-\pgfmathcounter\endcsname%
					{%
						\pgfpathcircle{%
							\pgf@ya\halfside%
							\pgf@yb2.0\pgf@ya%
							\pgf@xa\pgf@yb%
							\advance\pgf@yb-\halflinewidth%
							\pgfmathdivide@{\pgfmath@tonumber{\pgf@yc}}{\pgfmath@tonumber{\pgf@yb}}%
							\pgfmathasin@{\pgfmathresult}%
							\pgfmathcos@{\pgfmathresult}%
							%
							\pgf@x-1.5\pgf@ya%
							\advance\pgf@x-.866025\pgf@xa%
							\advance\pgf@x\pgfmathresult\pgf@yb%
							\advance\pgf@x-\invertedradius%
							\pgf@y\pgf@yc%
						}{+\invertedradius}%				
					}%
				\fi%				
			\repeatpgfmathloop%
			%
			% Now, some fooling around to stop the `tail' being filled.
			% Technically it still is, but it isn't visible.
			%
			\pgf@xc\halfside%
			\pgf@yc\halfside%
			\pgfpathmoveto{\pgfqpoint{-1.5\pgf@xc}{-\pgf@yc}}%
			\pgf@yc2.0\pgf@yc%
			\pgfmathloop%
			\ifnum\pgfmathcounter<61\relax%
				{%
					\pgfextract@process\point{%
						\pgfpointadd{%
							\pgf@x\halfside%
							\pgf@x-3.232051\pgf@x% (7/6 + 2*cos(30) + 1/3) * x
							\pgf@y0pt%
						}{%
							\pgfpointpolar{\pgfmathcounter-30}{+\pgf@yc}%
						}%
					}%
					\pgfpathlineto{\point}%
					\pgfpathmoveto{\point}%
				}
			\repeatpgfmathloop%
		}%
	}%
	\anchorborder{%
		\pgfextract@process\externalpoint{}%
		\pgfextract@process\externalpoint{\pgfpointadd{\centerpoint}{\externalpoint}}%
		\pgf@xa\pgf@x%
		\pgf@ya\pgf@y%
		\centerpoint%
		\pgf@xb\pgf@x%
		\pgf@yb\pgf@y%
		\pgfmathanglebetweenpoints{\centerpoint}{\externalpoint}%
		\let\externalangle\pgfmathresult%
		\dimensions%
		\pgf@xc\halfside%
		%
		\pgf@xc-.166666\pgf@xc%
		\ifdim\pgf@xa<\pgf@xc%
			\pgfmathanglebetweenpoints{\centerpoint}%
			{%
				\pgfgateanchorsuseboundingrectangletrue%
				\csname pgf@anchor@xor gate US@north west\endcsname%
			}%
			\ifdim\externalangle pt<\pgfmathresult pt\relax%
				\pgfpointintersectionoflines{\externalpoint}{\centerpoint}%
					{%
						\pgfgateanchorsuseboundingrectangletrue%
						\csname pgf@anchor@xor gate US@north\endcsname%
					}%
					{%
						\pgfgateanchorsuseboundingrectangletrue%
						\csname pgf@anchor@xor gate US@north west\endcsname%
					}%
			\else%
				\pgfmathsubtract@{360}{\pgfmathresult}%
				\ifdim\externalangle pt>\pgfmathresult pt\relax%
					\pgfpointintersectionoflines{\externalpoint}{\centerpoint}%
					{%
						\pgfgateanchorsuseboundingrectangletrue%
						\csname pgf@anchor@xor gate US@south\endcsname%
					}%
					{%
						\pgfgateanchorsuseboundingrectangletrue%
						\csname pgf@anchor@xor gate US@south west\endcsname%
					}%
				\else%
					\ifdim\pgf@ya>\pgf@yb%
						\pgf@yc\halfheight%
						\advance\pgf@yc\halfside%
						\advance\pgf@yc-\outerxsep%
						\pgf@process{%
							\pgfmathpointintersectionoflineandarc{\centerpoint}{\externalpoint}%
							{%
								\centerpoint%
								\pgf@xa\halfside%
								\advance\pgf@x-1.166666\pgf@xa%
								\pgf@xa2.0\pgf@xa%
								\advance\pgf@x-.866025\pgf@xa%
								\advance\pgf@x-\outerxsep%
								\advance\pgf@x-.166666\pgf@xa%
							}%
							{0}{90}{+\pgf@yc}%
						}%
					\else%
						\pgf@yc\halfheight%
						\advance\pgf@yc\halfside%
						\advance\pgf@yc-\outerxsep%
						\pgf@process{%
							\pgfmathpointintersectionoflineandarc{\centerpoint}{\externalpoint}%
							{%
								\centerpoint%
								\pgf@xa\halfside%
								\advance\pgf@x-1.166666\pgf@xa%
								\pgf@xa2.0\pgf@xa%
								\advance\pgf@x-.866025\pgf@xa%
								\advance\pgf@x-\outerxsep%
								\advance\pgf@x-.166666\pgf@xa%
							}%
							{270}{360}{+\pgf@yc}%
						}%
					\fi%
				\fi%
			\fi%
		\else%
			\ifdim\pgf@y=0pt\relax%
				\csname pgf@anchor@and gate US@east\endcsname%
			\else%
				\pgf@xc\halfwidth%
				\advance\pgf@xc\halfside%
				\pgf@yc\halfheight%
				\advance\pgf@yc\halfside%
				\pgf@xb\halfside%
				\pgf@xb-.166666\pgf@xb%
				\ifdim\pgf@ya<0pt%
					\pgfmathpointintersectionoflineandarc{\externalpoint}{\centerpoint}%
					{%
						\centerpoint%
						\advance\pgf@y\halfside%	
						\advance\pgf@x\pgf@xb%		
					}%
					{270}{330}{+\pgf@yc}%
				\else%
					\pgfmathpointintersectionoflineandarc{\externalpoint}{\centerpoint}%
					{%
						\centerpoint%
						\advance\pgf@y-\halfside%	
						\advance\pgf@x\pgf@xb%
					}%
					{30}{90}{+\pgf@xc and +\pgf@yc}%
				\fi%
			\fi%
		\fi%
	}%
}




% Shape xnor gate US
%
\pgfdeclareshape{xnor gate US}{%
	\expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@xnor gate US\endcsname{%
		\pgf@lib@sh@logicgate@parseinputs{2}% 
		%
		\pgfmathloop%
		\ifnum\pgfmathcounter>\pgf@lib@sh@logicgate@numinputs%
		\else%
			\pgfutil@ifundefined{pgf@anchor@xnor gate US@input \pgfmathcounter}{%
				\expandafter\xdef\csname pgf@anchor@xnor gate US@input \pgfmathcounter\endcsname{%
					\noexpand\pgf@lib@sh@logicgate@XOR@inputanchor{\pgfmathcounter}%
				}%
			}{}%
		\repeatpgfmathloop%
		\ifnum\pgf@lib@sh@logicgate@numinputs<2\relax%
			\PackageError{PGF}{An xnor gate must have two inputs}{}%
		\fi%
	}%
	\savedmacro\numinputs{\let\numinputs\pgf@lib@sh@logicgate@numinputs}%
	\saveddimen\invertedradius{%
		\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
	}%
	\saveddimen\halflinewidth{%
		\pgf@x.5\pgflinewidth%
	}%
	\saveddimen\outerinvertedradius{%
		\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
		\advance\pgf@x.5\pgflinewidth%
	}
	\savedmacro\dimensions{\pgf@lib@sh@logicgates@dimensions@orUS}
	\savedanchor\centerpoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgf@y.5\ht\pgfnodeparttextbox%
		\advance\pgf@y-.5\dp\pgfnodeparttextbox%
	}
	\savedanchor\midpoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgfmathsetlength\pgf@y{+0.5ex}%
	}
	\savedanchor\basepoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgf@y0pt%
	}
	\anchor{center}{\centerpoint}%
	\anchor{mid}{\midpoint}%
	\inheritanchor[from=xor gate US]{mid east}
	\inheritanchor[from=xor gate US]{mid west}
	\anchor{base}{\basepoint}%
	\inheritanchor[from=xor gate US]{base east}
	\inheritanchor[from=xor gate US]{base west}
	\inheritanchor[from=xor gate US]{base}
	\inheritanchor[from=nor gate US]{output}
	\inheritanchor[from=xor gate US]{east}
	\inheritanchor[from=xor gate US]{north east}
	\inheritanchor[from=xor gate US]{south east}
	\inheritanchor[from=xor gate US]{north}
	\inheritanchor[from=xor gate US]{south}
	\inheritanchor[from=xor gate US]{south west}
	\inheritanchor[from=xor gate US]{north west}
	\inheritanchor[from=xor gate US]{west}
	\backgroundpath{%
		\dimensions%
		\pgf@xc\halfwidth%
		\pgf@yc\halfheight%
		\advance\pgf@xc-\outerxsep%
		\advance\pgf@yc-\outerysep%
		{%
			\pgftransformshift{\centerpoint}%
			\pgfpathmoveto{\pgfqpoint{-.16666\pgf@xc}{\pgf@yc}}%
			{%
				\pgf@yc2.0\pgf@yc%
				\edef\pgf@marshal{%
					\noexpand\pgfpatharc{90}{30}{\the\pgf@yc}%
				}%
				\pgf@marshal%
			}
			{%
				\pgf@yc2.0\pgf@yc%
				\edef\pgf@marshal{%
					\noexpand\pgfpatharc{-30}{-90}{\the\pgf@yc}%
				}%
				\pgf@marshal%
			}
			\pgfpathlineto{\pgfqpoint{-1.16666\pgf@xc}{-\pgf@yc}}%
			{%
				\pgf@yc2.66666\pgf@yc%
				\pgfpatharc{-22}{0}{+1.166666\pgf@yc and +\pgf@yc}%			
			}
			{%
				\pgf@yc2.66666\pgf@yc%
				\pgfpatharc{0}{22}{+1.166666\pgf@yc and +\pgf@yc}%			
			}%
			\pgfpathclose%
			%
			% Draw the output inverter.
			%
			{%
				\pgfpathcircle{%
					\pgf@x-.166666\pgf@xc%
					\pgf@yc2.0\pgf@yc%
					\advance\pgf@x.866025\pgf@yc%
					\advance\pgf@x\outerinvertedradius%
					\pgf@y0pt%
				}{+\invertedradius}%			
			}%
			%
			% Draw the inputs.
			%
			\pgfutil@tempdima2.0\pgf@yc%
			\c@pgf@counta\numinputs%
			\advance\c@pgf@counta1\relax%
			\divide\pgfutil@tempdima\c@pgf@counta%
			\pgfmathloop%
			\ifnum\pgfmathcounter>\numinputs%
			\else%
				\advance\pgf@yc-\pgfutil@tempdima%
				\expandafter\ifx\expandafter\pgf@lib@sh@itext\csname input-\pgfmathcounter\endcsname%
					{%
						\pgfpathcircle{%
							\pgf@ya\halfside%
							\pgf@yb2.0\pgf@ya%
							\pgf@xa\pgf@yb%
							\advance\pgf@yb-\halflinewidth%
							\pgfmathdivide@{\pgfmath@tonumber{\pgf@yc}}{\pgfmath@tonumber{\pgf@yb}}%
							\pgfmathasin@{\pgfmathresult}%
							\pgfmathcos@{\pgfmathresult}%
							%
							\pgf@x-1.5\pgf@ya%
							\advance\pgf@x-.866025\pgf@xa%
							\advance\pgf@x\pgfmathresult\pgf@yb%
							\advance\pgf@x-\invertedradius%
							\pgf@y\pgf@yc%
						}{+\invertedradius}%				
					}%
				\fi%				
			\repeatpgfmathloop%
			%
			% Now, some fooling around to stop the `tail' being filled.
			%
			\pgf@xc\halfside%
			\pgf@yc\halfside%
			\pgfpathmoveto{\pgfqpoint{-1.5\pgf@xc}{-\pgf@yc}}%
			\pgf@yc2.0\pgf@yc%
			\pgfmathloop%
			\ifnum\pgfmathcounter<61\relax%
				{%
					\pgfextract@process\point{%
						\pgfpointadd{%
							\pgf@x\halfside%
							\pgf@x-3.232051\pgf@x% (7/6 + 2*cos(30) + 1/3) * x
							\pgf@y0pt%
						}{%
							\pgfpointpolar{\pgfmathcounter-30}{+\pgf@yc}%
						}%
					}%
					\pgfpathlineto{\point}%
					\pgfpathmoveto{\point}%
				}
			\repeatpgfmathloop%
		}%
	}%
	\inheritanchorborder[from=xor gate US]
}






% Shape not gate US
%
\pgfdeclareshape{not gate US}{%
	\expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@not gate US\endcsname{%
		\pgf@lib@sh@logicgate@parseinputs{1}% 
		\ifnum\pgf@lib@sh@logicgate@numinputs=0\relax%
			\PackageError{PGF}{A not gate must have one input}{}%
		\fi%
	}%
	\savedmacro\numinputs{\let\numinputs\pgf@lib@sh@logicgate@numinputs}%
	\saveddimen\invertedradius{%
		\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
	}%
	\saveddimen\halflinewidth{%
		\pgf@x.5\pgflinewidth%
	}%
	\saveddimen\outerinvertedradius{%
		\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
		\advance\pgf@x.5\pgflinewidth%
	}
	\savedmacro\dimensions{\pgf@lib@sh@logicgates@dimensions@orUS}
	\savedanchor\centerpoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgf@y.5\ht\pgfnodeparttextbox%
		\advance\pgf@y-.5\dp\pgfnodeparttextbox%
	}
	\savedanchor\midpoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgfmathsetlength\pgf@y{+0.5ex}%
	}
	\savedanchor\basepoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgf@y0pt%
	}
	\anchor{center}{\centerpoint}%
	\anchor{mid}{\midpoint}%
	\anchor{mid east}{%
		\midpoint%
		\pgfutil@tempdima\pgf@y%
		\csname pgf@anchor@not gate US@east\endcsname%
		\pgf@y\pgfutil@tempdima%
	}
	\anchor{mid west}{%
		\dimensions%
		\pgf@xc\halfside%
		\pgf@xc-.833333\pgf@xc%
		\advance\pgf@xc-\outerxsep%
		\midpoint%
		\advance\pgf@x\pgf@xc%	
	}%
	\anchor{base}{\basepoint}%
	\anchor{base east}{%
		\basepoint%
		\pgfutil@tempdima\pgf@y%
		\csname pgf@anchor@not gate US@east\endcsname%
		\pgf@y\pgfutil@tempdima%
	}
	\anchor{base west}{%
		\dimensions%
		\pgf@xc\halfside%
		\pgf@xc-.833333\pgf@xc%
		\advance\pgf@xc-\outerxsep%
		\basepoint%
		\advance\pgf@x\pgf@xc%	
	}%
	\inheritanchor[from=and gate US]{base}
	\anchor{output}{%
		\dimensions%
		\pgf@xc\halfside%
		\centerpoint%
		\advance\pgf@x-.166666\pgf@xc%
		\pgf@xc2.0\pgf@xc%
		\advance\pgf@x.866025\pgf@xc%
		\advance\pgf@x\invertedradius%
		\advance\pgf@x\outerinvertedradius%
		\advance\pgf@x\outerxsep%
	}
	\anchor{east}{%
		\dimensions%
		\tipanchor%
		\pgf@xa\pgf@x%
		\centerpoint%
		\advance\pgf@x\pgf@xa%
	}%
	\anchor{north}{%
		\dimensions%
		\ifpgfgateanchorsuseboundingrectangle%
			\csname pgf@anchor@not gate US@north west\endcsname%
			\pgf@ya\pgf@y%
			\centerpoint%
			\pgf@y\pgf@ya%
		\else%
			\pgfpointintersectionoflines{\centerpoint}{\centerpoint\advance\pgf@y1pt}%
				{\csname pgf@anchor@not gate US@north west\endcsname}%
				{%
					\pgfpointadd{\centerpoint}{%
						\tipanchor%
						\pgf@xa\outerxsep%
						\advance\pgf@x2.350943\pgf@xa%	
					}%
				}%	
		\fi%
	}
	\anchor{south}{%
		\dimensions%
		\ifpgfgateanchorsuseboundingrectangle%
			\csname pgf@anchor@not gate US@south west\endcsname%
			\pgf@ya\pgf@y%
			\centerpoint%
			\pgf@y\pgf@ya%
		\else%
			\pgfpointintersectionoflines{\centerpoint}{\centerpoint\advance\pgf@y-1pt}%
				{\csname pgf@anchor@not gate US@south west\endcsname}%
				{%
					\pgfpointadd{\centerpoint}{%
						\tipanchor%
						\pgf@xa\outerxsep%
						\advance\pgf@x2.350943\pgf@xa%	
					}%
				}%	
		\fi%
	}
	\anchor{south east}{%
		\dimensions%
		\ifpgfgateanchorsuseboundingrectangle%
			\csname pgf@anchor@not gate US@south west\endcsname%
			\pgf@ya\pgf@y%
			\pgf@process{%	
				\dimensions%
				\tipanchor%
				\pgf@xa\pgf@x%
				\centerpoint%
				\advance\pgf@x\pgf@xa%
			}%
			\pgf@y\pgf@ya%
		\else%
			\pgfpointintersectionoflines{\centerpoint}{\centerpoint\advance\pgf@x1pt\advance\pgf@y-1pt}%
				{\csname pgf@anchor@not gate US@south west\endcsname}%
				{%
					\pgfpointadd{\centerpoint}{%
						\tipanchor%
						\pgf@xa\outerxsep%
						\advance\pgf@x2.350943\pgf@xa%	
					}%
				}%	
		\fi%
	}
	\anchor{north east}{%
		\dimensions%
		\ifpgfgateanchorsuseboundingrectangle%
			\csname pgf@anchor@not gate US@north west\endcsname%
			\pgf@ya\pgf@y%
			\pgf@process{%	
				\dimensions%
				\tipanchor%
				\pgf@xa\pgf@x%
				\centerpoint%
				\advance\pgf@x\pgf@xa%
			}%
			\pgf@y\pgf@ya%
		\else%
			\pgfpointintersectionoflines{\centerpoint}{\centerpoint\advance\pgf@x1pt\advance\pgf@y1pt}%
				{\csname pgf@anchor@not gate US@north west\endcsname}%
				{%
					\pgfpointadd{\centerpoint}{%
						\tipanchor%
						\pgf@xa\outerxsep%
						\advance\pgf@x2.350943\pgf@xa%	
					}%
				}%	
		\fi%
	}
	\anchor{south west}{%
		\dimensions%
		\pgf@xa\halfside%
		\centerpoint%
		\advance\pgf@x-.833333\pgf@xa
		\advance\pgf@x-\outerxsep%
		\advance\pgf@y-1.166666\pgf@xa%
		\pgf@ya\outerysep%
		\advance\pgf@y-1.565\pgf@ya% Selected by trial and error.	
	}%
	\anchor{north west}{%
		\dimensions%
		\pgf@xa\halfside%
		\centerpoint%
		\advance\pgf@x-.833333\pgf@xa
		\advance\pgf@x-\outerxsep%
		\advance\pgf@y1.166666\pgf@xa%
		\pgf@ya\outerysep%
		\advance\pgf@y1.565\pgf@ya% Selected by trial and error.	
	}%
	\anchor{input}{%
		\dimensions%
		\pgf@lib@sh@logicgate@AND@inputanchor{1}%
		\pgf@xc\halfside%
		\advance\pgf@x.333333\pgf@xc%
	}%
	\anchor{west}{%
		\dimensions%
		\pgf@xc\halfside%
		\pgf@xc-.833333\pgf@xc%
		\advance\pgf@xc-\outerxsep%
		\centerpoint%
		\advance\pgf@x\pgf@xc%
	}
	\backgroundpath{%
		\dimensions%
		\pgf@xc\halfwidth%
		\pgf@yc\halfheight%
		\advance\pgf@xc-\outerxsep%
		\advance\pgf@yc-\outerysep%
		{%
			\pgftransformshift{\centerpoint}%
			\pgfpathmoveto{\tipanchor}%
			\pgfpathlineto{\pgfqpoint{-.833333\pgf@xc}{1.166666\pgf@yc}}%
			\pgfpathlineto{\pgfqpoint{-.833333\pgf@xc}{-1.166666\pgf@yc}}%
			\pgfpathlineto{\tipanchor}%
			%
			% Draw the output inverter.
			%
			{%
				\pgfpathcircle{%
					\pgf@x-.166666\pgf@xc%
					\pgf@yc2.0\pgf@yc%
					\advance\pgf@x.866025\pgf@yc%
					\advance\pgf@x\outerinvertedradius%
					\pgf@y0pt%
				}{+\invertedradius}%			
			}%
			%
			% Draw the input.
			%
			\expandafter\ifx\expandafter\pgf@lib@sh@itext\csname input-1\endcsname%
				{%
					\pgfpathcircle{%
						\pgf@xa\halfside%
						\pgf@x-.833333\pgf@xa%
						\advance\pgf@x-.5\pgflinewidth%
						\advance\pgf@x-\invertedradius%
						\pgf@y0pt%
					}{+\invertedradius}%				
				}%
			\fi%				
		}%
	}%
	\anchorborder{%
		\pgfextract@process\externalpoint{}%
		\pgfextract@process\externalpoint{\pgfpointadd{\centerpoint}{\externalpoint}}%
		\pgfmathanglebetweenpoints{\centerpoint}{\externalpoint}%
		\let\externalangle\pgfmathresult%
		\dimensions%
		\pgfmathanglebetweenpoints{\centerpoint}{\csname pgf@anchor@not gate US@north west\endcsname}%
		\ifdim\externalangle pt<\pgfmathresult pt%
			\pgfpointintersectionoflines{\centerpoint}{\externalpoint}%
				{\csname pgf@anchor@not gate US@north west\endcsname}%
				{%
					\pgfpointadd{\centerpoint}{%
						\tipanchor%
						\pgf@xa\outerxsep%
						\advance\pgf@x2.350943\pgf@xa%	
					}%
				}%	
		\else%
			\pgfmathanglebetweenpoints{\centerpoint}{\csname pgf@anchor@not gate US@south west\endcsname}%
			\ifdim\externalangle pt<\pgfmathresult pt%
				\pgfpointintersectionoflines{\externalpoint}{\centerpoint}%
					{\csname pgf@anchor@not gate US@north west\endcsname}%
					{\csname pgf@anchor@not gate US@south west\endcsname}%
			\else%
				\pgfpointintersectionoflines{\centerpoint}{\externalpoint}%
				{\csname pgf@anchor@not gate US@south west\endcsname}%
				{%
					\pgfpointadd{\centerpoint}{%
						\tipanchor%
						\pgf@xa\outerxsep%
						\advance\pgf@x2.350943\pgf@xa%	
					}%
				}%	
			\fi%
		\fi%		
	}%
}





% Shape buffer gate US.
%
\pgfdeclareshape{buffer gate US}{%
	\expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@buffer gate US\endcsname{%
		\pgf@lib@sh@logicgate@parseinputs{1}% 
		\ifnum\pgf@lib@sh@logicgate@numinputs=0\relax%
			\PackageError{PGF}{A buffer gate must have one input}{}%
		\fi%
	}%
	\savedmacro\numinputs{\let\numinputs\pgf@lib@sh@logicgate@numinputs}%
	\saveddimen\invertedradius{%
		\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
	}%
	\saveddimen\halflinewidth{%
		\pgf@x.5\pgflinewidth%
	}%
	\saveddimen\outerinvertedradius{%
		\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
		\advance\pgf@x.5\pgflinewidth%
	}
	\savedmacro\dimensions{\pgf@lib@sh@logicgates@dimensions@orUS}
	\savedanchor\centerpoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgf@y.5\ht\pgfnodeparttextbox%
		\advance\pgf@y-.5\dp\pgfnodeparttextbox%
	}
	\savedanchor\midpoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgfmathsetlength\pgf@y{+0.5ex}%
	}
	\savedanchor\basepoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgf@y0pt%
	}
	\anchor{center}{\centerpoint}%
	\anchor{mid}{\midpoint}%
	\anchor{mid east}{%
		\midpoint%
		\pgfutil@tempdima\pgf@y%
		\csname pgf@anchor@buffer gate US@east\endcsname%
		\pgf@y\pgfutil@tempdima%
	}
	\anchor{mid west}{%
		\dimensions%
		\pgf@xc\halfside%
		\pgf@xc-.833333\pgf@xc%
		\advance\pgf@xc-\outerxsep%
		\midpoint%
		\advance\pgf@x\pgf@xc%	
	}%
	\anchor{base}{\basepoint}%
	\anchor{base east}{%
		\basepoint%
		\pgfutil@tempdima\pgf@y%
		\csname pgf@anchor@buffer gate US@east\endcsname%
		\pgf@y\pgfutil@tempdima%
	}
	\inheritanchor[from=not gate US]{base west}
	\inheritanchor[from=and gate US]{base}
	\anchor{output}{\csname pgf@anchor@buffer gate US@east\endcsname}
	\anchor{east}{%
		\dimensions%
		\pgfpointadd{\centerpoint}{%
			\tipanchor%
			\pgf@xa\outerxsep%
			\advance\pgf@x2.350943\pgf@xa%	
		}%
	}%
	\anchor{north}{%
		\dimensions%
		\ifpgfgateanchorsuseboundingrectangle%
			\csname pgf@anchor@not gate US@north west\endcsname%
			\pgf@ya\pgf@y%
			\centerpoint%
			\pgf@y\pgf@ya%
		\else%
			\pgfpointintersectionoflines{\centerpoint}{\centerpoint\advance\pgf@y1pt}%
				{\csname pgf@anchor@buffer gate US@north west\endcsname}%
				{%
					\pgfpointadd{\centerpoint}{%
						\tipanchor%
						\pgf@xa\outerxsep%
						\advance\pgf@x2.350943\pgf@xa%	
					}%
				}%	
		\fi%
	}
	\anchor{south}{%
		\dimensions%
		\ifpgfgateanchorsuseboundingrectangle%
			\csname pgf@anchor@not gate US@south west\endcsname%
			\pgf@ya\pgf@y%
			\centerpoint%
			\pgf@y\pgf@ya%
		\else%
			\pgfpointintersectionoflines{\centerpoint}{\centerpoint\advance\pgf@y-1pt}%
				{\csname pgf@anchor@not gate US@south west\endcsname}%
				{%
					\pgfpointadd{\centerpoint}{%
						\tipanchor%
						\pgf@xa\outerxsep%
						\advance\pgf@x2.350943\pgf@xa%	
					}%
				}%	
		\fi%
	}
	\anchor{south east}{%
		\dimensions%
		\ifpgfgateanchorsuseboundingrectangle%
			\csname pgf@anchor@not gate US@south west\endcsname%
			\pgfutil@tempdima\pgf@y%
			\csname pgf@anchor@buffer gate US@east\endcsname%
			\pgf@y\pgfutil@tempdima%
		\else%
			\pgfpointintersectionoflines{\centerpoint}{\centerpoint\advance\pgf@x1pt\advance\pgf@y-1pt}%
				{\csname pgf@anchor@buffer gate US@south west\endcsname}%
				{%
					\pgfpointadd{\centerpoint}{%
						\tipanchor%
						\pgf@xc\outerxsep%
						\advance\pgf@x2.350943\pgf@xc%	
					}%
				}%	
		\fi%
	}
	\anchor{north east}{%
		\dimensions%
		\ifpgfgateanchorsuseboundingrectangle%
			\csname pgf@anchor@not gate US@north west\endcsname%
			\pgfutil@tempdima\pgf@y%
			\csname pgf@anchor@buffer gate US@east\endcsname%
			\pgf@y\pgfutil@tempdima%
		\else%
			\pgfpointintersectionoflines{\centerpoint}{\centerpoint\advance\pgf@x1pt\advance\pgf@y1pt}%
				{\csname pgf@anchor@buffer gate US@north west\endcsname}%
				{%
					\pgfpointadd{\centerpoint}{%
						\tipanchor%
						\pgf@xa\outerxsep%
						\advance\pgf@x2.350943\pgf@xa%	
					}%
				}%	
		\fi%
	}
	\inheritanchor[from=not gate US]{south west}
	\inheritanchor[from=not gate US]{north west}
	\inheritanchor[from=not gate US]{input}
	\inheritanchor[from=not gate US]{west}
	\backgroundpath{%
		\dimensions%
		\pgf@xc\halfwidth%
		\pgf@yc\halfheight%
		\advance\pgf@xc-\outerxsep%
		\advance\pgf@yc-\outerysep%
		{%
			\pgftransformshift{\centerpoint}%
			\pgfpathmoveto{\tipanchor}%
			\pgfpathlineto{\pgfqpoint{-.833333\pgf@xc}{1.166666\pgf@yc}}%
			\pgfpathlineto{\pgfqpoint{-.833333\pgf@xc}{-1.166666\pgf@yc}}%
			\pgfpathclose%
			%
			% Draw the input.
			%
			\expandafter\ifx\expandafter\pgf@lib@sh@itext\csname input-1\endcsname%
				{%
					\pgfpathcircle{%
						\pgf@xa\halfside%
						\pgf@x-.833333\pgf@xa%
						\advance\pgf@x-.5\pgflinewidth%
						\advance\pgf@x-\invertedradius%
						\pgf@y0pt%
					}{+\invertedradius}%				
				}%
			\fi%				
		}%
	}%
	\inheritanchorborder[from=not gate US]
}





\pgfdeclareshape{and gate CDH}{%
	\expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@and gate CDH\endcsname{%
		\pgf@lib@sh@logicgate@parseinputs{1024}% Maximum 1024 (!) inputs.
		%
		\pgfmathloop%
		\ifnum\pgfmathcounter>\pgf@lib@sh@logicgate@numinputs%
		\else%
			\pgfutil@ifundefined{pgf@anchor@and gate CDH@input \pgfmathcounter}{%
				\expandafter\xdef\csname pgf@anchor@and gate CDH@input \pgfmathcounter\endcsname{%
					\noexpand\pgf@lib@sh@logicgate@AND@inputanchor{\pgfmathcounter}%
				}%
			}{}%
		\repeatpgfmathloop%
		\ifnum\pgf@lib@sh@logicgate@numinputs<2\relax%
			\PackageError{PGF}{An and gate must have at least two inputs}{}%
		\fi%
	}%
	\savedmacro\numinputs{\let\numinputs\pgf@lib@sh@logicgate@numinputs}%
	\saveddimen\invertedradius{%
		\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
	}%
	\saveddimen\outerinvertedradius{%
		\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
		\advance\pgf@x.5\pgflinewidth%
	}
	\savedmacro\dimensions{\pgf@lib@sh@logicgates@dimensions@orUS}
	\savedanchor\centerpoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgf@y.5\ht\pgfnodeparttextbox%
		\advance\pgf@y-.5\dp\pgfnodeparttextbox%
	}
	\savedanchor\midpoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgfmathsetlength\pgf@y{+0.5ex}%
	}
	\savedanchor\basepoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgf@y0pt%
	}
	\anchor{center}{\centerpoint}%
	\anchor{mid}{\midpoint}%
	\anchor{mid east}{%
		\csname pgf@anchor@and gate CDH@east\endcsname%
		\pgf@xa\pgf@x%
		\midpoint%
		\pgf@x\pgf@xa}
	\anchor{mid west}{%
		\csname pgf@anchor@and gate CDH@west\endcsname%
		\pgf@xa\pgf@x%
		\midpoint%
		\pgf@x\pgf@xa}
	\anchor{base}{\basepoint}%
	\anchor{base east}{%
		\csname pgf@anchor@and gate CDH@east\endcsname%
		\pgf@xa\pgf@x%
		\basepoint%
		\pgf@x\pgf@xa}
	\anchor{base west}{%
		\csname pgf@anchor@and gate CDH@west\endcsname%
		\pgf@xa\pgf@x%
		\basepoint%
		\pgf@x\pgf@xa}
	\anchor{east}{%
		\dimensions%
		\pgfpointadd{\centerpoint}{\tipanchor}%
	}
	\anchor{output}{\csname pgf@anchor@and gate CDH@east\endcsname}
	\anchor{north east}{%
		\dimensions%
		\ifpgfgateanchorsuseboundingrectangle%
			\tipanchor%
			\pgf@xa\pgf@x%
			\centerpoint%
			\advance\pgf@x\pgf@xa%
			\advance\pgf@y\halfheight%
		\else%
			\centerpoint%
			\pgf@xa\halfwidth%
			\pgf@ya\halfheight%
			\pgf@xb\halfside%
			\advance\pgf@x-.166666\pgf@xb%
			\advance\pgf@xa\pgf@xb%
			\advance\pgf@x.5\pgf@xa%
			\advance\pgf@y-\pgf@xb%
			\advance\pgf@ya\pgf@xb%
			\advance\pgf@y.866025\pgf@ya%
		\fi%
	}
	\anchor{south east}{%
		\dimensions%
		\ifpgfgateanchorsuseboundingrectangle%
			\tipanchor%
			\pgf@xa\pgf@x%
			\centerpoint%
			\advance\pgf@x\pgf@xa%
			\advance\pgf@y-\halfheight%
		\else
			\centerpoint%
			\pgf@xa\halfwidth%
			\pgf@ya\halfheight%
			\pgf@xb\halfside%
			\advance\pgf@x-.166666\pgf@xb%
			\advance\pgf@xa\pgf@xb%
			\advance\pgf@x.5\pgf@xa%
			\advance\pgf@y\pgf@xb%
			\advance\pgf@ya\pgf@xb%
			\advance\pgf@y-.866025\pgf@ya%
		\fi%
	}
	\anchor{north}{%
		\dimensions%
		\centerpoint%
		\ifpgfgateanchorsuseboundingrectangle%
			\advance\pgf@y\halfheight%
		\else%
			\pgf@ya\halfheight%
			\advance\pgf@y.993043\pgf@ya%
		\fi%	
	}
	\anchor{south}{%
		\dimensions%
		\centerpoint%
		\ifpgfgateanchorsuseboundingrectangle%
			\advance\pgf@y-\halfheight%
		\else%
			\pgf@ya\halfheight%
			\advance\pgf@y-.993043\pgf@ya%
		\fi%	
	}
	\anchor{south west}{%
		\dimensions%
		\centerpoint%
		\pgf@xa\halfside%
		\advance\pgf@x-1.16666\pgf@xa%
		\advance\pgf@x-\outerxsep%
		\advance\pgf@y-\halfheight%
	}
	\anchor{north west}{%
		\dimensions%
		\centerpoint%
		\pgf@xa\halfside%
		\advance\pgf@x-1.16666\pgf@xa%
		\advance\pgf@x-\outerxsep%
		\advance\pgf@y\halfheight%
	}
	\anchor{west}{%
		\dimensions%
		\centerpoint%
		\pgf@xa\halfside%
		\advance\pgf@x-1.16666\pgf@xa%
		\advance\pgf@x-\outerxsep%
	}
	\backgroundpath{%
		\dimensions%
		\pgf@xc\halfwidth%
		\pgf@yc\halfheight%
		\advance\pgf@xc-\outerxsep%
		\advance\pgf@yc-\outerysep%
		{%
			\pgftransformshift{\centerpoint}%
			\pgfpathmoveto{\pgfqpoint{-.16666\pgf@xc}{\pgf@yc}}%
			{%
				\pgf@yc2.0\pgf@yc%
				\edef\pgf@marshal{%
					\noexpand\pgfpatharc{90}{30}{\the\pgf@yc}%
				}%
				\pgf@marshal%
			}
			{%
				\pgf@yc2.0\pgf@yc%
				\edef\pgf@marshal{%
					\noexpand\pgfpatharc{-30}{-90}{\the\pgf@yc}%
				}%
				\pgf@marshal%
			}
			\pgfpathlineto{\pgfqpoint{-1.16666\pgf@xc}{-\pgf@yc}}%
			\pgfpathlineto{\pgfqpoint{-1.16666\pgf@xc}{\pgf@yc}}%
			\pgfpathclose%
			%
			% Draw the inputs.
			%
			\pgfutil@tempdima2.0\pgf@yc%
			\c@pgf@counta\numinputs%
			\advance\c@pgf@counta1\relax%
			\divide\pgfutil@tempdima\c@pgf@counta%
			\pgfmathloop%
			\ifnum\pgfmathcounter>\numinputs%
			\else%
				\advance\pgf@yc-\pgfutil@tempdima%
				\expandafter\ifx\expandafter\pgf@lib@sh@itext\csname input-\pgfmathcounter\endcsname%
					{%
						\pgfpathcircle{%
							\pgf@xa\halfside%
							\pgf@x-1.16666\pgf@xa%
							\advance\pgf@x-.5\pgflinewidth%
							\advance\pgf@x-\invertedradius%
							\pgf@y\pgf@yc%
						}{+\invertedradius}%				
					}%
				\fi%				
			\repeatpgfmathloop%
		}%
	}%
	\anchorborder{%
		\pgfextract@process\externalpoint{}%
		\pgf@xa\pgf@x%
		\pgf@ya\pgf@y%
		\dimensions%
		\pgf@xb\halfside%
		%
		\pgf@xb-.166666\pgf@xb%
		\ifdim\pgf@xa<\pgf@xb%
			\pgfpointadd{\centerpoint}{%
				\pgfpointborderrectangle{\externalpoint}{%
					\pgf@xc\halfside%
					\pgf@xc.166666\pgf@xc%
					\advance\pgf@xc\halfwidth%
					\pgfqpoint{\pgf@xc}{\halfheight}%
				}%
			}%
		\else%
			\ifdim\pgf@y=0pt\relax%
				\csname pgf@anchor@and gate CDH@east\endcsname%
			\else%
				\pgfextract@process\externalpoint{\pgfpointadd{\centerpoint}{\externalpoint}}%
				\pgf@xc\halfwidth%
				\advance\pgf@xc\halfside%
				\pgf@yc\halfheight%
				\advance\pgf@yc\halfside%
				\ifdim\pgf@ya<0pt%
					\pgfmathpointintersectionoflineandarc{\externalpoint}{\centerpoint}%
					{%
						\centerpoint%
						\advance\pgf@y\halfside%	
						\advance\pgf@x\pgf@xb%		
					}%
					{270}{330}{+\pgf@yc}%
				\else%
					\pgfmathpointintersectionoflineandarc{\externalpoint}{\centerpoint}%
					{%
						\centerpoint%
						\advance\pgf@y-\halfside%	
						\advance\pgf@x\pgf@xb%
					}%
					{30}{90}{+\pgf@xc and +\pgf@yc}%
				\fi%
			\fi%
		\fi%
	}
}


\pgfdeclareshape{nand gate CDH}{%
	\expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@nand gate CDH\endcsname{%
		\pgf@lib@sh@logicgate@parseinputs{1024}% Maximum 1024 (!) inputs.
		%
		\pgfmathloop%
		\ifnum\pgfmathcounter>\pgf@lib@sh@logicgate@numinputs%
		\else%
			\pgfutil@ifundefined{pgf@anchor@nand gate CDH@input \pgfmathcounter}{%
				\expandafter\xdef\csname pgf@anchor@nand gate CDH@input \pgfmathcounter\endcsname{%
					\noexpand\pgf@lib@sh@logicgate@AND@inputanchor{\pgfmathcounter}%
				}%
			}{}%
		\repeatpgfmathloop%
		\ifnum\pgf@lib@sh@logicgate@numinputs<2\relax%
			\PackageError{PGF}{A nand gate must have at least two inputs}{}%
		\fi%
	}%
	\savedmacro\numinputs{\let\numinputs\pgf@lib@sh@logicgate@numinputs}%
	\saveddimen\invertedradius{%
		\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
	}%
	\saveddimen\outerinvertedradius{%
		\pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/logic gate inverted radius}}%
		\advance\pgf@x.5\pgflinewidth%
	}
	\savedmacro\dimensions{\pgf@lib@sh@logicgates@dimensions@orUS}
	\savedanchor\centerpoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgf@y.5\ht\pgfnodeparttextbox%
		\advance\pgf@y-.5\dp\pgfnodeparttextbox%
	}
	\savedanchor\midpoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgfmathsetlength\pgf@y{+0.5ex}%
	}
	\savedanchor\basepoint{%
		\pgf@x.5\wd\pgfnodeparttextbox%
		\pgf@y0pt%
	}
	\anchor{center}{\centerpoint}%
	\anchor{mid}{\midpoint}%
	\inheritanchor[from=and gate CDH]{mid east}
	\inheritanchor[from=and gate CDH]{mid west}
	\anchor{base}{\basepoint}%
	\inheritanchor[from=and gate CDH]{base east}
	\inheritanchor[from=and gate CDH]{base west}
	\anchor{output}{%
		\dimensions
		\pgfpointadd{\centerpoint}{%
			\pgf@xa\halfside%
			\pgf@x-.166666\pgf@xa%
			\pgf@xb2.0\pgf@xa%
			\advance\pgf@x.866025\pgf@xb%
			\advance\pgf@x\invertedradius%
			\advance\pgf@x\outerinvertedradius%
			\advance\pgf@x\outerxsep%
			\pgf@y0pt%
		}%
	}
	\inheritanchor[from=and gate CDH]{east}
	\inheritanchor[from=and gate CDH]{north east}
	\inheritanchor[from=and gate CDH]{south east}
	\inheritanchor[from=and gate CDH]{north}
	\inheritanchor[from=and gate CDH]{south}
	\inheritanchor[from=and gate CDH]{south west}
	\inheritanchor[from=and gate CDH]{north west}
	\inheritanchor[from=and gate CDH]{west}
	\backgroundpath{%
		\dimensions%
		\pgf@xc\halfwidth%
		\pgf@yc\halfheight%
		\advance\pgf@xc-\outerxsep%
		\advance\pgf@yc-\outerysep%
		{%
			\pgftransformshift{\centerpoint}%
			\pgfpathmoveto{\pgfqpoint{-.16666\pgf@xc}{\pgf@yc}}%
			{%
				\pgf@yc2.0\pgf@yc%
				\edef\pgf@marshal{%
					\noexpand\pgfpatharc{90}{30}{\the\pgf@yc}%
				}%
				\pgf@marshal%
			}
			{%
				\pgf@yc2.0\pgf@yc%
				\edef\pgf@marshal{%
					\noexpand\pgfpatharc{-30}{-90}{\the\pgf@yc}%
				}%
				\pgf@marshal%
			}
			\pgfpathlineto{\pgfqpoint{-1.16666\pgf@xc}{-\pgf@yc}}%
			\pgfpathlineto{\pgfqpoint{-1.16666\pgf@xc}{\pgf@yc}}%
			\pgfpathclose%
			%
			% Draw the output inverter.
			%
			{%
				\pgfpathcircle{%
					\pgf@x-.166666\pgf@xc%
					\pgf@yc2.0\pgf@yc%
					\advance\pgf@x.866025\pgf@yc%
					\advance\pgf@x\outerinvertedradius%
					\pgf@y0pt%
				}{+\invertedradius}%			
			}%
			%
			% Draw the inputs.
			%
			\pgfutil@tempdima2.0\pgf@yc%
			\c@pgf@counta\numinputs%
			\advance\c@pgf@counta1\relax%
			\divide\pgfutil@tempdima\c@pgf@counta%
			\pgfmathloop%
			\ifnum\pgfmathcounter>\numinputs%
			\else%
				\advance\pgf@yc-\pgfutil@tempdima%
				\expandafter\ifx\expandafter\pgf@lib@sh@itext\csname input-\pgfmathcounter\endcsname%
					{%
						\pgfpathcircle{%
							\pgf@xa\halfside%
							\pgf@x-1.16666\pgf@xa%
							\advance\pgf@x-.5\pgflinewidth%
							\advance\pgf@x-\invertedradius%
							\pgf@y\pgf@yc%
						}{+\invertedradius}%				
					}%
				\fi%				
			\repeatpgfmathloop%
		}%
	}%
	\inheritanchorborder[from=and gate CDH]
}