Skip to content

Commit

Permalink
Added clang-format.
Browse files Browse the repository at this point in the history
  • Loading branch information
tfpf committed Apr 22, 2024
1 parent 31817eb commit b31c912
Show file tree
Hide file tree
Showing 13 changed files with 461 additions and 225 deletions.
192 changes: 192 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
---
Language: Cpp
# BasedOnStyle: WebKit
AccessModifierOffset: -4
AlignAfterOpenBracket: DontAlign
AlignArrayOfStructures: None
AlignConsecutiveMacros: None
AlignConsecutiveAssignments: None
AlignConsecutiveBitFields: None
AlignConsecutiveDeclarations: None
AlignEscapedNewlines: Right
AlignOperands: DontAlign
AlignTrailingComments: false
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortLambdasOnASingleLine: false
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: All
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
AttributeMacros:
- __capability
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: true
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: All
BreakBeforeConceptDeclarations: true
BreakBeforeBraces: Allman
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeComma
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 119
CommentPragmas: '^ IWYU pragma:'
QualifierAlignment: Leave
CompactNamespaces: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: false
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock
ExperimentalAutoDetectBinPacking: false
PackConstructorInitializers: BinPack
BasedOnStyle: ''
ConstructorInitializerAllOnOneLineOrOnePerLine: false
AllowAllConstructorInitializersOnNextLine: true
FixNamespaceComments: false
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IfMacros:
- KJ_IF_MAYBE
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
SortPriority: 0
CaseSensitive: false
- Regex: '.*'
Priority: 1
SortPriority: 0
CaseSensitive: false
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentAccessModifiers: false
IndentCaseLabels: false
IndentCaseBlocks: false
IndentGotoLabels: true
IndentPPDirectives: None
IndentExternBlock: AfterExternBlock
IndentRequires: false
IndentWidth: 4
IndentWrappedFunctionNames: false
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
LambdaBodyIndentation: Signature
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: Inner
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 4
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakOpenParenthesis: 0
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PenaltyIndentedWhitespace: 0
PointerAlignment: Right
PPIndentWidth: -1
ReferenceAlignment: Pointer
ReflowComments: true
RemoveBracesLLVM: false
SeparateDefinitionBlocks: Leave
ShortNamespaceLines: 1
SortIncludes: CaseSensitive
SortJavaStaticImport: Before
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeParensOptions:
AfterControlStatements: true
AfterForeachMacros: true
AfterFunctionDefinitionName: false
AfterFunctionDeclarationName: false
AfterIfMacros: true
AfterOverloadedOperator: false
BeforeNonEmptyParentheses: false
SpaceAroundPointerQualifiers: Default
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: Never
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
BitFieldColonSpacing: Both
Standard: Latest
StatementAttributeLikeMacros:
- Q_EMIT
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 8
UseCRLF: false
UseTab: Never
WhitespaceSensitiveMacros:
- STRINGIZE
- PP_STRINGIZE
- BOOST_PP_STRINGIZE
- NS_SWIFT_NAME
- CF_SWIFT_NAME
...

42 changes: 20 additions & 22 deletions benchmarks/benchmarks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,29 @@
#include <cstdio>
#include <hdrbg.h>

#define benchmark(function, iterations) \
{ \
auto delay = std::chrono::microseconds::max(); \
for(int i = 0; i < 32; ++i) \
{ \
auto begin = std::chrono::high_resolution_clock::now(); \
for(int i = 0; i < iterations; ++i) \
{ \
function(0); \
} \
auto end = std::chrono::high_resolution_clock::now(); \
auto delay_ = std::chrono::duration_cast<std::chrono::microseconds>(end - begin); \
delay = std::min(delay, delay_); \
} \
auto result = delay.count() / static_cast<double>(iterations); \
std::printf("%20s %8.2lf µs\n", #function, result); \
}
#define benchmark(function, iterations) \
{ \
auto delay = std::chrono::microseconds::max(); \
for (int i = 0; i < 32; ++i) \
{ \
auto begin = std::chrono::high_resolution_clock::now(); \
for (int i = 0; i < iterations; ++i) \
{ \
function(0); \
} \
auto end = std::chrono::high_resolution_clock::now(); \
auto delay_ = std::chrono::duration_cast<std::chrono::microseconds>(end - begin); \
delay = std::min(delay, delay_); \
} \
auto result = delay.count() / static_cast<double>(iterations); \
std::printf("%20s %8.2lf µs\n", #function, result); \
}

/******************************************************************************
* Main function.
*****************************************************************************/
int main(void)
int
main(void)
{
benchmark(hdrbg_init, 100)
benchmark(hdrbg_reinit, 100)
benchmark(hdrbg_rand, 800)
benchmark(hdrbg_real, 800)
benchmark(hdrbg_init, 100) benchmark(hdrbg_reinit, 100) benchmark(hdrbg_rand, 800) benchmark(hdrbg_real, 800)
}
14 changes: 14 additions & 0 deletions clang-format.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#! /usr/bin/env bash

shopt -s globstar

# Switch to the directory containing the script so that relative paths may be
# used.
cd "${0%/*}"
files=(**/*.c **/*.cc **/*.h)
if [ "$1" = check ]
then
clang-format --verbose --dry-run -Werror ${files[@]}
else
clang-format --verbose -i ${files[@]}
fi
7 changes: 4 additions & 3 deletions examples/C++/thread-safe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
#include <hdrbg.h>
#include <iostream>

int main(void)
int
main(void)
{
hdrbg_t *hd = hdrbg_init(true);
std::uint8_t r_bytes[64];
hdrbg_fill(hd, false, r_bytes, 64);
hdrbg_dump(r_bytes, 64);

for(int i = 0; i < 4; ++i)
for (int i = 0; i < 4; ++i)
{
std::cout << hdrbg_rand(hd) << ' ';
}
for(int i = 0; i < 4; ++i)
for (int i = 0; i < 4; ++i)
{
std::cout << hdrbg_real(hd) << ' ';
}
Expand Down
7 changes: 4 additions & 3 deletions examples/C++/thread-unsafe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
#include <hdrbg.h>
#include <iostream>

int main(void)
int
main(void)
{
hdrbg_init(false);
std::uint8_t r_bytes[64];
hdrbg_fill(NULL, false, r_bytes, 64);
hdrbg_dump(r_bytes, 64);

for(int i = 0; i < 4; ++i)
for (int i = 0; i < 4; ++i)
{
std::cout << hdrbg_rand(NULL) << ' ';
}
for(int i = 0; i < 4; ++i)
for (int i = 0; i < 4; ++i)
{
std::cout << hdrbg_real(NULL) << ' ';
}
Expand Down
9 changes: 5 additions & 4 deletions examples/C/thread-safe.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
#include <stdbool.h>
#include <stdio.h>

int main(void)
int
main(void)
{
struct hdrbg_t *hd = hdrbg_init(true);
uint8_t r_bytes[64];
hdrbg_fill(hd, false, r_bytes, 64);
hdrbg_dump(r_bytes, 64);

for(int i = 0; i < 4; ++i)
for (int i = 0; i < 4; ++i)
{
printf("%"PRIu64" ", hdrbg_rand(hd));
printf("%" PRIu64 " ", hdrbg_rand(hd));
}
for(int i = 0; i < 4; ++i)
for (int i = 0; i < 4; ++i)
{
printf("%Lf ", hdrbg_real(hd));
}
Expand Down
9 changes: 5 additions & 4 deletions examples/C/thread-unsafe.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
#include <stddef.h>
#include <stdio.h>

int main(void)
int
main(void)
{
hdrbg_init(false);
uint8_t r_bytes[64];
hdrbg_fill(NULL, false, r_bytes, 64);
hdrbg_dump(r_bytes, 64);

for(int i = 0; i < 4; ++i)
for (int i = 0; i < 4; ++i)
{
printf("%"PRIu64" ", hdrbg_rand(NULL));
printf("%" PRIu64 " ", hdrbg_rand(NULL));
}
for(int i = 0; i < 4; ++i)
for (int i = 0; i < 4; ++i)
{
printf("%Lf ", hdrbg_real(NULL));
}
Expand Down
24 changes: 12 additions & 12 deletions include/hdrbg.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ enum hdrbg_err_t
extern "C"
{
#endif
enum hdrbg_err_t hdrbg_err_get(void);
struct hdrbg_t *hdrbg_init(bool dma);
struct hdrbg_t *hdrbg_reinit(struct hdrbg_t *hd);
int hdrbg_fill(struct hdrbg_t *hd, bool prediction_resistance, uint8_t *r_bytes, int long unsigned r_length);
uint64_t hdrbg_rand(struct hdrbg_t *hd);
uint64_t hdrbg_uint(struct hdrbg_t *hd, uint64_t modulus);
int64_t hdrbg_span(struct hdrbg_t *hd, int64_t left, int64_t right);
double long hdrbg_real(struct hdrbg_t *hd);
int hdrbg_drop(struct hdrbg_t *hd, int long long count);
void hdrbg_zero(struct hdrbg_t *hd);
void hdrbg_dump(uint8_t const *m_bytes, size_t m_length);
void hdrbg_tests(struct hdrbg_t *hd, void *tv);
enum hdrbg_err_t hdrbg_err_get(void);
struct hdrbg_t *hdrbg_init(bool dma);
struct hdrbg_t *hdrbg_reinit(struct hdrbg_t *hd);
int hdrbg_fill(struct hdrbg_t *hd, bool prediction_resistance, uint8_t *r_bytes, int long unsigned r_length);
uint64_t hdrbg_rand(struct hdrbg_t *hd);
uint64_t hdrbg_uint(struct hdrbg_t *hd, uint64_t modulus);
int64_t hdrbg_span(struct hdrbg_t *hd, int64_t left, int64_t right);
double long hdrbg_real(struct hdrbg_t *hd);
int hdrbg_drop(struct hdrbg_t *hd, int long long count);
void hdrbg_zero(struct hdrbg_t *hd);
void hdrbg_dump(uint8_t const *m_bytes, size_t m_length);
void hdrbg_tests(struct hdrbg_t *hd, void *tv);
#ifdef __cplusplus
}
#endif
Expand Down
Loading

0 comments on commit b31c912

Please sign in to comment.