Skip to content

Commit

Permalink
Include a pile of infrastructure from himalayan-yeti git repository
Browse files Browse the repository at this point in the history
  • Loading branch information
jleffler committed Jul 1, 2016
1 parent a287f64 commit ba5cd65
Show file tree
Hide file tree
Showing 24 changed files with 992 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ heapprt
if
if-jl
if-strict
inc/*.h
incunabulum
junk
ll19
Expand All @@ -104,7 +105,6 @@ pipecircle
piped-merge-sort
pipeline
pipesize
posixver.h
pthread-37
pthread-?
q7
Expand Down
3 changes: 3 additions & 0 deletions etc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Miscellaneous directory

Initially, to hold soq.mk, the common makefile. Other files if needed.
36 changes: 36 additions & 0 deletions etc/soq.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Makefile fragment for SOQ (aka Himalayan-Yeti) GitHub Project
#
# Use: include ../../etc/soq.mk in a per-directory source file

BASEDIR = ../..

LIBBASE = soq
LIBNAME = lib${LIBBASE}.a
LIBDIR = ${BASEDIR}/lib
HDRDIR = ${BASEDIR}/inc
LIBPATH = ${LIBDIR}/${LIBNAME}

CC = gcc
SFLAGS = -std=c11
WFLAG1 = -Wall
WFLAG2 = -Wextra
WFLAG3 = -Werror
WFLAG4 = -Wmissing-prototypes
WFLAG5 = -Wstrict-prototypes
WFLAG6 = #-Wold-style-declaration # Strict GCC only (not clang, it would seem).
WFLAG7 = -Wold-style-definition
WFLAGS = ${WFLAG1} ${WFLAG2} ${WFLAG3} ${WFLAG4} ${WFLAG5} ${WFLAG6} ${WFLAG7}
OFLAGS = -O3
GFLAGS = -g
UFLAGS = # Set on command line
IFLAGS = -I${BASEDIR}/inc

CFLAGS = ${SFLAGS} ${OFLAGS} ${GFLAGS} ${WFLAGS} ${UFLAGS} ${IFLAGS}

LDFLAGS = -L${BASEDIR}/lib
LDLIBS = -lsoq

DEBRIS = core a.out *~ *.o
RM_FR = rm -fr
RM_F = rm -f

8 changes: 8 additions & 0 deletions inc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Header directory

I use 3-letter directories for src, bin, etc, tmp, lib — I've never seen a good reason not to use inc rather than include.

This directory contains 'released' headers.
Headers from the library source directory are installed here when they're ready for general use.

The directory should be ignored apart from the README.md file.
6 changes: 6 additions & 0 deletions lib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Library directory

Apart from the README.md file, this is a destination for compiled libraries.
There shouldn't be any source code in here.

The directory content should be ignored except for the README.md file.
5 changes: 5 additions & 0 deletions src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Source directory

Contains other directories, a makefile, and this README.

Should not normally contain any other files.
6 changes: 6 additions & 0 deletions src/lib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Library source directory

This is the source directory for library code which is compiled into the SOQ library.

Most of this will be copies of code from main computers with (under protest) history missing.
Also RCS markers will be removed after an initial commit.
119 changes: 119 additions & 0 deletions src/lib/gcd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
@(#)File: gcd.c
@(#)Purpose: Implement Greatest Common Divisor for two integers
@(#)Author: J Leffler
@(#)Copyright: (C) JLSS 1993,1997,2000,2005,2012,2015-16
@(#)Derivation: gcd.c,v 1.7 2015/06/02 03:05:40
*/

/*TABSTOP=4*/

#include "gcd.h"

int gcd(int x, int y)
{
int r;

if (x <= 0 || y <= 0)
return(0);

while ((r = x % y) != 0)
{
x = y;
y = r;
}
return(y);
}

unsigned long long gcd_ull(unsigned long long x, unsigned long long y)
{
unsigned long long r;

if (x == 0 || y == 0)
return(0);

while ((r = x % y) != 0)
{
x = y;
y = r;
}
return(y);
}

#if defined(TEST)

#include <stdio.h>
#include <stdlib.h>

#define DIM(x) (sizeof(x)/sizeof(*(x)))

typedef struct gcd_test
{
int x;
int y;
int r;
} gcd_test;

static gcd_test gcd_list[] =
{
{ 0, 1, 0 },
{ 1, 1, 1 },
{ 2, 1, 1 },
{ 3, 1, 1 },
{ 3, 2, 1 },
{ 4, 2, 2 },
{ 8, 2, 2 },
{ 16, 3, 1 },
{ 16, 4, 4 },
{ 16, 6, 2 },
{ 16, 12, 4 },
{ 4795615, 2310, 55 },
};

typedef unsigned long long ULL;

static size_t test_gcd(int x, int y, int z)
{
size_t f = 0;
int r = gcd(x, y);

if (r != z)
f = 1;
printf("%s GCD(%7d, %7d) = %7d (%7d expected)\n",
(r == z) ? "PASS" : "FAIL", x, y, r, z);
return(f);
}

static size_t test_gcd_ull(unsigned long long x, unsigned long long y, unsigned long long z)
{
size_t f = 0;
unsigned long long r = gcd_ull(x, y);

if (r != z)
f = 1;
printf("%s GCD(%7lld, %7lld) = %7lld (%7lld expected)\n",
(r == z) ? "PASS" : "FAIL", x, y, r, z);
return(f);
}

int main(void)
{
size_t i;
size_t n = DIM(gcd_list);
size_t f = 0;

for (i = 0; i < n; i++)
{
f += test_gcd(gcd_list[i].x, gcd_list[i].y, gcd_list[i].r);
f += test_gcd(gcd_list[i].y, gcd_list[i].x, gcd_list[i].r);
f += test_gcd_ull((ULL)gcd_list[i].x, (ULL)gcd_list[i].y, (ULL)gcd_list[i].r);
f += test_gcd_ull((ULL)gcd_list[i].y, (ULL)gcd_list[i].x, (ULL)gcd_list[i].r);
}
if (f != 0)
printf("*** FAILED *** (%lu tests out of %lu)\n", (unsigned long)f, (unsigned long)(2 * n));
else
printf("=== PASSED === (%lu tests)\n", (unsigned long)(2 * n));
return((f == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
}

#endif /* TEST */
15 changes: 15 additions & 0 deletions src/lib/gcd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
@(#)File: gcd.h
@(#)Purpose: GCD - Greatest Common Denominator
@(#)Author: J Leffler
@(#)Copyright: (C) JLSS 2005,2008,2012,2016
@(#)Derivation: gcd.h,v 1.4 2012/06/29 15:25:57
*/

#ifndef JLSS_ID_GCD_H
#define JLSS_ID_GCD_H

extern int gcd(int x, int y);
extern unsigned long long gcd_ull(unsigned long long x, unsigned long long y);

#endif /* JLSS_ID_GCD_H */
28 changes: 28 additions & 0 deletions src/lib/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Makefile for SOQ library

include ../../etc/soq.mk

CP = cp
CPFLAGS = -fp

FILES.c = \
gcd.c \
stderr.c

FILES.o = ${FILES.c:.c=.o}
FILES.h = ${FILES.c:.c=.h}

all: ${LIBNAME} ${FILES.h}

${LIBNAME}: ${FILES.o}
${AR} ${ARFLAGS} ${LIBNAME} ${FILES.o}

install: ${LIBNAME} ${FILES.h}
${CP} ${CPFLAGS} ${LIBNAME} ${LIBDIR}
${CP} ${CPFLAGS} ${FILES.h} ${HDRDIR}

clean:
${RM_F} ${DEBRIS} ${FILES.o}

realclean: clean
${RM_F} ${LIBNAME}
35 changes: 35 additions & 0 deletions src/lib/posixver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
@(#)File: posixver.h
@(#)Purpose: Request appropriate POSIX and X/Open Support
@(#)Author: J Leffler
@(#)Copyright: (C) JLSS 2010,2015-16
@(#)Derivation: posixver.h,v 1.3 2015/07/05 21:28:18
*/

/*TABSTOP=4*/

#ifndef JLSS_ID_POSIXVER_H
#define JLSS_ID_POSIXVER_H

/*
** Include this file before including system headers. By default, with
** C99 support from the compiler, it requests POSIX 2001 support. With
** C89 support only, it requests POSIX 1997 support. Override the
** default behaviour by setting either _XOPEN_SOURCE or _POSIX_C_SOURCE.
*/

/* _XOPEN_SOURCE 700 is loosely equivalent to _POSIX_C_SOURCE 200809L */
/* _XOPEN_SOURCE 600 is loosely equivalent to _POSIX_C_SOURCE 200112L */
/* _XOPEN_SOURCE 500 is loosely equivalent to _POSIX_C_SOURCE 199506L */

#if !defined(_XOPEN_SOURCE) && !defined(_POSIX_C_SOURCE)
#if defined(__cplusplus)
#define _XOPEN_SOURCE 700 /* SUS v4, POSIX 1003.1 2008/13 (POSIX 2008/13) */
#elif __STDC_VERSION__ >= 199901L
#define _XOPEN_SOURCE 700 /* SUS v4, POSIX 1003.1 2008/13 (POSIX 2008/13) */
#else
#define _XOPEN_SOURCE 500 /* SUS v2, POSIX 1003.1 1997 */
#endif /* __STDC_VERSION__ */
#endif /* !_XOPEN_SOURCE && !_POSIX_C_SOURCE */

#endif /* JLSS_ID_POSIXVER_H */
Loading

0 comments on commit ba5cd65

Please sign in to comment.