mscroggs.co.uk
mscroggs.co.uk

subscribe

Comment

Comments

Comments in green were written by me. Comments in blue were not written by me.
@Lord Sméagol: Hello and happy New Year!
9 minutes is cool!
The answer is wrong because of _lzcnt instruction, as you suspected, as turns out it works differently on different cpus: https://nextmovesoftware.com/blog/2017...
With this error, solutions having 1x1 square directly in the center are not counted.

I guess, gcc/clang do it correctly because I specify -march=native (so it checks cpu and generates correct instruction), and run where I compile. But it's a potential problem I probably need to add some assertions to the code.

Maybe on your hardware you can either use WSL and clang compiler, or set constexpr bool USE_SSE_QUADRANT_FILL=false, to fall back to slower.
You could also try to use BitScanReverse instead of __lzcnt, but it has different input/output so I'm not sure how hard would that be to fix it.
Oleg
on /blog/119
               
@Oleg:

The includes got filtered:

Util.h

//#include [bits/stdc++.h] // not including all
#include [filesystem] // just include what's needed
#include [array] // just include what's needed
#include [mutex] // just include what's needed

:)
Lord Sméagol
on /blog/119
               
@Oleg:

I removed my macros:
#define __tzcnt_u32(v) ((v) ? (_tzcnt_u32(v)) : (32))
#define __lzcnt32(v) ((v) ? (_lzcnt_u32(v)) : (32))
replacing them with simple inline code


Util.h

//#include // not including all
#include // just include what's needed
#include // just include what's needed
#include // just include what's needed

#if 1 // use safe localtime
struct tm buf; // use safe localtime
auto err = localtime_s(&buf, &cur_time); // use safe localtime
return std::put_time(&buf, "%F %T"); // use safe localtime
#else // use safe localtime
return std::put_time(std::localtime(&cur_time), "%F %T");
#endif // use safe localtime


State.h

changed _mm_set_epi8(0x80 to -0x80 to stop warnings

inline replacement:
//int i = __tzcnt_u32(mask); // for no BMI; without zero test, as not needed here
int i = _tzcnt_u32(mask); // for no BMI; without zero test, as not needed here

inline replacement:
//int last_idx_before_mid = 31 - __lzcnt32(off_mask); // for no BMI; without zero test, as not needed here
int last_idx_before_mid = _lzcnt_u32(off_mask); // for no BMI; without zero test, as not needed here


Solver.h

inline replacement:
//return ini.size(); // to stop warning
return (int)ini.size(); // to stop warning

inline replacement:
//const int dim = __tzcnt_u32(mask); // for no BMI; without zero test, as not needed here
const int dim = _tzcnt_u32(mask); // for no BMI; without zero test, as not needed here


I tried '9' runs: with asserts: 10:31, without: 10:18 (saved 2%)
A minute slower than the faulty version, but still not too bad for a 2013 (Q3) CPU :)
Lord Sméagol
on /blog/119
               
@Oleg: Happy new year!

I just added this:

#if 0
int last_idx_before_mid = 31 - __lzcnt32(off_mask); // 31 - LZCNT ==> index of MSb
#else

// if off_mask can never be zero, no need for check to override BSR result
assert(off_mask);
// a '9' run didn't reveal any 0 [you would know for sure for other sizes]

// need unsigned long result
unsigned long last_idx_before_mid;

// get index of MSb [no need for adjustment if off_mask can never be zero]
_BitScanReverse(&last_idx_before_mid, off_mask);
#endif

a run of '9' now produces the correct result: 1,730,280 :)
Lord Sméagol
on /blog/119
               

Archive

Show me a random blog post
 2026 

Feb 2026

Christmas (2025) is over
 2025 
▼ show ▼
 2024 
▼ show ▼
 2023 
▼ show ▼
 2022 
▼ show ▼
 2021 
▼ show ▼
 2020 
▼ show ▼
 2019 
▼ show ▼
 2018 
▼ show ▼
 2017 
▼ show ▼
 2016 
▼ show ▼
 2015 
▼ show ▼
 2014 
▼ show ▼
 2013 
▼ show ▼
 2012 
▼ show ▼

Tags

london news accuracy football statistics correlation kings craft newcastle flexagons folding paper people maths exponential growth error bars friendly squares matrices computational complexity gaussian elimination finite group big internet math-off pac-man puzzles game of life logs finite element method matrix of minors palindromes mathsteroids data warwick approximation logic royal baby geogebra dinosaurs pascal's triangle mean bots wave scattering regular expressions draughts mathsjam numerical analysis hexapawn golden spiral recursion zines sobolev spaces convergence electromagnetic field trigonometry the aperiodical alphabets dates realhats plastic ratio logo ucl books countdown polynomials rhombicuboctahedron errors wool boundary element methods captain scarlet guest posts games coins map projections dragon curves gerry anderson graph theory estimation fonts binary tmip braiding latex simultaneous equations live stream bodmas stirling numbers reddit christmas card pythagoras tennis pizza cutting matt parker programming fractals javascript inline code platonic solids nine men's morris manchester ternary signorini conditions thirteen menace mathslogicbot crossnumber final fantasy frobel chebyshev rust hannah fry geometry phd christmas radio 4 data visualisation asteroids kenilworth propositional calculus determinants bubble bobble turtles harriss spiral coventry machine learning european cup python matrix multiplication national lottery light raspberry pi fence posts anscombe's quartet sorting standard deviation datasaurus dozen crossnumbers talking maths in public squares advent calendar youtube matrix of cofactors go preconditioning golden ratio cambridge weather station curvature misleading statistics numbers interpolation sport arithmetic oeis hyperbolic surfaces speed dataset stickers pi approximation day a gamut of games crochet databet 24 hour maths gather town edinburgh bempp probability london underground rugby cross stitch reuleaux polygons video games world cup noughts and crosses manchester science festival partridge puzzle graphs game show probability weak imposition folding tube maps triangles chess php pi martin gardner hats crosswords inverse matrices chalkdust magazine quadrilaterals royal institution runge's phenomenon sound nonograms

Archive

Show me a random blog post
▼ show ▼
© Matthew Scroggs 2012–2026