Compare commits

...

2 Commits

Author SHA1 Message Date
blueloveTH
26a073de4f fix a bug of random 2025-02-15 00:49:32 +08:00
blueloveTH
069e80f2f5 fix a bug 2025-02-14 20:16:02 +08:00
2 changed files with 33 additions and 32 deletions

View File

@ -849,25 +849,31 @@ static py_TValue* c11_chunked_array2d__new_chunk(c11_chunked_array2d* self, c11_
return data;
}
static void cpy11__divmod_int_uint(int a, int b_log2, int b_mask, int* q, int* r) {
if(a >= 0) {
*q = a >> b_log2;
*r = a & b_mask;
} else {
*q = -1 - ((-a - 1) >> b_log2);
*r = b_mask - ((-a - 1) & b_mask);
}
}
static void c11_chunked_array2d__world_to_chunk(c11_chunked_array2d* self,
int col,
int row,
c11_vec2i* chunk_pos,
c11_vec2i* local_pos) {
if(col >= 0) {
chunk_pos->x = col >> self->chunk_size_log2;
local_pos->x = col & self->chunk_size_mask;
} else {
chunk_pos->x = -((-col) >> self->chunk_size_log2);
local_pos->x = (-col) & self->chunk_size_mask;
}
if(row >= 0) {
chunk_pos->y = row >> self->chunk_size_log2;
local_pos->y = row & self->chunk_size_mask;
} else {
chunk_pos->y = -((-row) >> self->chunk_size_log2);
local_pos->y = (-row) & self->chunk_size_mask;
}
cpy11__divmod_int_uint(col,
self->chunk_size_log2,
self->chunk_size_mask,
&chunk_pos->x,
&local_pos->x);
cpy11__divmod_int_uint(row,
self->chunk_size_log2,
self->chunk_size_mask,
&chunk_pos->y,
&local_pos->y);
}
static py_TValue* c11_chunked_array2d__parse_col_row(c11_chunked_array2d* self,
@ -938,7 +944,7 @@ static bool chunked_array2d__new__(int argc, py_Ref argv) {
case 1024: self->chunk_size_log2 = 10; break;
case 2048: self->chunk_size_log2 = 11; break;
case 4096: self->chunk_size_log2 = 12; break;
default: return ValueError("invalid chunk_size: %d", chunk_size);
default: return ValueError("invalid chunk_size: %d, not power of 2", chunk_size);
}
self->chunk_size_mask = chunk_size - 1;
memset(&self->last_visited, 0, sizeof(c11_chunked_array2d_chunks_KV));
@ -1118,8 +1124,8 @@ static bool chunked_array2d_view(int argc, py_Ref argv) {
max_chunk_x = c11__max(max_chunk_x, chunk_pos.x);
max_chunk_y = c11__max(max_chunk_y, chunk_pos.y);
}
int start_col = min_chunk_x << self->chunk_size_log2;
int start_row = min_chunk_y << self->chunk_size_log2;
int start_col = min_chunk_x * self->chunk_size;
int start_row = min_chunk_y * self->chunk_size;
int width = (max_chunk_x - min_chunk_x + 1) * self->chunk_size;
int height = (max_chunk_y - min_chunk_y + 1) * self->chunk_size;
return _chunked_array2d_view(py_retval(), argv, self, start_col, start_row, width, height);
@ -1142,8 +1148,8 @@ static bool chunked_array2d_view_chunk(int argc, py_Ref argv) {
PY_CHECK_ARG_TYPE(1, tp_vec2i);
c11_chunked_array2d* self = py_touserdata(&argv[0]);
c11_vec2i chunk_pos = py_tovec2i(&argv[1]);
int start_col = chunk_pos.x << self->chunk_size_log2;
int start_row = chunk_pos.y << self->chunk_size_log2;
int start_col = chunk_pos.x * self->chunk_size;
int start_row = chunk_pos.y * self->chunk_size;
return _chunked_array2d_view(py_retval(),
argv,
self,
@ -1162,8 +1168,8 @@ static bool chunked_array2d_view_chunks(int argc, py_Ref argv) {
c11_vec2i chunk_pos = py_tovec2i(&argv[1]);
int width = py_toint(&argv[2]) * self->chunk_size;
int height = py_toint(&argv[3]) * self->chunk_size;
int start_col = chunk_pos.x << self->chunk_size_log2;
int start_row = chunk_pos.y << self->chunk_size_log2;
int start_col = chunk_pos.x * self->chunk_size;
int start_row = chunk_pos.y * self->chunk_size;
return _chunked_array2d_view(py_retval(), argv, self, start_col, start_row, width, height);
}

View File

@ -137,14 +137,7 @@ int64_t mt19937__randint(mt19937* self, int64_t a, int64_t b) {
static bool Random__new__(int argc, py_Ref argv) {
mt19937* ud = py_newobject(py_retval(), py_totype(argv), 0, sizeof(mt19937));
mt19937__ctor(ud);
if(argc == 1) return true;
if(argc == 2) {
PY_CHECK_ARG_TYPE(1, tp_int);
py_i64 seed = py_toint(py_arg(1));
mt19937__seed(ud, seed);
return true;
}
return TypeError("Random(): expected 0 or 1 arguments, got %d", argc - 1);
return true;
}
static bool Random__init__(int argc, py_Ref argv) {
@ -152,9 +145,11 @@ static bool Random__init__(int argc, py_Ref argv) {
// do nothing
} else if(argc == 2) {
mt19937* ud = py_touserdata(py_arg(0));
PY_CHECK_ARG_TYPE(1, tp_int);
py_i64 seed = py_toint(py_arg(1));
mt19937__seed(ud, (uint32_t)seed);
if(!py_isnone(&argv[1])){
PY_CHECK_ARG_TYPE(1, tp_int);
py_i64 seed = py_toint(py_arg(1));
mt19937__seed(ud, (uint32_t)seed);
}
} else {
return TypeError("Random(): expected 1 or 2 arguments, got %d");
}