Skip to content

Commit

Permalink
Formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
sletz committed Aug 9, 2024
1 parent 53d0a76 commit cb8e1c2
Show file tree
Hide file tree
Showing 68 changed files with 1,167 additions and 829 deletions.
3 changes: 1 addition & 2 deletions FaustAlgebra/FaustAlgebra.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
//=====================================================================================================================

template <typename T>
class FaustAlgebra
{
class FaustAlgebra {
public:
//--------------------------------------------------------------------------------
// Dispatch tables
Expand Down
6 changes: 3 additions & 3 deletions format-files
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
find interval -path -prune -o -iname '*.cpp' -execdir clang-format -i -style=file {} \;
find interval -path -prune -o -iname '*.hh' -execdir clang-format -i -style=file {} \;
find interval -path -prune -o -iname '*.h' -execdir clang-format -i -style=file {} \;
find interval tlib FaustAlgebra main.cpp -path -prune -o -iname '*.cpp' -execdir clang-format -i -style=file {} \;
find interval tlib FaustAlgebra -path -prune -o -iname '*.hh' -execdir clang-format -i -style=file {} \;
find interval tlib FaustAlgebra -path -prune -o -iname '*.h' -execdir clang-format -i -style=file {} \;

112 changes: 81 additions & 31 deletions interval/bitwiseOperations.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* Copyright 2023 Yann ORLAREY
*
*
* Licensed 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.
Expand All @@ -18,17 +18,22 @@

#include "bitwiseOperations.hh"

namespace itv
{
namespace itv {

/**
* Split a signed interval into two unsigned intervals, for the negative and the positive part
*/
std::pair<UInterval, UInterval> signSplit(const SInterval& x)
{
if (isEmpty(x)) return {UEMPTY, UEMPTY};
if (x.hi < 0) return {{(unsigned int)(x.lo), (unsigned int)(x.hi)}, UEMPTY};
if (x.lo >= 0) return {UEMPTY, {(unsigned int)(x.lo), (unsigned int)(x.hi)}};
if (isEmpty(x)) {
return {UEMPTY, UEMPTY};
}
if (x.hi < 0) {
return {{(unsigned int)(x.lo), (unsigned int)(x.hi)}, UEMPTY};
}
if (x.lo >= 0) {
return {UEMPTY, {(unsigned int)(x.lo), (unsigned int)(x.hi)}};
}
return {{(unsigned int)(x.lo), (unsigned int)(-1)}, {(unsigned int)(0), (unsigned int)(x.hi)}};
}

Expand All @@ -38,7 +43,9 @@ std::pair<UInterval, UInterval> signSplit(const SInterval& x)
SInterval signMerge(const UInterval& np, const UInterval& pp)
{
if (isEmpty(np)) {
if (isEmpty(pp)) return SEMPTY;
if (isEmpty(pp)) {
return SEMPTY;
}
return {(int)(pp.lo), (int)(pp.hi)};
}
if (isEmpty(pp)) {
Expand Down Expand Up @@ -69,10 +76,18 @@ static bool contains(const UInterval& i, unsi

UInterval bitwiseUnsignedOr(const UInterval& a, const UInterval& b)
{
if (a == UInterval{0, 0}) return b;
if (b == UInterval{0, 0}) return a;
if (isEmpty(a)) return a;
if (isEmpty(b)) return b;
if (a == UInterval{0, 0}) {
return b;
}
if (b == UInterval{0, 0}) {
return a;
}
if (isEmpty(a)) {
return a;
}
if (isEmpty(b)) {
return b;
}
UInterval r{loOr2(a, b), hiOr2(a, b)};
return r;
}
Expand All @@ -86,8 +101,12 @@ static bool contains(const UInterval& i, unsigned int x)
unsigned int hiOr2(UInterval a, UInterval b)
{
// simple cases
if (a.lo == 0 && a.hi == 0) return b.hi;
if (b.lo == 0 && b.hi == 0) return a.hi;
if (a.lo == 0 && a.hi == 0) {
return b.hi;
}
if (b.lo == 0 && b.hi == 0) {
return a.hi;
}

// analyze and split the intervals
auto [ma, a0, a1] = splitInterval(a);
Expand All @@ -99,28 +118,44 @@ unsigned int hiOr2(UInterval a, UInterval b)
}

if (mb > ma) {
if (contains(a, mb - 1)) return 2 * mb - 1;
if (contains(a, mb - 1)) {
return 2 * mb - 1;
}
return hiOr2(b1 - mb, a) + mb;
}
if (ma > mb) {
if (contains(b, ma - 1)) return 2 * ma - 1;
if (contains(b, ma - 1)) {
return 2 * ma - 1;
}
return hiOr2(a1 - ma, b) + ma;
}
// ma == mb != 0
if (isEmpty(a0) && isEmpty(b0)) return hiOr2(a1 - ma, b1 - ma) + ma;
if (isEmpty(a0)) return std::max(hiOr2(a1 - ma, b1 - ma), hiOr2(a1 - ma, b0)) + ma;
if (isEmpty(b0)) return std::max(hiOr2(a1 - ma, b1 - ma), hiOr2(a0, b1 - ma)) + ma;
if (isEmpty(a0) && isEmpty(b0)) {
return hiOr2(a1 - ma, b1 - ma) + ma;
}
if (isEmpty(a0)) {
return std::max(hiOr2(a1 - ma, b1 - ma), hiOr2(a1 - ma, b0)) + ma;
}
if (isEmpty(b0)) {
return std::max(hiOr2(a1 - ma, b1 - ma), hiOr2(a0, b1 - ma)) + ma;
}
return std::max(hiOr2(a1 - ma, b1 - ma), std::max(hiOr2(a1 - ma, b0), hiOr2(a0, b1 - ma))) + ma;
}

unsigned int loOr2(UInterval a, UInterval b)
{
// isEmpty case
if (isEmpty(a) || isEmpty(b)) return 0;
if (isEmpty(a) || isEmpty(b)) {
return 0;
}

// zero cases
if (a.lo == 0) return b.lo;
if (b.lo == 0) return a.lo;
if (a.lo == 0) {
return b.lo;
}
if (b.lo == 0) {
return a.lo;
}

// non zero cases
auto [ma, a0, a1] = splitInterval(a);
Expand All @@ -129,17 +164,27 @@ unsigned int loOr2(UInterval a, UInterval b)

// obvious cases
if (ma > mb) {
if (isEmpty(a0)) return loOr2(a1 - ma, b) | ma; // ma bit unavoidable !
if (isEmpty(a0)) {
return loOr2(a1 - ma, b) | ma; // ma bit unavoidable !
}
return loOr2(a0, b);
}
if (mb > ma) {
if (isEmpty(b0)) return loOr2(a, b1 - mb) | mb;
if (isEmpty(b0)) {
return loOr2(a, b1 - mb) | mb;
}
return loOr2(a, b0);
}
// ma == mb != 0
if (!isEmpty(a0) && !isEmpty(b0)) return loOr2(a0, b0); // obvious case !
if (isEmpty(a0) && isEmpty(b0)) return loOr2(a1 - ma, b1 - ma) | ma; // ma bit unavoidable !
if (isEmpty(a0)) return std::min(loOr2(a1 - ma, b0) | ma, loOr2(a1 - ma, b1 - ma) | ma);
if (!isEmpty(a0) && !isEmpty(b0)) {
return loOr2(a0, b0); // obvious case !
}
if (isEmpty(a0) && isEmpty(b0)) {
return loOr2(a1 - ma, b1 - ma) | ma; // ma bit unavoidable !
}
if (isEmpty(a0)) {
return std::min(loOr2(a1 - ma, b0) | ma, loOr2(a1 - ma, b1 - ma) | ma);
}
return std::min(loOr2(a0, b1 - mb) | mb, loOr2(a1 - ma, b1 - ma) | ma);
}

Expand Down Expand Up @@ -169,11 +214,15 @@ unsigned int msb32(unsigned int x)
// split interval according to its msb
std::tuple<unsigned int, UInterval, UInterval> splitInterval(UInterval x)
{
if (x.lo == 0 && x.hi == 0) return {0, {1, 0}, x}; // special case, no msb
if (x.lo == 0 && x.hi == 0) {
return {0, {1, 0}, x}; // special case, no msb
}
unsigned int m = msb32(x.hi);
assert(m > 0);

if (m <= x.lo) return {m, {1, 0}, x}; // no msb in the interval
if (m <= x.lo) {
return {m, {1, 0}, x}; // no msb in the interval
}
return {m, {x.lo, (unsigned int)(m - 1)}, {m, x.hi}};
}

Expand Down Expand Up @@ -210,7 +259,8 @@ SInterval bitwiseSignedAnd(const SInterval& a, const SInterval& b)

UInterval bitwiseUnsignedXOr(const UInterval& a, const UInterval& b)
{
return bitwiseUnsignedAnd(bitwiseUnsignedOr(a, b), bitwiseUnsignedNot(bitwiseUnsignedAnd(a, b)));
return bitwiseUnsignedAnd(bitwiseUnsignedOr(a, b),
bitwiseUnsignedNot(bitwiseUnsignedAnd(a, b)));
}

SInterval bitwiseSignedXOr(const SInterval& a, const SInterval& b)
Expand Down
29 changes: 20 additions & 9 deletions interval/bitwiseOperations.hh
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#pragma once

#include <limits.h>
#include <algorithm>
#include <iostream>
#include <utility>
#include <algorithm>

namespace itv
{
namespace itv {
//==============================================================================
// Definitions of signed and unsigned intervals for bitwise operations
//==============================================================================
Expand Down Expand Up @@ -41,15 +40,19 @@ bool isEmpty(const BitwiseInterval<T>& i)
template <typename T>
bool operator==(const BitwiseInterval<T>& a, const BitwiseInterval<T>& b)
{
if (isEmpty(a)) return isEmpty(b);
if (isEmpty(a)) {
return isEmpty(b);
}
return (a.lo == b.lo) && (a.hi == b.hi);
}

// Equality of intervals.
template <typename T>
bool operator!=(const BitwiseInterval<T>& a, const BitwiseInterval<T>& b)
{
if (isEmpty(a)) return !isEmpty(b);
if (isEmpty(a)) {
return !isEmpty(b);
}
return (a.lo != b.lo) || (a.hi != b.hi);
}

Expand All @@ -59,13 +62,17 @@ bool operator!=(const BitwiseInterval<T>& a, const BitwiseInterval<T>& b)

inline std::ostream& operator<<(std::ostream& os, const UInterval& x)
{
if (isEmpty(x)) return os << "U[]";
if (isEmpty(x)) {
return os << "U[]";
}
return os << "U[" << x.lo << ".." << x.hi << "]";
}

inline std::ostream& operator<<(std::ostream& os, const SInterval& x)
{
if (isEmpty(x)) return os << "S[]";
if (isEmpty(x)) {
return os << "S[]";
}
return os << "S[" << x.lo << ".." << x.hi << "]";
}

Expand All @@ -77,8 +84,12 @@ inline std::ostream& operator<<(std::ostream& os, const SInterval& x)
template <typename T>
BitwiseInterval<T> operator+(const BitwiseInterval<T>& a, const BitwiseInterval<T>& b)
{
if (isEmpty(a)) return b;
if (isEmpty(b)) return a;
if (isEmpty(a)) {
return b;
}
if (isEmpty(b)) {
return a;
}
return {std::min(a.lo, b.lo), std::max(a.hi, b.hi)};
}

Expand Down
Loading

0 comments on commit cb8e1c2

Please sign in to comment.