Hackfix and re-enable strtoull and wcstoull, see bug #3798.
[sdcc.git] / sdcc / device / lib / _modulonglong.c
blob173fc30a52c628f75f02c3ae79979338e24b5c1c
1 /*-------------------------------------------------------------------------
2 _modulonglong.c - routine for modulus of 64 bit unsigned long long
4 Copyright (C) 1999, Sandeep Dutta . sandeep.dutta@usa.net
5 Bug fixes by Martijn van Balen, aed@iae.nl
6 Copyright (C) 2014, Philipp Klaus Krause . pkk@spth.de
8 This library is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this library; see the file COPYING. If not, write to the
20 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
21 MA 02110-1301, USA.
23 As a special exception, if you link this library with other files,
24 some of which are compiled with SDCC, to produce an executable,
25 this library does not by itself cause the resulting executable to
26 be covered by the GNU General Public License. This exception does
27 not however invalidate any other reasons why the executable file
28 might be covered by the GNU General Public License.
29 -------------------------------------------------------------------------*/
31 #include <stdint.h>
33 #include <sdcc-lib.h>
35 #ifdef __SDCC_LONGLONG
37 #define MSB_SET(x) ((x >> (8*sizeof(x)-1)) & 1)
39 unsigned long long
40 _modulonglong (unsigned long long a, unsigned long long b) __SDCC_NONBANKED
42 unsigned char count = 0;
44 while (!MSB_SET(b))
46 b <<= 1;
47 if (b > a)
49 b >>=1;
50 break;
52 count++;
56 if (a >= b)
57 a -= b;
58 b >>= 1;
60 while (count--);
62 return a;
64 #endif