Skip to content

Commit b7f6ade

Browse files
committed
no more bitwise violations. also whitespace begone
1 parent 7019482 commit b7f6ade

4 files changed

Lines changed: 77 additions & 59 deletions

File tree

.github/workflows/ci.yml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,23 @@ jobs:
133133
echo "Checking for bitwise operators (forbidden by educational constraints):"
134134
135135
# Check for actual bitwise operators (the ones we want to avoid)
136-
# Exclude && and || (logical ops) by using negative lookbehind/ahead
137-
BITWISE_COUNT=$(grep -rn -E '(\&[^&]|\|[^|]|\^|<<|>>|~)' src/ | grep -v '//' | grep -v 'math.h.*\^' | grep -v '&&' | grep -v '||' | wc -l)
136+
# Use sed to strip all comments before checking
137+
BITWISE_VIOLATIONS=$(find src/ -name "*.c" -o -name "*.h" | xargs sed -e 's|//.*||g' -e 's|/\*.*\*/||g' | grep -n -E '(\&[^&]|\|[^|]|\^|<<|>>|~)' | grep -v '&&' | grep -v '||' || true)
138+
139+
BITWISE_COUNT=$(echo "$BITWISE_VIOLATIONS" | grep -c . 2>/dev/null || true)
140+
if [ -z "$BITWISE_COUNT" ] || [ "$BITWISE_COUNT" = "" ]; then
141+
BITWISE_COUNT=0
142+
fi
143+
138144
if [ "$BITWISE_COUNT" -eq 0 ]; then
139145
echo "✅ No bitwise operators found - constraint maintained!"
140146
else
141-
echo "❌ Found $BITWISE_COUNT bitwise operations - review needed:"
142-
grep -rn -E '(\&[^&]|\|[^|]|\^|<<|>>|~)' src/ | grep -v '//' | grep -v 'math.h.*\^' | grep -v '&&' | grep -v '||' | head -5
147+
echo "❌ Found $BITWISE_COUNT bitwise operations - CONSTRAINT VIOLATION!"
148+
echo "These violate the educational constraints:"
149+
echo "$BITWISE_VIOLATIONS" | head -10
150+
echo ""
151+
echo "FAILING BUILD due to constraint violations"
152+
exit 1
143153
fi
144154
145155
echo ""

.github/workflows/security.yml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,19 @@ jobs:
118118
119119
# Check for bitwise operators (should be forbidden by project rules)
120120
echo "### Bitwise operators:"
121-
if grep -rn -E "(\&[^&]|\|[^|]|\^|<<|>>|~)" src/ | grep -v "//" | head -10; then
122-
echo "❌ Found bitwise operators - may violate project constraints"
123-
echo "Review: Are these compliant with the educational constraints?"
121+
BITWISE_VIOLATIONS=$(find src/ -name "*.c" -o -name "*.h" | xargs sed -e 's|//.*||g' -e 's|/\*.*\*/||g' | grep -n -E '(\&[^&]|\|[^|]|\^|<<|>>|~)' | grep -v '&&' | grep -v '||' || true)
122+
123+
BITWISE_COUNT=$(echo "$BITWISE_VIOLATIONS" | grep -c . 2>/dev/null || true)
124+
if [ -z "$BITWISE_COUNT" ] || [ "$BITWISE_COUNT" = "" ]; then
125+
BITWISE_COUNT=0
126+
fi
127+
128+
if [ "$BITWISE_COUNT" -eq 0 ]; then
129+
echo "✅ No bitwise operators found - constraint maintained!"
124130
else
125-
echo "✅ No bitwise operators found"
131+
echo "❌ Found $BITWISE_COUNT bitwise operations - SECURITY RISK!"
132+
echo "These may violate educational constraints and pose security risks:"
133+
echo "$BITWISE_VIOLATIONS" | head -10
126134
fi
127135
128136
# Check for arithmetic operators beyond basic ones

src/math.c

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
bin binAdd(const bin x, const bin y) {
88
bin r = binZERO;
99
bin_int_t carry = 0;
10-
10+
1111
for (bin_int_t i = 0; i < BIN_BITS; i++) {
1212
bin_int_t sum = x.bits[i] + y.bits[i] + carry;
13-
r.bits[i] = sum & 1; // Extract least significant bit
14-
carry = sum >> 1; // Extract carry bit
13+
r.bits[i] = sum % 2; // Extract least significant bit (check if odd)
14+
carry = sum / 2; // Extract carry bit (divide by 2)
1515
}
1616

1717
// Check for overflow
@@ -112,18 +112,18 @@ bin binModulus(const bin x, const bin y) {
112112

113113
if (binLT(x, y))
114114
return x;
115-
115+
116116
// Use the same algorithm as division but return remainder instead
117117
bin remainder = binZERO;
118-
118+
119119
for (int i = BIN_BITS-1; i >= 0; --i) {
120120
remainder = binShiftL1(remainder);
121121
remainder.bits[0] = x.bits[i];
122-
122+
123123
if (binGTEQ(remainder, y))
124124
remainder = binSubtract(remainder, y);
125125
}
126-
126+
127127
return remainder;
128128
}
129129

@@ -134,7 +134,7 @@ bin binPow(const bin x, const bin y) {
134134
return binONE;
135135
else if (binEQOne(y))
136136
return x;
137-
137+
138138
// Use binary exponentiation (square and multiply)
139139
bin result = binONE;
140140
bin base = x;
@@ -146,7 +146,7 @@ bin binPow(const bin x, const bin y) {
146146
base = binMultiply(base, base);
147147
exponent = binShiftR1(exponent);
148148
}
149-
149+
150150
return result;
151151
}
152152

@@ -156,39 +156,39 @@ bin binSqrt(const bin x) {
156156
return binZERO;
157157
else if (binEQOne(x))
158158
return binONE;
159-
159+
160160
// Use Newton-Raphson method: x_{n+1} = (x_n + a/x_n) / 2
161161
// Start with x/2 as initial guess
162162
bin guess = binShiftR1(x);
163163
bin prev_guess;
164164
bin_int_t iterations = 0;
165165
const bin_int_t max_iterations = 30; // Prevent infinite loops
166-
166+
167167
do {
168168
prev_guess = guess;
169-
169+
170170
// Calculate a/guess
171171
bin quotient = binDivide(x, guess);
172-
172+
173173
// Calculate (guess + a/guess) / 2
174174
bin sum = binAdd(guess, quotient);
175175
guess = binShiftR1(sum);
176-
176+
177177
iterations++;
178-
178+
179179
// Stop if we've converged or hit max iterations
180180
} while (!binEQ(guess, prev_guess) && iterations < max_iterations);
181-
181+
182182
return guess;
183183
}
184184

185185
// Calculate the base2 logarithm of a binary number
186186
bin binLog2(const bin x) {
187187
assert(!binEQZero(x)); // log2(0) is undefined
188-
188+
189189
if (binEQOne(x))
190190
return binZERO; // log2(1) = 0
191-
191+
192192
// Find the position of the most significant bit (MSB)
193193
// This gives us the integer part of log2(x)
194194
return binMSBi(x);
@@ -198,24 +198,24 @@ bin binLog2(const bin x) {
198198
// Calculate the base10 logarithm of a binary number
199199
bin binLog10(const bin x) {
200200
assert(!binEQZero(x)); // log10(0) is undefined
201-
201+
202202
if (binEQOne(x))
203203
return binZERO; // log10(1) = 0
204-
204+
205205
// Use a lookup table approach for common values
206206
// For values 1-1000, we can pre-calculate log10
207207
if (binLTEQ(x, binNew(1000))) {
208208
// Simple lookup table for powers of 10 and common values
209209
if (binEQ(x, binNew(10))) return binNew(1);
210210
if (binEQ(x, binNew(100))) return binNew(2);
211211
if (binEQ(x, binNew(1000))) return binNew(3);
212-
212+
213213
// For other values, find the largest power of 10 that fits
214214
if (binGTEQ(x, binNew(100))) return binNew(2);
215215
if (binGTEQ(x, binNew(10))) return binNew(1);
216216
return binZERO; // x < 10
217217
}
218-
218+
219219
// For larger values, use a different approach
220220
// Count the number of digits in base 10 representation
221221
bin temp = x;
@@ -224,7 +224,7 @@ bin binLog10(const bin x) {
224224
temp = binDivide(temp, binNew(10));
225225
digit_count++;
226226
}
227-
227+
228228
// log10(x) is approximately (number of digits - 1)
229229
return binNew(digit_count - 1);
230230
}
@@ -234,17 +234,17 @@ bin binLog10(const bin x) {
234234
// This is a simplified integer-based approach that maintains the constraint-based design
235235
bin binLog(const bin x) {
236236
assert(!binEQZero(x)); // log(0) is undefined
237-
237+
238238
if (binEQOne(x))
239239
return binZERO; // log(1) = 0
240-
240+
241241
// For integer natural logarithm, we use a simple approximation:
242242
// ln(x) ≈ ln(2) * log2(x) where ln(2) ≈ 0.693
243243
// Since we're working with integers, we'll use a scaled approach
244-
244+
245245
// Get the log2 value (MSB position)
246246
bin log2_val = binLog2(x);
247-
247+
248248
// Simple lookup table approach for small values (more accurate)
249249
if (binLTEQ(x, binNew(16))) {
250250
// Pre-calculated ln(x) values scaled by 1 (rounded to nearest integer)
@@ -256,15 +256,15 @@ bin binLog(const bin x) {
256256
else if (x_int <= 12) return binNew(2);
257257
else return binNew(3);
258258
}
259-
259+
260260
// For larger values, use the approximation ln(x) ≈ 0.7 * log2(x)
261261
// We'll use integer arithmetic: ln(x) ≈ (7 * log2(x)) / 10
262262
// But since we need integer results, we'll round: ln(x) ≈ (7 * log2(x) + 5) / 10
263-
263+
264264
bin seven = binNew(7);
265265
bin ten = binNew(10);
266266
bin five = binNew(5);
267-
267+
268268
// Calculate (7 * log2_val + 5) / 10
269269
bin scaled = binAdd(binMultiply(seven, log2_val), five);
270270
return binDivide(scaled, ten);

src/math.h

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
/**
66
* @brief Add two binary numbers
7-
*
7+
*
88
* Performs binary addition of two binary numbers. The function handles
99
* binary arithmetic with proper carry propagation.
10-
*
10+
*
1111
* @param x The first binary number (addend)
1212
* @param y The second binary number (addend)
1313
* @return The sum of x and y as a binary number
14-
*
14+
*
1515
* @note This function performs standard binary addition with carry handling.
1616
* The result is a binary number representing the arithmetic sum.
1717
*/
@@ -21,22 +21,22 @@ bin binAdd(const bin x, const bin y);
2121
* @brief Increment a binary number by one
2222
* @param x The binary number to increment
2323
* @return The incremented binary number (x + 1)
24-
*
24+
*
2525
* @note Don't try to increment the maximum binary number value.
2626
* Currently this won't crash but it should.
2727
*/
2828
bin binIncrement(const bin x);
2929

3030
/**
3131
* @brief Subtract two binary numbers
32-
*
32+
*
3333
* Performs binary subtraction of y from x. The function handles
3434
* binary arithmetic with proper borrow propagation.
35-
*
35+
*
3636
* @param x The binary number to subtract from (minuend)
3737
* @param y The binary number to subtract (subtrahend)
3838
* @return The difference of x and y as a binary number (x - y)
39-
*
39+
*
4040
* @note This function performs standard binary subtraction with borrow handling.
4141
* If y > x, the result will be the two's complement representation.
4242
*/
@@ -46,17 +46,17 @@ bin binSubtract(const bin x, const bin y);
4646
* @brief Decrement a binary number by one
4747
* @param x The binary number to decrement
4848
* @return The decremented binary number (x - 1)
49-
*
49+
*
5050
* @note Don't try to decrement the minimum binary number value (0).
5151
*/
5252
bin binDecrement(const bin x);
5353

5454
/**
5555
* @brief Multiply two binary numbers
56-
*
56+
*
5757
* Performs binary multiplication of two binary numbers using
5858
* the standard recursive multiplication algorithm.
59-
*
59+
*
6060
* @param x The first binary number (multiplicand)
6161
* @param y The second binary number (multiplier)
6262
* @return The product of x and y as a binary number
@@ -65,42 +65,42 @@ bin binMultiply(const bin x, const bin y);
6565

6666
/**
6767
* @brief Divide two binary numbers
68-
*
68+
*
6969
* Performs binary division of x by y using the standard
7070
* long division algorithm.
71-
*
71+
*
7272
* @param x The binary number to divide (dividend)
7373
* @param y The binary number to divide by (divisor)
7474
* @return The quotient of x divided by y as a binary number
75-
*
75+
*
7676
* @note This function assert()s that the divisor is not zero.
7777
*/
7878
bin binDivide(const bin x, const bin y);
7979

8080
/**
8181
* @brief Calculate the remainder of division
82-
*
82+
*
8383
* Performs binary division and returns the remainder
8484
* (modulo operation).
85-
*
85+
*
8686
* @param x The binary number to divide (dividend)
8787
* @param y The binary number to divide by (divisor)
8888
* @return The remainder of x divided by y as a binary number
89-
*
89+
*
9090
* @note This function assert()s that the divisor is not zero.
91-
*
91+
*
9292
*/
9393
bin binModulus(const bin x, const bin y);
9494

9595
/**
9696
* @brief Raise a binary number to a power
97-
*
97+
*
9898
* Calculates x raised to the power of y (x^y).
99-
*
99+
*
100100
* @param x The base binary number
101101
* @param y The exponent binary number
102102
* @return x raised to the power of y as a binary number
103-
*
103+
*
104104
* @note This function implements binary exponentiation.
105105
* For y = 0, the result is 1.
106106
* For y = 1, the result is x.
@@ -112,7 +112,7 @@ bin binPow(const bin x, const bin y);
112112
* @brief Calculate the square root of a binary number
113113
* @param x The binary number to calculate the square root of
114114
* @return The square root of x as a binary number
115-
*
115+
*
116116
* @note This function implements the Newton-Raphson method for square root calculation.
117117
*/
118118
bin binSqrt(const bin x);

0 commit comments

Comments
 (0)