Increased alignment of global symbols GCC x86/x86-64 - c

Why does gcc overalign global arrays? I would guess its for performance, but
how if so? Furthermore this overalignment hard stops at 32 for a 16 bit type.
Here is an example of code generation that demonstrates this:
#include <stdint.h>
uint16_t scalar_aligned_2 = 1;
uint16_t table_aligned_2[] = { 1, 1 };
uint16_t table_aligned_8[] = { 1, 1, 1, 1 };
uint16_t table_aligned_16[] = { 1, 1, 1, 1, 1, 1, 1, 1 };
uint16_t table_aligned_32[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
Compiler Explorer
The assembly generated will essentially be:
.data
scalar_aligned_2:
.align 2
.value 1
table_aligned_2:
.align 2
.value 1
.value 1
table_aligned_8:
.align 8
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
table_aligned_16:
.align 16
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
table_aligned_32:
.align 32
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1
.value 1

Related

Add a column to a numpy array that counts if rows change

I have the following array:
[[1 2 1 0 2 0]
[1 2 1 0 2 0]
[1 2 1 0 2 0]
[1 2 1 0 2 0]
[0 1 2 1 0 0]
[0 1 2 1 0 0]
[0 0 1 0 1 0]
[0 0 0 1 1 0]
[0 0 0 0 1 0]
[0 0 0 0 0 1]]
I need to add a column to this array that adds a number whenever the values in the rows change starting with number 3. So the result would look like this:
[[1 2 1 0 2 0 3]
[1 2 1 0 2 0 3]
[1 2 1 0 2 0 3]
[1 2 1 0 2 0 3]
[0 1 2 1 0 0 4]
[0 1 2 1 0 0 4]
[0 0 1 0 1 0 5]
[0 0 0 1 1 0 6]
[0 0 0 0 1 0 7]
[0 0 0 0 0 1 8]]
Thank you
If a is your array as:
a = np.array([[1, 2, 1, 0, 2, 0], [1, 2, 1, 0, 2, 0], [1, 2, 1, 0, 2, 0], [1, 2, 1, 0, 2, 0],
[0, 1, 2, 1, 0, 0], [0, 1, 2, 1, 0, 0], [0, 0, 1, 0, 1, 0], [0, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1]])
using the following code will get you the results:
n = 3
a = a.tolist()
for i, j in enumerate(a):
if i == 0:
j.append(n)
elif i > 0 and j == a[i-1][:-1]:
j.append(n)
else:
n += 1
j.append(n)
# a = np.array(a)
which will give:
[[1 2 1 0 2 0 3]
[1 2 1 0 2 0 3]
[1 2 1 0 2 0 3]
[1 2 1 0 2 0 3]
[0 1 2 1 0 0 4]
[0 1 2 1 0 0 4]
[0 0 1 0 1 0 5]
[0 0 0 1 1 0 6]
[0 0 0 0 1 0 7]
[0 0 0 0 0 1 8]]

Printing a simple sudoku board array

So my code is currently missing the 8th index of the array and pushing it down 1 row. I'm new to kotlin but I honestly have no clue why its doing that.
fun main() {
val board = arrayOf(
arrayOf(7, 8, 0, 4, 0, 0, 1, 2, 0),
arrayOf(6, 0, 0, 0, 7, 5, 0, 0, 9),
arrayOf(0, 0, 0, 6, 0, 1, 0, 7, 8),
arrayOf(0, 0, 7, 0, 4, 0, 2, 6, 0),
arrayOf(0, 0, 1, 0, 5, 0, 9, 3, 0),
arrayOf(9, 0, 4, 0, 6, 0, 0, 0, 5),
arrayOf(0, 7, 0, 3, 0, 0, 0, 1, 2),
arrayOf(1, 2, 0, 0, 0, 7, 4, 0, 0),
arrayOf(0, 4, 9, 2, 0, 6, 0, 0, 7)
)
for (row in 0 until board.size) {
if(row % 3 == 0 && row != 0) {
println(" - - - - - - - - - " )
}
for(col in 0 until board.size) {
if(col == 8){
println(" ")
}
if(col % 3 == 0 && col != 0) {
print(" |")
}
print(" ")
print(board[row][col])
}
}
}
With the output being
7 8 0 | 4 0 0 | 1 2
0 6 0 0 | 0 7 5 | 0 0
9 0 0 0 | 6 0 1 | 0 7
8 - - - - - - - - -
0 0 7 | 0 4 0 | 2 6
0 0 0 1 | 0 5 0 | 9 3
0 9 0 4 | 0 6 0 | 0 0
5 - - - - - - - - -
0 7 0 | 3 0 0 | 0 1
2 1 2 0 | 0 0 7 | 4 0
0 0 4 9 | 2 0 6 | 0 0
7
You're adding a new line inside the inner for loop which is actually required after it. I've modified your code as follows:
for (row in 0 until board.size) {
if(row % 3 == 0 && row != 0) {
println(" - - - - - - - - - " )
}
for(col in 0 until board.size) {
if(col % 3 == 0 && col != 0) {
print(" |")
}
print(" " +board[row][col])
}
println()
}
This works flawlessly according to your requirement (as you wanted trailing spaces before and after)
Code:
val board = arrayOf(
arrayOf(7, 8, 0, 4, 0, 0, 1, 2, 0),
arrayOf(6, 0, 0, 0, 7, 5, 0, 0, 9),
arrayOf(0, 0, 0, 6, 0, 1, 0, 7, 8),
arrayOf(0, 0, 7, 0, 4, 0, 2, 6, 0),
arrayOf(0, 0, 1, 0, 5, 0, 9, 3, 0),
arrayOf(9, 0, 4, 0, 6, 0, 0, 0, 5),
arrayOf(0, 7, 0, 3, 0, 0, 0, 1, 2),
arrayOf(1, 2, 0, 0, 0, 7, 4, 0, 0),
arrayOf(0, 4, 9, 2, 0, 6, 0, 0, 7)
)
for (row in board.indices) {
if (row % 3 == 0 && row != 0) {
println(" - - - - - - - - - - - ")
}
for (col in board.indices) {
if (col % 3 == 0 && col != 0) {
print(" |")
}
print(" " + board[row][col])
}
println()
}
Result:
7 8 0 | 4 0 0 | 1 2 0
6 0 0 | 0 7 5 | 0 0 9
0 0 0 | 6 0 1 | 0 7 8
- - - - - - - - - - -
0 0 7 | 0 4 0 | 2 6 0
0 0 1 | 0 5 0 | 9 3 0
9 0 4 | 0 6 0 | 0 0 5
- - - - - - - - - - -
0 7 0 | 3 0 0 | 0 1 2
1 2 0 | 0 0 7 | 4 0 0
0 4 9 | 2 0 6 | 0 0 7
Edit: <>.indices is just a short extension for 0 until <>.size

How to creat a loop to assign a value given a condition?

So I have tried different types of visual basic code for this problem that i'm trying to solve but non of them is working :(
I have a jagged array for example {{1 10 20 50 53};{5 15 25 55}}
and I want to convert this information in a binary matrix given the condition that each array in the jagged array corresponds to a row, and the element in it corresponds to a column number. Am I being clear?
The matrix has the same rows as the number of arrays in the jagged array 2, but the columns in the matrix can go for example till 200.
1 0 ... 1 ... 1 ... 1 .. 1..................0
0 0 ... 1 ... 1 ... 1 .... 1 ...............0
My last attempt was this one:
For a = 0 To noRows - 1
For b = 0 To noCols - 1
Do While d < jarray(a).GetUpperBound(0)
If array(a)(d) = b Then
matrix(a, b) = 1
Exit Do
Else
matrix(a, b) = 0
d += 1
End If
Loop
Next
Next
What should I do?
When you make a new array its values are automatically initialised to the default value of the type of variable in the array - for an Integer, that is 0. So you don't need to do the setting to zero unless there is something else that isn't shown in the question.
You have the indexes of the locations which need to be set to 1, so you can do that directly:
Option Infer On
Option Strict On
Module Module1
Sub Main()
'Dim jArray = {New Integer() {1, 10, 20, 50, 53}, New Integer() {5, 15, 25, 55}}
Dim jArray = {New Integer() {1, 3, 4}, New Integer() {2, 5, 6, 7, 9}}
' find the number of columns needed for the resultant array
Dim nCols = jArray.Max(Function(r) r.Max())
' find the number of rows needed for the resultant array
Dim nRows = jArray.GetUpperBound(0) + 1
' the resultant array is automatically initialised to zeros
Dim matrix(nRows, nCols) As Integer
' fill in the ones as specified by the locations in jArray:
For i = 0 To nRows - 1
For j = 0 To jArray(i).GetUpperBound(0)
matrix(i, jArray(i)(j) - 1) = 1
Next
Next
' show the resultant array:
For i = 0 To nRows - 1
For j = 0 To nCols - 1
Console.Write(matrix(i, j) & If(j < nCols - 1, ", ", ""))
Next
Console.WriteLine()
Next
Console.ReadLine()
End Sub
End Module
Outputs:
1, 0, 1, 1, 0, 0, 0, 0, 0
0, 1, 0, 0, 1, 1, 1, 0, 1

Searching for maximum path of zeroes in matrix

Given an 10 x N matrix of 1's and 0s, such as:
1 1 1 1 1 1 1 1
1 1 0 1 1 0 0 1
0 0 0 1 1 0 0 1
0 0 0 1 1 0 0 0
1 0 0 0 0 1 0 0
1 0 1 1 1 1 1 1
1 0 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
notes:
the zeroes in a column are always between two runs of consecutive 1s. for example, a column such as 1 1 1 0 1 0 1 1 1 1 is not permitted
there must be at least a gap of one zero in each column, ie a column such as: 1 1 1 1 1 1 1 1 1 1 is not allowed
I want to find the longest consecutive streak of zeroes from left to right. In this case, that would be 4, which corresponds to the path starting in the second column of the 5th row from the top,
The second longest is 3 and there are 3 examples of that.
I'm a bit stumped on this, especially for very large N (~10M). I am looking for suggestions for the right approach/data structure to use or a similar problem and the algorithm used there. Another potential way to model the problem is to represent the problem using two lists:
L1 = [2, 2, 1, 4, 4, 1, 1, 3]
L2 = [6, 3, 5, 5, 5, 5, 5, 5]
but still not quite sure how to come up with an efficient solution
The solution using itertools.groupby(), sum() and max() functions:
import itertools
m = [
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 0, 1, 1, 0, 0, 1],
[0, 0, 0, 1, 1, 0, 0, 1],
[0, 0, 0, 1, 1, 0, 0, 0],
[1, 0, 0, 0, 0, 1, 0, 0],
[1, 0, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1]
]
max_zero_len = max(sum(1 for z in g if z == 0)
for l in m for k,g in itertools.groupby(l))
print(max_zero_len)
The output:
4
for l in m for k,g in itertools.groupby(l) - will generate a separate group for each consecutive sequences of 1 and 0 values for each nested list. (like [1,1], [0], [1,1], [0,0] ...)
sum(1 for z in g if z == 0) - considers only 0's sequences and counts its length using sum function
max(...) - gets the maximum length among zero(0) sequences

How can i fix this code that removes all non-zero elements from one array from another in VBA?

I have written some code that removes all non-zero terms from one array from another. But it has some bugs and I can't seem to solve it.
for instance if
A(1) = 0
A(2) = 3
A(3) = 0
A(4) = 4
for i = 1 to 4
B(i) = i
next i
I want B to look like this after
B= (1, 2)
For i = 1 To UBound(A) - 1
If A(i) <> 0 Then
count = count + 1
End If
Next i
For j = 1 To count
k = 1
Do While k < UBound(A)
If A(k) <> 0 Then 'If A is not equal to 0
A(k) = m
For i = m To UBound(B) - 1
B(i) = B(i + 1)
Next i
ReDim Preserve B(1 To UBound(B) - 1)
Else
End If
k = k + 1
Loop
A ReDim Preserve is not necessary because you have all the information you need after you count the number of non-zero items in your original array. Here's an example:
Option Explicit
Sub test()
Dim testdata() As Variant
Dim resultdata() As Variant
testdata = Array(2, 1, 0, 3, 0, 4, 2, 4, 5, 0, 0, 3, 6, 0, 0, 1)
RemoveZeros testdata, resultdata
Debug.Print "Original array len= " & UBound(testdata)
Debug.Print "Results array len= " & UBound(resultdata)
End Sub
Function RemoveZeros(ByRef inputArray() As Variant, _
ByRef outputArray() As Variant) As Variant
Dim numNonZeros As Long
Dim i As Long
Dim j As Long
numNonZeros = 0
For i = LBound(inputArray) To UBound(inputArray)
If inputArray(i) <> 0 Then
numNonZeros = numNonZeros + 1
End If
Next i
If numNonZeros > 0 Then
'--- create the array and load it up
j = 1
ReDim outputArray(j To numNonZeros)
For i = LBound(inputArray) To UBound(inputArray)
If inputArray(i) <> 0 Then
outputArray(j) = inputArray(i)
j = j + 1
End If
Next i
Else
ReDim outputArray(1 To 1)
outputArray(1) = 0
End If
RemoveZeros = outputArray
End Function
Does this do the job for you?
Dim A(4) As Integer
Dim B() As Integer
Dim i As Integer
Dim j As Integer
A(1) = 0
A(2) = 3
A(3) = 0
A(4) = 4
j = 1
For i = 1 To UBound(A)
If A(i) = 0 Then
ReDim Preserve B(j)
B(j) = i
j = j + 1
End If
Next i

Resources