Initial Spring 2016 commit.
This commit is contained in:
7
common/gcc-millicode/README
Normal file
7
common/gcc-millicode/README
Normal file
@@ -0,0 +1,7 @@
|
||||
These are support functions needed by gcc to perform operations on
|
||||
64-bit integer types on 32-bit machines. The names are meaningful to
|
||||
the gcc back end. The term "millicode" is sometimes used to describe
|
||||
such compiler support code.
|
||||
|
||||
This code was taken from NetBSD's src/common/lib/libc/quad, and
|
||||
polished some for OS/161.
|
56
common/gcc-millicode/adddi3.c
Normal file
56
common/gcc-millicode/adddi3.c
Normal file
@@ -0,0 +1,56 @@
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This software was developed by the Computer Systems Engineering group
|
||||
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
||||
* contributed to Berkeley.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* From:
|
||||
* @(#)adddi3.c 8.1 (Berkeley) 6/4/93
|
||||
* NetBSD: adddi3.c,v 1.1 2005/12/20 19:28:51 christos Exp
|
||||
*/
|
||||
|
||||
#include "longlong.h"
|
||||
|
||||
/*
|
||||
* Add two long longs. This is trivial since a one-bit carry from a
|
||||
* single unsigned int addition x+y occurs if and only if the sum x+y
|
||||
* is less than either x or y (the choice to compare with x or y is
|
||||
* arbitrary).
|
||||
*/
|
||||
long long
|
||||
__adddi3(long long a, long long b)
|
||||
{
|
||||
union uu aa, bb, sum;
|
||||
|
||||
aa.ll = a;
|
||||
bb.ll = b;
|
||||
sum.ui[L] = aa.ui[L] + bb.ui[L];
|
||||
sum.ui[H] = aa.ui[H] + bb.ui[H] + (sum.ui[L] < bb.ui[L]);
|
||||
return (sum.ll);
|
||||
}
|
53
common/gcc-millicode/anddi3.c
Normal file
53
common/gcc-millicode/anddi3.c
Normal file
@@ -0,0 +1,53 @@
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This software was developed by the Computer Systems Engineering group
|
||||
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
||||
* contributed to Berkeley.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* From:
|
||||
* @(#)anddi3.c 8.1 (Berkeley) 6/4/93
|
||||
* NetBSD: anddi3.c,v 1.1 2005/12/20 19:28:51 christos Exp
|
||||
*/
|
||||
|
||||
#include "longlong.h"
|
||||
|
||||
/*
|
||||
* Return a & b, in long long.
|
||||
*/
|
||||
long long
|
||||
__anddi3(long long a, long long b)
|
||||
{
|
||||
union uu aa, bb;
|
||||
|
||||
aa.ll = a;
|
||||
bb.ll = b;
|
||||
aa.ui[0] &= bb.ui[0];
|
||||
aa.ui[1] &= bb.ui[1];
|
||||
return (aa.ll);
|
||||
}
|
61
common/gcc-millicode/ashldi3.c
Normal file
61
common/gcc-millicode/ashldi3.c
Normal file
@@ -0,0 +1,61 @@
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This software was developed by the Computer Systems Engineering group
|
||||
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
||||
* contributed to Berkeley.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* From:
|
||||
* @(#)ashldi3.c 8.1 (Berkeley) 6/4/93
|
||||
* NetBSD: ashldi3.c,v 1.1 2005/12/20 19:28:51 christos Exp
|
||||
*/
|
||||
|
||||
#include "longlong.h"
|
||||
|
||||
/*
|
||||
* Shift a (signed) long long value left (arithmetic shift left).
|
||||
* This is the same as logical shift left!
|
||||
*/
|
||||
long long
|
||||
__ashldi3(long long a, unsigned int shift)
|
||||
{
|
||||
union uu aa;
|
||||
|
||||
if (shift == 0)
|
||||
return(a);
|
||||
aa.ll = a;
|
||||
if (shift >= INT_BITS) {
|
||||
aa.ui[H] = aa.ui[L] << (shift - INT_BITS);
|
||||
aa.ui[L] = 0;
|
||||
} else {
|
||||
aa.ui[H] = (aa.ui[H] << shift) |
|
||||
(aa.ui[L] >> (INT_BITS - shift));
|
||||
aa.ui[L] <<= shift;
|
||||
}
|
||||
return (aa.ll);
|
||||
}
|
73
common/gcc-millicode/ashrdi3.c
Normal file
73
common/gcc-millicode/ashrdi3.c
Normal file
@@ -0,0 +1,73 @@
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This software was developed by the Computer Systems Engineering group
|
||||
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
||||
* contributed to Berkeley.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* From:
|
||||
* @(#)ashrdi3.c 8.1 (Berkeley) 6/4/93
|
||||
* NetBSD: ashrdi3.c,v 1.1 2005/12/20 19:28:51 christos Exp
|
||||
*/
|
||||
|
||||
#include "longlong.h"
|
||||
|
||||
/*
|
||||
* Shift a (signed) long long value right (arithmetic shift right).
|
||||
*/
|
||||
long long
|
||||
__ashrdi3(long long a, unsigned int shift)
|
||||
{
|
||||
union uu aa;
|
||||
|
||||
if (shift == 0)
|
||||
return(a);
|
||||
aa.ll = a;
|
||||
if (shift >= INT_BITS) {
|
||||
int s;
|
||||
|
||||
/*
|
||||
* Smear bits rightward using the machine's right-shift
|
||||
* method, whether that is sign extension or zero fill,
|
||||
* to get the `sign word' s. Note that shifting by
|
||||
* INT_BITS is undefined, so we shift (INT_BITS-1),
|
||||
* then 1 more, to get our answer.
|
||||
*/
|
||||
/* LINTED inherits machine dependency */
|
||||
s = (aa.si[H] >> (INT_BITS - 1)) >> 1;
|
||||
/* LINTED inherits machine dependency*/
|
||||
aa.ui[L] = aa.si[H] >> (shift - INT_BITS);
|
||||
aa.ui[H] = s;
|
||||
} else {
|
||||
aa.ui[L] = (aa.ui[L] >> shift) |
|
||||
(aa.ui[H] << (INT_BITS - shift));
|
||||
/* LINTED inherits machine dependency */
|
||||
aa.si[H] >>= shift;
|
||||
}
|
||||
return (aa.ll);
|
||||
}
|
54
common/gcc-millicode/cmpdi2.c
Normal file
54
common/gcc-millicode/cmpdi2.c
Normal file
@@ -0,0 +1,54 @@
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This software was developed by the Computer Systems Engineering group
|
||||
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
||||
* contributed to Berkeley.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* From:
|
||||
* @(#)cmpdi2.c 8.1 (Berkeley) 6/4/93
|
||||
* NetBSD: cmpdi2.c,v 1.1 2005/12/20 19:28:51 christos Exp
|
||||
*/
|
||||
|
||||
#include "longlong.h"
|
||||
|
||||
/*
|
||||
* Return 0, 1, or 2 as a <, =, > b respectively.
|
||||
* Both a and b are considered signed---which means only the high word is
|
||||
* signed.
|
||||
*/
|
||||
int
|
||||
__cmpdi2(long long a, long long b)
|
||||
{
|
||||
union uu aa, bb;
|
||||
|
||||
aa.ll = a;
|
||||
bb.ll = b;
|
||||
return (aa.si[H] < bb.si[H] ? 0 : aa.si[H] > bb.si[H] ? 2 :
|
||||
aa.ui[L] < bb.ui[L] ? 0 : aa.ui[L] > bb.ui[L] ? 2 : 1);
|
||||
}
|
62
common/gcc-millicode/divdi3.c
Normal file
62
common/gcc-millicode/divdi3.c
Normal file
@@ -0,0 +1,62 @@
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This software was developed by the Computer Systems Engineering group
|
||||
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
||||
* contributed to Berkeley.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* From:
|
||||
* @(#)divdi3.c 8.1 (Berkeley) 6/4/93
|
||||
* NetBSD: divdi3.c,v 1.1 2005/12/20 19:28:51 christos Exp
|
||||
*/
|
||||
|
||||
#include "longlong.h"
|
||||
|
||||
/*
|
||||
* Divide two signed long longs.
|
||||
* ??? if -1/2 should produce -1 on this machine, this code is wrong
|
||||
*/
|
||||
long long
|
||||
__divdi3(long long a, long long b)
|
||||
{
|
||||
unsigned long long ua, ub, uq;
|
||||
int neg = 0;
|
||||
|
||||
ua = a;
|
||||
ub = b;
|
||||
|
||||
if (a < 0)
|
||||
ua = -ua, neg ^= 1;
|
||||
if (b < 0)
|
||||
ub = -ub, neg ^= 1;
|
||||
|
||||
uq = __qdivrem(ua, ub, NULL);
|
||||
if (neg)
|
||||
uq = - uq;
|
||||
return uq;
|
||||
}
|
53
common/gcc-millicode/iordi3.c
Normal file
53
common/gcc-millicode/iordi3.c
Normal file
@@ -0,0 +1,53 @@
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This software was developed by the Computer Systems Engineering group
|
||||
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
||||
* contributed to Berkeley.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* From:
|
||||
* @(#)iordi3.c 8.1 (Berkeley) 6/4/93
|
||||
* NetBSD: iordi3.c,v 1.1 2005/12/20 19:28:51 christos Exp
|
||||
*/
|
||||
|
||||
#include "longlong.h"
|
||||
|
||||
/*
|
||||
* Return a | b, in long long.
|
||||
*/
|
||||
long long
|
||||
__iordi3(long long a, long long b)
|
||||
{
|
||||
union uu aa, bb;
|
||||
|
||||
aa.ll = a;
|
||||
bb.ll = b;
|
||||
aa.ui[0] |= bb.ui[0];
|
||||
aa.ui[1] |= bb.ui[1];
|
||||
return (aa.ll);
|
||||
}
|
144
common/gcc-millicode/longlong.h
Normal file
144
common/gcc-millicode/longlong.h
Normal file
@@ -0,0 +1,144 @@
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This software was developed by the Computer Systems Engineering group
|
||||
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
||||
* contributed to Berkeley.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* From:
|
||||
* @(#)quad.h 8.1 (Berkeley) 6/4/93
|
||||
* NetBSD: quad.h,v 1.1 2005/12/20 20:29:40 christos Exp
|
||||
*/
|
||||
|
||||
/*
|
||||
* Long long arithmetic.
|
||||
*
|
||||
* This library makes the following assumptions:
|
||||
*
|
||||
* - The type long long exists.
|
||||
*
|
||||
* - A long long variable is exactly twice as long as `int'.
|
||||
*
|
||||
* - The machine's arithmetic is two's complement.
|
||||
*
|
||||
* This library can provide 128-bit arithmetic on a machine with
|
||||
* 128-bit long longs and 64-bit ints, for instance, or 96-bit
|
||||
* arithmetic on machines with 48-bit ints.
|
||||
*
|
||||
* The names are built into gcc.
|
||||
*/
|
||||
|
||||
#if defined(_KERNEL)
|
||||
#include <types.h>
|
||||
#include <endian.h>
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/endian.h>
|
||||
#endif
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
/*
|
||||
* Depending on the desired operation, we view a `long long' in
|
||||
* one or more of the following formats.
|
||||
*/
|
||||
union uu {
|
||||
long long ll; /* as a (signed) long long */
|
||||
unsigned long long ull; /* as an unsigned long long */
|
||||
int si[2]; /* as two (signed) ints */
|
||||
unsigned int ui[2]; /* as two unsigned ints */
|
||||
};
|
||||
|
||||
/*
|
||||
* Define high and low parts of a long long.
|
||||
*/
|
||||
#if _BYTE_ORDER == _LITTLE_ENDIAN
|
||||
#define H 1
|
||||
#define L 0
|
||||
#endif
|
||||
|
||||
#if _BYTE_ORDER == _BIG_ENDIAN
|
||||
#define H 0
|
||||
#define L 1
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* Total number of bits in a long long and in the pieces that make it up.
|
||||
* These are used for shifting, and also below for halfword extraction
|
||||
* and assembly.
|
||||
*/
|
||||
#define LONGLONG_BITS (sizeof(long long) * CHAR_BIT)
|
||||
#define INT_BITS (sizeof(int) * CHAR_BIT)
|
||||
#define HALF_BITS (sizeof(int) * CHAR_BIT / 2)
|
||||
|
||||
/*
|
||||
* Extract high and low shortwords from longword, and move low shortword of
|
||||
* longword to upper half of long, i.e., produce the upper longword of
|
||||
* ((long long)(x) << (number_of_bits_in_int/2)).
|
||||
* [`x' must actually be unsigned int.]
|
||||
*
|
||||
* These are used in the multiply code, to split a longword into upper
|
||||
* and lower halves, and to reassemble a product as a long long, shifted
|
||||
* left (sizeof(int)*CHAR_BIT/2).
|
||||
*/
|
||||
#define HHALF(x) ((unsigned int)(x) >> HALF_BITS)
|
||||
#define LHALF(x) ((unsigned int)(x) & (((int)1 << HALF_BITS) - 1))
|
||||
#define LHUP(x) ((unsigned int)(x) << HALF_BITS)
|
||||
|
||||
long long __adddi3 ( long long, long long);
|
||||
long long __anddi3 ( long long, long long);
|
||||
long long __ashldi3 ( long long, unsigned int);
|
||||
long long __ashrdi3 ( long long, unsigned int);
|
||||
int __cmpdi2 ( long long, long long);
|
||||
long long __divdi3 ( long long, long long);
|
||||
long long __iordi3 ( long long, long long);
|
||||
long long __lshldi3 ( long long, unsigned int);
|
||||
long long __lshrdi3 ( long long, unsigned int);
|
||||
long long __moddi3 ( long long, long long);
|
||||
long long __muldi3 ( long long, long long);
|
||||
long long __negdi2 ( long long);
|
||||
long long __one_cmpldi2 ( long long);
|
||||
long long __subdi3 ( long long, long long);
|
||||
int __ucmpdi2 (unsigned long long, unsigned long long);
|
||||
unsigned long long __udivdi3 (unsigned long long, unsigned long long);
|
||||
unsigned long long __umoddi3 (unsigned long long, unsigned long long);
|
||||
long long __xordi3 ( long long, long long);
|
||||
|
||||
#ifndef _KERNEL
|
||||
long long __fixdfdi (double);
|
||||
long long __fixsfdi (float);
|
||||
unsigned long long __fixunsdfdi (double);
|
||||
unsigned long long __fixunssfdi (float);
|
||||
double __floatdidf (long long);
|
||||
float __floatdisf (long long);
|
||||
double __floatunsdidf(unsigned long long);
|
||||
#endif
|
||||
|
||||
unsigned long long __qdivrem (unsigned long long, unsigned long long,
|
||||
unsigned long long *);
|
61
common/gcc-millicode/lshldi3.c
Normal file
61
common/gcc-millicode/lshldi3.c
Normal file
@@ -0,0 +1,61 @@
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This software was developed by the Computer Systems Engineering group
|
||||
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
||||
* contributed to Berkeley.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* From:
|
||||
* @(#)lshldi3.c 8.1 (Berkeley) 6/4/93
|
||||
* NetBSD: lshldi3.c,v 1.1 2005/12/20 19:28:51 christos Exp
|
||||
*/
|
||||
|
||||
#include "longlong.h"
|
||||
|
||||
/*
|
||||
* Shift an (unsigned) long long value left (logical shift left).
|
||||
* This is the same as arithmetic shift left!
|
||||
*/
|
||||
long long
|
||||
__lshldi3(long long a, unsigned int shift)
|
||||
{
|
||||
union uu aa;
|
||||
|
||||
if (shift == 0)
|
||||
return(a);
|
||||
aa.ll = a;
|
||||
if (shift >= INT_BITS) {
|
||||
aa.ui[H] = aa.ui[L] << (shift - INT_BITS);
|
||||
aa.ui[L] = 0;
|
||||
} else {
|
||||
aa.ui[H] = (aa.ui[H] << shift) |
|
||||
(aa.ui[L] >> (INT_BITS - shift));
|
||||
aa.ui[L] <<= shift;
|
||||
}
|
||||
return (aa.ll);
|
||||
}
|
60
common/gcc-millicode/lshrdi3.c
Normal file
60
common/gcc-millicode/lshrdi3.c
Normal file
@@ -0,0 +1,60 @@
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This software was developed by the Computer Systems Engineering group
|
||||
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
||||
* contributed to Berkeley.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* From:
|
||||
* @(#)lshrdi3.c 8.1 (Berkeley) 6/4/93
|
||||
* $NetBSD: lshrdi3.c,v 1.1 2005/12/20 19:28:51 christos Exp
|
||||
*/
|
||||
|
||||
#include "longlong.h"
|
||||
|
||||
/*
|
||||
* Shift an (unsigned) long long value right (logical shift right).
|
||||
*/
|
||||
long long
|
||||
__lshrdi3(long long a, unsigned int shift)
|
||||
{
|
||||
union uu aa;
|
||||
|
||||
if (shift == 0)
|
||||
return(a);
|
||||
aa.ll = a;
|
||||
if (shift >= INT_BITS) {
|
||||
aa.ui[L] = aa.ui[H] >> (shift - INT_BITS);
|
||||
aa.ui[H] = 0;
|
||||
} else {
|
||||
aa.ui[L] = (aa.ui[L] >> shift) |
|
||||
(aa.ui[H] << (INT_BITS - shift));
|
||||
aa.ui[H] >>= shift;
|
||||
}
|
||||
return (aa.ll);
|
||||
}
|
62
common/gcc-millicode/moddi3.c
Normal file
62
common/gcc-millicode/moddi3.c
Normal file
@@ -0,0 +1,62 @@
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This software was developed by the Computer Systems Engineering group
|
||||
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
||||
* contributed to Berkeley.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* From:
|
||||
* @(#)moddi3.c 8.1 (Berkeley) 6/4/93
|
||||
* NetBSD: moddi3.c,v 1.1 2005/12/20 19:28:51 christos Exp
|
||||
*/
|
||||
|
||||
#include "longlong.h"
|
||||
|
||||
/*
|
||||
* Return remainder after dividing two signed long longs.
|
||||
*
|
||||
* XXX we assume a % b < 0 iff a < 0, but this is actually machine-dependent.
|
||||
*/
|
||||
long long
|
||||
__moddi3(long long a, long long b)
|
||||
{
|
||||
unsigned long long ua, ub, ur;
|
||||
int neg = 0;
|
||||
|
||||
ua = a;
|
||||
ub = b;
|
||||
|
||||
if (a < 0)
|
||||
ua = -ua, neg ^= 1;
|
||||
if (b < 0)
|
||||
ub = -ub;
|
||||
(void)__qdivrem(ua, ub, &ur);
|
||||
if (neg)
|
||||
ur = -ur;
|
||||
return (ur);
|
||||
}
|
241
common/gcc-millicode/muldi3.c
Normal file
241
common/gcc-millicode/muldi3.c
Normal file
@@ -0,0 +1,241 @@
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This software was developed by the Computer Systems Engineering group
|
||||
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
||||
* contributed to Berkeley.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* From:
|
||||
* @(#)muldi3.c 8.1 (Berkeley) 6/4/93
|
||||
* NetBSD: muldi3.c,v 1.1 2005/12/20 19:28:51 christos Exp
|
||||
*/
|
||||
|
||||
#include "longlong.h"
|
||||
|
||||
/*
|
||||
* Multiply two long longs.
|
||||
*
|
||||
* Our algorithm is based on the following. Split incoming long long
|
||||
* values u and v (where u,v >= 0) into
|
||||
*
|
||||
* u = 2^n u1 * u0 (n = number of bits in `unsigned int', usu. 32)
|
||||
*
|
||||
* and
|
||||
*
|
||||
* v = 2^n v1 * v0
|
||||
*
|
||||
* Then
|
||||
*
|
||||
* uv = 2^2n u1 v1 + 2^n u1 v0 + 2^n v1 u0 + u0 v0
|
||||
* = 2^2n u1 v1 + 2^n (u1 v0 + v1 u0) + u0 v0
|
||||
*
|
||||
* Now add 2^n u1 v1 to the first term and subtract it from the middle,
|
||||
* and add 2^n u0 v0 to the last term and subtract it from the middle.
|
||||
* This gives:
|
||||
*
|
||||
* uv = (2^2n + 2^n) (u1 v1) +
|
||||
* (2^n) (u1 v0 - u1 v1 + u0 v1 - u0 v0) +
|
||||
* (2^n + 1) (u0 v0)
|
||||
*
|
||||
* Factoring the middle a bit gives us:
|
||||
*
|
||||
* uv = (2^2n + 2^n) (u1 v1) + [u1v1 = high]
|
||||
* (2^n) (u1 - u0) (v0 - v1) + [(u1-u0)... = mid]
|
||||
* (2^n + 1) (u0 v0) [u0v0 = low]
|
||||
*
|
||||
* The terms (u1 v1), (u1 - u0) (v0 - v1), and (u0 v0) can all be done
|
||||
* in just half the precision of the original. (Note that either or both
|
||||
* of (u1 - u0) or (v0 - v1) may be negative.)
|
||||
*
|
||||
* This algorithm is from Knuth vol. 2 (2nd ed), section 4.3.3, p. 278.
|
||||
*
|
||||
* Since C does not give us a `int * int = long long' operator, we split
|
||||
* our input long longs into two ints, then split the two ints into two
|
||||
* shorts. We can then calculate `short * short = int' in native
|
||||
* arithmetic.
|
||||
*
|
||||
* Our product should, strictly speaking, be a `long long long', with
|
||||
* 128 bits, but we are going to discard the upper 64. In other words,
|
||||
* we are not interested in uv, but rather in (uv mod 2^2n). This
|
||||
* makes some of the terms above vanish, and we get:
|
||||
*
|
||||
* (2^n)(high) + (2^n)(mid) + (2^n + 1)(low)
|
||||
*
|
||||
* or
|
||||
*
|
||||
* (2^n)(high + mid + low) + low
|
||||
*
|
||||
* Furthermore, `high' and `mid' can be computed mod 2^n, as any factor
|
||||
* of 2^n in either one will also vanish. Only `low' need be computed
|
||||
* mod 2^2n, and only because of the final term above.
|
||||
*/
|
||||
static long long __lmulq(unsigned int, unsigned int);
|
||||
|
||||
long long
|
||||
__muldi3(long long a, long long b)
|
||||
{
|
||||
union uu u, v, low, prod;
|
||||
unsigned int high, mid, udiff, vdiff;
|
||||
int negall, negmid;
|
||||
#define u1 u.ui[H]
|
||||
#define u0 u.ui[L]
|
||||
#define v1 v.ui[H]
|
||||
#define v0 v.ui[L]
|
||||
|
||||
/*
|
||||
* Get u and v such that u, v >= 0. When this is finished,
|
||||
* u1, u0, v1, and v0 will be directly accessible through the
|
||||
* int fields.
|
||||
*/
|
||||
if (a >= 0)
|
||||
u.ll = a, negall = 0;
|
||||
else
|
||||
u.ll = -a, negall = 1;
|
||||
if (b >= 0)
|
||||
v.ll = b;
|
||||
else
|
||||
v.ll = -b, negall ^= 1;
|
||||
|
||||
if (u1 == 0 && v1 == 0) {
|
||||
/*
|
||||
* An (I hope) important optimization occurs when u1 and v1
|
||||
* are both 0. This should be common since most numbers
|
||||
* are small. Here the product is just u0*v0.
|
||||
*/
|
||||
prod.ll = __lmulq(u0, v0);
|
||||
} else {
|
||||
/*
|
||||
* Compute the three intermediate products, remembering
|
||||
* whether the middle term is negative. We can discard
|
||||
* any upper bits in high and mid, so we can use native
|
||||
* unsigned int * unsigned int => unsigned int arithmetic.
|
||||
*/
|
||||
low.ll = __lmulq(u0, v0);
|
||||
|
||||
if (u1 >= u0)
|
||||
negmid = 0, udiff = u1 - u0;
|
||||
else
|
||||
negmid = 1, udiff = u0 - u1;
|
||||
if (v0 >= v1)
|
||||
vdiff = v0 - v1;
|
||||
else
|
||||
vdiff = v1 - v0, negmid ^= 1;
|
||||
mid = udiff * vdiff;
|
||||
|
||||
high = u1 * v1;
|
||||
|
||||
/*
|
||||
* Assemble the final product.
|
||||
*/
|
||||
prod.ui[H] = high + (negmid ? -mid : mid) + low.ui[L] +
|
||||
low.ui[H];
|
||||
prod.ui[L] = low.ui[L];
|
||||
}
|
||||
return (negall ? -prod.ll : prod.ll);
|
||||
#undef u1
|
||||
#undef u0
|
||||
#undef v1
|
||||
#undef v0
|
||||
}
|
||||
|
||||
/*
|
||||
* Multiply two 2N-bit ints to produce a 4N-bit long long, where N is
|
||||
* half the number of bits in an int (whatever that is---the code
|
||||
* below does not care as long as the header file does its part of the
|
||||
* bargain---but typically N==16).
|
||||
*
|
||||
* We use the same algorithm from Knuth, but this time the modulo refinement
|
||||
* does not apply. On the other hand, since N is half the size of an int,
|
||||
* we can get away with native multiplication---none of our input terms
|
||||
* exceeds (UINT_MAX >> 1).
|
||||
*
|
||||
* Note that, for unsigned int l, the quad-precision (long long) result
|
||||
*
|
||||
* l << N
|
||||
*
|
||||
* splits into high and low ints as HHALF(l) and LHUP(l) respectively.
|
||||
*/
|
||||
static long long
|
||||
__lmulq(unsigned int u, unsigned int v)
|
||||
{
|
||||
unsigned int u1, u0, v1, v0, udiff, vdiff, high, mid, low;
|
||||
unsigned int prodh, prodl, was;
|
||||
union uu prod;
|
||||
int neg;
|
||||
|
||||
u1 = HHALF(u);
|
||||
u0 = LHALF(u);
|
||||
v1 = HHALF(v);
|
||||
v0 = LHALF(v);
|
||||
|
||||
low = u0 * v0;
|
||||
|
||||
/* This is the same small-number optimization as before. */
|
||||
if (u1 == 0 && v1 == 0)
|
||||
return (low);
|
||||
|
||||
if (u1 >= u0)
|
||||
udiff = u1 - u0, neg = 0;
|
||||
else
|
||||
udiff = u0 - u1, neg = 1;
|
||||
if (v0 >= v1)
|
||||
vdiff = v0 - v1;
|
||||
else
|
||||
vdiff = v1 - v0, neg ^= 1;
|
||||
mid = udiff * vdiff;
|
||||
|
||||
high = u1 * v1;
|
||||
|
||||
/* prod = (high << 2N) + (high << N); */
|
||||
prodh = high + HHALF(high);
|
||||
prodl = LHUP(high);
|
||||
|
||||
/* if (neg) prod -= mid << N; else prod += mid << N; */
|
||||
if (neg) {
|
||||
was = prodl;
|
||||
prodl -= LHUP(mid);
|
||||
prodh -= HHALF(mid) + (prodl > was);
|
||||
} else {
|
||||
was = prodl;
|
||||
prodl += LHUP(mid);
|
||||
prodh += HHALF(mid) + (prodl < was);
|
||||
}
|
||||
|
||||
/* prod += low << N */
|
||||
was = prodl;
|
||||
prodl += LHUP(low);
|
||||
prodh += HHALF(low) + (prodl < was);
|
||||
/* ... + low; */
|
||||
if ((prodl += low) < low)
|
||||
prodh++;
|
||||
|
||||
/* return 4N-bit product */
|
||||
prod.ui[H] = prodh;
|
||||
prod.ui[L] = prodl;
|
||||
return (prod.ll);
|
||||
}
|
52
common/gcc-millicode/negdi2.c
Normal file
52
common/gcc-millicode/negdi2.c
Normal file
@@ -0,0 +1,52 @@
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This software was developed by the Computer Systems Engineering group
|
||||
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
||||
* contributed to Berkeley.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* From:
|
||||
* @(#)negdi2.c 8.1 (Berkeley) 6/4/93
|
||||
* NetBSD: negdi2.c,v 1.1 2005/12/20 19:28:51 christos Exp
|
||||
*/
|
||||
|
||||
#include "longlong.h"
|
||||
|
||||
/*
|
||||
* Return -a (or, equivalently, 0 - a), in long long. See subdi3.c.
|
||||
*/
|
||||
long long
|
||||
__negdi2(long long a)
|
||||
{
|
||||
union uu aa, res;
|
||||
|
||||
aa.ll = a;
|
||||
res.ui[L] = -aa.ui[L];
|
||||
res.ui[H] = -aa.ui[H] - (res.ui[L] > 0);
|
||||
return (res.ll);
|
||||
}
|
53
common/gcc-millicode/notdi2.c
Normal file
53
common/gcc-millicode/notdi2.c
Normal file
@@ -0,0 +1,53 @@
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This software was developed by the Computer Systems Engineering group
|
||||
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
||||
* contributed to Berkeley.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* From:
|
||||
* @(#)notdi2.c 8.1 (Berkeley) 6/4/93
|
||||
* NetBSD: notdi2.c,v 1.1 2005/12/20 19:28:51 christos Exp
|
||||
*/
|
||||
|
||||
#include "longlong.h"
|
||||
|
||||
/*
|
||||
* Return ~a. For some reason gcc calls this `one's complement' rather
|
||||
* than `not'.
|
||||
*/
|
||||
long long
|
||||
__one_cmpldi2(long long a)
|
||||
{
|
||||
union uu aa;
|
||||
|
||||
aa.ll = a;
|
||||
aa.ui[0] = ~aa.ui[0];
|
||||
aa.ui[1] = ~aa.ui[1];
|
||||
return (aa.ll);
|
||||
}
|
279
common/gcc-millicode/qdivrem.c
Normal file
279
common/gcc-millicode/qdivrem.c
Normal file
@@ -0,0 +1,279 @@
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This software was developed by the Computer Systems Engineering group
|
||||
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
||||
* contributed to Berkeley.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* From:
|
||||
* @(#)qdivrem.c 8.1 (Berkeley) 6/4/93
|
||||
* NetBSD: qdivrem.c,v 1.1 2005/12/20 19:28:51 christos Exp
|
||||
*/
|
||||
|
||||
/*
|
||||
* Multiprecision divide. This algorithm is from Knuth vol. 2 (2nd ed),
|
||||
* section 4.3.1, pp. 257--259.
|
||||
*/
|
||||
|
||||
#include "longlong.h"
|
||||
|
||||
#define B ((int)1 << HALF_BITS) /* digit base */
|
||||
|
||||
/* Combine two `digits' to make a single two-digit number. */
|
||||
#define COMBINE(a, b) (((unsigned int)(a) << HALF_BITS) | (b))
|
||||
|
||||
/* select a type for digits in base B: use unsigned short if they fit */
|
||||
#if UINT_MAX == 0xffffffffU && USHRT_MAX >= 0xffff
|
||||
typedef unsigned short digit;
|
||||
#else
|
||||
typedef unsigned int digit;
|
||||
#endif
|
||||
|
||||
static void shl(digit *p, int len, int sh);
|
||||
|
||||
/*
|
||||
* __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v.
|
||||
*
|
||||
* We do this in base 2-sup-HALF_BITS, so that all intermediate
|
||||
* products fit within unsigned int. As a consequence, the maximum
|
||||
* length dividend and divisor are 4 `digits' in this base (they are
|
||||
* shorter if they have leading zeros).
|
||||
*/
|
||||
unsigned long long
|
||||
__qdivrem(unsigned long long ull, unsigned long long vll,
|
||||
unsigned long long *arq)
|
||||
{
|
||||
union uu tmp;
|
||||
digit *u, *v, *q;
|
||||
digit v1, v2;
|
||||
unsigned int qhat, rhat, t;
|
||||
int m, n, d, j, i;
|
||||
digit uspace[5], vspace[5], qspace[5];
|
||||
|
||||
/*
|
||||
* Take care of special cases: divide by zero, and u < v.
|
||||
*/
|
||||
if (vll == 0) {
|
||||
/* divide by zero. */
|
||||
static volatile const unsigned int zero = 0;
|
||||
|
||||
tmp.ui[H] = tmp.ui[L] = 1 / zero;
|
||||
if (arq)
|
||||
*arq = ull;
|
||||
return (tmp.ll);
|
||||
}
|
||||
if (ull < vll) {
|
||||
if (arq)
|
||||
*arq = ull;
|
||||
return (0);
|
||||
}
|
||||
u = &uspace[0];
|
||||
v = &vspace[0];
|
||||
q = &qspace[0];
|
||||
|
||||
/*
|
||||
* Break dividend and divisor into digits in base B, then
|
||||
* count leading zeros to determine m and n. When done, we
|
||||
* will have:
|
||||
* u = (u[1]u[2]...u[m+n]) sub B
|
||||
* v = (v[1]v[2]...v[n]) sub B
|
||||
* v[1] != 0
|
||||
* 1 < n <= 4 (if n = 1, we use a different division algorithm)
|
||||
* m >= 0 (otherwise u < v, which we already checked)
|
||||
* m + n = 4
|
||||
* and thus
|
||||
* m = 4 - n <= 2
|
||||
*/
|
||||
tmp.ull = ull;
|
||||
u[0] = 0;
|
||||
u[1] = (digit)HHALF(tmp.ui[H]);
|
||||
u[2] = (digit)LHALF(tmp.ui[H]);
|
||||
u[3] = (digit)HHALF(tmp.ui[L]);
|
||||
u[4] = (digit)LHALF(tmp.ui[L]);
|
||||
tmp.ull = vll;
|
||||
v[1] = (digit)HHALF(tmp.ui[H]);
|
||||
v[2] = (digit)LHALF(tmp.ui[H]);
|
||||
v[3] = (digit)HHALF(tmp.ui[L]);
|
||||
v[4] = (digit)LHALF(tmp.ui[L]);
|
||||
for (n = 4; v[1] == 0; v++) {
|
||||
if (--n == 1) {
|
||||
unsigned int rbj; /* r*B+u[j] (not root boy jim) */
|
||||
digit q1, q2, q3, q4;
|
||||
|
||||
/*
|
||||
* Change of plan, per exercise 16.
|
||||
* r = 0;
|
||||
* for j = 1..4:
|
||||
* q[j] = floor((r*B + u[j]) / v),
|
||||
* r = (r*B + u[j]) % v;
|
||||
* We unroll this completely here.
|
||||
*/
|
||||
t = v[2]; /* nonzero, by definition */
|
||||
q1 = (digit)(u[1] / t);
|
||||
rbj = COMBINE(u[1] % t, u[2]);
|
||||
q2 = (digit)(rbj / t);
|
||||
rbj = COMBINE(rbj % t, u[3]);
|
||||
q3 = (digit)(rbj / t);
|
||||
rbj = COMBINE(rbj % t, u[4]);
|
||||
q4 = (digit)(rbj / t);
|
||||
if (arq)
|
||||
*arq = rbj % t;
|
||||
tmp.ui[H] = COMBINE(q1, q2);
|
||||
tmp.ui[L] = COMBINE(q3, q4);
|
||||
return (tmp.ll);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* By adjusting q once we determine m, we can guarantee that
|
||||
* there is a complete four-digit quotient at &qspace[1] when
|
||||
* we finally stop.
|
||||
*/
|
||||
for (m = 4 - n; u[1] == 0; u++)
|
||||
m--;
|
||||
for (i = 4 - m; --i >= 0;)
|
||||
q[i] = 0;
|
||||
q += 4 - m;
|
||||
|
||||
/*
|
||||
* Here we run Program D, translated from MIX to C and acquiring
|
||||
* a few minor changes.
|
||||
*
|
||||
* D1: choose multiplier 1 << d to ensure v[1] >= B/2.
|
||||
*/
|
||||
d = 0;
|
||||
for (t = v[1]; t < B / 2; t <<= 1)
|
||||
d++;
|
||||
if (d > 0) {
|
||||
shl(&u[0], m + n, d); /* u <<= d */
|
||||
shl(&v[1], n - 1, d); /* v <<= d */
|
||||
}
|
||||
/*
|
||||
* D2: j = 0.
|
||||
*/
|
||||
j = 0;
|
||||
v1 = v[1]; /* for D3 -- note that v[1..n] are constant */
|
||||
v2 = v[2]; /* for D3 */
|
||||
do {
|
||||
digit uj0, uj1, uj2;
|
||||
|
||||
/*
|
||||
* D3: Calculate qhat (\^q, in TeX notation).
|
||||
* Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and
|
||||
* let rhat = (u[j]*B + u[j+1]) mod v[1].
|
||||
* While rhat < B and v[2]*qhat > rhat*B+u[j+2],
|
||||
* decrement qhat and increase rhat correspondingly.
|
||||
* Note that if rhat >= B, v[2]*qhat < rhat*B.
|
||||
*/
|
||||
uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */
|
||||
uj1 = u[j + 1]; /* for D3 only */
|
||||
uj2 = u[j + 2]; /* for D3 only */
|
||||
if (uj0 == v1) {
|
||||
qhat = B;
|
||||
rhat = uj1;
|
||||
goto qhat_too_big;
|
||||
} else {
|
||||
unsigned int nn = COMBINE(uj0, uj1);
|
||||
qhat = nn / v1;
|
||||
rhat = nn % v1;
|
||||
}
|
||||
while (v2 * qhat > COMBINE(rhat, uj2)) {
|
||||
qhat_too_big:
|
||||
qhat--;
|
||||
if ((rhat += v1) >= B)
|
||||
break;
|
||||
}
|
||||
/*
|
||||
* D4: Multiply and subtract.
|
||||
* The variable `t' holds any borrows across the loop.
|
||||
* We split this up so that we do not require v[0] = 0,
|
||||
* and to eliminate a final special case.
|
||||
*/
|
||||
for (t = 0, i = n; i > 0; i--) {
|
||||
t = u[i + j] - v[i] * qhat - t;
|
||||
u[i + j] = (digit)LHALF(t);
|
||||
t = (B - HHALF(t)) & (B - 1);
|
||||
}
|
||||
t = u[j] - t;
|
||||
u[j] = (digit)LHALF(t);
|
||||
/*
|
||||
* D5: test remainder.
|
||||
* There is a borrow if and only if HHALF(t) is nonzero;
|
||||
* in that (rare) case, qhat was too large (by exactly 1).
|
||||
* Fix it by adding v[1..n] to u[j..j+n].
|
||||
*/
|
||||
if (HHALF(t)) {
|
||||
qhat--;
|
||||
for (t = 0, i = n; i > 0; i--) { /* D6: add back. */
|
||||
t += u[i + j] + v[i];
|
||||
u[i + j] = (digit)LHALF(t);
|
||||
t = HHALF(t);
|
||||
}
|
||||
u[j] = (digit)LHALF(u[j] + t);
|
||||
}
|
||||
q[j] = (digit)qhat;
|
||||
} while (++j <= m); /* D7: loop on j. */
|
||||
|
||||
/*
|
||||
* If caller wants the remainder, we have to calculate it as
|
||||
* u[m..m+n] >> d (this is at most n digits and thus fits in
|
||||
* u[m+1..m+n], but we may need more source digits).
|
||||
*/
|
||||
if (arq) {
|
||||
if (d) {
|
||||
for (i = m + n; i > m; --i)
|
||||
u[i] = (digit)(((unsigned int)u[i] >> d) |
|
||||
LHALF((unsigned int)u[i - 1] <<
|
||||
(HALF_BITS - d)));
|
||||
u[i] = 0;
|
||||
}
|
||||
tmp.ui[H] = COMBINE(uspace[1], uspace[2]);
|
||||
tmp.ui[L] = COMBINE(uspace[3], uspace[4]);
|
||||
*arq = tmp.ll;
|
||||
}
|
||||
|
||||
tmp.ui[H] = COMBINE(qspace[1], qspace[2]);
|
||||
tmp.ui[L] = COMBINE(qspace[3], qspace[4]);
|
||||
return (tmp.ll);
|
||||
}
|
||||
|
||||
/*
|
||||
* Shift p[0]..p[len] left `sh' bits, ignoring any bits that
|
||||
* `fall out' the left (there never will be any such anyway).
|
||||
* We may assume len >= 0. NOTE THAT THIS WRITES len+1 DIGITS.
|
||||
*/
|
||||
static void
|
||||
shl(digit *p, int len, int sh)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
p[i] = (digit)(LHALF((unsigned int)p[i] << sh) |
|
||||
((unsigned int)p[i + 1] >> (HALF_BITS - sh)));
|
||||
p[i] = (digit)(LHALF((unsigned int)p[i] << sh));
|
||||
}
|
55
common/gcc-millicode/subdi3.c
Normal file
55
common/gcc-millicode/subdi3.c
Normal file
@@ -0,0 +1,55 @@
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This software was developed by the Computer Systems Engineering group
|
||||
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
||||
* contributed to Berkeley.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* From:
|
||||
* @(#)subdi3.c 8.1 (Berkeley) 6/4/93
|
||||
* NetBSD: subdi3.c,v 1.1 2005/12/20 19:28:51 christos Exp
|
||||
*/
|
||||
|
||||
#include "longlong.h"
|
||||
|
||||
/*
|
||||
* Subtract two long long values. This is trivial since a one-bit
|
||||
* carry from a single unsigned int difference x-y occurs if and only
|
||||
* if (x-y) > x.
|
||||
*/
|
||||
long long
|
||||
__subdi3(long long a, long long b)
|
||||
{
|
||||
union uu aa, bb, diff;
|
||||
|
||||
aa.ll = a;
|
||||
bb.ll = b;
|
||||
diff.ui[L] = aa.ui[L] - bb.ui[L];
|
||||
diff.ui[H] = aa.ui[H] - bb.ui[H] - (diff.ui[L] > aa.ui[L]);
|
||||
return (diff.ll);
|
||||
}
|
53
common/gcc-millicode/ucmpdi2.c
Normal file
53
common/gcc-millicode/ucmpdi2.c
Normal file
@@ -0,0 +1,53 @@
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This software was developed by the Computer Systems Engineering group
|
||||
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
||||
* contributed to Berkeley.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* From:
|
||||
* @(#)ucmpdi2.c 8.1 (Berkeley) 6/4/93
|
||||
* NetBSD: ucmpdi2.c,v 1.1 2005/12/20 19:28:51 christos Exp
|
||||
*/
|
||||
|
||||
#include "longlong.h"
|
||||
|
||||
/*
|
||||
* Return 0, 1, or 2 as a <, =, > b respectively.
|
||||
* Neither a nor b are considered signed.
|
||||
*/
|
||||
int
|
||||
__ucmpdi2(unsigned long long a, unsigned long long b)
|
||||
{
|
||||
union uu aa, bb;
|
||||
|
||||
aa.ull = a;
|
||||
bb.ull = b;
|
||||
return (aa.ui[H] < bb.ui[H] ? 0 : aa.ui[H] > bb.ui[H] ? 2 :
|
||||
aa.ui[L] < bb.ui[L] ? 0 : aa.ui[L] > bb.ui[L] ? 2 : 1);
|
||||
}
|
48
common/gcc-millicode/udivdi3.c
Normal file
48
common/gcc-millicode/udivdi3.c
Normal file
@@ -0,0 +1,48 @@
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This software was developed by the Computer Systems Engineering group
|
||||
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
||||
* contributed to Berkeley.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* From:
|
||||
* @(#)udivdi3.c 8.1 (Berkeley) 6/4/93
|
||||
* NetBSD: udivdi3.c,v 1.1 2005/12/20 19:28:51 christos Exp
|
||||
*/
|
||||
|
||||
#include "longlong.h"
|
||||
|
||||
/*
|
||||
* Divide two unsigned long longs.
|
||||
*/
|
||||
unsigned long long
|
||||
__udivdi3(unsigned long long a, unsigned long long b)
|
||||
{
|
||||
|
||||
return __qdivrem(a, b, NULL);
|
||||
}
|
50
common/gcc-millicode/umoddi3.c
Normal file
50
common/gcc-millicode/umoddi3.c
Normal file
@@ -0,0 +1,50 @@
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This software was developed by the Computer Systems Engineering group
|
||||
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
||||
* contributed to Berkeley.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* From:
|
||||
* @(#)umoddi3.c 8.1 (Berkeley) 6/4/93
|
||||
* NetBSD: umoddi3.c,v 1.1 2005/12/20 19:28:51 christos Exp
|
||||
*/
|
||||
|
||||
#include "longlong.h"
|
||||
|
||||
/*
|
||||
* Return remainder after dividing two unsigned long longs.
|
||||
*/
|
||||
unsigned long long
|
||||
__umoddi3(unsigned long long a, unsigned long long b)
|
||||
{
|
||||
unsigned long long r;
|
||||
|
||||
(void)__qdivrem(a, b, &r);
|
||||
return (r);
|
||||
}
|
53
common/gcc-millicode/xordi3.c
Normal file
53
common/gcc-millicode/xordi3.c
Normal file
@@ -0,0 +1,53 @@
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This software was developed by the Computer Systems Engineering group
|
||||
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
||||
* contributed to Berkeley.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* From:
|
||||
* @(#)xordi3.c 8.1 (Berkeley) 6/4/93
|
||||
* NetBSD: xordi3.c,v 1.1 2005/12/20 19:28:51 christos Exp
|
||||
*/
|
||||
|
||||
#include "longlong.h"
|
||||
|
||||
/*
|
||||
* Return a ^ b, in long long.
|
||||
*/
|
||||
long long
|
||||
__xordi3(long long a, long long b)
|
||||
{
|
||||
union uu aa, bb;
|
||||
|
||||
aa.ll = a;
|
||||
bb.ll = b;
|
||||
aa.ui[0] ^= bb.ui[0];
|
||||
aa.ui[1] ^= bb.ui[1];
|
||||
return (aa.ll);
|
||||
}
|
104
common/libc/arch/mips/setjmp.S
Normal file
104
common/libc/arch/mips/setjmp.S
Normal file
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
|
||||
* The President and Fellows of Harvard College.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* setjmp and longjmp for MIPS.
|
||||
*/
|
||||
|
||||
#include <kern/mips/regdefs.h>
|
||||
|
||||
.text
|
||||
.set noreorder
|
||||
|
||||
/*
|
||||
* int setjmp(jmp_buf jb);
|
||||
*
|
||||
* Save the current state so we can return again from the call later
|
||||
* if/when longjmp is called. (If the function that called setjmp
|
||||
* returns before longjmp is called, the results are undefined. We
|
||||
* only need to save registers, not the whole contents of the stack.)
|
||||
*/
|
||||
|
||||
.globl setjmp
|
||||
.type setjmp,@function
|
||||
.ent setjmp
|
||||
setjmp:
|
||||
/*
|
||||
* jmp_buf is in a0. We need to save s0-s8, sp, and ra in it.
|
||||
* Don't store more registers without adjusting machine/setjmp.h.
|
||||
*/
|
||||
|
||||
sw sp, 0(a0) /* save registers */
|
||||
sw ra, 4(a0)
|
||||
sw s0, 8(a0)
|
||||
sw s1, 12(a0)
|
||||
sw s2, 16(a0)
|
||||
sw s3, 20(a0)
|
||||
sw s4, 24(a0)
|
||||
sw s5, 28(a0)
|
||||
sw s6, 32(a0)
|
||||
sw s7, 36(a0)
|
||||
sw s8, 40(a0)
|
||||
|
||||
j ra /* done */
|
||||
li v0, 0 /* return 0 (in delay slot) */
|
||||
.end setjmp
|
||||
|
||||
|
||||
/*
|
||||
* void longjmp(jmp_buf jb, int code);
|
||||
*/
|
||||
.globl longjmp
|
||||
.type longjmp,@function
|
||||
.ent longjmp
|
||||
longjmp:
|
||||
/*
|
||||
* jmp_buf is in a0. Return code is in a1.
|
||||
* We need to restore s0-s8, sp, and ra from the jmp_buf.
|
||||
* The return code is forced to 1 if 0 is passed in.
|
||||
*/
|
||||
|
||||
sltiu t0, a1, 1 /* set t0 to 1 if return code is 0... otherwise 0 */
|
||||
addu a1, a1, t0 /* update the return code */
|
||||
|
||||
lw sp, 0(a0) /* restore registers */
|
||||
lw ra, 4(a0)
|
||||
lw s0, 8(a0)
|
||||
lw s1, 12(a0)
|
||||
lw s2, 16(a0)
|
||||
lw s3, 20(a0)
|
||||
lw s4, 24(a0)
|
||||
lw s5, 28(a0)
|
||||
lw s6, 32(a0)
|
||||
lw s7, 36(a0)
|
||||
lw s8, 40(a0)
|
||||
|
||||
j ra /* return, to where setjmp was called from */
|
||||
move v0, a1 /* set return value */
|
||||
.end longjmp
|
592
common/libc/printf/__printf.c
Normal file
592
common/libc/printf/__printf.c
Normal file
@@ -0,0 +1,592 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
|
||||
* The President and Fellows of Harvard College.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Guts of printf.
|
||||
*
|
||||
* This file is used in both libc and the kernel and needs to work in both
|
||||
* contexts. This makes a few things a bit awkward.
|
||||
*
|
||||
* This is a slightly simplified version of the real-life printf
|
||||
* originally used in the VINO kernel.
|
||||
*/
|
||||
|
||||
#ifdef _KERNEL
|
||||
#include <types.h>
|
||||
#include <lib.h>
|
||||
#define assert KASSERT
|
||||
#else
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
|
||||
/*
|
||||
* Do we want to support "long long" types with %lld?
|
||||
*
|
||||
* Using 64-bit types with gcc causes gcc to emit calls to functions
|
||||
* like __moddi3 and __divdi3. These need to be provided at link time,
|
||||
* which can be a hassle; this switch is provided to help avoid
|
||||
* needing them.
|
||||
*/
|
||||
#define USE_LONGLONG
|
||||
|
||||
/*
|
||||
* Define a type that holds the longest signed integer we intend to support.
|
||||
*/
|
||||
#ifdef USE_LONGLONG
|
||||
#define INTTYPE long long
|
||||
#else
|
||||
#define INTTYPE long
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* Space for a long long in base 8, plus a NUL, plus one
|
||||
* character extra for slop.
|
||||
*
|
||||
* CHAR_BIT is the number of bits in a char; thus sizeof(long long)*CHAR_BIT
|
||||
* is the number of bits in a long long. Each octal digit prints 3 bits.
|
||||
* Values printed in larger bases will be shorter strings.
|
||||
*/
|
||||
#define NUMBER_BUF_SIZE ((sizeof(INTTYPE) * CHAR_BIT) / 3 + 2)
|
||||
|
||||
/*
|
||||
* Structure holding the state for printf.
|
||||
*/
|
||||
typedef struct {
|
||||
/* Callback for sending printed string data */
|
||||
void (*sendfunc)(void *clientdata, const char *str, size_t len);
|
||||
void *clientdata;
|
||||
|
||||
/* The varargs argument pointer */
|
||||
va_list ap;
|
||||
|
||||
/* Total count of characters printed */
|
||||
int charcount;
|
||||
|
||||
/* Flag that's true if we are currently looking in a %-format */
|
||||
int in_pct;
|
||||
|
||||
/* Size of the integer argument to retrieve */
|
||||
enum {
|
||||
INTSZ,
|
||||
LONGSZ,
|
||||
#ifdef USE_LONGLONG
|
||||
LLONGSZ,
|
||||
#endif
|
||||
SIZETSZ,
|
||||
} size;
|
||||
|
||||
/* The value of the integer argument retrieved */
|
||||
unsigned INTTYPE num;
|
||||
|
||||
/* Sign of the integer argument (0 = positive; -1 = negative) */
|
||||
int sign;
|
||||
|
||||
/* Field width (number of spaces) */
|
||||
int spacing;
|
||||
|
||||
/* Flag: align to left in field instead of right */
|
||||
int rightspc;
|
||||
|
||||
/* Character to pad to field size with (space or 0) */
|
||||
int fillchar;
|
||||
|
||||
/* Number base to print the integer argument in (8, 10, 16) */
|
||||
int base;
|
||||
|
||||
/* Flag: if set, print 0x before hex and 0 before octal numbers */
|
||||
int baseprefix;
|
||||
|
||||
/* Flag: alternative output format selected with %#... */
|
||||
int altformat;
|
||||
} PF;
|
||||
|
||||
/*
|
||||
* Send some text onward to the output.
|
||||
*
|
||||
* We count the total length we send out so we can return it from __vprintf,
|
||||
* since that's what most printf-like functions want to return.
|
||||
*/
|
||||
static
|
||||
void
|
||||
__pf_print(PF *pf, const char *txt, size_t len)
|
||||
{
|
||||
pf->sendfunc(pf->clientdata, txt, len);
|
||||
pf->charcount += len;
|
||||
}
|
||||
|
||||
/*
|
||||
* Reset the state for the next %-field.
|
||||
*/
|
||||
static
|
||||
void
|
||||
__pf_endfield(PF *pf)
|
||||
{
|
||||
pf->in_pct = 0;
|
||||
pf->size = INTSZ;
|
||||
pf->num = 0;
|
||||
pf->sign = 0;
|
||||
pf->spacing = 0;
|
||||
pf->rightspc = 0;
|
||||
pf->fillchar = ' ';
|
||||
pf->base = 0;
|
||||
pf->baseprefix = 0;
|
||||
pf->altformat = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Process modifier chars (between the % and the type specifier)
|
||||
* # use "alternate display format"
|
||||
* - left align in field instead of right align
|
||||
* l value is long (ll = long long)
|
||||
* z value is size_t
|
||||
* 0-9 field width
|
||||
* leading 0 pad with zeros instead of spaces
|
||||
*/
|
||||
static
|
||||
void
|
||||
__pf_modifier(PF *pf, int ch)
|
||||
{
|
||||
switch (ch) {
|
||||
case '#':
|
||||
pf->altformat = 1;
|
||||
break;
|
||||
case '-':
|
||||
pf->rightspc = 1;
|
||||
break;
|
||||
case 'l':
|
||||
if (pf->size==LONGSZ) {
|
||||
#ifdef USE_LONGLONG
|
||||
pf->size = LLONGSZ;
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
pf->size = LONGSZ;
|
||||
}
|
||||
break;
|
||||
case 'z':
|
||||
pf->size = SIZETSZ;
|
||||
break;
|
||||
case '0':
|
||||
if (pf->spacing>0) {
|
||||
/*
|
||||
* Already seen some digits; this is part of the
|
||||
* field size.
|
||||
*/
|
||||
pf->spacing = pf->spacing*10;
|
||||
}
|
||||
else {
|
||||
/*
|
||||
* Leading zero; set the padding character to 0.
|
||||
*/
|
||||
pf->fillchar = '0';
|
||||
}
|
||||
break;
|
||||
default:
|
||||
/*
|
||||
* Invalid characters should be filtered out by a
|
||||
* higher-level function, so if this assert goes off
|
||||
* it's our fault.
|
||||
*/
|
||||
assert(ch>'0' && ch<='9');
|
||||
|
||||
/*
|
||||
* Got a digit; accumulate the field size.
|
||||
*/
|
||||
pf->spacing = pf->spacing*10 + (ch-'0');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Retrieve a numeric argument from the argument list and store it
|
||||
* in pf->num, according to the size recorded in pf->size and using
|
||||
* the numeric type specified by ch.
|
||||
*/
|
||||
static
|
||||
void
|
||||
__pf_getnum(PF *pf, int ch)
|
||||
{
|
||||
if (ch=='p') {
|
||||
/*
|
||||
* Pointer.
|
||||
*
|
||||
* uintptr_t is a C99 standard type that's an unsigned
|
||||
* integer the same size as a pointer.
|
||||
*/
|
||||
pf->num = (uintptr_t) va_arg(pf->ap, void *);
|
||||
}
|
||||
else if (ch=='d') {
|
||||
/* signed integer */
|
||||
INTTYPE signednum=0;
|
||||
switch (pf->size) {
|
||||
case INTSZ:
|
||||
/* %d */
|
||||
signednum = va_arg(pf->ap, int);
|
||||
break;
|
||||
case LONGSZ:
|
||||
/* %ld */
|
||||
signednum = va_arg(pf->ap, long);
|
||||
break;
|
||||
#ifdef USE_LONGLONG
|
||||
case LLONGSZ:
|
||||
/* %lld */
|
||||
signednum = va_arg(pf->ap, long long);
|
||||
break;
|
||||
#endif
|
||||
case SIZETSZ:
|
||||
/* %zd */
|
||||
signednum = va_arg(pf->ap, ssize_t);
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check for negative numbers.
|
||||
*/
|
||||
if (signednum < 0) {
|
||||
pf->sign = -1;
|
||||
pf->num = -signednum;
|
||||
}
|
||||
else {
|
||||
pf->num = signednum;
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* unsigned integer */
|
||||
switch (pf->size) {
|
||||
case INTSZ:
|
||||
/* %u (or %o, %x) */
|
||||
pf->num = va_arg(pf->ap, unsigned int);
|
||||
break;
|
||||
case LONGSZ:
|
||||
/* %lu (or %lo, %lx) */
|
||||
pf->num = va_arg(pf->ap, unsigned long);
|
||||
break;
|
||||
#ifdef USE_LONGLONG
|
||||
case LLONGSZ:
|
||||
/* %llu, %llo, %llx */
|
||||
pf->num = va_arg(pf->ap, unsigned long long);
|
||||
break;
|
||||
#endif
|
||||
case SIZETSZ:
|
||||
/* %zu, %zo, %zx */
|
||||
pf->num = va_arg(pf->ap, size_t);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Set the printing base based on the numeric type specified in ch.
|
||||
* %o octal
|
||||
* %d,%u decimal
|
||||
* %x hex
|
||||
* %p pointer (print as hex)
|
||||
*
|
||||
* If the "alternate format" was requested, or always for pointers,
|
||||
* note to print the C prefix for the type.
|
||||
*/
|
||||
static
|
||||
void
|
||||
__pf_setbase(PF *pf, int ch)
|
||||
{
|
||||
switch (ch) {
|
||||
case 'd':
|
||||
case 'u':
|
||||
pf->base = 10;
|
||||
break;
|
||||
case 'x':
|
||||
case 'p':
|
||||
pf->base = 16;
|
||||
break;
|
||||
case 'o':
|
||||
pf->base = 8;
|
||||
break;
|
||||
}
|
||||
if (pf->altformat || ch=='p') {
|
||||
pf->baseprefix = 1;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Function to print "spc" instances of the fill character.
|
||||
*/
|
||||
static
|
||||
void
|
||||
__pf_fill(PF *pf, int spc)
|
||||
{
|
||||
char f = pf->fillchar;
|
||||
int i;
|
||||
for (i=0; i<spc; i++) {
|
||||
__pf_print(pf, &f, 1);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* General printing function. Prints the string "stuff".
|
||||
* The two prefixes (in practice one is a type prefix, such as "0x",
|
||||
* and the other is the sign) get printed *after* space padding but
|
||||
* *before* zero padding, if padding is on the left.
|
||||
*/
|
||||
static
|
||||
void
|
||||
__pf_printstuff(PF *pf,
|
||||
const char *prefix, const char *prefix2,
|
||||
const char *stuff)
|
||||
{
|
||||
/* Total length to print. */
|
||||
int len = strlen(prefix)+strlen(prefix2)+strlen(stuff);
|
||||
|
||||
/* Get field width and compute amount of padding in "spc". */
|
||||
int spc = pf->spacing;
|
||||
if (spc > len) {
|
||||
spc -= len;
|
||||
}
|
||||
else {
|
||||
spc = 0;
|
||||
}
|
||||
|
||||
/* If padding on left and the fill char is not 0, pad first. */
|
||||
if (spc > 0 && pf->rightspc==0 && pf->fillchar!='0') {
|
||||
__pf_fill(pf, spc);
|
||||
}
|
||||
|
||||
/* Print the prefixes. */
|
||||
__pf_print(pf, prefix, strlen(prefix));
|
||||
__pf_print(pf, prefix2, strlen(prefix2));
|
||||
|
||||
/* If padding on left and the fill char *is* 0, pad here. */
|
||||
if (spc > 0 && pf->rightspc==0 && pf->fillchar=='0') {
|
||||
__pf_fill(pf, spc);
|
||||
}
|
||||
|
||||
/* Print the actual string. */
|
||||
__pf_print(pf, stuff, strlen(stuff));
|
||||
|
||||
/* If padding on the right, pad afterwards. */
|
||||
if (spc > 0 && pf->rightspc!=0) {
|
||||
__pf_fill(pf, spc);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Function to convert a number to ascii and then print it.
|
||||
*
|
||||
* Works from right to left in a buffer of NUMBER_BUF_SIZE bytes.
|
||||
* NUMBER_BUF_SIZE is set so that the longest number string we can
|
||||
* generate (a long long printed in octal) will fit. See above.
|
||||
*/
|
||||
static
|
||||
void
|
||||
__pf_printnum(PF *pf)
|
||||
{
|
||||
/* Digits to print with. */
|
||||
const char *const digits = "0123456789abcdef";
|
||||
|
||||
char buf[NUMBER_BUF_SIZE]; /* Accumulation buffer for string. */
|
||||
char *x; /* Current pointer into buf. */
|
||||
unsigned INTTYPE xnum; /* Current value to print. */
|
||||
const char *bprefix; /* Base prefix (0, 0x, or nothing) */
|
||||
const char *sprefix; /* Sign prefix (- or nothing) */
|
||||
|
||||
/* Start in the last slot of the buffer. */
|
||||
x = buf+sizeof(buf)-1;
|
||||
|
||||
/* Insert null terminator. */
|
||||
*x-- = 0;
|
||||
|
||||
/* Initialize value. */
|
||||
xnum = pf->num;
|
||||
|
||||
/*
|
||||
* Convert a single digit.
|
||||
* Do this loop at least once - that way 0 prints as 0 and not "".
|
||||
*/
|
||||
do {
|
||||
/*
|
||||
* Get the digit character for the least significant
|
||||
* part of xnum.
|
||||
*/
|
||||
*x = digits[xnum % pf->base];
|
||||
|
||||
/*
|
||||
* Back up the pointer to point to the next space to the left.
|
||||
*/
|
||||
x--;
|
||||
|
||||
/*
|
||||
* Drop the value of the digit we just printed from xnum.
|
||||
*/
|
||||
xnum = xnum / pf->base;
|
||||
|
||||
/*
|
||||
* If xnum hits 0 there's no more number left.
|
||||
*/
|
||||
} while (xnum > 0);
|
||||
|
||||
/*
|
||||
* x points to the *next* slot in the buffer to use.
|
||||
* However, we're done printing the number. So it's pointing
|
||||
* one slot *before* the start of the actual number text.
|
||||
* So advance it by one so it actually points at the number.
|
||||
*/
|
||||
x++;
|
||||
|
||||
/*
|
||||
* If a base prefix was requested, select it.
|
||||
*/
|
||||
if (pf->baseprefix && pf->base==16) {
|
||||
bprefix = "0x";
|
||||
}
|
||||
else if (pf->baseprefix && pf->base==8) {
|
||||
bprefix = "0";
|
||||
}
|
||||
else {
|
||||
bprefix = "";
|
||||
}
|
||||
|
||||
/*
|
||||
* Choose the sign prefix.
|
||||
*/
|
||||
sprefix = pf->sign ? "-" : "";
|
||||
|
||||
/*
|
||||
* Now actually print the string we just generated.
|
||||
*/
|
||||
__pf_printstuff(pf, sprefix, bprefix, x);
|
||||
}
|
||||
|
||||
/*
|
||||
* Process a single character out of the format string.
|
||||
*/
|
||||
static
|
||||
void
|
||||
__pf_send(PF *pf, int ch)
|
||||
{
|
||||
/* Cannot get NULs here. */
|
||||
assert(ch!=0);
|
||||
|
||||
if (pf->in_pct==0 && ch!='%') {
|
||||
/*
|
||||
* Not currently in a format, and not a %. Just send
|
||||
* the character on through.
|
||||
*/
|
||||
char c = ch;
|
||||
__pf_print(pf, &c, 1);
|
||||
}
|
||||
else if (pf->in_pct==0) {
|
||||
/*
|
||||
* Not in a format, but got a %. Start a format.
|
||||
*/
|
||||
pf->in_pct = 1;
|
||||
}
|
||||
else if (strchr("#-lz0123456789", ch)) {
|
||||
/*
|
||||
* These are the modifier characters we recognize.
|
||||
* (These are the characters between the % and the type.)
|
||||
*/
|
||||
__pf_modifier(pf, ch);
|
||||
}
|
||||
else if (strchr("doupx", ch)) {
|
||||
/*
|
||||
* Integer types.
|
||||
* Fetch the number, set the base, print it, then
|
||||
* reset for the next format.
|
||||
*/
|
||||
__pf_getnum(pf, ch);
|
||||
__pf_setbase(pf, ch);
|
||||
__pf_printnum(pf);
|
||||
__pf_endfield(pf);
|
||||
}
|
||||
else if (ch=='s') {
|
||||
/*
|
||||
* Print a string.
|
||||
*/
|
||||
const char *str = va_arg(pf->ap, const char *);
|
||||
if (str==NULL) {
|
||||
str = "(null)";
|
||||
}
|
||||
__pf_printstuff(pf, "", "", str);
|
||||
__pf_endfield(pf);
|
||||
}
|
||||
else {
|
||||
/*
|
||||
* %%, %c, or illegal character.
|
||||
* Illegal characters are printed like %%.
|
||||
* for example, %5k prints " k".
|
||||
*/
|
||||
char x[2];
|
||||
if (ch=='c') {
|
||||
x[0] = va_arg(pf->ap, int);
|
||||
}
|
||||
else {
|
||||
x[0] = ch;
|
||||
}
|
||||
x[1] = 0;
|
||||
__pf_printstuff(pf, "", "", x);
|
||||
__pf_endfield(pf);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Do a whole printf session.
|
||||
* Create and initialize a printf state object,
|
||||
* then send it each character from the format string.
|
||||
*/
|
||||
int
|
||||
__vprintf(void (*func)(void *clientdata, const char *str, size_t len),
|
||||
void *clientdata, const char *format, va_list ap)
|
||||
{
|
||||
PF pf;
|
||||
int i;
|
||||
|
||||
pf.sendfunc = func;
|
||||
pf.clientdata = clientdata;
|
||||
#ifdef va_copy
|
||||
va_copy(pf.ap, ap);
|
||||
#else
|
||||
pf.ap = ap;
|
||||
#endif
|
||||
pf.charcount = 0;
|
||||
__pf_endfield(&pf);
|
||||
|
||||
for (i=0; format[i]; i++) {
|
||||
__pf_send(&pf, format[i]);
|
||||
}
|
||||
|
||||
return pf.charcount;
|
||||
}
|
157
common/libc/printf/snprintf.c
Normal file
157
common/libc/printf/snprintf.c
Normal file
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
|
||||
* The President and Fellows of Harvard College.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is shared between libc and the kernel, so don't put anything
|
||||
* in here that won't work in both contexts.
|
||||
*/
|
||||
|
||||
#ifdef _KERNEL
|
||||
#include <types.h>
|
||||
#include <lib.h>
|
||||
|
||||
#else
|
||||
#include <stdio.h>
|
||||
|
||||
#endif /* _KERNEL */
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
/*
|
||||
* Standard C string/IO function: printf into a character buffer.
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* Context structure for snprintf: buffer to print into, maximum
|
||||
* length, and index of the next character to write.
|
||||
*
|
||||
* Note that while the length argument to snprintf includes space for
|
||||
* a null terminator, SNP.buflen does not. This is to make something
|
||||
* vaguely reasonable happen if a length of 0 is passed to snprintf.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
char *buf;
|
||||
size_t buflen;
|
||||
size_t bufpos;
|
||||
} SNP;
|
||||
|
||||
/*
|
||||
* Send function for snprintf. This is the function handed to the
|
||||
* printf guts. It gets called with mydata pointing to the context,
|
||||
* and some string data of length LEN in DATA. DATA is not necessarily
|
||||
* null-terminated.
|
||||
*/
|
||||
|
||||
static
|
||||
void
|
||||
__snprintf_send(void *mydata, const char *data, size_t len)
|
||||
{
|
||||
SNP *snp = mydata;
|
||||
unsigned i;
|
||||
|
||||
/* For each character we're sent... */
|
||||
for (i=0; i<len; i++) {
|
||||
|
||||
/* If we aren't past the length, */
|
||||
if (snp->bufpos < snp->buflen) {
|
||||
|
||||
/* store the character */
|
||||
snp->buf[snp->bufpos] = data[i];
|
||||
|
||||
/* and increment the position. */
|
||||
snp->bufpos++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* The va_list version of snprintf.
|
||||
*/
|
||||
int
|
||||
vsnprintf(char *buf, size_t len, const char *fmt, va_list ap)
|
||||
{
|
||||
int chars;
|
||||
SNP snp;
|
||||
|
||||
/*
|
||||
* Fill in the context structure.
|
||||
* We set snp.buflen to the number of characters that can be
|
||||
* written (excluding the null terminator) so as not to have
|
||||
* to special-case the possibility that we got passed a length
|
||||
* of zero elsewhere.
|
||||
*/
|
||||
snp.buf = buf;
|
||||
if (len==0) {
|
||||
snp.buflen = 0;
|
||||
}
|
||||
else {
|
||||
snp.buflen = len-1;
|
||||
}
|
||||
snp.bufpos = 0;
|
||||
|
||||
/* Call __vprintf to do the actual work. */
|
||||
chars = __vprintf(__snprintf_send, &snp, fmt, ap);
|
||||
|
||||
/*
|
||||
* Add a null terminator. If the length *we were passed* is greater
|
||||
* than zero, we reserved a space in the buffer for the terminator,
|
||||
* so this won't overflow. If the length we were passed is zero,
|
||||
* nothing will have been or should be written anyway, and buf
|
||||
* might even be NULL. (C99 explicitly endorses this possibility.)
|
||||
*/
|
||||
if (len > 0) {
|
||||
buf[snp.bufpos] = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Return the number of characters __vprintf processed.
|
||||
* According to C99, snprintf should return this number, not
|
||||
* the number of characters actually stored, and should not
|
||||
* return -1 on overflow but only on other errors. (All none
|
||||
* of them since we don't do multibyte characters...)
|
||||
*/
|
||||
return chars;
|
||||
}
|
||||
|
||||
/*
|
||||
* snprintf - hand off to vsnprintf.
|
||||
*/
|
||||
int
|
||||
snprintf(char *buf, size_t len, const char *fmt, ...)
|
||||
{
|
||||
int chars;
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
chars = vsnprintf(buf, len, fmt, ap);
|
||||
va_end(ap);
|
||||
return chars;
|
||||
}
|
||||
|
101
common/libc/stdlib/atoi.c
Normal file
101
common/libc/stdlib/atoi.c
Normal file
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
|
||||
* The President and Fellows of Harvard College.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is shared between libc and the kernel, so don't put anything
|
||||
* in here that won't work in both contexts.
|
||||
*/
|
||||
|
||||
#ifdef _KERNEL
|
||||
#include <types.h>
|
||||
#include <lib.h>
|
||||
#else
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Standard C function: parse a string that represents a decimal integer.
|
||||
* Leading whitespace is allowed. Trailing gunk is allowed too. Doesn't
|
||||
* really report syntax errors or overflow in any useful way.
|
||||
*/
|
||||
|
||||
int
|
||||
atoi(const char *s)
|
||||
{
|
||||
static const char digits[] = "0123456789"; /* legal digits in order */
|
||||
unsigned val=0; /* value we're accumulating */
|
||||
int neg=0; /* set to true if we see a minus sign */
|
||||
|
||||
/* skip whitespace */
|
||||
while (*s==' ' || *s=='\t') {
|
||||
s++;
|
||||
}
|
||||
|
||||
/* check for sign */
|
||||
if (*s=='-') {
|
||||
neg=1;
|
||||
s++;
|
||||
}
|
||||
else if (*s=='+') {
|
||||
s++;
|
||||
}
|
||||
|
||||
/* process each digit */
|
||||
while (*s) {
|
||||
const char *where;
|
||||
unsigned digit;
|
||||
|
||||
/* look for the digit in the list of digits */
|
||||
where = strchr(digits, *s);
|
||||
if (where==NULL) {
|
||||
/* not found; not a digit, so stop */
|
||||
break;
|
||||
}
|
||||
|
||||
/* get the index into the digit list, which is the value */
|
||||
digit = (where - digits);
|
||||
|
||||
/* could (should?) check for overflow here */
|
||||
|
||||
/* shift the number over and add in the new digit */
|
||||
val = val*10 + digit;
|
||||
|
||||
/* look at the next character */
|
||||
s++;
|
||||
}
|
||||
|
||||
/* handle negative numbers */
|
||||
if (neg) {
|
||||
return -val;
|
||||
}
|
||||
|
||||
/* done */
|
||||
return val;
|
||||
}
|
76
common/libc/string/bzero.c
Normal file
76
common/libc/string/bzero.c
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
|
||||
* The President and Fellows of Harvard College.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is shared between libc and the kernel, so don't put anything
|
||||
* in here that won't work in both contexts.
|
||||
*/
|
||||
|
||||
#ifdef _KERNEL
|
||||
#include <types.h>
|
||||
#include <lib.h>
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Standard (well, semi-standard) C string function - zero a block of
|
||||
* memory.
|
||||
*/
|
||||
|
||||
void
|
||||
bzero(void *vblock, size_t len)
|
||||
{
|
||||
char *block = vblock;
|
||||
size_t i;
|
||||
|
||||
/*
|
||||
* For performance, optimize the common case where the pointer
|
||||
* and the length are word-aligned, and write word-at-a-time
|
||||
* instead of byte-at-a-time. Otherwise, write bytes.
|
||||
*
|
||||
* The alignment logic here should be portable. We rely on the
|
||||
* compiler to be reasonably intelligent about optimizing the
|
||||
* divides and moduli out. Fortunately, it is.
|
||||
*/
|
||||
|
||||
if ((uintptr_t)block % sizeof(long) == 0 &&
|
||||
len % sizeof(long) == 0) {
|
||||
long *lb = (long *)block;
|
||||
for (i=0; i<len/sizeof(long); i++) {
|
||||
lb[i] = 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (i=0; i<len; i++) {
|
||||
block[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
86
common/libc/string/memcpy.c
Normal file
86
common/libc/string/memcpy.c
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
|
||||
* The President and Fellows of Harvard College.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is shared between libc and the kernel, so don't put anything
|
||||
* in here that won't work in both contexts.
|
||||
*/
|
||||
|
||||
#ifdef _KERNEL
|
||||
#include <types.h>
|
||||
#include <lib.h>
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
/*
|
||||
* C standard function - copy a block of memory.
|
||||
*/
|
||||
|
||||
void *
|
||||
memcpy(void *dst, const void *src, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
/*
|
||||
* memcpy does not support overlapping buffers, so always do it
|
||||
* forwards. (Don't change this without adjusting memmove.)
|
||||
*
|
||||
* For speedy copying, optimize the common case where both pointers
|
||||
* and the length are word-aligned, and copy word-at-a-time instead
|
||||
* of byte-at-a-time. Otherwise, copy by bytes.
|
||||
*
|
||||
* The alignment logic below should be portable. We rely on
|
||||
* the compiler to be reasonably intelligent about optimizing
|
||||
* the divides and modulos out. Fortunately, it is.
|
||||
*/
|
||||
|
||||
if ((uintptr_t)dst % sizeof(long) == 0 &&
|
||||
(uintptr_t)src % sizeof(long) == 0 &&
|
||||
len % sizeof(long) == 0) {
|
||||
|
||||
long *d = dst;
|
||||
const long *s = src;
|
||||
|
||||
for (i=0; i<len/sizeof(long); i++) {
|
||||
d[i] = s[i];
|
||||
}
|
||||
}
|
||||
else {
|
||||
char *d = dst;
|
||||
const char *s = src;
|
||||
|
||||
for (i=0; i<len; i++) {
|
||||
d[i] = s[i];
|
||||
}
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
117
common/libc/string/memmove.c
Normal file
117
common/libc/string/memmove.c
Normal file
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
|
||||
* The President and Fellows of Harvard College.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is shared between libc and the kernel, so don't put anything
|
||||
* in here that won't work in both contexts.
|
||||
*/
|
||||
|
||||
#ifdef _KERNEL
|
||||
#include <types.h>
|
||||
#include <lib.h>
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
/*
|
||||
* C standard function - copy a block of memory, handling overlapping
|
||||
* regions correctly.
|
||||
*/
|
||||
|
||||
void *
|
||||
memmove(void *dst, const void *src, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
/*
|
||||
* If the buffers don't overlap, it doesn't matter what direction
|
||||
* we copy in. If they do, it does, so just assume they always do.
|
||||
* We don't concern ourselves with the possibility that the region
|
||||
* to copy might roll over across the top of memory, because it's
|
||||
* not going to happen.
|
||||
*
|
||||
* If the destination is above the source, we have to copy
|
||||
* back to front to avoid overwriting the data we want to
|
||||
* copy.
|
||||
*
|
||||
* dest: dddddddd
|
||||
* src: ssssssss ^
|
||||
* | ^ |___|
|
||||
* |___|
|
||||
*
|
||||
* If the destination is below the source, we have to copy
|
||||
* front to back.
|
||||
*
|
||||
* dest: dddddddd
|
||||
* src: ^ ssssssss
|
||||
* |___| ^ |
|
||||
* |___|
|
||||
*/
|
||||
|
||||
if ((uintptr_t)dst < (uintptr_t)src) {
|
||||
/*
|
||||
* As author/maintainer of libc, take advantage of the
|
||||
* fact that we know memcpy copies forwards.
|
||||
*/
|
||||
return memcpy(dst, src, len);
|
||||
}
|
||||
|
||||
/*
|
||||
* Copy by words in the common case. Look in memcpy.c for more
|
||||
* information.
|
||||
*/
|
||||
|
||||
if ((uintptr_t)dst % sizeof(long) == 0 &&
|
||||
(uintptr_t)src % sizeof(long) == 0 &&
|
||||
len % sizeof(long) == 0) {
|
||||
|
||||
long *d = dst;
|
||||
const long *s = src;
|
||||
|
||||
/*
|
||||
* The reason we copy index i-1 and test i>0 is that
|
||||
* i is unsigned -- so testing i>=0 doesn't work.
|
||||
*/
|
||||
|
||||
for (i=len/sizeof(long); i>0; i--) {
|
||||
d[i-1] = s[i-1];
|
||||
}
|
||||
}
|
||||
else {
|
||||
char *d = dst;
|
||||
const char *s = src;
|
||||
|
||||
for (i=len; i>0; i--) {
|
||||
d[i-1] = s[i-1];
|
||||
}
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
52
common/libc/string/memset.c
Normal file
52
common/libc/string/memset.c
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
|
||||
* The President and Fellows of Harvard College.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef _KERNEL
|
||||
#include <types.h>
|
||||
#include <lib.h>
|
||||
#else
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
/*
|
||||
* C standard function - initialize a block of memory
|
||||
*/
|
||||
|
||||
void *
|
||||
memset(void *ptr, int ch, size_t len)
|
||||
{
|
||||
char *p = ptr;
|
||||
size_t i;
|
||||
|
||||
for (i=0; i<len; i++) {
|
||||
p[i] = ch;
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
54
common/libc/string/strcat.c
Normal file
54
common/libc/string/strcat.c
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
|
||||
* The President and Fellows of Harvard College.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is shared between libc and the kernel, so don't put anything
|
||||
* in here that won't work in both contexts.
|
||||
*/
|
||||
|
||||
#ifdef _KERNEL
|
||||
#include <types.h>
|
||||
#include <lib.h>
|
||||
#else
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Standard C string function: append one string to another.
|
||||
*/
|
||||
|
||||
char *
|
||||
strcat(char *dest, const char *src)
|
||||
{
|
||||
size_t offset;
|
||||
|
||||
offset = strlen(dest);
|
||||
strcpy(dest+offset, src);
|
||||
return dest;
|
||||
}
|
68
common/libc/string/strchr.c
Normal file
68
common/libc/string/strchr.c
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
|
||||
* The President and Fellows of Harvard College.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is shared between libc and the kernel, so don't put anything
|
||||
* in here that won't work in both contexts.
|
||||
*/
|
||||
|
||||
#ifdef _KERNEL
|
||||
#include <types.h>
|
||||
#include <lib.h>
|
||||
#else
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
/*
|
||||
* C standard string function: find leftmost instance of a character
|
||||
* in a string.
|
||||
*/
|
||||
char *
|
||||
strchr(const char *s, int ch_arg)
|
||||
{
|
||||
/* avoid sign-extension problems */
|
||||
const char ch = ch_arg;
|
||||
|
||||
/* scan from left to right */
|
||||
while (*s) {
|
||||
/* if we hit it, return it */
|
||||
if (*s == ch) {
|
||||
return (char *)s;
|
||||
}
|
||||
s++;
|
||||
}
|
||||
|
||||
/* if we were looking for the 0, return that */
|
||||
if (*s == ch) {
|
||||
return (char *)s;
|
||||
}
|
||||
|
||||
/* didn't find it */
|
||||
return NULL;
|
||||
}
|
90
common/libc/string/strcmp.c
Normal file
90
common/libc/string/strcmp.c
Normal file
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
|
||||
* The President and Fellows of Harvard College.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is shared between libc and the kernel, so don't put anything
|
||||
* in here that won't work in both contexts.
|
||||
*/
|
||||
|
||||
#ifdef _KERNEL
|
||||
#include <types.h>
|
||||
#include <lib.h>
|
||||
#else
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Standard C string function: compare two strings and return their
|
||||
* sort order.
|
||||
*/
|
||||
|
||||
int
|
||||
strcmp(const char *a, const char *b)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
/*
|
||||
* Walk down both strings until either they're different
|
||||
* or we hit the end of A.
|
||||
*
|
||||
* If A and B strings are not the same length, when the
|
||||
* shorter one ends, the two will be different, and we'll
|
||||
* stop before running off the end of either.
|
||||
*
|
||||
* If they *are* the same length, it's sufficient to check
|
||||
* that we haven't run off the end of A, because that's the
|
||||
* same as checking to make sure we haven't run off the end of
|
||||
* B.
|
||||
*/
|
||||
|
||||
for (i=0; a[i]!=0 && a[i]==b[i]; i++) {
|
||||
/* nothing */
|
||||
}
|
||||
|
||||
/*
|
||||
* If A is greater than B, return 1. If A is less than B,
|
||||
* return -1. If they're the same, return 0. Since we have
|
||||
* stopped at the first character of difference (or the end of
|
||||
* both strings) checking the character under I accomplishes
|
||||
* this.
|
||||
*
|
||||
* Note that strcmp does not handle accented characters,
|
||||
* internationalization, or locale sort order; strcoll() does
|
||||
* that.
|
||||
*
|
||||
* The rules say we compare order in terms of *unsigned* char.
|
||||
*/
|
||||
if ((unsigned char)a[i] > (unsigned char)b[i]) {
|
||||
return 1;
|
||||
}
|
||||
else if (a[i] == b[i]) {
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
63
common/libc/string/strcpy.c
Normal file
63
common/libc/string/strcpy.c
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
|
||||
* The President and Fellows of Harvard College.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is shared between libc and the kernel, so don't put anything
|
||||
* in here that won't work in both contexts.
|
||||
*/
|
||||
|
||||
#ifdef _KERNEL
|
||||
#include <types.h>
|
||||
#include <lib.h>
|
||||
#else
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Standard C string function: copy one string to another.
|
||||
*/
|
||||
char *
|
||||
strcpy(char *dest, const char *src)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
/*
|
||||
* Copy characters until we hit the null terminator.
|
||||
*/
|
||||
for (i=0; src[i]; i++) {
|
||||
dest[i] = src[i];
|
||||
}
|
||||
|
||||
/*
|
||||
* Add null terminator to result.
|
||||
*/
|
||||
dest[i] = 0;
|
||||
|
||||
return dest;
|
||||
}
|
55
common/libc/string/strlen.c
Normal file
55
common/libc/string/strlen.c
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
|
||||
* The President and Fellows of Harvard College.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is shared between libc and the kernel, so don't put anything
|
||||
* in here that won't work in both contexts.
|
||||
*/
|
||||
|
||||
#ifdef _KERNEL
|
||||
#include <types.h>
|
||||
#include <lib.h>
|
||||
#else
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
/*
|
||||
* C standard string function: get length of a string
|
||||
*/
|
||||
|
||||
size_t
|
||||
strlen(const char *str)
|
||||
{
|
||||
size_t ret = 0;
|
||||
|
||||
while (str[ret]) {
|
||||
ret++;
|
||||
}
|
||||
return ret;
|
||||
}
|
69
common/libc/string/strrchr.c
Normal file
69
common/libc/string/strrchr.c
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
|
||||
* The President and Fellows of Harvard College.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is shared between libc and the kernel, so don't put anything
|
||||
* in here that won't work in both contexts.
|
||||
*/
|
||||
|
||||
#ifdef _KERNEL
|
||||
#include <types.h>
|
||||
#include <lib.h>
|
||||
#else
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
/*
|
||||
* C standard string function: find rightmost instance of a character
|
||||
* in a string.
|
||||
*/
|
||||
char *
|
||||
strrchr(const char *s, int ch_arg)
|
||||
{
|
||||
/* avoid sign-extension problems */
|
||||
const char ch = ch_arg;
|
||||
|
||||
/* start one past the last character INCLUDING NULL TERMINATOR */
|
||||
size_t i = strlen(s)+1;
|
||||
|
||||
/* go from right to left; stop at 0 */
|
||||
while (i > 0) {
|
||||
|
||||
/* decrement first */
|
||||
i--;
|
||||
|
||||
/* now check the character we're over */
|
||||
if (s[i] == ch) {
|
||||
return (char *)(s+i);
|
||||
}
|
||||
}
|
||||
|
||||
/* didn't find it */
|
||||
return NULL;
|
||||
}
|
96
common/libc/string/strtok_r.c
Normal file
96
common/libc/string/strtok_r.c
Normal file
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
|
||||
* The President and Fellows of Harvard College.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is shared between libc and the kernel, so don't put anything
|
||||
* in here that won't work in both contexts.
|
||||
*/
|
||||
|
||||
#ifdef _KERNEL
|
||||
#include <types.h>
|
||||
#include <lib.h>
|
||||
#else
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Standard C string function: tokenize a string splitting based on a
|
||||
* list of separator characters. Reentrant version.
|
||||
*
|
||||
* The "context" argument should point to a "char *" that is preserved
|
||||
* between calls to strtok_r that wish to operate on same string.
|
||||
*/
|
||||
char *
|
||||
strtok_r(char *string, const char *seps, char **context)
|
||||
{
|
||||
char *head; /* start of word */
|
||||
char *tail; /* end of word */
|
||||
|
||||
/* If we're starting up, initialize context */
|
||||
if (string) {
|
||||
*context = string;
|
||||
}
|
||||
|
||||
/* Get potential start of this next word */
|
||||
head = *context;
|
||||
if (head == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Skip any leading separators */
|
||||
while (*head && strchr(seps, *head)) {
|
||||
head++;
|
||||
}
|
||||
|
||||
/* Did we hit the end? */
|
||||
if (*head == 0) {
|
||||
/* Nothing left */
|
||||
*context = NULL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* skip over word */
|
||||
tail = head;
|
||||
while (*tail && !strchr(seps, *tail)) {
|
||||
tail++;
|
||||
}
|
||||
|
||||
/* Save head for next time in context */
|
||||
if (*tail == 0) {
|
||||
*context = NULL;
|
||||
}
|
||||
else {
|
||||
*tail = 0;
|
||||
tail++;
|
||||
*context = tail;
|
||||
}
|
||||
|
||||
/* Return current word */
|
||||
return head;
|
||||
}
|
Reference in New Issue
Block a user