Mercurial > nand-ecoscentric
comparison packages/language/c/libc/current/src/stdlib/ldiv.cxx @ 0:3111d98ba7b3 ecos-v1_1-release
Initial commit of eCos version 1.1
| author | jlarmour |
|---|---|
| date | Tue, 11 May 1999 11:16:07 +0000 |
| parents | |
| children | 443894e2e912 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:3111d98ba7b3 |
|---|---|
| 1 //=========================================================================== | |
| 2 // | |
| 3 // ldiv.cxx | |
| 4 // | |
| 5 // Real alternative for inline implementation of the ANSI standard | |
| 6 // ldiv() utility function defined in section 7.10.6.4 of the standard | |
| 7 // | |
| 8 //=========================================================================== | |
| 9 //####COPYRIGHTBEGIN#### | |
| 10 // | |
| 11 // ------------------------------------------- | |
| 12 // The contents of this file are subject to the Cygnus eCos Public License | |
| 13 // Version 1.0 (the "License"); you may not use this file except in | |
| 14 // compliance with the License. You may obtain a copy of the License at | |
| 15 // http://sourceware.cygnus.com/ecos | |
| 16 // | |
| 17 // Software distributed under the License is distributed on an "AS IS" | |
| 18 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the | |
| 19 // License for the specific language governing rights and limitations under | |
| 20 // the License. | |
| 21 // | |
| 22 // The Original Code is eCos - Embedded Cygnus Operating System, released | |
| 23 // September 30, 1998. | |
| 24 // | |
| 25 // The Initial Developer of the Original Code is Cygnus. Portions created | |
| 26 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved. | |
| 27 // ------------------------------------------- | |
| 28 // | |
| 29 //####COPYRIGHTEND#### | |
| 30 //=========================================================================== | |
| 31 //#####DESCRIPTIONBEGIN#### | |
| 32 // | |
| 33 // Author(s): jlarmour | |
| 34 // Contributors: jlarmour@cygnus.co.uk | |
| 35 // Date: 1998-02-13 | |
| 36 // Purpose: | |
| 37 // Description: | |
| 38 // Usage: | |
| 39 // | |
| 40 //####DESCRIPTIONEND#### | |
| 41 // | |
| 42 //=========================================================================== | |
| 43 // | |
| 44 // This code is based on code with the following copyright: | |
| 45 // | |
| 46 /* | |
| 47 * Copyright (c) 1990 Regents of the University of California. | |
| 48 * All rights reserved. | |
| 49 * | |
| 50 * This code is derived from software contributed to Berkeley by | |
| 51 * Chris Torek. | |
| 52 * | |
| 53 * Redistribution and use in source and binary forms, with or without | |
| 54 * modification, are permitted provided that the following conditions | |
| 55 * are met: | |
| 56 * 1. Redistributions of source code must retain the above copyright | |
| 57 * notice, this list of conditions and the following disclaimer. | |
| 58 * 2. Redistributions in binary form must reproduce the above copyright | |
| 59 * notice, this list of conditions and the following disclaimer in the | |
| 60 * documentation and/or other materials provided with the distribution. | |
| 61 * 3. All advertising materials mentioning features or use of this software | |
| 62 * must display the following acknowledgement: | |
| 63 * This product includes software developed by the University of | |
| 64 * California, Berkeley and its contributors. | |
| 65 * 4. Neither the name of the University nor the names of its contributors | |
| 66 * may be used to endorse or promote products derived from this software | |
| 67 * without specific prior written permission. | |
| 68 * | |
| 69 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
| 70 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 71 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 72 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
| 73 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 74 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 75 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 76 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 77 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 78 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 79 * SUCH DAMAGE. | |
| 80 */ | |
| 81 | |
| 82 | |
| 83 // CONFIGURATION | |
| 84 | |
| 85 #include <pkgconf/libc.h> // Configuration header | |
| 86 | |
| 87 // Include the C library? | |
| 88 #ifdef CYGPKG_LIBC | |
| 89 | |
| 90 // INCLUDES | |
| 91 | |
| 92 #include <cyg/infra/cyg_type.h> // Common type definitions and support | |
| 93 #include <cyg/infra/cyg_trac.h> // Tracing support | |
| 94 | |
| 95 // We don't want the inline versions of stdlib functions defined here | |
| 96 | |
| 97 #ifdef CYGIMP_LIBC_STDLIB_INLINES | |
| 98 #undef CYGIMP_LIBC_STDLIB_INLINES | |
| 99 #endif | |
| 100 | |
| 101 #include <stddef.h> // NULL, wchar_t and size_t from compiler | |
| 102 #include <stdlib.h> // Main header for stdlib functions | |
| 103 #include "clibincl/stdlibsupp.hxx" // Support for stdlib functions | |
| 104 | |
| 105 | |
| 106 // EXPORTED SYMBOLS | |
| 107 | |
| 108 externC ldiv_t | |
| 109 ldiv( long numer, long denom ) CYGPRI_LIBC_WEAK_ALIAS("_ldiv"); | |
| 110 | |
| 111 | |
| 112 // FUNCTIONS | |
| 113 | |
| 114 ldiv_t | |
| 115 _ldiv( long numer, long denom ) | |
| 116 { | |
| 117 ldiv_t result; | |
| 118 | |
| 119 CYG_REPORT_FUNCNAMETYPE( "_ldiv", "quotient: %d"); | |
| 120 CYG_REPORT_FUNCARG2( "numer=%d, denom=%d", numer, denom ); | |
| 121 | |
| 122 // The ANSI standard says that |r.quot| <= |n/d|, where | |
| 123 // n/d is to be computed in infinite precision. In other | |
| 124 // words, we should always truncate the quotient towards | |
| 125 // 0, never -infinity. | |
| 126 // | |
| 127 // Machine ldivision and remainer may work either way when | |
| 128 // one or both of n or d is negative. If only one is | |
| 129 // negative and r.quot has been truncated towards -inf, | |
| 130 // r.rem will have the same sign as denom and the opposite | |
| 131 // sign of num; if both are negative and r.quot has been | |
| 132 // truncated towards -inf, r.rem will be positive (will | |
| 133 // have the opposite sign of num). These are considered | |
| 134 // `wrong'. | |
| 135 // | |
| 136 // If both are num and denom are positive, r will always | |
| 137 // be positive. | |
| 138 // | |
| 139 // This all boils down to: | |
| 140 // if num >= 0, but r.rem < 0, we got the wrong answer. | |
| 141 // In that case, to get the right answer, add 1 to r.quot and | |
| 142 // subtract denom from r.rem. | |
| 143 | |
| 144 result.quot = numer / denom; | |
| 145 result.rem = numer % denom; | |
| 146 | |
| 147 if ( (numer >= 0) && (result.rem < 0) ) | |
| 148 { | |
| 149 result.quot++; | |
| 150 result.rem -= denom; | |
| 151 } // if | |
| 152 | |
| 153 CYG_REPORT_RETVAL( result.quot ); | |
| 154 | |
| 155 return result; | |
| 156 } // _ldiv() | |
| 157 | |
| 158 #endif // ifdef CYGPKG_LIBC | |
| 159 | |
| 160 // EOF ldiv.cxx |
