comp.lang.ada
 help / color / mirror / Atom feed
From: "jimmaureenrogers@worldnet.att.net" <jimmaureenrogers@worldnet.att.net>
Subject: Re: How do you bitwise operations in Ada '83 and '95
Date: 19 Jul 2006 06:05:04 -0700
Date: 2006-07-19T06:05:04-07:00	[thread overview]
Message-ID: <1153314304.534739.178430@75g2000cwc.googlegroups.com> (raw)
In-Reply-To: DGivg.844752$084.196149@attbi_s22

Jeffrey R. Carter wrote:
> jimmaureenrogers@worldnet.att.net wrote:
> >
> > Actually it does not work exactly the same. The algorithm is
> > exactly the same, and the same numbers are produced, but the
> > packed array of bits executes faster on my machine by a factor
> > of 3 and uses significantly less space (by a factor of 8).
>
> "The algorithm is the same and the same numbers are produced" = "works
> the same". Performance may differ, but behavior is the same. Since
> performance is a function of many things in addition to the SW, only
> behavior is a meaningful comparison. No doubt there is a
> platform/OS/compiler combination such that the unpacked version would
> run faster than the packed version does on your machine.
>
> > Your definition of bit-wise appears to me to be based upon the
> > desire to use a low level abstraction to access the bits. My
> > definition includes all abstractions that allow access to
> > individual bits.  When I only programmed in C, I only
> > thought of bit-wise access in low level abstractions. Since I
> > have programmed in Ada I think of bit-wise operations in
> > many abstraction levels.
>
> "Bitwise operations" is an inherently low-level concept. At a high
> level, your abstraction is a set. If we treat that as a high-level
> abstraction, then we hide the implementation, and neither bits nor
> Booleans appear in the handling of sets in the main program. An array of
> Booleans, packed or not, is only one possible implementation of a set.
> We could rewrite your program using the ordered-set package from the
> Ada-0X data-structures library and get the same behavior. It would not
> involve any bit-level access, though.

My example was a contribution to the Computer Language Shootout.
The corresponding C code was:

/*
 * The Great Computer Language Shootout
 * http://shootout.alioth.debian.org/
 *
 * Written by Dima Dorfman, 2004
 * Compile: gcc -std=c99 -O2 -o nsieve_bits_gcc nsieve_bits.c
 */

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

typedef uint_fast8_t bits;
#define	NBITS	(CHAR_BIT * sizeof(bits))

static uintmax_t
nsieve(uintmax_t m)
{
	uintmax_t count, i, j;
	bits a[m / NBITS];

	memset(a, (1 << CHAR_BIT) - 1, sizeof(a));
	count = 0;
	for (i = 2; i < m; ++i)
		if (a[i / NBITS] & (1 << i % NBITS)) {
			for (j = i + i; j < m; j += i)
				a[j / NBITS] &= ~(1 << j % NBITS);
			++count;
		}
	return (count);
}

static void
test(unsigned long n)
{
	uintmax_t count, m;

	m = (1 << n) * 10000;
	count = nsieve(m);
	printf("Primes up to %8ju %8ju\n", m, count);
}

int
main(int ac, char **av)
{
	unsigned long n;
	char *cp;

	if (ac < 2) {
usage:		fprintf(stderr, "usage: nsieve N\n");
		exit(2);
	}
	n = strtoul(av[1], &cp, 10);
	if (*av[1] == '\0' || *cp != '\0' || n == ULONG_MAX)
		goto usage;
	test(n);
	if (n >= 1)
		test(n - 1);
	if (n >= 2)
		test(n - 2);
	exit(0);
}

Here you will note that the algorithm is the same as the
Ada version. The bit-wise operations in the nsieve function
are expressed in the manner you described. According to
your analysis, if the algorithm is the same and the results
are the same, the programs are the same. Logic tells me,
therefore, that accessing individual bits of
contiguous storage through an array of boolean in Ada is
a bit-wise operation just as the combination of AND, OR and
shift operations used in the C program.

Jim Rogers




  reply	other threads:[~2006-07-19 13:05 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1153244316.853254.291560@m79g2000cwm.googlegroups.com>
2006-07-18 18:45 ` How do you bitwise operations in Ada '83 and '95 Robert A Duff
2006-07-18 18:53 ` jimmaureenrogers
2006-07-18 19:22   ` Jeffrey R. Carter
2006-07-18 21:32     ` jimmaureenrogers
2006-07-19  0:40       ` Jeffrey R. Carter
2006-07-19  3:55         ` jimmaureenrogers
2006-07-19  4:37           ` Jeffrey R. Carter
2006-07-19 13:05             ` jimmaureenrogers [this message]
2006-07-19 19:43               ` Jeffrey R. Carter
     [not found]             ` <1153313832.389434.144930@s13g2000cwa.googlegroups.com>
2006-07-19 13:55               ` Georg Bauhaus
2006-07-19 14:20               ` Robert A Duff
2006-07-19 19:30               ` Jeffrey R. Carter
2006-07-19 14:41             ` Robert A Duff
2006-07-18 19:21 ` Jeffrey R. Carter
2006-07-19  3:01 ` tmoran
     [not found] <9315684D-C216-4EDA-8852-0A6BD4C275B0@amado-alves.info>
2006-07-19 22:30 ` Marius Amado-Alves
2006-07-20  7:40   ` Georg Bauhaus
2006-07-20  9:29     ` Colin Paul Gloster
2006-07-20 12:31       ` Georg Bauhaus
2006-07-20 13:08         ` Colin Paul Gloster
2006-07-20 13:29           ` Marius Amado-Alves
2006-07-20 13:49           ` Georg Bauhaus
2006-07-21  5:23             ` Colin Paul Gloster
2006-07-21  8:00               ` Georg Bauhaus
2006-07-20  9:03   ` Stephen Leake
2006-07-20  9:38     ` Marius Amado-Alves
2006-07-21  9:53       ` Stephen Leake
2006-07-20 11:31     ` Dmitry A. Kazakov
2006-07-20 13:18       ` Marius Amado-Alves
2006-07-21  9:58       ` Stephen Leake
2006-07-21 12:09         ` Dmitry A. Kazakov
2006-07-21 19:03           ` Simon Wright
2006-07-22  8:32             ` Dmitry A. Kazakov
2006-07-22  8:57               ` Simon Wright
2006-07-22 10:52               ` Georg Bauhaus
2006-07-22 13:31                 ` Dmitry A. Kazakov
2006-07-20  9:39 Fwd: " Marius Amado-Alves
2006-07-20 17:54 ` tmoran
2006-07-20 18:30   ` Marius Amado-Alves
2006-07-20 19:36     ` tmoran
2006-07-20 22:09       ` Simon Wright
2006-07-21 10:07         ` Stephen Leake
2006-07-21 19:09           ` Simon Wright
2006-07-21 19:45             ` tmoran
2006-07-23 15:59             ` Stephen Leake
2006-07-24  6:08               ` Simon Wright
     [not found] <BFF12262-F906-4F9A-B867-D0373609F038@amado-alves.info>
2006-07-20 16:40 ` Marius Amado-Alves
     [not found] <CD6E3BB8-52D2-4EED-A790-0184FE56A99A@amado-alves.info>
2006-07-20 20:41 ` Marius Amado-Alves
2006-07-20 23:13   ` Randy Brukardt
2006-07-21  5:38     ` Marius Amado-Alves
2006-07-21 22:09       ` Randy Brukardt
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox