Skip to content

Commit

Permalink
fix(improvement): enhance usability (#24)
Browse files Browse the repository at this point in the history
* support modulo arithmetic

* minimize duplicate code in CMakeLists for tests

* supoort bit operations for integer and uinteger

* add a Github Actions workflow for code style checking

* support clang-format checking on macOS

* add __int128 support for uint128

* support __int128 for int128
  • Loading branch information
guuzaa authored Mar 23, 2024
1 parent f6bb7b3 commit a22bda7
Show file tree
Hide file tree
Showing 22 changed files with 922 additions and 283 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ jobs:
include:
- os: ubuntu-latest
compiler: GCC
install: sudo apt-get update && sudo apt-get install -y build-essential ninja-build
install: sudo apt-get update && sudo apt-get install -y build-essential ninja-build clang-format-14
build-dir: build-gcc
- os: macos-latest
compiler: Clang
install: "brew install ninja"
install: "brew install ninja clang-format"
build-dir: build-clang
- os: windows-latest
compiler: MSVC
Expand All @@ -44,6 +44,10 @@ jobs:
- name: Test
run: |
cmake --build ${{ matrix.build-dir }} --target run-tests
- name: Check Format
if: runner.os != 'Windows'
run: |
cmake --build ${{ matrix.build-dir }} --target check-format
- name: Example
run: |
cmake --build ${{ matrix.build-dir }} --target example
43 changes: 42 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ include_directories(${SRC_INCLUDE_DIR})
add_subdirectory(src)

if(NUMBERS_EXAMPLE)
message(STATUS "Building examples")
add_subdirectory(examples)
endif()

if(NUMBERS_TEST)
message(STATUS "Building and running tests")
message(STATUS "Building tests")
set(THIRD_PARTY_INCLUDE_DIR
${PROJECT_SOURCE_DIR}/third_party
)
Expand All @@ -45,6 +46,46 @@ if(NUMBERS_TEST)
add_subdirectory(third_party)
add_subdirectory(tests)

set(BUILD_SUPPORT_DIR "${CMAKE_SOURCE_DIR}/build_support")
set(CLANG_SEARCH_PATH "/usr/local/bin" "/usr/bin" "/usr/local/opt/llvm/bin" "/usr/local/opt/llvm@14/bin"
"/opt/homebrew/opt/llvm@14/bin/")

find_program(CLANG_FORMAT_BIN
NAMES clang-format clang-format-14
HINTS ${CLANG_SEARCH_PATH}
)

if("${CLANG_FORMAT_BIN}" STREQUAL "CLANG_FORMAT_BIN-NOTFOUND")
message(WARNING "numbers/main couldn't find clang-format.")
else()
message(STATUS "numbers/main found clang-format at ${CLANG_FORMAT_BIN}")
endif()

string(CONCAT FORMAT_DIRS
"${CMAKE_CURRENT_SOURCE_DIR}/src,"
"${CMAKE_CURRENT_SOURCE_DIR}/examples,"
"${CMAKE_CURRENT_SOURCE_DIR}/tests,"
)

# Runs clang format and updates files in place.
add_custom_target(format ${BUILD_SUPPORT_DIR}/run_clang_format.py
${CLANG_FORMAT_BIN}
${BUILD_SUPPORT_DIR}/clang_format_exclusions.txt
--source_dirs
${FORMAT_DIRS}
--fix
--quiet
)

# Runs clang format and exits with a non-zero exit code if any files need to be reformatted
add_custom_target(check-format ${BUILD_SUPPORT_DIR}/run_clang_format.py
${CLANG_FORMAT_BIN}
${BUILD_SUPPORT_DIR}/clang_format_exclusions.txt
--source_dirs
${FORMAT_DIRS}
--quiet
)

# Provide only the minimum source files needed by downstream users
set(package_files src/ CMakeLists.txt LICENSE.txt)
set(packaged_zip ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-src.zip)
Expand Down
Empty file.
131 changes: 131 additions & 0 deletions build_support/run_clang_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#!/usr/bin/env python3
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
# Modified from the Apache Arrow project for the numbers project.

import argparse
import codecs
import difflib
import fnmatch
import os
import subprocess
import sys


def has_correct_extensions(filename, extensions=(".h", ".hh", ".cc", ".inc")):
return filename.endswith(extensions)


def check(arguments, source_dir):
formatted_filenames = []
error = False
for directory, subdirs, filenames in os.walk(source_dir):
fullpaths = (os.path.join(directory, filename)
for filename in filenames)
source_files = [x for x in fullpaths
if has_correct_extensions(x)]
formatted_filenames.extend(
# Filter out files that match the globs in the globs file
[filename for filename in source_files
if not any((fnmatch.fnmatch(filename, exclude_glob)
for exclude_glob in exclude_globs))])

if arguments.fix:
if not arguments.quiet:
# Print out each file on its own line, but run
# clang format once for all of the files
print("\n".join(map(lambda x: "Formatting {}".format(x),
formatted_filenames)))
subprocess.check_call([arguments.clang_format_binary,
"-i"] + formatted_filenames)
else:
for filename in formatted_filenames:
if not arguments.quiet:
print("Checking {}".format(filename))
#
# Due to some incompatibilities between Python 2 and
# Python 3, there are some specific actions we take here
# to make sure the difflib.unified_diff call works.
#
# In Python 2, the call to subprocess.check_output return
# a 'str' type. In Python 3, however, the call returns a
# 'bytes' type unless the 'encoding' argument is
# specified. Unfortunately, the 'encoding' argument is not
# in the Python 2 API. We could do an if/else here based
# on the version of Python we are running, but it's more
# straightforward to read the file in binary and do utf-8
# conversion. In Python 2, it's just converting string
# types to unicode types, whereas in Python 3 it's
# converting bytes types to utf-8 encoded str types. This
# approach ensures that the arguments to
# difflib.unified_diff are acceptable string types in both
# Python 2 and Python 3.
with open(filename, "rb") as reader:
# Run clang-format and capture its output
formatted = subprocess.check_output(
[arguments.clang_format_binary,
filename])
formatted = codecs.decode(formatted, "utf-8")
# Read the original file
original = codecs.decode(reader.read(), "utf-8")
# Run the equivalent of diff -u
diff = list(difflib.unified_diff(
original.splitlines(True),
formatted.splitlines(True),
fromfile=filename,
tofile="{} (after clang format)".format(
filename)))
if diff:
print("{} had clang-format style issues".format(filename))
# Print out the diff to stderr
error = True
sys.stderr.writelines(diff)
return error


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Runs clang format on all of the source "
"files. If --fix is specified, and compares the output "
"with the existing file, outputting a unifiied diff if "
"there are any necessary changes")
parser.add_argument("clang_format_binary",
help="Path to the clang-format binary")
parser.add_argument("exclude_globs",
help="Filename containing globs for files "
"that should be excluded from the checks")
parser.add_argument("--source_dirs",
help="Comma-separated root directories of the code")
parser.add_argument("--fix", default=False,
action="store_true",
help="If specified, will re-format the source "
"code instead of comparing the re-formatted "
"output, defaults to %(default)s")
parser.add_argument("--quiet", default=False,
action="store_true",
help="If specified, only print errors")

args = parser.parse_args()

had_err = False
exclude_globs = [line.strip() for line in open(args.exclude_globs)]
for source_dir in args.source_dirs.split(','):
if len(source_dir) > 0:
had_err = had_err or check(args, source_dir)

sys.exit(1 if had_err else 0)
5 changes: 2 additions & 3 deletions examples/overflowing-sub.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ void for_error() {

// error - infinite loop
for (i = 10; i >= 0; i--) {
printf("[ID %u] Hello, World\n", i);
printf("[ID %lu] Hello, World\n", i);
}
}

void for_correct() {
using namespace numbers;

u8 i;
bool flag = false;
for (i = 10; !flag && i >= 0; std::tie(i, flag) = i.overflowing_sub(1)) {
for (u8 i = 10; !flag && i >= 0; std::tie(i, flag) = i.overflowing_sub(1)) {
printf("[ID %u] Hello, World\n", i);
}
}
Expand Down
23 changes: 11 additions & 12 deletions examples/size.cc
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#include "numbers.h"

int main(int argc, char const *argv[])
{
int main(int argc, char const *argv[]) {
std::cout << "==== size example ==== \n";
std::cout << "sizeof(numbers::i8) = "<< sizeof(numbers::i8) << '\n';
std::cout << "sizeof(numbers::i16) = "<< sizeof(numbers::i16) << '\n';
std::cout << "sizeof(numbers::i32) = "<< sizeof(numbers::i32) << '\n';
std::cout << "sizeof(numbers::i64) = "<< sizeof(numbers::i64) << '\n';
std::cout << "sizeof(numbers::i128) = "<< sizeof(numbers::i128) << '\n';
std::cout << "sizeof(numbers::i8) = " << sizeof(numbers::i8) << '\n';
std::cout << "sizeof(numbers::i16) = " << sizeof(numbers::i16) << '\n';
std::cout << "sizeof(numbers::i32) = " << sizeof(numbers::i32) << '\n';
std::cout << "sizeof(numbers::i64) = " << sizeof(numbers::i64) << '\n';
std::cout << "sizeof(numbers::i128) = " << sizeof(numbers::i128) << '\n';

std::cout << "sizeof(numbers::u8) = "<< sizeof(numbers::u8) << '\n';
std::cout << "sizeof(numbers::u16) = "<< sizeof(numbers::u16) << '\n';
std::cout << "sizeof(numbers::u32) = "<< sizeof(numbers::u32) << '\n';
std::cout << "sizeof(numbers::u64) = "<< sizeof(numbers::u64) << '\n';
std::cout << "sizeof(numbers::u128) = "<< sizeof(numbers::u128) << '\n';
std::cout << "sizeof(numbers::u8) = " << sizeof(numbers::u8) << '\n';
std::cout << "sizeof(numbers::u16) = " << sizeof(numbers::u16) << '\n';
std::cout << "sizeof(numbers::u32) = " << sizeof(numbers::u32) << '\n';
std::cout << "sizeof(numbers::u64) = " << sizeof(numbers::u64) << '\n';
std::cout << "sizeof(numbers::u128) = " << sizeof(numbers::u128) << '\n';
return 0;
}
2 changes: 1 addition & 1 deletion src/include/bits.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include <type_traits>

#include "internal/config.h"
#include "internal/bits.hh"
#include "internal/config.h"

#if NUMBERS_INTERNAL_CPLUSPLUS_LANG >= 202002L
#include <bit>
Expand Down
Loading

0 comments on commit a22bda7

Please sign in to comment.