00001 /* BurrTools 00002 * 00003 * BurrTools is the legal property of its developers, whose 00004 * names are listed in the COPYRIGHT file, which is included 00005 * within the source distribution. 00006 * 00007 * This program is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU General Public License 00009 * as published by the Free Software Foundation; either version 2 00010 * of the License, or (at your option) any later version. 00011 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00020 */ 00021 #ifndef __INTDIV_H__ 00022 #define __INTDIV_H__ 00023 00031 template <class T> 00032 inline T intdiv_inf(T x, T y) 00033 { 00034 return x/y - ((x%y && (x%y^y)<0) ? 1 : 0); 00035 } 00036 00037 #if 0 00038 00039 /* the original Python function which also calculates the remainder */ 00040 00041 static enum divmod_result 00042 i_divmod(register long x, register long y, 00043 long *p_xdivy, long *p_xmody) 00044 { 00045 long xdivy, xmody; 00046 00047 if (y == 0) { 00048 PyErr_SetString(PyExc_ZeroDivisionError, 00049 "integer division or modulo by zero"); 00050 return DIVMOD_ERROR; 00051 } 00052 /* (-sys.maxint-1)/-1 is the only overflow case. */ 00053 if (y == -1 && UNARY_NEG_WOULD_OVERFLOW(x)) 00054 return DIVMOD_OVERFLOW; 00055 xdivy = x / y; 00056 xmody = x - xdivy * y; 00057 /* If the signs of x and y differ, and the remainder is non-0, 00058 * C89 doesn't define whether xdivy is now the floor or the 00059 * ceiling of the infinitely precise quotient. We want the floor, 00060 * and we have it iff the remainder's sign matches y's. 00061 */ 00062 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) { 00063 xmody += y; 00064 --xdivy; 00065 assert(xmody && ((y ^ xmody) >= 0)); 00066 } 00067 *p_xdivy = xdivy; 00068 *p_xmody = xmody; 00069 return DIVMOD_OK; 00070 } 00071 00072 #endif 00073 00074 #endif 00075