-4
\$\begingroup\$

Not to be confused with Is my OS 32-bit or 64-bit.

This one is very simple, tell me whether or not my CPU supports 64-bit instructions. If my CPU supports 32-bit instructions, print '32', if my CPU supports 64 bit instructions, print '64', if my processor naitively supports other lengths of instructions, print 'other'.

Your program must run on both 32 and 64-bit instruction modes, and if interpreted run properly on both 32 and 64-bit interpreters.

Test cases:

Arch: x86, i386, i686, RiscV32
Output: '32'

Arch: x86-64, x64, x86_64, RiscV64
Output: '3264'

Arch: R700
Output '32other'

The usual rules apply.

Best of luck!

\$\endgroup\$
8
  • 6
    \$\begingroup\$ I don't think this should be tagged as a decision-problem, since there are more than 2 possible answers. (Although both the title and the 1st sentence of the description suggest that it's a "yes/no" kind of problem.) \$\endgroup\$ – Arnauld Sep 10 '20 at 14:28
  • 8
    \$\begingroup\$ Sure, but it's not a decision-problem anymore as defined in computability theory. \$\endgroup\$ – Arnauld Sep 10 '20 at 14:46
  • 1
    \$\begingroup\$ Is that the full list of inputs we're expected to handle and is there any flexibility in the output? \$\endgroup\$ – Shaggy Sep 11 '20 at 0:50
  • 1
    \$\begingroup\$ I don't think any of the existing answers ever print other, maybe you should consider removing that requirement. \$\endgroup\$ – Jonathan Allan Sep 11 '20 at 11:56
  • 1
    \$\begingroup\$ @user253751 yes \$\endgroup\$ – tuskiomi Sep 15 '20 at 19:27
3
\$\begingroup\$

Bash + coreutils, 33 25 bytes

lscpu|grep -Po '..(?=-b)'

Don't try it online! Won't work because the TIO sandbox doesn't support lscpu. Edit: Saved 8 bytes thanks to @DigitalTrauma.

\$\endgroup\$
6
  • \$\begingroup\$ it even works on RISC-V Debian! \$\endgroup\$ – tuskiomi Sep 10 '20 at 15:10
  • 1
    \$\begingroup\$ That's Linux specific, isn't? lscpu isn't present on all OSses, not even all Unices. \$\endgroup\$ – Abigail Sep 10 '20 at 15:20
  • \$\begingroup\$ @Abigail I'd still say it's legal, as it's interpreted, and it runs properly for 32 or 64 bit linux \$\endgroup\$ – tuskiomi Sep 10 '20 at 15:29
  • 2
    \$\begingroup\$ @tuskiomi I'm pretty sure what Abigail meant was that the title of the post isn't correct. I'd guess it should say something like Bash + coreutils on Linux, at the very least. I'm not sure if there's a better name or way to identify what contains this command. \$\endgroup\$ – FryAmTheEggman Sep 10 '20 at 16:30
  • \$\begingroup\$ Use grep's PCRE flag to obviate the need for sed: lscpu|grep -Po '..(?=-bit)' \$\endgroup\$ – Digital Trauma Sep 10 '20 at 16:49
1
\$\begingroup\$

Rust, 45 bytes

||if std::usize::MAX>4294967295{3264}else{32}

Try it online! (Cross compile for 32-bit locally by installing a toolchain for i686. For example: stable-i686-pc-windows-msvc)

Can't really test this on any architectures that support "other" lengths of instructions, but this can be used to check for 32-bit or 64-bit architectures. This is shorter than the built-in way using cfg!(target_pointer_width="64").

\$\endgroup\$
6
  • \$\begingroup\$ Does not work. This is 3264 when compiled as 32-bit on a 64-bit processor. \$\endgroup\$ – user253751 Sep 15 '20 at 13:34
  • \$\begingroup\$ @user253751 Can you share what steps you took to compile for 32-bit? The documentation for usize states that the actual size of usize is based on the pointer size. It was working for me fine when I targeted i686, but I'm not completely sure how to target 32-bit aside from that. \$\endgroup\$ – TehPers Sep 15 '20 at 16:48
  • \$\begingroup\$ Oops, I meant to say the correct output would be 3264 and this outputs 32. I did not run it, but it is obvious by inspection... \$\endgroup\$ – user253751 Sep 15 '20 at 16:56
  • \$\begingroup\$ @tuskiomi Can you clarify what should happen if your code is cross-compiled? Currently my solution works if you target the architecture you're on, but assumes the target architecture if you cross-compile. \$\endgroup\$ – TehPers Sep 15 '20 at 17:22
  • \$\begingroup\$ it seemed pretty clear to me. "If my CPU supports 64 bit instructions". NOT "If the program contains 64 bit instructions" \$\endgroup\$ – user253751 Sep 15 '20 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.