douglasg14b / BetterConsoleTables

Faster, colorable, more configurable, and more robust console colors & tables for C# console applications
GNU Lesser General Public License v3.0
92 stars 16 forks source link

[BUG] RPI 4 ARM Linux System.PlatformNotSupportedException "Popcnt.PopCount" #23

Closed pedromsilvapt closed 2 years ago

pedromsilvapt commented 2 years ago

Hello. I was trying to run a project using this library in a RaspberryPi 4, and I get this exception:

Unhandled exception: System.PlatformNotSupportedException: Operation is not supported on this platform.
   at System.Runtime.Intrinsics.X86.Popcnt.PopCount(UInt32 value)
   at BetterConsoles.Colors.Extensions.FormatExtensions.BitCount(FontStyleExt styles)
   at BetterConsoles.Colors.Extensions.FormatExtensions.GetAnsiCodes(IFormat format, Int32 extraArraySize)
   at BetterConsoles.Colors.Extensions.StringExtensions.SetStyle(String value, IFormat format)
   at BetterConsoles.Tables.Table.FormatRow(IList`1 values, IList`1 formats, Int32[] columnLengths, Char& innerDelimiter, Char& outerDelimiter)
   at BetterConsoles.Tables.Table.ToString(Int32[] columnLengths)
   at BetterConsoles.Tables.Table.ToString()
   at Media.Storage.Console.Commands.HealthCommand.Handle(IConsole console, IHost host, String[] labels, ErrorHandler errorHandler, OutputFormat outputFormat, String output, String colSep, String rowSep, Boolean exact) in C:\Users\PedroSilva\source\repos\Media\Media.Storage.Console\Commands\HealthCommand.cs:line 143

It works fine when running on a Windows 10 x64 machine. I think the issue is with this PopCount call here, which I assume is just a performance optimization for platforms that support it. Would it be possible to exclude based on the processor (ARM vs x86) instead of just the framework version?

https://github.com/douglasg14b/BetterConsoleTables/blob/b3275b50ce8f24693a051c04b979805e9f77663c/BetterConsoles.Colors/Extensions/FormatExtensions.cs#L1-L32

In the meantime, since I assume this conditional block can just be removed without any functionality being lost, I will just git clone the project and make the changes locally on my end, so it's cool for me. Just wanted to put this here in case other people stumble across the same issue as me.

Thanks ☺️

douglasg14b commented 2 years ago

Hm, I'm not sure if there is a preprocessor constant for CPU platform 🤔

I'll have to poke around. I think I can define my own that I can use in a directive, unsure.

emmekappa commented 2 years ago

I think it should be sufficient to do add a runtime check on System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture