From patchwork Fri Apr 21 00:49:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 9691561 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id BC8D16038D for ; Fri, 21 Apr 2017 00:51:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AE4512843F for ; Fri, 21 Apr 2017 00:51:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A32AD2847C; Fri, 21 Apr 2017 00:51:27 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4B78D2843F for ; Fri, 21 Apr 2017 00:51:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1034327AbdDUAvZ (ORCPT ); Thu, 20 Apr 2017 20:51:25 -0400 Received: from mail-io0-f180.google.com ([209.85.223.180]:36338 "EHLO mail-io0-f180.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1034013AbdDUAu2 (ORCPT ); Thu, 20 Apr 2017 20:50:28 -0400 Received: by mail-io0-f180.google.com with SMTP id o22so104071733iod.3 for ; Thu, 20 Apr 2017 17:50:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=sA2TO0fcfUUbdR+JDY6tE1NDCZpoFB7twmUDDknXOB8=; b=La/Im9eDQjIAH565EuGsv/mPcjLoTbqpqxa4riqNvhYTKwLrEHyqO1VCU0udy5YLI/ y0Tgip8RYtWQPTEmVkyaDAn5Wd7Zj/hZL9UPlCfDAA4mnjInwPRI01RF1c8vz0hScRGZ smddMi78hPgL84HcWVD6ODiWrb4GaHElSrJpCdPUKucBojYXPDsLaYsCvUkX0PBN+O3S DHI2Rl5VRlkmfVZdpmiRdgDcKdO+TPwVDzag/pCnBXGlp6eEHVpbXPcS06TBzL4EqUZG +bp5sSpE023X6sX+JZq5+rT1rqodqI/AJd+YRDc+gPFQT/Itr9moJsF2q6JvSl4+wrmm sDMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=sA2TO0fcfUUbdR+JDY6tE1NDCZpoFB7twmUDDknXOB8=; b=Gtf1UP5gDnHdBj9h4l6KCHdRv6fsDm+jRj6YkXuvDCk2SHeL5lLAk0Iw/Lua9mrQH+ KtyLgb55IC+AKHIsXRQc8XN/fqru6ZwyoR4EVdpnzZmeLx67ngi/g/cyAK++oYXoiElU gept8816nQf5fEWVrG48s/z8pW+bWH6yfMnut0TUdqwgpB1jHDNIYge6jQCZuFaZqz6T mI7zxGb1DfDm3sK4bDPobRnD7OKL4iJyN3jZixmAubGKhPfUJAjz3mBndS8T5RVa9Al0 d/fgjQt3szpmJpjmQwNIXDlsObjC6jJenn3LaM7fk/EqNweHsEdMGHeeu/NCcLQl9Wmn qCpQ== X-Gm-Message-State: AN3rC/4tgU12z9ZdRsILIDv1a57AEJDf8oIHI3Pfa/Q31RaS8qUcJGzw r4RyHXka4eP8KRrqJ26YXw== X-Received: by 10.98.200.24 with SMTP id z24mr10090318pff.235.1492735822602; Thu, 20 Apr 2017 17:50:22 -0700 (PDT) Received: from dmatlack.sea.corp.google.com ([100.100.206.82]) by smtp.gmail.com with ESMTPSA id e13sm12466486pfb.30.2017.04.20.17.50.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 20 Apr 2017 17:50:22 -0700 (PDT) From: David Matlack To: kvm@vger.kernel.org Cc: Peter Feiner , David Matlack Subject: [kvm-unit-tests PATCH 15/32] x86: binstr utility function Date: Thu, 20 Apr 2017 17:49:47 -0700 Message-Id: <20170421005004.137260-16-dmatlack@google.com> X-Mailer: git-send-email 2.12.2.816.g2cccc81164-goog In-Reply-To: <20170421005004.137260-1-dmatlack@google.com> References: <20170421005004.137260-1-dmatlack@google.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Peter Feiner Signed-off-by: Peter Feiner Signed-off-by: David Matlack --- lib/libcflat.h | 7 +++++++ lib/printf.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/lib/libcflat.h b/lib/libcflat.h index 248fd023626e..1d2eba980e1a 100644 --- a/lib/libcflat.h +++ b/lib/libcflat.h @@ -131,4 +131,11 @@ static inline bool is_power_of_2(unsigned long n) return n && !(n & (n - 1)); } +/* + * One byte per bit, a ' between each group of 4 bits, and a null terminator. + */ +#define BINSTR_SZ (sizeof(unsigned long) * 8 + sizeof(unsigned long) * 2) +void binstr(unsigned long x, char out[BINSTR_SZ]); +void print_binstr(unsigned long x); + #endif diff --git a/lib/printf.c b/lib/printf.c index 2aec59aa6d6d..cecbeadc4440 100644 --- a/lib/printf.c +++ b/lib/printf.c @@ -259,3 +259,33 @@ int printf(const char *fmt, ...) puts(buf); return r; } + +void binstr(unsigned long x, char out[BINSTR_SZ]) +{ + int i; + char *c; + int n; + + n = sizeof(unsigned long) * 8; + i = 0; + c = &out[0]; + for (;;) { + *c++ = (x & (1ul << (n - i - 1))) ? '1' : '0'; + i++; + + if (i == n) { + *c = '\0'; + break; + } + if (i % 4 == 0) + *c++ = '\''; + } + assert(c + 1 - &out[0] == BINSTR_SZ); +} + +void print_binstr(unsigned long x) +{ + char out[BINSTR_SZ]; + binstr(x, out); + printf("%s", out); +}