//-----------------------------------------------------------------------------
// Copyright © 2004 - Philip Howard - All rights reserved
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//-----------------------------------------------------------------------------
// package	libh/div
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	libh at ipal dot org
// homepage	http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 120 columns wide.
//-----------------------------------------------------------------------------

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// macro	quo_ni
//
// purpose	Calculate the integer quotient result of division truncated
//		towards negative infinity.  This operation would be used for
//		situations involving repeating periods that are not inverted
//		in the negative dividend range, such as calculations for date
//		and time purposes with arbitrary epochs.
//
// note		Standard C 1999 defines the / and % operators to calculate the
//		integer quotient result of division truncated towards zero.
//
// example	for the following values divided by 4:
//
// dividend	-9 -8 -7 -6 -5 -4 -3 -2 -1  0  1  2  3  4  5  6  7  8  9
//
//		the following is the result of division towards zero
// quotient	-2 -2 -1 -1 -1 -1  0  0  0  0  0  0  0  1  1  1  1  2  2
// remainder	-1  0 -3 -2 -1  0 -3 -2 -1  0  1  2  3  0  1  2  3  0  1
//
//		the following is the result of division towards -infinity
// quotient	-3 -2 -2 -2 -2 -1 -1 -1 -1  0  0  0  0  1  1  1  1  2  2
// modulus	 3  0  1  2  3  0  1  2  3  0  1  2  3  0  1  2  3  0  1
//
// arguments	1 (integer) dividend
//		2 (integer) divisor
//
// returns	(integer) quotient
//
// note		This macro assumes compliance with C99 / and % operators.
//		Implementations of C that are not in compliance with C99 in
//		this regard may produce incorrect results.
//
// note		Do not use this macro with non-integer argument types.
//		Results are undefined for anything but integers.
//-----------------------------------------------------------------------------
#define quo_ni(x,y) ({								\
	__typeof__( (x) ) libh__x;						\
	__typeof__( (y) ) libh__y;						\
	libh__x = (x);								\
	libh__y = (y);								\
	libh__x >= 0								\
		? libh__x / libh__y						\
		: ( libh__x + 1 ) / libh__y + ( libh__y >= 0 ? - 1 : 1 );	\
})

__FMACRO_END__

