Get rid of GLIBC_2.27 symbols #2240

This commit is contained in:
Alexey Milovidov 2018-05-11 14:54:25 +03:00
parent 274704d0df
commit 3ff088f270
3 changed files with 370 additions and 1 deletions

View File

@ -10,6 +10,8 @@ musl/posix_spawn.c
musl/futimens.c
musl/syscall.s
musl/syscall_ret.c
musl/sched_cpucount.c)
musl/sched_cpucount.c
musl/glob.c
musl/exp2f.c)
add_subdirectory (tests)

View File

@ -0,0 +1,127 @@
/* origin: FreeBSD /usr/src/lib/msun/src/s_exp2f.c */
/*-
* Copyright (c) 2005 David Schultz <das@FreeBSD.ORG>
* All rights reserved.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*/
#include <math.h>
#include <stdint.h>
#define TBLSIZE 16
static const float
redux = 0x1.8p23f / TBLSIZE,
P1 = 0x1.62e430p-1f,
P2 = 0x1.ebfbe0p-3f,
P3 = 0x1.c6b348p-5f,
P4 = 0x1.3b2c9cp-7f;
static const double exp2ft[TBLSIZE] = {
0x1.6a09e667f3bcdp-1,
0x1.7a11473eb0187p-1,
0x1.8ace5422aa0dbp-1,
0x1.9c49182a3f090p-1,
0x1.ae89f995ad3adp-1,
0x1.c199bdd85529cp-1,
0x1.d5818dcfba487p-1,
0x1.ea4afa2a490dap-1,
0x1.0000000000000p+0,
0x1.0b5586cf9890fp+0,
0x1.172b83c7d517bp+0,
0x1.2387a6e756238p+0,
0x1.306fe0a31b715p+0,
0x1.3dea64c123422p+0,
0x1.4bfdad5362a27p+0,
0x1.5ab07dd485429p+0,
};
/*
* exp2f(x): compute the base 2 exponential of x
*
* Accuracy: Peak error < 0.501 ulp; location of peak: -0.030110927.
*
* Method: (equally-spaced tables)
*
* Reduce x:
* x = k + y, for integer k and |y| <= 1/2.
* Thus we have exp2f(x) = 2**k * exp2(y).
*
* Reduce y:
* y = i/TBLSIZE + z for integer i near y * TBLSIZE.
* Thus we have exp2(y) = exp2(i/TBLSIZE) * exp2(z),
* with |z| <= 2**-(TBLSIZE+1).
*
* We compute exp2(i/TBLSIZE) via table lookup and exp2(z) via a
* degree-4 minimax polynomial with maximum error under 1.4 * 2**-33.
* Using double precision for everything except the reduction makes
* roundoff error insignificant and simplifies the scaling step.
*
* This method is due to Tang, but I do not use his suggested parameters:
*
* Tang, P. Table-driven Implementation of the Exponential Function
* in IEEE Floating-Point Arithmetic. TOMS 15(2), 144-157 (1989).
*/
float exp2f(float x)
{
double_t t, r, z;
union {float f; uint32_t i;} u = {x};
union {double f; uint64_t i;} uk;
uint32_t ix, i0, k;
/* Filter out exceptional cases. */
ix = u.i & 0x7fffffff;
if (ix > 0x42fc0000) { /* |x| > 126 */
if (ix > 0x7f800000) /* NaN */
return x;
if (u.i >= 0x43000000 && u.i < 0x80000000) { /* x >= 128 */
x *= 0x1p127f;
return x;
}
if (u.i >= 0x80000000) { /* x < -126 */
if (u.i >= 0xc3160000 || (u.i & 0x0000ffff))
{ volatile float tmp; tmp = (-0x1p-149f/x); }
if (u.i >= 0xc3160000) /* x <= -150 */
return 0;
}
} else if (ix <= 0x33000000) { /* |x| <= 0x1p-25 */
return 1.0f + x;
}
/* Reduce x, computing z, i0, and k. */
u.f = x + redux;
i0 = u.i;
i0 += TBLSIZE / 2;
k = i0 / TBLSIZE;
uk.i = (uint64_t)(0x3ff + k)<<52;
i0 &= TBLSIZE - 1;
u.f -= redux;
z = x - u.f;
/* Compute r = exp2(y) = exp2ft[i0] * p(z). */
r = exp2ft[i0];
t = r * z;
r = r + t * (P1 + z * P2) + t * (z * z) * (P3 + z * P4);
/* Scale by 2**k */
return r * uk.f;
}

View File

@ -0,0 +1,240 @@
#include <glob.h>
#include <fnmatch.h>
#include <sys/stat.h>
#include <dirent.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stddef.h>
struct match
{
struct match *next;
char name[1];
};
static int is_literal(const char *p, int useesc)
{
int bracket = 0;
for (; *p; p++) {
switch (*p) {
case '\\':
if (!useesc) break;
case '?':
case '*':
return 0;
case '[':
bracket = 1;
break;
case ']':
if (bracket) return 0;
break;
}
}
return 1;
}
static int append(struct match **tail, const char *name, size_t len, int mark)
{
struct match *new = malloc(sizeof(struct match) + len + 1);
if (!new) return -1;
(*tail)->next = new;
new->next = NULL;
strcpy(new->name, name);
if (mark) strcat(new->name, "/");
*tail = new;
return 0;
}
static int match_in_dir(const char *d, const char *p, int flags, int (*errfunc)(const char *path, int err), struct match **tail)
{
DIR *dir;
struct dirent *de;
char pat[strlen(p)+1];
char *p2;
size_t l = strlen(d);
int literal;
int fnm_flags= ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
| ((!(flags & GLOB_PERIOD)) ? FNM_PERIOD : 0);
int error;
if ((p2 = strchr(p, '/'))) {
strcpy(pat, p);
pat[p2-p] = 0;
for (; *p2 == '/'; p2++);
p = pat;
}
literal = is_literal(p, !(flags & GLOB_NOESCAPE));
if (*d == '/' && !*(d+1)) l = 0;
/* rely on opendir failing for nondirectory objects */
dir = opendir(*d ? d : ".");
error = errno;
if (!dir) {
/* this is not an error -- we let opendir call stat for us */
if (error == ENOTDIR) return 0;
if (error == EACCES && !*p) {
struct stat st;
if (!stat(d, &st) && S_ISDIR(st.st_mode)) {
if (append(tail, d, l, l))
return GLOB_NOSPACE;
return 0;
}
}
if (errfunc(d, error) || (flags & GLOB_ERR))
return GLOB_ABORTED;
return 0;
}
if (!*p) {
error = append(tail, d, l, l) ? GLOB_NOSPACE : 0;
closedir(dir);
return error;
}
while ((de = readdir(dir))) {
char namebuf[l+de->d_reclen+2], *name = namebuf;
if (!literal && fnmatch(p, de->d_name, fnm_flags))
continue;
if (literal && strcmp(p, de->d_name))
continue;
if (p2 && de->d_type && !S_ISDIR(de->d_type<<12) && !S_ISLNK(de->d_type<<12))
continue;
/* With GLOB_PERIOD, don't allow matching . or .. unless
* fnmatch would match them with FNM_PERIOD rules in effect. */
if (p2 && (flags & GLOB_PERIOD) && de->d_name[0]=='.'
&& (!de->d_name[1] || (de->d_name[1]=='.' && !de->d_name[2]))
&& fnmatch(p, de->d_name, fnm_flags | FNM_PERIOD))
continue;
if (*d) {
memcpy(name, d, l);
name[l] = '/';
strcpy(name+l+1, de->d_name);
} else {
name = de->d_name;
}
if (p2) {
if ((error = match_in_dir(name, p2, flags, errfunc, tail))) {
closedir(dir);
return error;
}
} else {
int mark = 0;
if (flags & GLOB_MARK) {
if (de->d_type && !S_ISLNK(de->d_type<<12))
mark = S_ISDIR(de->d_type<<12);
else {
struct stat st;
stat(name, &st);
mark = S_ISDIR(st.st_mode);
}
}
if (append(tail, name, l+de->d_reclen+1, mark)) {
closedir(dir);
return GLOB_NOSPACE;
}
}
}
closedir(dir);
if (error && (errfunc(d, error) || (flags & GLOB_ERR)))
return GLOB_ABORTED;
return 0;
}
static int ignore_err(const char *path, int err)
{
return 0;
}
static void freelist(struct match *head)
{
struct match *match, *next;
for (match=head->next; match; match=next) {
next = match->next;
free(match);
}
}
static int sort(const void *a, const void *b)
{
return strcmp(*(const char **)a, *(const char **)b);
}
int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, int err), glob_t *restrict g)
{
const char *p=pat, *d;
struct match head = { .next = NULL }, *tail = &head;
size_t cnt, i;
size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0;
int error = 0;
if (*p == '/') {
for (; *p == '/'; p++);
d = "/";
} else {
d = "";
}
if (!errfunc) errfunc = ignore_err;
if (!(flags & GLOB_APPEND)) {
g->gl_offs = offs;
g->gl_pathc = 0;
g->gl_pathv = NULL;
}
if (strnlen(p, PATH_MAX+1) > PATH_MAX) return GLOB_NOSPACE;
if (*pat) error = match_in_dir(d, p, flags, errfunc, &tail);
if (error == GLOB_NOSPACE) {
freelist(&head);
return error;
}
for (cnt=0, tail=head.next; tail; tail=tail->next, cnt++);
if (!cnt) {
if (flags & GLOB_NOCHECK) {
tail = &head;
if (append(&tail, pat, strlen(pat), 0))
return GLOB_NOSPACE;
cnt++;
} else
return GLOB_NOMATCH;
}
if (flags & GLOB_APPEND) {
char **pathv = realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *));
if (!pathv) {
freelist(&head);
return GLOB_NOSPACE;
}
g->gl_pathv = pathv;
offs += g->gl_pathc;
} else {
g->gl_pathv = malloc((offs + cnt + 1) * sizeof(char *));
if (!g->gl_pathv) {
freelist(&head);
return GLOB_NOSPACE;
}
for (i=0; i<offs; i++)
g->gl_pathv[i] = NULL;
}
for (i=0, tail=head.next; i<cnt; tail=tail->next, i++)
g->gl_pathv[offs + i] = tail->name;
g->gl_pathv[offs + i] = NULL;
g->gl_pathc += cnt;
if (!(flags & GLOB_NOSORT))
qsort(g->gl_pathv+offs, cnt, sizeof(char *), sort);
return error;
}
void globfree(glob_t *g)
{
size_t i;
for (i=0; i<g->gl_pathc; i++)
free(g->gl_pathv[g->gl_offs + i] - offsetof(struct match, name));
free(g->gl_pathv);
g->gl_pathc = 0;
g->gl_pathv = NULL;
}