Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sbin/nvmectl 128 bit integers are displayed in decimal.
details: https://anonhg.NetBSD.org/src/rev/4739086206e3
branches: trunk
changeset: 351444:4739086206e3
user: nonaka <nonaka%NetBSD.org@localhost>
date: Mon Feb 13 11:16:46 2017 +0000
description:
128 bit integers are displayed in decimal.
bn.h and bignum.c from netgpg.
diffstat:
sbin/nvmectl/Makefile | 4 +-
sbin/nvmectl/bignum.c | 5773 ++++++++++++++++++++++++++++++++++++++++
sbin/nvmectl/bn.h | 155 +
sbin/nvmectl/humanize_bignum.c | 278 +
sbin/nvmectl/logpage.c | 99 +-
5 files changed, 6272 insertions(+), 37 deletions(-)
diffs (truncated from 6370 to 300 lines):
diff -r 4d60a949059c -r 4739086206e3 sbin/nvmectl/Makefile
--- a/sbin/nvmectl/Makefile Mon Feb 13 11:11:32 2017 +0000
+++ b/sbin/nvmectl/Makefile Mon Feb 13 11:16:46 2017 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.1 2016/06/04 16:29:35 nonaka Exp $
+# $NetBSD: Makefile,v 1.2 2017/02/13 11:16:46 nonaka Exp $
.include <bsd.own.mk>
@@ -11,6 +11,8 @@
SRCS+= perftest.c
SRCS+= power.c
SRCS+= reset.c
+SRCS+= bignum.c
+SRCS+= humanize_bignum.c
MAN= nvmectl.8
DPADD+= ${LIBUTIL}
diff -r 4d60a949059c -r 4739086206e3 sbin/nvmectl/bignum.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sbin/nvmectl/bignum.c Mon Feb 13 11:16:46 2017 +0000
@@ -0,0 +1,5773 @@
+/* $NetBSD: bignum.c,v 1.1 2017/02/13 11:16:46 nonaka Exp $ */
+
+/*-
+ * Copyright (c) 2012 Alistair Crooks <agc%NetBSD.org@localhost>
+ * 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 ``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 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.
+ */
+/* LibTomMath, multiple-precision integer library -- Tom St Denis
+ *
+ * LibTomMath is a library that provides multiple-precision
+ * integer arithmetic as well as number theoretic functionality.
+ *
+ * The library was designed directly after the MPI library by
+ * Michael Fromberger but has been written from scratch with
+ * additional optimizations in place.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ *
+ * Tom St Denis, tomstdenis%gmail.com@localhost, http://libtom.org
+ */
+
+#include <sys/types.h>
+#include <sys/param.h>
+
+#include <limits.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "bn.h"
+
+/**************************************************************************/
+
+/* LibTomMath, multiple-precision integer library -- Tom St Denis
+ *
+ * LibTomMath is a library that provides multiple-precision
+ * integer arithmetic as well as number theoretic functionality.
+ *
+ * The library was designed directly after the MPI library by
+ * Michael Fromberger but has been written from scratch with
+ * additional optimizations in place.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ *
+ * Tom St Denis, tomstdenis%gmail.com@localhost, http://libtom.org
+ */
+
+#define MP_PREC 32
+#define DIGIT_BIT 28
+#define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
+
+#define MP_WARRAY /*LINTED*/(1U << (((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT) + 1)))
+
+#define MP_NO 0
+#define MP_YES 1
+
+#ifndef USE_ARG
+#define USE_ARG(x) /*LINTED*/(void)&(x)
+#endif
+
+#ifndef __arraycount
+#define __arraycount(__x) (sizeof(__x) / sizeof(__x[0]))
+#endif
+
+#ifndef MIN
+#define MIN(a,b) (((a)<(b))?(a):(b))
+#endif
+
+#define MP_ISZERO(a) (((a)->used == 0) ? MP_YES : MP_NO)
+
+typedef int mp_err;
+
+static int signed_multiply(mp_int * a, mp_int * b, mp_int * c);
+static int square(mp_int * a, mp_int * b);
+
+static int signed_subtract_word(mp_int *a, mp_digit b, mp_int *c);
+
+static inline void *
+allocate(size_t n, size_t m)
+{
+ return calloc(n, m);
+}
+
+static inline void
+deallocate(void *v, size_t sz)
+{
+ USE_ARG(sz);
+ free(v);
+}
+
+/* set to zero */
+static inline void
+mp_zero(mp_int *a)
+{
+ a->sign = MP_ZPOS;
+ a->used = 0;
+ memset(a->dp, 0x0, a->alloc * sizeof(*a->dp));
+}
+
+/* grow as required */
+static int
+mp_grow(mp_int *a, int size)
+{
+ mp_digit *tmp;
+
+ /* if the alloc size is smaller alloc more ram */
+ if (a->alloc < size) {
+ /* ensure there are always at least MP_PREC digits extra on top */
+ size += (MP_PREC * 2) - (size % MP_PREC);
+
+ /* reallocate the array a->dp
+ *
+ * We store the return in a temporary variable
+ * in case the operation failed we don't want
+ * to overwrite the dp member of a.
+ */
+ tmp = realloc(a->dp, sizeof(*tmp) * size);
+ if (tmp == NULL) {
+ /* reallocation failed but "a" is still valid [can be freed] */
+ return MP_MEM;
+ }
+
+ /* reallocation succeeded so set a->dp */
+ a->dp = tmp;
+ /* zero excess digits */
+ memset(&a->dp[a->alloc], 0x0, (size - a->alloc) * sizeof(*a->dp));
+ a->alloc = size;
+ }
+ return MP_OKAY;
+}
+
+/* shift left a certain amount of digits */
+static int
+lshift_digits(mp_int * a, int b)
+{
+ mp_digit *top, *bottom;
+ int x, res;
+
+ /* if its less than zero return */
+ if (b <= 0) {
+ return MP_OKAY;
+ }
+
+ /* grow to fit the new digits */
+ if (a->alloc < a->used + b) {
+ if ((res = mp_grow(a, a->used + b)) != MP_OKAY) {
+ return res;
+ }
+ }
+
+ /* increment the used by the shift amount then copy upwards */
+ a->used += b;
+
+ /* top */
+ top = a->dp + a->used - 1;
+
+ /* base */
+ bottom = a->dp + a->used - 1 - b;
+
+ /* much like rshift_digits this is implemented using a sliding window
+ * except the window goes the otherway around. Copying from
+ * the bottom to the top.
+ */
+ for (x = a->used - 1; x >= b; x--) {
+ *top-- = *bottom--;
+ }
+
+ /* zero the lower digits */
+ memset(a->dp, 0x0, b * sizeof(*a->dp));
+ return MP_OKAY;
+}
+
+/* trim unused digits
+ *
+ * This is used to ensure that leading zero digits are
+ * trimed and the leading "used" digit will be non-zero
+ * Typically very fast. Also fixes the sign if there
+ * are no more leading digits
+ */
+static void
+trim_unused_digits(mp_int * a)
+{
+ /* decrease used while the most significant digit is
+ * zero.
+ */
+ while (a->used > 0 && a->dp[a->used - 1] == 0) {
+ a->used -= 1;
+ }
+ /* reset the sign flag if used == 0 */
+ if (a->used == 0) {
+ a->sign = MP_ZPOS;
+ }
+}
+
+/* copy, b = a */
+static int
+mp_copy(BIGNUM *a, BIGNUM *b)
+{
+ int res;
+
+ /* if dst == src do nothing */
+ if (a == b) {
+ return MP_OKAY;
+ }
+ if (a == NULL || b == NULL) {
+ return MP_VAL;
+ }
+
+ /* grow dest */
+ if (b->alloc < a->used) {
+ if ((res = mp_grow(b, a->used)) != MP_OKAY) {
+ return res;
+ }
+ }
+
+ memcpy(b->dp, a->dp, a->used * sizeof(*b->dp));
+ if (b->used > a->used) {
+ memset(&b->dp[a->used], 0x0, (b->used - a->used) * sizeof(*b->dp));
+ }
+
+ /* copy used count and sign */
+ b->used = a->used;
+ b->sign = a->sign;
+ return MP_OKAY;
+}
+
+/* shift left by a certain bit count */
+static int
+lshift_bits(mp_int *a, int b, mp_int *c)
+{
+ mp_digit d;
+ int res;
+
+ /* copy */
+ if (a != c) {
+ if ((res = mp_copy(a, c)) != MP_OKAY) {
+ return res;
+ }
+ }
+
+ if (c->alloc < (int)(c->used + b/DIGIT_BIT + 1)) {
+ if ((res = mp_grow(c, c->used + b / DIGIT_BIT + 1)) != MP_OKAY) {
+ return res;
+ }
+ }
+
+ /* shift by as many digits in the bit count */
+ if (b >= (int)DIGIT_BIT) {
+ if ((res = lshift_digits(c, b / DIGIT_BIT)) != MP_OKAY) {
+ return res;
+ }
+ }
+
+ /* shift any bit count < DIGIT_BIT */
+ d = (mp_digit) (b % DIGIT_BIT);
Home |
Main Index |
Thread Index |
Old Index